Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/cffwrite/cffwrite_charset.c

raw
1 /* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4 /***********************************************************************/
5
6 #include "cffwrite_charset.h"
7
8 #include <string.h>
9
10 /* Declarations used for determining sizes and for format documentation */
11 typedef struct {
12 SID *glyph; /* [nGlyphs] */
13 } Format0;
14 #define FORMAT0_SIZE(n) (1 + (n)*2)
15
16 typedef struct {
17 SID first; /* First glyph in range */
18 unsigned char nLeft; /* Number of glyphs left in range (excluding first) */
19 } Range1;
20 #define RANGE1_SIZE (2 + 1)
21
22 typedef struct {
23 unsigned char format;
24 Range1 *range; /* [nRanges] */
25 } Format1;
26 #define FORMAT1_SIZE(n) (1 + (n)*RANGE1_SIZE)
27
28 typedef struct {
29 SID first; /* First glyph in range */
30 unsigned short nLeft; /* Number of glyphs left in range (excluding first) */
31 } Range2;
32 #define RANGE2_SIZE (2 * 2)
33
34 typedef struct {
35 unsigned char format;
36 Range2 *range; /* [nRanges] */
37 } Format2;
38 #define FORMAT2_SIZE(n) (1 + (n)*RANGE2_SIZE)
39
40 typedef struct {
41 dnaDCL(unsigned short, nameids); /* Name ids (SID/CID) */
42 unsigned char format; /* Selected format */
43 Offset offset; /* Offset relative to first charset */
44 int is_cid; /* Flags CID charset */
45 } Charset;
46
47 /* Predefined charsets */
48 static const SID isoadobe[] = {
49 #include "isocs0.h"
50 };
51 static const SID expert[] = {
52 #include "excs0.h"
53 };
54 static const SID expertsubset[] = {
55 #include "exsubcs0.h"
56 };
57 static const struct {
58 long cnt;
59 const SID *gnames;
60 } predef[] = {
61 {ARRAY_LEN(isoadobe), isoadobe}, /* 0 isoadobe */
62 {ARRAY_LEN(expert), expert}, /* 1 Expert */
63 {ARRAY_LEN(expertsubset), expertsubset}, /* 2 Expert subset */
64 };
65 #define PREDEF_CNT ARRAY_LEN(predef)
66
67 /* ----------------------------- Module context ---------------------------- */
68
69 struct charsetCtx_ {
70 dnaDCL(Charset, charsets);
71 Charset *_new; /* Charset being added */
72 cfwCtx g; /* Package context */
73 };
74
75 /* Initialize charset. */
76 static void initCharset(void *ctx, long cnt, Charset *charset) {
77 cfwCtx g = (cfwCtx)ctx;
78 while (cnt--) {
79 dnaINIT(g->ctx.dnaSafe, charset->nameids, 256, 750);
80 charset++;
81 }
82 }
83
84 /* Initialize module. */
85 void cfwCharsetNew(cfwCtx g) {
86 charsetCtx h = (charsetCtx)cfwMemNew(g, sizeof(struct charsetCtx_));
87 unsigned int i;
88
89 /* Link contexts */
90 h->g = g;
91 g->ctx.charset = h;
92
93 dnaINIT(g->ctx.dnaSafe, h->charsets, 4, 10);
94 h->charsets.func = initCharset;
95
96 /* Add standard charsets to accumulator */
97 for (i = 0; i < PREDEF_CNT; i++) {
98 Charset *charset = dnaNEXT(h->charsets);
99 charset->nameids.cnt = predef[i].cnt;
100 charset->nameids.array = (SID *)predef[i].gnames;
101 }
102 }
103
104 /* Prepare module for reuse. */
105 void cfwCharsetReuse(cfwCtx g) {
106 charsetCtx h = g->ctx.charset;
107 h->charsets.cnt = PREDEF_CNT;
108 }
109
110 /* Free resources. */
111 void cfwCharsetFree(cfwCtx g) {
112 charsetCtx h = g->ctx.charset;
113 int i;
114
115 if (h == NULL) {
116 return;
117 }
118
119 for (i = PREDEF_CNT; i < h->charsets.size; i++) {
120 dnaFREE(h->charsets.array[i].nameids);
121 }
122 dnaFREE(h->charsets);
123
124 cfwMemFree(g, h);
125 g->ctx.charset = NULL;
126 }
127
128 /* Begin new charset. */
129 void cfwCharsetBeg(cfwCtx g, int is_cid) {
130 charsetCtx h = g->ctx.charset;
131 h->_new = dnaNEXT(h->charsets);
132 h->_new->nameids.cnt = 0;
133 h->_new->is_cid = is_cid;
134 }
135
136 /* Add SID/CID to charset. */
137 void cfwCharsetAddGlyph(cfwCtx g, unsigned short nameid) {
138 charsetCtx h = g->ctx.charset;
139 *dnaNEXT(h->_new->nameids) = nameid;
140 }
141
142 /* End glyph name addition and check for duplicate charset. */
143 int cfwCharsetEnd(cfwCtx g) {
144 charsetCtx h = g->ctx.charset;
145 int i;
146
147 for (i = h->_new->is_cid ? PREDEF_CNT : 0; i < h->charsets.cnt - 1; i++) {
148 Charset *charset = &h->charsets.array[i];
149 if (h->_new->nameids.cnt <= charset->nameids.cnt &&
150 memcmp(h->_new->nameids.array,
151 charset->nameids.array,
152 h->_new->nameids.cnt *
153 sizeof(h->_new->nameids.array[0])) == 0) {
154 /* Found match; remove new charset from list */
155 h->charsets.cnt--;
156 return i;
157 }
158 }
159
160 /* No match found; leave charset in list */
161 return h->charsets.cnt - 1;
162 }
163
164 /* Fill charsets. */
165 long cfwCharsetFill(cfwCtx g) {
166 charsetCtx h = g->ctx.charset;
167 int i;
168 Offset offset;
169
170 /* Compute charset offsets */
171 offset = 0;
172 for (i = PREDEF_CNT; i < h->charsets.cnt; i++) {
173 unsigned j;
174 unsigned nRanges1;
175 unsigned nRanges2;
176 unsigned nLeft1;
177 unsigned nLeft2;
178 Charset *charset = &h->charsets.array[i];
179 long size0 = FORMAT0_SIZE(charset->nameids.cnt);
180 long size1;
181 long size2;
182
183 /* Determine range1 size */
184 nRanges1 = nRanges2 = 1;
185 nLeft1 = nLeft2 = 0;
186 for (j = 1; j < (unsigned)charset->nameids.cnt; j++) {
187 int breakSeq =
188 charset->nameids.array[j - 1] + 1 != charset->nameids.array[j];
189 if (breakSeq || nLeft1 == 255) {
190 nRanges1++;
191 nLeft1 = 0;
192 } else {
193 nLeft1++;
194 }
195 if (breakSeq) {
196 nRanges2++;
197 nLeft2 = 0;
198 } else {
199 nLeft2++;
200 }
201 }
202 size1 = FORMAT1_SIZE(nRanges1);
203 size2 = FORMAT2_SIZE(nRanges2);
204
205 /* Save format and offset */
206 charset->offset = offset;
207 if (size0 < size1) {
208 if (size0 < size2) {
209 charset->format = 0;
210 offset += size0;
211 } else {
212 charset->format = 2;
213 offset += size2;
214 }
215 } else {
216 if (size1 < size2) {
217 charset->format = 1;
218 offset += size1;
219 } else {
220 charset->format = 2;
221 offset += size2;
222 }
223 }
224 }
225 return offset;
226 }
227
228 /* Write charsets and encodings. */
229 void cfwCharsetWrite(cfwCtx g) {
230 charsetCtx h = g->ctx.charset;
231 int i;
232
233 /* Write non-standard charsets */
234 for (i = PREDEF_CNT; i < h->charsets.cnt; i++) {
235 unsigned j;
236 Charset *charset = &h->charsets.array[i];
237
238 cfwWrite1(g, charset->format);
239 switch (charset->format) {
240 case 0:
241 /* Write format 0 */
242 for (j = 0; j < (unsigned)charset->nameids.cnt; j++) {
243 cfwWrite2(g, charset->nameids.array[j]);
244 }
245 break;
246
247 case 1: {
248 /* Write format 1 */
249 unsigned char nLeft = 0;
250
251 cfwWrite2(g, charset->nameids.array[0]);
252 for (j = 1; j < (unsigned)charset->nameids.cnt; j++) {
253 if (charset->nameids.array[j - 1] + 1 !=
254 charset->nameids.array[j] ||
255 nLeft == 255) {
256 cfwWrite1(g, nLeft);
257 cfwWrite2(g, charset->nameids.array[j]);
258 nLeft = 0;
259 } else {
260 nLeft++;
261 }
262 }
263 cfwWrite1(g, nLeft);
264 } break;
265
266 case 2: {
267 /* Write format 2 */
268 unsigned short nLeft = 0;
269
270 cfwWrite2(g, charset->nameids.array[0]);
271 for (j = 1; j < (unsigned)charset->nameids.cnt; j++) {
272 if (charset->nameids.array[j - 1] + 1 !=
273 charset->nameids.array[j]) {
274 cfwWrite2(g, nLeft);
275 cfwWrite2(g, charset->nameids.array[j]);
276 nLeft = 0;
277 } else {
278 nLeft++;
279 }
280 }
281 cfwWrite2(g, nLeft);
282 } break;
283 }
284 }
285 }
286
287 /* Get absolute charset offset. */
288 Offset cfwCharsetGetOffset(cfwCtx g, int iCharset, Offset base) {
289 charsetCtx h = g->ctx.charset;
290 return (iCharset < (int)PREDEF_CNT) ? iCharset : base + h->charsets.array[iCharset].offset;
291 }