mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
Compare commits
No commits in common. "fd3d96fa747bd454378231248947ac7c542013ba" and "5685d442fff0ff0656215861670fb84032cf8164" have entirely different histories.
fd3d96fa74
...
5685d442ff
16
.idea/.gitignore
vendored
Normal file
16
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Custom ignored
|
||||
/caches/
|
||||
/dictionaries/
|
||||
/modules/
|
||||
/libraries/
|
||||
/*.xml
|
||||
# GitHub Copilot persisted chat sessions
|
||||
/copilot/chatSessions
|
1
.idea/.name
Normal file
1
.idea/.name
Normal file
@ -0,0 +1 @@
|
||||
legado
|
123
.idea/codeStyles/Project.xml
Normal file
123
.idea/codeStyles/Project.xml
Normal file
@ -0,0 +1,123 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<JetCodeStyleSettings>
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
</JetCodeStyleSettings>
|
||||
<codeStyleSettings language="XML">
|
||||
<option name="FORCE_REARRANGE_MODE" value="1" />
|
||||
<indentOptions>
|
||||
<option name="CONTINUATION_INDENT_SIZE" value="4" />
|
||||
</indentOptions>
|
||||
<arrangement>
|
||||
<rules>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>xmlns:android</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>xmlns:.*</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>BY_NAME</order>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:id</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*:name</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>name</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>style</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>^$</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>BY_NAME</order>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>ANDROID_ATTRIBUTE_ORDER</order>
|
||||
</rule>
|
||||
</section>
|
||||
<section>
|
||||
<rule>
|
||||
<match>
|
||||
<AND>
|
||||
<NAME>.*</NAME>
|
||||
<XML_ATTRIBUTE />
|
||||
<XML_NAMESPACE>.*</XML_NAMESPACE>
|
||||
</AND>
|
||||
</match>
|
||||
<order>BY_NAME</order>
|
||||
</rule>
|
||||
</section>
|
||||
</rules>
|
||||
</arrangement>
|
||||
</codeStyleSettings>
|
||||
<codeStyleSettings language="kotlin">
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
</codeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
|
||||
</state>
|
||||
</component>
|
20
.idea/inspectionProfiles/Project_Default.xml
Normal file
20
.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="ConstantConditions" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="SUGGEST_NULLABLE_ANNOTATIONS" value="false" />
|
||||
<option name="DONT_REPORT_TRUE_ASSERT_STATEMENTS" value="false" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="HtmlUnknownAttribute" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="myValues">
|
||||
<value>
|
||||
<list size="1">
|
||||
<item index="0" class="java.lang.String" itemvalue="name" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
<option name="myCustomValuesEnabled" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="NonAsciiCharacters" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
7
.idea/inspectionProfiles/profiles_settings.xml
Normal file
7
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="PROJECT_PROFILE" value="Default" />
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
@ -768,7 +768,15 @@ interface JsExtensions : JsEncodeUtils {
|
||||
var qTTF = CacheManager.getQueryTTF(key)
|
||||
if (qTTF != null) return qTTF
|
||||
val font: ByteArray? = when {
|
||||
str.isAbsUrl() -> AnalyzeUrl(str, source = getSource()).getByteArray()
|
||||
str.isAbsUrl() -> {
|
||||
var x = CacheManager.getByteArray(key)
|
||||
if (x == null) {
|
||||
x = AnalyzeUrl(str, source = getSource()).getByteArray()
|
||||
CacheManager.put(key, x)
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
str.isContentScheme() -> Uri.parse(str).readBytes(appCtx)
|
||||
str.startsWith("/storage") -> File(str).readBytes()
|
||||
else -> base64DecodeToByteArray(str)
|
||||
|
@ -1,19 +1,15 @@
|
||||
package io.legado.app.model.analyzeRule;
|
||||
|
||||
import android.util.Log;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.commons.lang3.tuple.Triple;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings({"FieldCanBeLocal", "unused"})
|
||||
public class QueryTTF {
|
||||
@ -133,7 +129,7 @@ public class QueryTTF {
|
||||
public int length;
|
||||
public int language;
|
||||
public int numGroups;
|
||||
public List<int[]> groups;
|
||||
public List<Triple<Integer, Integer, Integer>> groups;
|
||||
}
|
||||
|
||||
private static class GlyfLayout {
|
||||
@ -151,335 +147,103 @@ public class QueryTTF {
|
||||
}
|
||||
|
||||
private static class ByteArrayReader {
|
||||
private final byte[] buffer;
|
||||
public final ByteBuffer byteBuffer;
|
||||
public int index;
|
||||
public byte[] buffer;
|
||||
|
||||
public ByteArrayReader(byte[] buffer, int index) {
|
||||
this.buffer = buffer;
|
||||
this.byteBuffer = ByteBuffer.wrap(buffer);
|
||||
this.byteBuffer.order(ByteOrder.BIG_ENDIAN); // 设置为大端模式
|
||||
this.byteBuffer.position(index); // 设置起始索引
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public void position(int index) {
|
||||
byteBuffer.position(index); // 设置起始索引
|
||||
public long ReadUIntX(long len) {
|
||||
long result = 0;
|
||||
for (long i = 0; i < len; ++i) {
|
||||
result <<= 8;
|
||||
result |= buffer[index++] & 0xFF;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int position() {
|
||||
return byteBuffer.position();
|
||||
}
|
||||
|
||||
|
||||
public long ReadUInt64() {
|
||||
return byteBuffer.getLong();
|
||||
return ReadUIntX(8);
|
||||
}
|
||||
|
||||
public int ReadUInt32() {
|
||||
return byteBuffer.getInt();
|
||||
}
|
||||
|
||||
public int ReadInt32() {
|
||||
return byteBuffer.getInt();
|
||||
return (int) ReadUIntX(4);
|
||||
}
|
||||
|
||||
public int ReadUInt16() {
|
||||
return byteBuffer.getShort() & 0xFFFF;
|
||||
return (int) ReadUIntX(2);
|
||||
}
|
||||
|
||||
public short ReadInt16() {
|
||||
return byteBuffer.getShort();
|
||||
return (short) ReadUIntX(2);
|
||||
}
|
||||
|
||||
public short ReadUInt8() {
|
||||
return (short) (byteBuffer.get() & 0xFF);
|
||||
return (short) ReadUIntX(1);
|
||||
}
|
||||
|
||||
public byte ReadInt8() {
|
||||
return byteBuffer.get();
|
||||
|
||||
public String ReadStrings(int len, Charset charset) {
|
||||
byte[] result = len > 0 ? new byte[len] : null;
|
||||
for (int i = 0; i < len; ++i) result[i] = buffer[index++];
|
||||
return new String(result, charset);
|
||||
}
|
||||
|
||||
public byte[] ReadByteArray(int len) {
|
||||
if (len < 0) throw new IllegalArgumentException("Length must not be negative");
|
||||
byte[] result = new byte[len];
|
||||
byteBuffer.get(result);
|
||||
public byte GetByte() {
|
||||
return buffer[index++];
|
||||
}
|
||||
|
||||
public byte[] GetBytes(int len) {
|
||||
byte[] result = len > 0 ? new byte[len] : null;
|
||||
for (int i = 0; i < len; ++i) result[i] = buffer[index++];
|
||||
return result;
|
||||
}
|
||||
|
||||
public short[] ReadInt16Array(int len) {
|
||||
if (len < 0) throw new IllegalArgumentException("Length must not be negative");
|
||||
var result = new short[len];
|
||||
for (int i = 0; i < len; ++i) result[i] = byteBuffer.getShort();
|
||||
public int[] GetUInt16Array(int len) {
|
||||
int[] result = len > 0 ? new int[len] : null;
|
||||
for (int i = 0; i < len; ++i) result[i] = ReadUInt16();
|
||||
return result;
|
||||
}
|
||||
|
||||
public int[] ReadUInt16Array(int len) {
|
||||
if (len < 0) throw new IllegalArgumentException("Length must not be negative");
|
||||
var result = new int[len];
|
||||
for (int i = 0; i < len; ++i) result[i] = byteBuffer.getShort() & 0xFFFF;
|
||||
public short[] GetInt16Array(int len) {
|
||||
short[] result = len > 0 ? new short[len] : null;
|
||||
for (int i = 0; i < len; ++i) result[i] = ReadInt16();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private final ByteArrayReader fontReader;
|
||||
private final Header fileHeader = new Header();
|
||||
private final Map<String, Directory> directorys = new HashMap<>();
|
||||
private final List<Directory> directorys = new LinkedList<>();
|
||||
private final NameLayout name = new NameLayout();
|
||||
private final HeadLayout head = new HeadLayout();
|
||||
private final MaxpLayout maxp = new MaxpLayout();
|
||||
private final List<Integer> loca = new LinkedList<>();
|
||||
private final CmapLayout Cmap = new CmapLayout();
|
||||
private final ConcurrentHashMap<Integer, String> glyf = new ConcurrentHashMap<>();
|
||||
private final int[][] pps = new int[][]{
|
||||
{3, 10},
|
||||
{0, 4},
|
||||
{3, 1},
|
||||
{1, 0},
|
||||
{0, 3},
|
||||
{0, 1}
|
||||
private final List<String> glyf = new LinkedList<>();
|
||||
@SuppressWarnings("unchecked")
|
||||
private final Pair<Integer, Integer>[] pps = new Pair[]{
|
||||
Pair.of(3, 10),
|
||||
Pair.of(0, 4),
|
||||
Pair.of(3, 1),
|
||||
Pair.of(1, 0),
|
||||
Pair.of(0, 3),
|
||||
Pair.of(0, 1)
|
||||
};
|
||||
|
||||
public final Map<Integer, String> unicodeToGlyph = new HashMap<>();
|
||||
public final Map<String, Integer> glyphToUnicode = new HashMap<>();
|
||||
public final Map<Integer, Integer> unicodeToGlyphIndex = new HashMap<>();
|
||||
|
||||
private void readNameTable(byte[] buffer) {
|
||||
var dataTable = Objects.requireNonNull(directorys.get("name"));
|
||||
var reader = new ByteArrayReader(buffer, dataTable.offset);
|
||||
name.format = reader.ReadUInt16();
|
||||
name.count = reader.ReadUInt16();
|
||||
name.stringOffset = reader.ReadUInt16();
|
||||
for (int i = 0; i < name.count; ++i) {
|
||||
NameRecord record = new NameRecord();
|
||||
record.platformID = reader.ReadUInt16();
|
||||
record.encodingID = reader.ReadUInt16();
|
||||
record.languageID = reader.ReadUInt16();
|
||||
record.nameID = reader.ReadUInt16();
|
||||
record.length = reader.ReadUInt16();
|
||||
record.offset = reader.ReadUInt16();
|
||||
name.records.add(record);
|
||||
}
|
||||
}
|
||||
|
||||
private void readHeadTable(byte[] buffer) {
|
||||
var dataTable = Objects.requireNonNull(directorys.get("head"));
|
||||
var reader = new ByteArrayReader(buffer, dataTable.offset);
|
||||
head.majorVersion = reader.ReadUInt16();
|
||||
head.minorVersion = reader.ReadUInt16();
|
||||
head.fontRevision = reader.ReadUInt32();
|
||||
head.checkSumAdjustment = reader.ReadUInt32();
|
||||
head.magicNumber = reader.ReadUInt32();
|
||||
head.flags = reader.ReadUInt16();
|
||||
head.unitsPerEm = reader.ReadUInt16();
|
||||
head.created = reader.ReadUInt64();
|
||||
head.modified = reader.ReadUInt64();
|
||||
head.xMin = reader.ReadInt16();
|
||||
head.yMin = reader.ReadInt16();
|
||||
head.xMax = reader.ReadInt16();
|
||||
head.yMax = reader.ReadInt16();
|
||||
head.macStyle = reader.ReadUInt16();
|
||||
head.lowestRecPPEM = reader.ReadUInt16();
|
||||
head.fontDirectionHint = reader.ReadInt16();
|
||||
head.indexToLocFormat = reader.ReadInt16();
|
||||
head.glyphDataFormat = reader.ReadInt16();
|
||||
}
|
||||
|
||||
private void readCmapTable(byte[] buffer) {
|
||||
var dataTable = Objects.requireNonNull(directorys.get("cmap"));
|
||||
var reader = new ByteArrayReader(buffer, dataTable.offset);
|
||||
Cmap.version = reader.ReadUInt16();
|
||||
Cmap.numTables = reader.ReadUInt16();
|
||||
for (int i = 0; i < Cmap.numTables; ++i) {
|
||||
CmapRecord record = new CmapRecord();
|
||||
record.platformID = reader.ReadUInt16();
|
||||
record.encodingID = reader.ReadUInt16();
|
||||
record.offset = reader.ReadUInt32();
|
||||
Cmap.records.add(record);
|
||||
}
|
||||
for (int i = 0; i < Cmap.numTables; ++i) {
|
||||
int fmtOffset = Cmap.records.get(i).offset;
|
||||
reader.position(dataTable.offset + fmtOffset);
|
||||
int EndIndex = reader.position();
|
||||
|
||||
int format = reader.ReadUInt16();
|
||||
if (Cmap.tables.containsKey(fmtOffset)) continue;
|
||||
if (format == 0) {
|
||||
CmapFormat f = new CmapFormat();
|
||||
f.format = format;
|
||||
f.length = reader.ReadUInt16();
|
||||
f.language = reader.ReadUInt16();
|
||||
f.glyphIdArray = reader.ReadByteArray(f.length - 6);
|
||||
Cmap.tables.put(fmtOffset, f);
|
||||
} else if (format == 4) {
|
||||
CmapFormat4 f = new CmapFormat4();
|
||||
f.format = format;
|
||||
f.length = reader.ReadUInt16();
|
||||
f.language = reader.ReadUInt16();
|
||||
f.segCountX2 = reader.ReadUInt16();
|
||||
int segCount = f.segCountX2 >> 1;
|
||||
f.searchRange = reader.ReadUInt16();
|
||||
f.entrySelector = reader.ReadUInt16();
|
||||
f.rangeShift = reader.ReadUInt16();
|
||||
f.endCode = reader.ReadUInt16Array(segCount);
|
||||
f.reservedPad = reader.ReadUInt16();
|
||||
f.startCode = reader.ReadUInt16Array(segCount);
|
||||
f.idDelta = reader.ReadInt16Array(segCount);
|
||||
f.idRangeOffset = reader.ReadUInt16Array(segCount);
|
||||
f.glyphIdArray = reader.ReadUInt16Array((EndIndex + f.length - reader.position()) >> 1);
|
||||
Cmap.tables.put(fmtOffset, f);
|
||||
} else if (format == 6) {
|
||||
CmapFormat6 f = new CmapFormat6();
|
||||
f.format = format;
|
||||
f.length = reader.ReadUInt16();
|
||||
f.language = reader.ReadUInt16();
|
||||
f.firstCode = reader.ReadUInt16();
|
||||
f.entryCount = reader.ReadUInt16();
|
||||
f.glyphIdArray = reader.ReadUInt16Array(f.entryCount);
|
||||
Cmap.tables.put(fmtOffset, f);
|
||||
} else if (format == 12) {
|
||||
CmapFormat12 f = new CmapFormat12();
|
||||
f.format = format;
|
||||
f.reserved = reader.ReadUInt16();
|
||||
f.length = reader.ReadUInt32();
|
||||
f.language = reader.ReadUInt32();
|
||||
f.numGroups = reader.ReadUInt32();
|
||||
f.groups = new LinkedList<>();
|
||||
for (int n = 0; n < f.numGroups; ++n) {
|
||||
f.groups.add(new int[]{reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32()});
|
||||
}
|
||||
Cmap.tables.put(fmtOffset, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void readLocaTable(byte[] buffer) {
|
||||
var dataTable = Objects.requireNonNull(directorys.get("loca"));
|
||||
var reader = new ByteArrayReader(buffer, dataTable.offset);
|
||||
var locaTableSize = dataTable.length;
|
||||
if (head.indexToLocFormat == 0) {
|
||||
for (long i = 0; i < locaTableSize; i += 2) loca.add(reader.ReadUInt16());
|
||||
} else {
|
||||
for (long i = 0; i < locaTableSize; i += 4) loca.add(reader.ReadUInt32());
|
||||
}
|
||||
}
|
||||
|
||||
private void readMaxpTable(byte[] buffer) {
|
||||
var dataTable = Objects.requireNonNull(directorys.get("maxp"));
|
||||
var reader = new ByteArrayReader(buffer, dataTable.offset);
|
||||
maxp.majorVersion = reader.ReadUInt16();
|
||||
maxp.minorVersion = reader.ReadUInt16();
|
||||
maxp.numGlyphs = reader.ReadUInt16();
|
||||
maxp.maxPoints = reader.ReadUInt16();
|
||||
maxp.maxContours = reader.ReadUInt16();
|
||||
maxp.maxCompositePoints = reader.ReadUInt16();
|
||||
maxp.maxCompositeContours = reader.ReadUInt16();
|
||||
maxp.maxZones = reader.ReadUInt16();
|
||||
maxp.maxTwilightPoints = reader.ReadUInt16();
|
||||
maxp.maxStorage = reader.ReadUInt16();
|
||||
maxp.maxFunctionDefs = reader.ReadUInt16();
|
||||
maxp.maxInstructionDefs = reader.ReadUInt16();
|
||||
maxp.maxStackElements = reader.ReadUInt16();
|
||||
maxp.maxSizeOfInstructions = reader.ReadUInt16();
|
||||
maxp.maxComponentElements = reader.ReadUInt16();
|
||||
maxp.maxComponentDepth = reader.ReadUInt16();
|
||||
}
|
||||
|
||||
private void readGlyfTable(byte[] buffer) {
|
||||
var dataTable = Objects.requireNonNull(directorys.get("glyf"));
|
||||
int glyfCount = maxp.numGlyphs;
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
for (int i = 0; i < glyfCount; i++) {
|
||||
final int index = i;
|
||||
final var reader = new ByteArrayReader(buffer, dataTable.offset + loca.get(index));
|
||||
executor.submit(() -> {
|
||||
int glyfNextIndex = index + 1 < glyfCount ? (dataTable.offset + loca.get(index + 1)) : (dataTable.offset + dataTable.length);
|
||||
byte[] glyph;
|
||||
short numberOfContours = reader.ReadInt16();
|
||||
if (numberOfContours > 0) {
|
||||
short g_xMin = reader.ReadInt16();
|
||||
short g_yMin = reader.ReadInt16();
|
||||
short g_xMax = reader.ReadInt16();
|
||||
short g_yMax = reader.ReadInt16();
|
||||
int[] endPtsOfContours = reader.ReadUInt16Array(numberOfContours);
|
||||
int instructionLength = reader.ReadUInt16();
|
||||
var instructions = reader.ReadByteArray(instructionLength);
|
||||
int flagLength = endPtsOfContours[endPtsOfContours.length - 1] + 1;
|
||||
// 获取轮廓点描述标志
|
||||
var flags = new byte[flagLength];
|
||||
for (int n = 0; n < flagLength; ++n) {
|
||||
flags[n] = reader.ReadInt8();
|
||||
if ((flags[n] & 0x08) != 0x00) {
|
||||
for (int m = reader.ReadUInt8(); m > 0; --m) {
|
||||
flags[++n] = flags[n - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
// 获取轮廓点描述x,y相对值
|
||||
ByteBuffer xyCoordinatesBuffer = ByteBuffer.allocate(flagLength * 4);
|
||||
// 获取轮廓点描述x轴相对值
|
||||
for (int n = 0; n < flagLength; ++n) {
|
||||
short same = (short) ((flags[n] & 0x10) != 0 ? 1 : -1);
|
||||
if ((flags[n] & 0x02) != 0) {
|
||||
xyCoordinatesBuffer.putShort((short) (same * reader.ReadUInt8()));
|
||||
} else {
|
||||
xyCoordinatesBuffer.putShort(same == 1 ? (short) 0 : reader.ReadInt16());
|
||||
}
|
||||
}
|
||||
// 获取轮廓点描述y轴相对值
|
||||
for (int n = 0; n < flagLength; ++n) {
|
||||
short same = (short) ((flags[n] & 0x20) != 0 ? 1 : -1);
|
||||
if ((flags[n] & 0x04) != 0) {
|
||||
xyCoordinatesBuffer.putShort((short) (same * reader.ReadUInt8()));
|
||||
} else {
|
||||
xyCoordinatesBuffer.putShort(same == 1 ? (short) 0 : reader.ReadInt16());
|
||||
}
|
||||
}
|
||||
// 保存轮廓点描述x,y相对值ByteArray
|
||||
glyph = xyCoordinatesBuffer.array();
|
||||
} else {
|
||||
// 复合字体未做详细处理
|
||||
glyph = reader.ReadByteArray(glyfNextIndex - (reader.position() - 2));
|
||||
}
|
||||
glyf.put(index, getHexFromBytes(glyph));
|
||||
});
|
||||
}
|
||||
executor.shutdown();
|
||||
try {
|
||||
boolean b = executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Log.e("queryTTF", "glyf表解析出错: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
private void makeHashTable() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
for (int i = 0; i < 130000; ++i) {
|
||||
final int key = i;
|
||||
executor.submit(() -> {
|
||||
Integer gid = queryGlyfIndex(key);
|
||||
if (gid < glyf.size()) {
|
||||
unicodeToGlyphIndex.put(key, gid);
|
||||
var val = glyf.get(gid);
|
||||
unicodeToGlyph.put(key, val);
|
||||
if (!glyphToUnicode.containsKey(val)) glyphToUnicode.put(val, key);
|
||||
}
|
||||
});
|
||||
}
|
||||
executor.shutdown();
|
||||
try {
|
||||
boolean b = executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Log.e("queryTTF", "建立Unicode&Glyph映射表出错: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param buffer 传入TTF字体二进制数组
|
||||
*/
|
||||
public QueryTTF(byte[] buffer) {
|
||||
var fontReader = new ByteArrayReader(buffer, 0);
|
||||
Log.i("queryTTF", "读文件头");
|
||||
fontReader = new ByteArrayReader(buffer, 0);
|
||||
// 获取文件头
|
||||
fileHeader.majorVersion = fontReader.ReadUInt16();
|
||||
fileHeader.minorVersion = fontReader.ReadUInt16();
|
||||
@ -490,45 +254,215 @@ public class QueryTTF {
|
||||
// 获取目录
|
||||
for (int i = 0; i < fileHeader.numOfTables; ++i) {
|
||||
Directory d = new Directory();
|
||||
d.tag = new String(fontReader.ReadByteArray(4), StandardCharsets.US_ASCII);
|
||||
d.tag = fontReader.ReadStrings(4, StandardCharsets.US_ASCII);
|
||||
d.checkSum = fontReader.ReadUInt32();
|
||||
d.offset = fontReader.ReadUInt32();
|
||||
d.length = fontReader.ReadUInt32();
|
||||
directorys.put(d.tag, d);
|
||||
directorys.add(d);
|
||||
}
|
||||
// 解析表 name (字体信息,包含版权、名称、作者等...)
|
||||
for (Directory Temp : directorys) {
|
||||
if (Temp.tag.equals("name")) {
|
||||
fontReader.index = Temp.offset;
|
||||
name.format = fontReader.ReadUInt16();
|
||||
name.count = fontReader.ReadUInt16();
|
||||
name.stringOffset = fontReader.ReadUInt16();
|
||||
for (int i = 0; i < name.count; ++i) {
|
||||
NameRecord record = new NameRecord();
|
||||
record.platformID = fontReader.ReadUInt16();
|
||||
record.encodingID = fontReader.ReadUInt16();
|
||||
record.languageID = fontReader.ReadUInt16();
|
||||
record.nameID = fontReader.ReadUInt16();
|
||||
record.length = fontReader.ReadUInt16();
|
||||
record.offset = fontReader.ReadUInt16();
|
||||
name.records.add(record);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 解析表 head (获取 head.indexToLocFormat)
|
||||
for (Directory Temp : directorys) {
|
||||
if (Temp.tag.equals("head")) {
|
||||
fontReader.index = Temp.offset;
|
||||
head.majorVersion = fontReader.ReadUInt16();
|
||||
head.minorVersion = fontReader.ReadUInt16();
|
||||
head.fontRevision = fontReader.ReadUInt32();
|
||||
head.checkSumAdjustment = fontReader.ReadUInt32();
|
||||
head.magicNumber = fontReader.ReadUInt32();
|
||||
head.flags = fontReader.ReadUInt16();
|
||||
head.unitsPerEm = fontReader.ReadUInt16();
|
||||
head.created = fontReader.ReadUInt64();
|
||||
head.modified = fontReader.ReadUInt64();
|
||||
head.xMin = fontReader.ReadInt16();
|
||||
head.yMin = fontReader.ReadInt16();
|
||||
head.xMax = fontReader.ReadInt16();
|
||||
head.yMax = fontReader.ReadInt16();
|
||||
head.macStyle = fontReader.ReadUInt16();
|
||||
head.lowestRecPPEM = fontReader.ReadUInt16();
|
||||
head.fontDirectionHint = fontReader.ReadInt16();
|
||||
head.indexToLocFormat = fontReader.ReadInt16();
|
||||
head.glyphDataFormat = fontReader.ReadInt16();
|
||||
}
|
||||
}
|
||||
// 解析表 maxp (获取 maxp.numGlyphs)
|
||||
for (Directory Temp : directorys) {
|
||||
if (Temp.tag.equals("maxp")) {
|
||||
fontReader.index = Temp.offset;
|
||||
maxp.majorVersion = fontReader.ReadUInt16();
|
||||
maxp.minorVersion = fontReader.ReadUInt16();
|
||||
maxp.numGlyphs = fontReader.ReadUInt16();
|
||||
maxp.maxPoints = fontReader.ReadUInt16();
|
||||
maxp.maxContours = fontReader.ReadUInt16();
|
||||
maxp.maxCompositePoints = fontReader.ReadUInt16();
|
||||
maxp.maxCompositeContours = fontReader.ReadUInt16();
|
||||
maxp.maxZones = fontReader.ReadUInt16();
|
||||
maxp.maxTwilightPoints = fontReader.ReadUInt16();
|
||||
maxp.maxStorage = fontReader.ReadUInt16();
|
||||
maxp.maxFunctionDefs = fontReader.ReadUInt16();
|
||||
maxp.maxInstructionDefs = fontReader.ReadUInt16();
|
||||
maxp.maxStackElements = fontReader.ReadUInt16();
|
||||
maxp.maxSizeOfInstructions = fontReader.ReadUInt16();
|
||||
maxp.maxComponentElements = fontReader.ReadUInt16();
|
||||
maxp.maxComponentDepth = fontReader.ReadUInt16();
|
||||
}
|
||||
}
|
||||
// 解析表 loca (轮廓数据偏移地址表)
|
||||
for (Directory Temp : directorys) {
|
||||
if (Temp.tag.equals("loca")) {
|
||||
fontReader.index = Temp.offset;
|
||||
int offset = head.indexToLocFormat == 0 ? 2 : 4;
|
||||
for (long i = 0; i < Temp.length; i += offset) {
|
||||
loca.add(offset == 2 ? fontReader.ReadUInt16() << 1 : fontReader.ReadUInt32());
|
||||
}
|
||||
}
|
||||
}
|
||||
// 解析表 cmap (Unicode编码轮廓索引对照表)
|
||||
for (Directory Temp : directorys) {
|
||||
if (Temp.tag.equals("cmap")) {
|
||||
fontReader.index = Temp.offset;
|
||||
Cmap.version = fontReader.ReadUInt16();
|
||||
Cmap.numTables = fontReader.ReadUInt16();
|
||||
|
||||
for (int i = 0; i < Cmap.numTables; ++i) {
|
||||
CmapRecord record = new CmapRecord();
|
||||
record.platformID = fontReader.ReadUInt16();
|
||||
record.encodingID = fontReader.ReadUInt16();
|
||||
record.offset = fontReader.ReadUInt32();
|
||||
Cmap.records.add(record);
|
||||
}
|
||||
for (int i = 0; i < Cmap.numTables; ++i) {
|
||||
int fmtOffset = Cmap.records.get(i).offset;
|
||||
fontReader.index = Temp.offset + fmtOffset;
|
||||
int EndIndex = fontReader.index;
|
||||
|
||||
int format = fontReader.ReadUInt16();
|
||||
if (Cmap.tables.containsKey(fmtOffset)) continue;
|
||||
if (format == 0) {
|
||||
CmapFormat f = new CmapFormat();
|
||||
f.format = format;
|
||||
f.length = fontReader.ReadUInt16();
|
||||
f.language = fontReader.ReadUInt16();
|
||||
f.glyphIdArray = fontReader.GetBytes(f.length - 6);
|
||||
Cmap.tables.put(fmtOffset, f);
|
||||
} else if (format == 4) {
|
||||
CmapFormat4 f = new CmapFormat4();
|
||||
f.format = format;
|
||||
f.length = fontReader.ReadUInt16();
|
||||
f.language = fontReader.ReadUInt16();
|
||||
f.segCountX2 = fontReader.ReadUInt16();
|
||||
int segCount = f.segCountX2 >> 1;
|
||||
f.searchRange = fontReader.ReadUInt16();
|
||||
f.entrySelector = fontReader.ReadUInt16();
|
||||
f.rangeShift = fontReader.ReadUInt16();
|
||||
f.endCode = fontReader.GetUInt16Array(segCount);
|
||||
f.reservedPad = fontReader.ReadUInt16();
|
||||
f.startCode = fontReader.GetUInt16Array(segCount);
|
||||
f.idDelta = fontReader.GetInt16Array(segCount);
|
||||
f.idRangeOffset = fontReader.GetUInt16Array(segCount);
|
||||
f.glyphIdArray = fontReader.GetUInt16Array((EndIndex + f.length - fontReader.index) >> 1);
|
||||
Cmap.tables.put(fmtOffset, f);
|
||||
} else if (format == 6) {
|
||||
CmapFormat6 f = new CmapFormat6();
|
||||
f.format = format;
|
||||
f.length = fontReader.ReadUInt16();
|
||||
f.language = fontReader.ReadUInt16();
|
||||
f.firstCode = fontReader.ReadUInt16();
|
||||
f.entryCount = fontReader.ReadUInt16();
|
||||
f.glyphIdArray = fontReader.GetUInt16Array(f.entryCount);
|
||||
Cmap.tables.put(fmtOffset, f);
|
||||
} else if (format == 12) {
|
||||
CmapFormat12 f = new CmapFormat12();
|
||||
f.format = format;
|
||||
f.reserved = fontReader.ReadUInt16();
|
||||
f.length = fontReader.ReadUInt32();
|
||||
f.language = fontReader.ReadUInt32();
|
||||
f.numGroups = fontReader.ReadUInt32();
|
||||
f.groups = new ArrayList<>(f.numGroups);
|
||||
for (int n = 0; n < f.numGroups; ++n) {
|
||||
f.groups.add(Triple.of(fontReader.ReadUInt32(), fontReader.ReadUInt32(), fontReader.ReadUInt32()));
|
||||
}
|
||||
Cmap.tables.put(fmtOffset, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 读取表 glyf (字体轮廓数据表)
|
||||
for (Directory Temp : directorys) {
|
||||
if (Temp.tag.equals("glyf")) {
|
||||
int glyfCount = maxp.numGlyphs;
|
||||
for (int i = 0; i < glyfCount; ) {
|
||||
fontReader.index = Temp.offset + loca.get(i);
|
||||
++i;
|
||||
int glyfNextIndex = i < glyfCount ? loca.get(i) : Temp.length;
|
||||
|
||||
byte[] glyph;
|
||||
short numberOfContours = fontReader.ReadInt16();
|
||||
if (numberOfContours > 0) {
|
||||
short g_xMin = fontReader.ReadInt16();
|
||||
short g_yMin = fontReader.ReadInt16();
|
||||
short g_xMax = fontReader.ReadInt16();
|
||||
short g_yMax = fontReader.ReadInt16();
|
||||
int[] endPtsOfContours = fontReader.GetUInt16Array(numberOfContours);
|
||||
glyph = fontReader.GetBytes(glyfNextIndex - (fontReader.index - Temp.offset));
|
||||
} else {
|
||||
glyph = fontReader.GetBytes(glyfNextIndex - (fontReader.index - 2));
|
||||
}
|
||||
glyf.add(getHexFromBytes(glyph));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log.i("queryTTF", "解析表 name"); // 字体信息,包含版权、名称、作者等...
|
||||
readNameTable(buffer);
|
||||
Log.i("queryTTF", "解析表 head"); // 获取 head.indexToLocFormat
|
||||
readHeadTable(buffer);
|
||||
Log.i("queryTTF", "解析表 cmap"); // Unicode编码->轮廓索引 对照表
|
||||
readCmapTable(buffer);
|
||||
Log.i("queryTTF", "解析表 loca"); // 轮廓数据偏移地址表
|
||||
readLocaTable(buffer);
|
||||
Log.i("queryTTF", "解析表 maxp"); // 获取 maxp.numGlyphs 字体轮廓数量
|
||||
readMaxpTable(buffer);
|
||||
Log.i("queryTTF", "解析表 glyf"); // 字体轮廓数据表,需要解析loca,maxp表后计算
|
||||
readGlyfTable(buffer);
|
||||
Log.i("queryTTF", "建立Unicode&Glyph映射表");
|
||||
makeHashTable();
|
||||
Log.i("queryTTF", "字体处理完成");
|
||||
// 建立Unicode&Glyph双向表
|
||||
for (int key = 0; key < 130000; ++key) {
|
||||
// if (key == 0xFF) key = 0x3400;
|
||||
int gid = queryGlyfIndex(key);
|
||||
if (gid >= glyf.size()) continue;
|
||||
unicodeToGlyphIndex.put(key, gid);
|
||||
var val = glyf.get(gid);
|
||||
unicodeToGlyph.put(key, val);
|
||||
if (glyphToUnicode.containsKey(val)) continue;
|
||||
glyphToUnicode.put(val, key);
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 获取字体信息 (1=字体名称)
|
||||
// *
|
||||
// * @param nameId 传入十进制字体信息索引
|
||||
// * @return 返回查询结果字符串
|
||||
// */
|
||||
// public String getNameById(int nameId) {
|
||||
// fontReader.index = Objects.requireNonNull(directorys.get("name")).offset;
|
||||
// for (NameRecord record : name.records) {
|
||||
// if (record.nameID != nameId) continue;
|
||||
// fontReader.index += name.stringOffset + record.offset;
|
||||
// return fontReader.ReadStrings(record.length, record.platformID == 1 ? StandardCharsets.UTF_8 : StandardCharsets.UTF_16BE);
|
||||
// }
|
||||
// return "error";
|
||||
// }
|
||||
/**
|
||||
* 获取字体信息 (1=字体名称)
|
||||
*
|
||||
* @param nameId 传入十进制字体信息索引
|
||||
* @return 返回查询结果字符串
|
||||
*/
|
||||
public String getNameById(int nameId) {
|
||||
for (Directory Temp : directorys) {
|
||||
if (!Temp.tag.equals("name")) continue;
|
||||
fontReader.index = Temp.offset;
|
||||
break;
|
||||
}
|
||||
for (NameRecord record : name.records) {
|
||||
if (record.nameID != nameId) continue;
|
||||
fontReader.index += name.stringOffset + record.offset;
|
||||
return fontReader.ReadStrings(record.length, record.platformID == 1 ? StandardCharsets.UTF_8 : StandardCharsets.UTF_16BE);
|
||||
}
|
||||
return "error";
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用Unicode值查找轮廓索引
|
||||
@ -538,11 +472,10 @@ public class QueryTTF {
|
||||
*/
|
||||
public int queryGlyfIndex(int unicode) {
|
||||
if (unicode == 0) return 0;
|
||||
|
||||
int fmtKey = 0;
|
||||
for (var item : pps) {
|
||||
for (Pair<Integer, Integer> item : pps) {
|
||||
for (CmapRecord record : Cmap.records) {
|
||||
if ((item[0] == record.platformID) && (item[1] == record.encodingID)) {
|
||||
if ((item.getLeft() == record.platformID) && (item.getRight() == record.encodingID)) {
|
||||
fmtKey = record.offset;
|
||||
break;
|
||||
}
|
||||
@ -579,10 +512,10 @@ public class QueryTTF {
|
||||
if (0 <= index && index < tab.glyphIdArray.length) glyfID = tab.glyphIdArray[index];
|
||||
} else if (fmt == 12) {
|
||||
CmapFormat12 tab = (CmapFormat12) table;
|
||||
if (unicode > tab.groups.get(tab.numGroups - 1)[1]) return 0;
|
||||
if (unicode > tab.groups.get(tab.numGroups - 1).getMiddle()) return 0;
|
||||
for (int i = 0; i < tab.numGroups; i++) {
|
||||
if (tab.groups.get(i)[0] <= unicode && unicode <= tab.groups.get(i)[1]) {
|
||||
glyfID = tab.groups.get(i)[2] + unicode - tab.groups.get(i)[0];
|
||||
if (tab.groups.get(i).getLeft() <= unicode && unicode <= tab.groups.get(i).getMiddle()) {
|
||||
glyfID = tab.groups.get(i).getRight() + unicode - tab.groups.get(i).getLeft();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -631,8 +564,14 @@ public class QueryTTF {
|
||||
*/
|
||||
public String getHexFromBytes(byte[] glyph) {
|
||||
if (glyph == null) return "";
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : glyph) hexString.append(String.format("%02X", b));
|
||||
return hexString.toString();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : glyph) {
|
||||
String hex = Integer.toHexString(b);
|
||||
if (hex.length() == 1) {
|
||||
sb.append("0");//当16进制为个位数时,在前面补0
|
||||
}
|
||||
sb.append(hex);//将16进制加入字符串
|
||||
}
|
||||
return sb.toString().toUpperCase();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user