优化复合字体轮廓数据结构

This commit is contained in:
Antecer 2024-06-20 10:57:02 +08:00
parent d6c2b5eceb
commit 576a370da1

View File

@ -773,12 +773,12 @@ public class QueryTTF {
var dataTable = directorys.get("glyf"); var dataTable = directorys.get("glyf");
assert dataTable != null; assert dataTable != null;
int glyfCount = maxp.numGlyphs; int glyfCount = maxp.numGlyphs;
glyfArray = new GlyfLayout[glyfCount + 1]; // 创建容器多创建一个作为保留区 glyfArray = new GlyfLayout[glyfCount]; // 创建字形容器
var reader = new BufferReader(buffer, 0); var reader = new BufferReader(buffer, 0);
for (int index = 1; index <= glyfCount; index++) { for (int index = 0; index < glyfCount; index++) {
if (loca[index - 1] == loca[index]) continue; // 当前loca与下一个loca相同表示这个字形不存在 if (loca[index] == loca[index + 1]) continue; // 当前loca与下一个loca相同表示这个字形不存在
int offset = dataTable.offset + loca[index - 1]; int offset = dataTable.offset + loca[index];
// 读GlyphHeaders // 读GlyphHeaders
var glyph = new GlyfLayout(); var glyph = new GlyfLayout();
reader.position(offset); reader.position(offset);
@ -893,7 +893,7 @@ public class QueryTTF {
if ((glyphTableComponent.flags & 0x20) == 0) break; if ((glyphTableComponent.flags & 0x20) == 0) break;
} }
} }
glyfArray[index] = glyph; // 根据文档 glyfId=0 作为保留区使用这里赋值从索引1开始 glyfArray[index] = glyph;
} }
} }
@ -919,7 +919,15 @@ public class QueryTTF {
// 复合字形 // 复合字形
LinkedList<String> glyphIdList = new LinkedList<>(); LinkedList<String> glyphIdList = new LinkedList<>();
for (var g : glyph.glyphComponent) { for (var g : glyph.glyphComponent) {
glyphIdList.add(String.valueOf(g.glyphIndex)); glyphIdList.add("{" +
"flags:" + g.flags + "," +
"glyphIndex:" + g.glyphIndex + "," +
"arg1:" + g.argument1 + "," +
"arg2:" + g.argument2 + "," +
"xScale:" + g.xScale + "," +
"scale01:" + g.scale01 + "," +
"scale10:" + g.scale10 + "," +
"yScale:" + g.yScale + "}");
} }
glyphString = "[" + String.join(",", glyphIdList) + "]"; glyphString = "[" + String.join(",", glyphIdList) + "]";
} }
@ -965,10 +973,11 @@ public class QueryTTF {
int glyfArrayLength = glyfArray.length; int glyfArrayLength = glyfArray.length;
for (var item : unicodeToGlyphId.entrySet()) { for (var item : unicodeToGlyphId.entrySet()) {
int key = item.getKey(); int key = item.getKey();
int val = item.getValue() + 1; // glyphArray已根据TTF文档将索引0作为保留位这里从1开始索引 int val = item.getValue();
if (val >= glyfArrayLength) continue; if (val >= glyfArrayLength) continue;
String glyfString = getGlyfById(val); String glyfString = getGlyfById(val);
unicodeToGlyph.put(key, glyfString); unicodeToGlyph.put(key, glyfString);
if (glyfString == null) continue; // null 不能用作hashmap的key
glyphToUnicode.put(glyfString, key); glyphToUnicode.put(glyfString, key);
} }
// Log.i("QueryTTF", "字体处理完成"); // Log.i("QueryTTF", "字体处理完成");
@ -986,8 +995,8 @@ public class QueryTTF {
*/ */
public int getGlyfIdByUnicode(int unicode) { public int getGlyfIdByUnicode(int unicode) {
var result = unicodeToGlyphId.get(unicode); var result = unicodeToGlyphId.get(unicode);
if (result == null) return 0; if (result == null) return 0; // 如果找不到Unicode对应的轮廓索引就返回默认值0
return result + 1; // 根据TTF文档轮廓索引的定义从1开始 return result;
} }
/** /**
@ -1008,7 +1017,7 @@ public class QueryTTF {
*/ */
public int getUnicodeByGlyf(String glyph) { public int getUnicodeByGlyf(String glyph) {
var result = glyphToUnicode.get(glyph); var result = glyphToUnicode.get(glyph);
if (result == null) return 0; if (result == null) return 0; // 如果轮廓数据找不到对应的Unicode就返回默认值0
return result; return result;
} }