Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/table/glyf/build.c

raw
1 #include "../glyf.h"
2
3 #include "support/util.h"
4 #include "support/ttinstr/ttinstr.h"
5
6 static caryll_Buffer *shrinkFlags(caryll_Buffer *flags) {
7 if (!buflen(flags)) return (flags);
8 caryll_Buffer *shrunk = bufnew();
9 bufwrite8(shrunk, flags->data[0]);
10 int repeating = 0;
11 for (size_t j = 1; j < buflen(flags); j++) {
12 if (flags->data[j] == flags->data[j - 1]) {
13 if (repeating && repeating < 0xFE) {
14 shrunk->data[shrunk->cursor - 1] += 1;
15 repeating += 1;
16 } else if (repeating == 0) {
17 shrunk->data[shrunk->cursor - 1] |= GLYF_FLAG_REPEAT;
18 bufwrite8(shrunk, 1);
19 repeating += 1;
20 } else {
21 repeating = 0;
22 bufwrite8(shrunk, flags->data[j]);
23 }
24 } else {
25 repeating = 0;
26 bufwrite8(shrunk, flags->data[j]);
27 }
28 }
29 buffree(flags);
30 return shrunk;
31 }
32
33 // serialize
34 #define EPSILON (1e-5)
35 static void glyf_build_simple(const glyf_Glyph *g, caryll_Buffer *gbuf) {
36 caryll_Buffer *flags = bufnew();
37 caryll_Buffer *xs = bufnew();
38 caryll_Buffer *ys = bufnew();
39
40 bufwrite16b(gbuf, g->contours.length);
41 bufwrite16b(gbuf, (int16_t)g->stat.xMin);
42 bufwrite16b(gbuf, (int16_t)g->stat.yMin);
43 bufwrite16b(gbuf, (int16_t)g->stat.xMax);
44 bufwrite16b(gbuf, (int16_t)g->stat.yMax);
45
46 // endPtsOfContours[n]
47 shapeid_t ptid = 0;
48 for (shapeid_t j = 0; j < g->contours.length; j++) {
49 ptid += g->contours.items[j].length;
50 bufwrite16b(gbuf, ptid - 1);
51 }
52
53 // instructions
54 bufwrite16b(gbuf, g->instructionsLength);
55 if (g->instructions) bufwrite_bytes(gbuf, g->instructionsLength, g->instructions);
56
57 // flags and points
58 bufclear(flags);
59 bufclear(xs);
60 bufclear(ys);
61 int32_t cx = 0;
62 int32_t cy = 0;
63 for (shapeid_t cj = 0; cj < g->contours.length; cj++) {
64 for (shapeid_t k = 0; k < g->contours.items[cj].length; k++) {
65 glyf_Point *p = &(g->contours.items[cj].items[k]);
66 uint8_t flag = (p->onCurve & MASK_ON_CURVE) ? GLYF_FLAG_ON_CURVE : 0;
67 int32_t px = round(iVQ.getStill(p->x));
68 int32_t py = round(iVQ.getStill(p->y));
69 int16_t dx = (int16_t)(px - cx);
70 int16_t dy = (int16_t)(py - cy);
71 if (dx == 0) {
72 flag |= GLYF_FLAG_SAME_X;
73 } else if (dx >= -0xFF && dx <= 0xFF) {
74 flag |= GLYF_FLAG_X_SHORT;
75 if (dx > 0) {
76 flag |= GLYF_FLAG_POSITIVE_X;
77 bufwrite8(xs, dx);
78 } else {
79 bufwrite8(xs, -dx);
80 }
81 } else {
82 bufwrite16b(xs, dx);
83 }
84
85 if (dy == 0) {
86 flag |= GLYF_FLAG_SAME_Y;
87 } else if (dy >= -0xFF && dy <= 0xFF) {
88 flag |= GLYF_FLAG_Y_SHORT;
89 if (dy > 0) {
90 flag |= GLYF_FLAG_POSITIVE_Y;
91 bufwrite8(ys, dy);
92 } else {
93 bufwrite8(ys, -dy);
94 }
95 } else {
96 bufwrite16b(ys, dy);
97 }
98 bufwrite8(flags, flag);
99 cx = px;
100 cy = py;
101 }
102 }
103 flags = shrinkFlags(flags);
104 bufwrite_buf(gbuf, flags);
105 bufwrite_buf(gbuf, xs);
106 bufwrite_buf(gbuf, ys);
107
108 buffree(flags);
109 buffree(xs);
110 buffree(ys);
111 }
112 static void glyf_build_composite(const glyf_Glyph *g, caryll_Buffer *gbuf) {
113 bufwrite16b(gbuf, (-1));
114 bufwrite16b(gbuf, (int16_t)g->stat.xMin);
115 bufwrite16b(gbuf, (int16_t)g->stat.yMin);
116 bufwrite16b(gbuf, (int16_t)g->stat.xMax);
117 bufwrite16b(gbuf, (int16_t)g->stat.yMax);
118 for (shapeid_t rj = 0; rj < g->references.length; rj++) {
119 glyf_ComponentReference *r = &(g->references.items[rj]);
120 uint16_t flags =
121 (rj < g->references.length - 1 ? MORE_COMPONENTS
122 : g->instructionsLength > 0 ? WE_HAVE_INSTRUCTIONS : 0);
123 bool outputAnchor = r->isAnchored == REF_ANCHOR_CONSOLIDATED;
124
125 union {
126 uint16_t pointid;
127 int16_t coord;
128 } arg1, arg2;
129
130 // flags
131 if (outputAnchor) {
132 arg1.pointid = r->outer;
133 arg2.pointid = r->inner;
134 if (!(arg1.pointid < 0x100 && arg2.pointid < 0x100)) { flags |= ARG_1_AND_2_ARE_WORDS; }
135 } else {
136 flags |= ARGS_ARE_XY_VALUES;
137 arg1.coord = iVQ.getStill(r->x);
138 arg2.coord = iVQ.getStill(r->y);
139 if (!(arg1.coord < 128 && arg1.coord >= -128 && arg2.coord < 128 &&
140 arg2.coord >= -128)) {
141 flags |= ARG_1_AND_2_ARE_WORDS;
142 }
143 }
144 if (fabs(r->b) > EPSILON || fabs(r->c) > EPSILON) {
145 flags |= WE_HAVE_A_TWO_BY_TWO;
146 } else if (fabs(r->a - 1) > EPSILON || fabs(r->d - 1) > EPSILON) {
147 if (fabs(r->a - r->d) > EPSILON) {
148 flags |= WE_HAVE_AN_X_AND_Y_SCALE;
149 } else {
150 flags |= WE_HAVE_A_SCALE;
151 }
152 }
153 if (r->roundToGrid) flags |= ROUND_XY_TO_GRID;
154 if (r->useMyMetrics) flags |= USE_MY_METRICS;
155 flags |= UNSCALED_COMPONENT_OFFSET;
156 bufwrite16b(gbuf, flags);
157 bufwrite16b(gbuf, r->glyph.index);
158 if (flags & ARG_1_AND_2_ARE_WORDS) {
159 bufwrite16b(gbuf, arg1.pointid);
160 bufwrite16b(gbuf, arg2.pointid);
161 } else {
162 bufwrite8(gbuf, arg1.pointid);
163 bufwrite8(gbuf, arg2.pointid);
164 }
165 if (flags & WE_HAVE_A_SCALE) {
166 bufwrite16b(gbuf, otfcc_to_f2dot14(r->a));
167 } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
168 bufwrite16b(gbuf, otfcc_to_f2dot14(r->a));
169 bufwrite16b(gbuf, otfcc_to_f2dot14(r->d));
170 } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
171 bufwrite16b(gbuf, otfcc_to_f2dot14(r->a));
172 bufwrite16b(gbuf, otfcc_to_f2dot14(r->b));
173 bufwrite16b(gbuf, otfcc_to_f2dot14(r->c));
174 bufwrite16b(gbuf, otfcc_to_f2dot14(r->d));
175 }
176 }
177 if (g->instructionsLength) {
178 bufwrite16b(gbuf, g->instructionsLength);
179 if (g->instructions) bufwrite_bytes(gbuf, g->instructionsLength, g->instructions);
180 }
181 }
182 table_GlyfAndLocaBuffers otfcc_buildGlyf(const table_glyf *table, table_head *head,
183 const otfcc_Options *options) {
184 caryll_Buffer *bufglyf = bufnew();
185 caryll_Buffer *bufloca = bufnew();
186 if (table && head) {
187 caryll_Buffer *gbuf = bufnew();
188 uint32_t *loca;
189 NEW(loca, table->length + 1);
190 for (glyphid_t j = 0; j < table->length; j++) {
191 loca[j] = (uint32_t)bufglyf->cursor;
192 glyf_Glyph *g = table->items[j];
193 bufclear(gbuf);
194 if (g->contours.length > 0) {
195 glyf_build_simple(g, gbuf);
196 } else if (g->references.length > 0) {
197 glyf_build_composite(g, gbuf);
198 }
199 // pad extra zeroes
200 buflongalign(gbuf);
201 bufwrite_buf(bufglyf, gbuf);
202 }
203 loca[table->length] = (uint32_t)bufglyf->cursor;
204 if (bufglyf->cursor >= 0x20000) {
205 head->indexToLocFormat = 1;
206 } else {
207 head->indexToLocFormat = 0;
208 }
209 // write loca table
210 for (uint32_t j = 0; j <= table->length; j++) {
211 if (head->indexToLocFormat) {
212 bufwrite32b(bufloca, loca[j]);
213 } else {
214 bufwrite16b(bufloca, loca[j] >> 1);
215 }
216 }
217 buffree(gbuf);
218 FREE(loca);
219 }
220 table_GlyfAndLocaBuffers pair = {bufglyf, bufloca};
221 return pair;
222 }