Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/absfont/absfont_desc.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 * Abstract font descriptor management.
7 */
8
9 #include "abfdesc.h"
10 #include "absfont.h"
11 #include "dictops.h"
12
13 #include <string.h>
14
15 /* Fill out the size fields in an abfFontDescElement. */
16 static void fillFontDescElementSizes(abfFontDict *font,
17 abfFontDescSpecialValues *sv,
18 abfFontDescElement *elem) {
19 abfPrivateDict *Private = &font->Private;
20
21 elem->flags = 0;
22 elem->valueCnt = 0;
23
24 /* Count fixed-size values */
25 if (sv->CharstringType != cff_DFLT_CharstringType) {
26 elem->flags |= ABF_DESC_CharstringType;
27 elem->valueCnt++;
28 }
29 if (font->PaintType != cff_DFLT_PaintType) {
30 elem->flags |= ABF_DESC_PaintType;
31 elem->valueCnt++;
32 }
33 if (Private->BlueScale != (float)cff_DFLT_BlueScale) {
34 elem->flags |= ABF_DESC_BlueScale;
35 elem->valueCnt++;
36 }
37 if (Private->BlueShift != cff_DFLT_BlueShift) {
38 elem->flags |= ABF_DESC_BlueShift;
39 elem->valueCnt++;
40 }
41 if (Private->BlueFuzz != cff_DFLT_BlueFuzz) {
42 elem->flags |= ABF_DESC_BlueFuzz;
43 elem->valueCnt++;
44 }
45 if (Private->StdHW != ABF_UNSET_REAL) {
46 elem->flags |= ABF_DESC_StdHW;
47 elem->valueCnt++;
48 }
49 if (Private->StdVW != ABF_UNSET_REAL) {
50 elem->flags |= ABF_DESC_StdVW;
51 elem->valueCnt++;
52 }
53 if (Private->ForceBold != cff_DFLT_ForceBold) {
54 elem->flags |= ABF_DESC_ForceBold;
55 elem->valueCnt++;
56 }
57 if (Private->LanguageGroup != cff_DFLT_LanguageGroup) {
58 elem->flags |= ABF_DESC_LanguageGroup;
59 elem->valueCnt++;
60 }
61 if (Private->ExpansionFactor != (float)cff_DFLT_ExpansionFactor) {
62 elem->flags |= ABF_DESC_ExpansionFactor;
63 elem->valueCnt++;
64 }
65 if (Private->initialRandomSeed != cff_DFLT_initialRandomSeed) {
66 elem->flags |= ABF_DESC_initialRandomSeed;
67 elem->valueCnt++;
68 }
69 if (sv->defaultWidthX != cff_DFLT_defaultWidthX) {
70 elem->flags |= ABF_DESC_defaultWidthX;
71 elem->valueCnt++;
72 }
73 if (sv->nominalWidthX != cff_DFLT_nominalWidthX) {
74 elem->flags |= ABF_DESC_nominalWidthX;
75 elem->valueCnt++;
76 }
77 if (sv->lenSubrArray != 0) {
78 elem->flags |= ABF_DESC_lenSubrArray;
79 elem->valueCnt++;
80 }
81 if (!abfIsDefaultFontMatrix(&font->FontMatrix)) {
82 elem->flags |= ABF_DESC_FontMatrix;
83 elem->valueCnt += 6;
84 }
85
86 /* Count variable-size values */
87 elem->BlueValues = (unsigned char)Private->BlueValues.cnt;
88 elem->OtherBlues = (unsigned char)Private->OtherBlues.cnt;
89 elem->FamilyBlues = (unsigned char)Private->FamilyBlues.cnt;
90 elem->FamilyOtherBlues = (unsigned char)Private->FamilyOtherBlues.cnt;
91 elem->StemSnapH = (unsigned char)Private->StemSnapH.cnt;
92 elem->StemSnapV = (unsigned char)Private->StemSnapV.cnt;
93
94 /* Total values */
95 elem->valueCnt += (elem->BlueValues +
96 elem->OtherBlues +
97 elem->FamilyBlues +
98 elem->FamilyOtherBlues +
99 elem->StemSnapH +
100 elem->StemSnapV);
101 }
102
103 /* Save float value in destination array. */
104 static float *saveValue(float *dst, float src, int save) {
105 if (save != 0)
106 *dst++ = src;
107 return dst;
108 }
109
110 /* Save float values in destination array. */
111 static float *saveValues(float *dst, float *src, int cnt) {
112 if (cnt > 0)
113 memmove(dst, src, cnt * sizeof(float));
114 return dst + cnt;
115 }
116
117 /* Fill out the value fields in an abfFontDescElement. */
118 static void fillFontDescElementValues(abfFontDict *font,
119 abfFontDescSpecialValues *sv,
120 abfFontDescElement *elem) {
121 float *values = elem->values;
122 abfPrivateDict *Private = &font->Private;
123
124 /* Fill single-valued parameter values */
125 values = saveValue(values, (float)sv->CharstringType,
126 elem->flags & ABF_DESC_CharstringType);
127 values = saveValue(values, (float)font->PaintType,
128 elem->flags & ABF_DESC_PaintType);
129 values = saveValue(values, Private->BlueScale,
130 elem->flags & ABF_DESC_BlueScale);
131 values = saveValue(values, Private->BlueShift,
132 elem->flags & ABF_DESC_BlueShift);
133 values = saveValue(values, Private->BlueFuzz,
134 elem->flags & ABF_DESC_BlueFuzz);
135 values = saveValue(values, Private->StdHW,
136 elem->flags & ABF_DESC_StdHW);
137 values = saveValue(values, Private->StdVW,
138 elem->flags & ABF_DESC_StdVW);
139 values = saveValue(values, (float)Private->LanguageGroup,
140 elem->flags & ABF_DESC_LanguageGroup);
141 values = saveValue(values, (float)Private->ForceBold,
142 elem->flags & ABF_DESC_ForceBold);
143 values = saveValue(values, Private->ExpansionFactor,
144 elem->flags & ABF_DESC_ExpansionFactor);
145 values = saveValue(values, Private->initialRandomSeed,
146 elem->flags & ABF_DESC_initialRandomSeed);
147 values = saveValue(values, sv->defaultWidthX,
148 elem->flags & ABF_DESC_defaultWidthX);
149 values = saveValue(values, sv->nominalWidthX,
150 elem->flags & ABF_DESC_nominalWidthX);
151 values = saveValue(values, (float)sv->lenSubrArray,
152 elem->flags & ABF_DESC_lenSubrArray);
153
154 /* Fill multi-valued parameter values */
155 values = saveValues(values, font->FontMatrix.array,
156 (elem->flags & ABF_DESC_FontMatrix) ? 6 : 0);
157 values = saveValues(values, Private->BlueValues.array, elem->BlueValues);
158 values = saveValues(values, Private->OtherBlues.array, elem->OtherBlues);
159 values = saveValues(values, Private->FamilyBlues.array, elem->FamilyBlues);
160 values = saveValues(values, Private->FamilyOtherBlues.array,
161 elem->FamilyOtherBlues);
162 values = saveValues(values, Private->StemSnapH.array, elem->StemSnapH);
163 saveValues(values, Private->StemSnapV.array, elem->StemSnapV);
164 }
165
166 /* Create abstract font descriptor. */
167 abfFontDescHeader *abfNewFontDesc(ctlMemoryCallbacks *mem_cb,
168 abfFontDescCallbacks *desc_cb,
169 long lenGSubrArray,
170 abfTopDict *top) {
171 long i;
172 size_t size;
173 abfFontDescHeader *hdr;
174 abfFontDescElement *elem;
175 abfFontDescSpecialValues sv;
176
177 /* Compute total structure size */
178 size = sizeof(abfFontDescHeader);
179 for (i = 0; i < top->FDArray.cnt; i++) {
180 abfFontDescElement tmp;
181 desc_cb->getSpecialValues(desc_cb, (int)i, &sv);
182 fillFontDescElementSizes(&top->FDArray.array[i], &sv, &tmp);
183 size += (sizeof(abfFontDescElement) +
184 (tmp.valueCnt - 1) * sizeof(tmp.values[0]));
185 }
186
187 /* Allocate structure */
188 hdr = (abfFontDescHeader *)mem_cb->manage(mem_cb, NULL, size);
189 if (hdr == NULL)
190 return NULL;
191 // memset(hdr, 0, size);
192
193 /* Fill header */
194 hdr->length = (unsigned short)size;
195 hdr->FDElementCnt = (unsigned short)top->FDArray.cnt;
196 memcpy(hdr->FontBBox, top->FontBBox, sizeof(hdr->FontBBox));
197 hdr->StrokeWidth = top->StrokeWidth;
198 hdr->lenGSubrArray = (float)lenGSubrArray;
199
200 /* Fill elements */
201 elem = ABF_GET_FIRST_DESC(hdr);
202 for (i = 0; i < top->FDArray.cnt; i++) {
203 abfFontDict *fd = &top->FDArray.array[i];
204 desc_cb->getSpecialValues(desc_cb, (int)i, &sv);
205 fillFontDescElementSizes(fd, &sv, elem);
206 fillFontDescElementValues(fd, &sv, elem);
207 elem = ABF_GET_NEXT_DESC(elem);
208 }
209
210 return hdr;
211 }
212
213 /* Return FontMatrix from font descriptor. */
214 float *abfGetFontDescMatrix(abfFontDescHeader *hdr, int iFD) {
215 if (iFD >= 0 && iFD < hdr->FDElementCnt) {
216 /* Skip to FDArray element */
217 abfFontDescElement *elem = ABF_GET_FIRST_DESC(hdr);
218 while (iFD--)
219 elem = ABF_GET_NEXT_DESC(elem);
220
221 if (elem->flags & ABF_DESC_FontMatrix) {
222 /* Non-default FontMatrix; count values that precede it */
223 long count;
224 long bits = elem->flags & (ABF_DESC_FontMatrix - 1);
225 for (count = 0; bits; count++)
226 bits &= bits - 1;
227 if (count + 6 <= (long)elem->valueCnt)
228 return &elem->values[count];
229 }
230 }
231 return NULL;
232 }
233
234 /* Free abstract font descriptor. */
235 void abfFreeFontDesc(ctlMemoryCallbacks *mem_cb, abfFontDescHeader *hdr) {
236 (void)mem_cb->manage(mem_cb, hdr, 0);
237 }