Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/consolidate/otl/mark.c

raw
1 #include "mark.h"
2
3 typedef struct {
4 int gid;
5 sds name;
6 glyphclass_t markClass;
7 otl_Anchor anchor;
8 UT_hash_handle hh;
9 } mark_hash;
10 static int mark_by_gid(mark_hash *a, mark_hash *b) {
11 return a->gid - b->gid;
12 }
13 typedef struct {
14 int gid;
15 sds name;
16 otl_Anchor *anchors;
17 UT_hash_handle hh;
18 } base_hash;
19 static int base_by_gid(base_hash *a, base_hash *b) {
20 return a->gid - b->gid;
21 }
22 typedef struct {
23 int gid;
24 sds name;
25 glyphid_t componentCount;
26 otl_Anchor **anchors;
27 UT_hash_handle hh;
28 } lig_hash;
29 static int lig_by_gid(lig_hash *a, lig_hash *b) {
30 return a->gid - b->gid;
31 }
32
33 static void consolidateMarkArray(otfcc_Font *font, table_OTL *table, const otfcc_Options *options,
34 otl_MarkArray *markArray, glyphclass_t classCount) {
35 mark_hash *hm = NULL;
36 for (glyphid_t k = 0; k < markArray->length; k++) {
37 if (!GlyphOrder.consolidateHandle(font->glyph_order, &markArray->items[k].glyph)) {
38 logWarning("[Consolidate] Ignored unknown glyph name %s.",
39 markArray->items[k].glyph.name);
40 continue;
41 }
42 mark_hash *s = NULL;
43 int gid = markArray->items[k].glyph.index;
44 HASH_FIND_INT(hm, &gid, s);
45 if (!s && markArray->items[k].anchor.present &&
46 markArray->items[k].markClass < classCount) {
47 NEW(s);
48 s->gid = markArray->items[k].glyph.index;
49 s->name = sdsdup(markArray->items[k].glyph.name);
50 s->markClass = markArray->items[k].markClass;
51 s->anchor = markArray->items[k].anchor;
52 HASH_ADD_INT(hm, gid, s);
53 } else {
54 logWarning("[Consolidate] Ignored invalid or double-mapping mark definition for /%s.",
55 markArray->items[k].glyph.name);
56 }
57 }
58 HASH_SORT(hm, mark_by_gid);
59 otl_iMarkArray.clear(markArray);
60 mark_hash *s, *tmp;
61 HASH_ITER(hh, hm, s, tmp) {
62 otl_iMarkArray.push(markArray, ((otl_MarkRecord){
63 .glyph = Handle.fromConsolidated(s->gid, s->name),
64 .markClass = s->markClass,
65 .anchor = s->anchor,
66 }));
67 sdsfree(s->name);
68 HASH_DEL(hm, s);
69 FREE(s);
70 }
71 }
72
73 static void consolidateBaseArray(otfcc_Font *font, table_OTL *table, const otfcc_Options *options,
74 otl_BaseArray *baseArray) {
75 // consolidate bases
76 base_hash *hm = NULL;
77 for (glyphid_t k = 0; k < baseArray->length; k++) {
78 if (!GlyphOrder.consolidateHandle(font->glyph_order, &baseArray->items[k].glyph)) {
79 logWarning("[Consolidate] Ignored unknown glyph name %s.",
80 baseArray->items[k].glyph.name);
81 continue;
82 }
83 base_hash *s = NULL;
84 int gid = baseArray->items[k].glyph.index;
85 HASH_FIND_INT(hm, &gid, s);
86 if (!s) {
87 NEW(s);
88 s->gid = baseArray->items[k].glyph.index;
89 s->name = sdsdup(baseArray->items[k].glyph.name);
90 s->anchors = baseArray->items[k].anchors;
91 baseArray->items[k].anchors = NULL; // Transfer ownership
92 HASH_ADD_INT(hm, gid, s);
93 } else {
94 logWarning("[Consolidate] Ignored anchor double-definition for /%s.",
95 baseArray->items[k].glyph.name);
96 }
97 }
98 HASH_SORT(hm, base_by_gid);
99 otl_iBaseArray.clear(baseArray);
100 base_hash *s, *tmp;
101 HASH_ITER(hh, hm, s, tmp) {
102 otl_iBaseArray.push(
103 baseArray, ((otl_BaseRecord){
104 .glyph = Handle.fromConsolidated(s->gid, s->name), .anchors = s->anchors,
105 }));
106 sdsfree(s->name);
107 HASH_DEL(hm, s);
108 FREE(s);
109 }
110 }
111
112 static void consolidateLigArray(otfcc_Font *font, table_OTL *table, const otfcc_Options *options,
113 otl_LigatureArray *ligArray) {
114 lig_hash *hm = NULL;
115 for (glyphid_t k = 0; k < ligArray->length; k++) {
116 if (!GlyphOrder.consolidateHandle(font->glyph_order, &ligArray->items[k].glyph)) {
117 logWarning("[Consolidate] Ignored unknown glyph name %s.",
118 ligArray->items[k].glyph.name);
119 continue;
120 }
121 lig_hash *s = NULL;
122 int gid = ligArray->items[k].glyph.index;
123 HASH_FIND_INT(hm, &gid, s);
124 if (!s) {
125 NEW(s);
126 s->gid = ligArray->items[k].glyph.index;
127 s->name = sdsdup(ligArray->items[k].glyph.name);
128 s->componentCount = ligArray->items[k].componentCount;
129 s->anchors = ligArray->items[k].anchors;
130 ligArray->items[k].anchors = NULL;
131 HASH_ADD_INT(hm, gid, s);
132 } else {
133 logWarning("[Consolidate] Ignored anchor double-definition for /%s.",
134 ligArray->items[k].glyph.name);
135 }
136 }
137 HASH_SORT(hm, lig_by_gid);
138 otl_iLigatureArray.clear(ligArray);
139 lig_hash *s, *tmp;
140 HASH_ITER(hh, hm, s, tmp) {
141 otl_iLigatureArray.push(ligArray, ((otl_LigatureBaseRecord){
142 .glyph = Handle.fromConsolidated(s->gid, s->name),
143 .componentCount = s->componentCount,
144 .anchors = s->anchors,
145 }));
146 sdsfree(s->name);
147 HASH_DEL(hm, s);
148 FREE(s);
149 }
150 }
151
152 bool consolidate_mark_to_single(otfcc_Font *font, table_OTL *table, otl_Subtable *_subtable,
153 const otfcc_Options *options) {
154 subtable_gpos_markToSingle *subtable = &(_subtable->gpos_markToSingle);
155 consolidateMarkArray(font, table, options, &subtable->markArray, subtable->classCount);
156 consolidateBaseArray(font, table, options, &subtable->baseArray);
157 return (subtable->markArray.length == 0) || (subtable->baseArray.length == 0);
158 }
159
160 bool consolidate_mark_to_ligature(otfcc_Font *font, table_OTL *table, otl_Subtable *_subtable,
161 const otfcc_Options *options) {
162 subtable_gpos_markToLigature *subtable = &(_subtable->gpos_markToLigature);
163 consolidateMarkArray(font, table, options, &subtable->markArray, subtable->classCount);
164 consolidateLigArray(font, table, options, &subtable->ligArray);
165 return (subtable->markArray.length == 0) || (subtable->ligArray.length == 0);
166 }