Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/table/otl/subtables/gpos-common.c

raw
1 #include "gpos-common.h"
2
3 static void deleteMarkArrayItem(otl_MarkRecord *entry) {
4 Handle.dispose(&entry->glyph);
5 }
6 static caryll_ElementInterface(otl_MarkRecord) gss_typeinfo = {
7 .init = NULL, .copy = NULL, .dispose = deleteMarkArrayItem};
8
9 caryll_standardVectorImpl(otl_MarkArray, otl_MarkRecord, gss_typeinfo, otl_iMarkArray);
10
11 void otl_readMarkArray(otl_MarkArray *array, otl_Coverage *cov, font_file_pointer data,
12 uint32_t tableLength, uint32_t offset) {
13 checkLength(offset + 2);
14 glyphid_t markCount = read_16u(data + offset);
15 for (glyphid_t j = 0; j < markCount; j++) {
16 glyphclass_t markClass = read_16u(data + offset + 2 + j * 4);
17 uint16_t delta = read_16u(data + offset + 2 + j * 4 + 2);
18 if (delta) {
19 otl_iMarkArray.push(
20 array,
21 ((otl_MarkRecord){.glyph = Handle.dup(cov->glyphs[j]),
22 .markClass = markClass,
23 .anchor = otl_read_anchor(data, tableLength, offset + delta)}));
24 } else {
25 otl_iMarkArray.push(array, ((otl_MarkRecord){.glyph = Handle.dup(cov->glyphs[j]),
26 .markClass = markClass,
27 .anchor = otl_anchor_absent()}));
28 }
29 }
30 FAIL:
31 return;
32 }
33
34 static int compare_classHash(otl_ClassnameHash *a, otl_ClassnameHash *b) {
35 return strcmp(a->className, b->className);
36 }
37 void otl_parseMarkArray(json_value *_marks, otl_MarkArray *array, otl_ClassnameHash **h,
38 const otfcc_Options *options) {
39 for (glyphid_t j = 0; j < _marks->u.object.length; j++) {
40 otl_MarkRecord mark;
41 char *gname = _marks->u.object.values[j].name;
42 json_value *anchorRecord = _marks->u.object.values[j].value;
43 mark.glyph = Handle.fromName(sdsnewlen(gname, _marks->u.object.values[j].name_length));
44 mark.markClass = 0;
45 mark.anchor = otl_anchor_absent();
46
47 if (!anchorRecord || anchorRecord->type != json_object) {
48 otl_iMarkArray.push(array, mark);
49 continue;
50 }
51 json_value *_className = json_obj_get_type(anchorRecord, "class", json_string);
52 if (!_className) {
53 otl_iMarkArray.push(array, mark);
54 continue;
55 }
56
57 sds className = sdsnewlen(_className->u.string.ptr, _className->u.string.length);
58 otl_ClassnameHash *s;
59 HASH_FIND_STR(*h, className, s);
60 if (!s) {
61 NEW(s);
62 s->className = className;
63 s->classID = HASH_COUNT(*h);
64 HASH_ADD_STR(*h, className, s);
65 } else {
66 sdsfree(className);
67 }
68 mark.markClass = s->classID;
69 mark.anchor.present = true;
70 mark.anchor.x = json_obj_getnum(anchorRecord, "x");
71 mark.anchor.y = json_obj_getnum(anchorRecord, "y");
72 otl_iMarkArray.push(array, mark);
73 }
74
75 HASH_SORT(*h, compare_classHash);
76 glyphid_t jAnchorIndex = 0;
77 otl_ClassnameHash *s;
78 foreach_hash(s, *h) {
79 s->classID = jAnchorIndex;
80 jAnchorIndex++;
81 }
82 for (glyphid_t j = 0; j < array->length; j++) {
83 if (!array->items[j].anchor.present) continue;
84 json_value *anchorRecord = _marks->u.object.values[j].value;
85 json_value *_className = json_obj_get_type(anchorRecord, "class", json_string);
86 sds className = sdsnewlen(_className->u.string.ptr, _className->u.string.length);
87 otl_ClassnameHash *s;
88 HASH_FIND_STR(*h, className, s);
89 if (s) {
90 array->items[j].markClass = s->classID;
91 } else {
92 array->items[j].markClass = 0;
93 }
94 sdsfree(className);
95 }
96 }
97
98 otl_Anchor otl_anchor_absent() {
99 otl_Anchor anchor = {.present = false, .x = 0, .y = 0};
100 return anchor;
101 }
102 otl_Anchor otl_read_anchor(font_file_pointer data, uint32_t tableLength, uint32_t offset) {
103 otl_Anchor anchor = {.present = false, .x = 0, .y = 0};
104 checkLength(offset + 6);
105 anchor.present = true;
106 anchor.x = read_16s(data + offset + 2);
107 anchor.y = read_16s(data + offset + 4);
108 return anchor;
109 FAIL:
110 anchor.present = false;
111 anchor.x = 0;
112 anchor.y = 0;
113 return anchor;
114 }
115 json_value *otl_dump_anchor(otl_Anchor a) {
116 if (a.present) {
117 json_value *v = json_object_new(2);
118 json_object_push(v, "x", json_new_position(a.x));
119 json_object_push(v, "y", json_new_position(a.y));
120 return v;
121 } else {
122 return json_null_new();
123 }
124 }
125 otl_Anchor otl_parse_anchor(json_value *v) {
126 otl_Anchor anchor = {.present = false, .x = 0, .y = 0};
127 if (!v || v->type != json_object) return anchor;
128 anchor.present = true;
129 anchor.x = json_obj_getnum_fallback(v, "x", 0);
130 anchor.y = json_obj_getnum_fallback(v, "y", 0);
131 return anchor;
132 }
133
134 bk_Block *bkFromAnchor(otl_Anchor a) {
135 if (!a.present) return NULL;
136 return bk_new_Block(b16, 1, // format
137 b16, (int16_t)a.x, // x
138 b16, (int16_t)a.y, // y
139 bkover);
140 }
141
142 // GPOS position value constants
143 const uint8_t FORMAT_DX = 1;
144 const uint8_t FORMAT_DY = 2;
145 const uint8_t FORMAT_DWIDTH = 4;
146 const uint8_t FORMAT_DHEIGHT = 8;
147
148 #define a(n) n + 0, n + 1, n + 1, n + 2 // 2 bits
149 #define b(n) a(n + 0), a(n + 1), a(n + 1), a(n + 2) // 4 bits
150 #define c(n) b(n + 0), b(n + 1), b(n + 1), b(n + 2) // 6 bits
151 #define d(n) c(n + 0), c(n + 1), c(n + 1), c(n + 2) // 8 bits
152 const uint8_t bits_in[0x100] = {d(0)};
153 #undef d
154 #undef c
155 #undef b
156 #undef a
157
158 // Length of a position value in bytes
159 uint8_t position_format_length(uint16_t format) {
160 return bits_in[format & 0xFF] << 1;
161 }
162 otl_PositionValue position_zero() {
163 otl_PositionValue v = {0.0, 0.0, 0.0, 0.0};
164 return v;
165 }
166 // Read a position value from SFNT
167 otl_PositionValue read_gpos_value(font_file_pointer data, uint32_t tableLength, uint32_t offset,
168 uint16_t format) {
169 otl_PositionValue v = {0.0, 0.0, 0.0, 0.0};
170 if (tableLength < offset + position_format_length(format)) return v;
171 if (format & FORMAT_DX) { v.dx = read_16s(data + offset), offset += 2; };
172 if (format & FORMAT_DY) { v.dy = read_16s(data + offset), offset += 2; };
173 if (format & FORMAT_DWIDTH) { v.dWidth = read_16s(data + offset), offset += 2; };
174 if (format & FORMAT_DHEIGHT) { v.dHeight = read_16s(data + offset), offset += 2; };
175 return v;
176 }
177 json_value *gpos_dump_value(otl_PositionValue value) {
178 json_value *v = json_object_new(4);
179 if (value.dx) json_object_push(v, "dx", json_new_position(value.dx));
180 if (value.dy) json_object_push(v, "dy", json_new_position(value.dy));
181 if (value.dWidth) json_object_push(v, "dWidth", json_new_position(value.dWidth));
182 if (value.dHeight) json_object_push(v, "dHeight", json_new_position(value.dHeight));
183 return preserialize(v);
184 }
185 otl_PositionValue gpos_parse_value(json_value *pos) {
186 otl_PositionValue v = {0.0, 0.0, 0.0, 0.0};
187 if (!pos || pos->type != json_object) return v;
188 v.dx = json_obj_getnum(pos, "dx");
189 v.dy = json_obj_getnum(pos, "dy");
190 v.dWidth = json_obj_getnum(pos, "dWidth");
191 v.dHeight = json_obj_getnum(pos, "dHeight");
192 return v;
193 }
194 // The required format of a position value
195 uint8_t required_position_format(otl_PositionValue v) {
196 return (v.dx ? FORMAT_DX : 0) | (v.dy ? FORMAT_DY : 0) | (v.dWidth ? FORMAT_DWIDTH : 0) |
197 (v.dHeight ? FORMAT_DHEIGHT : 0);
198 }
199 // Write gpos position value
200 void write_gpos_value(caryll_Buffer *buf, otl_PositionValue v, uint16_t format) {
201 if (format & FORMAT_DX) bufwrite16b(buf, (int16_t)v.dx);
202 if (format & FORMAT_DY) bufwrite16b(buf, (int16_t)v.dy);
203 if (format & FORMAT_DWIDTH) bufwrite16b(buf, (int16_t)v.dWidth);
204 if (format & FORMAT_DHEIGHT) bufwrite16b(buf, (int16_t)v.dHeight);
205 }
206
207 bk_Block *bk_gpos_value(otl_PositionValue v, uint16_t format) {
208 bk_Block *b = bk_new_Block(bkover);
209 if (format & FORMAT_DX) bk_push(b, b16, (int16_t)v.dx, bkover);
210 if (format & FORMAT_DY) bk_push(b, b16, (int16_t)v.dy, bkover);
211 if (format & FORMAT_DWIDTH) bk_push(b, b16, (int16_t)v.dWidth, bkover);
212 if (format & FORMAT_DHEIGHT) bk_push(b, b16, (int16_t)v.dHeight, bkover);
213 return b;
214 }