Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/absfont/absfont_afm.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 * Adobe Font Metrics (AFM) file support.
7 */
8
9 #include "absfont.h"
10
11 #include <time.h>
12 #include <string.h>
13
14 /* Print key name and string if set. */
15 static void writeString(abfAFMCtx h, char *key, abfString *value) {
16 if (value->ptr == ABF_UNSET_PTR)
17 return;
18 fprintf(h->fp, "%s %s\n", key, value->ptr);
19 }
20
21 void abfAFMBegFont(abfAFMCtx h) {
22 /* Initialize context */
23 h->err_code = abfSuccess;
24 h->glyph_metrics.cb = abfGlyphMetricsCallbacks;
25 h->glyph_metrics.cb.direct_ctx = &h->glyph_metrics.ctx;
26 h->glyph_metrics.ctx.flags = 0;
27 h->font_bbox.left = INT16_MAX;
28 h->font_bbox.bottom = INT16_MAX;
29 h->font_bbox.right = INT16_MIN;
30 h->font_bbox.top = INT16_MIN;
31 }
32
33 void abfAFMEndFont(abfAFMCtx h, abfTopDict *top) {
34 time_t seconds_since_epoch = time(NULL);
35 struct tm local_time;
36 char time_text[32];
37 int c;
38
39 /* update top dict's font bounding box with aggregate values */
40 top->FontBBox[0] = (float)h->font_bbox.left;
41 top->FontBBox[1] = (float)h->font_bbox.bottom;
42 top->FontBBox[2] = (float)h->font_bbox.right;
43 top->FontBBox[3] = (float)h->font_bbox.top;
44
45 if (top->sup.flags & ABF_CID_FONT)
46 fprintf(h->fp, "StartFontMetrics 4.1\n");
47 else
48 fprintf(h->fp, "StartFontMetrics 2.0\n");
49
50 SAFE_LOCALTIME(&seconds_since_epoch, &local_time);
51 fprintf(h->fp,
52 "Comment Copyright %d Adobe Systems Incorporated. "
53 "All Rights Reserved.\n",
54 local_time.tm_year + 1900);
55 SAFE_CTIME(&seconds_since_epoch, time_text);
56 fprintf(h->fp, "Comment Creation Date: %s", time_text);
57 if (top->UniqueID != ABF_UNSET_INT)
58 fprintf(h->fp, "Comment UniqueID %ld\n", top->UniqueID);
59 if (top->sup.UnitsPerEm != 1000)
60 fprintf(h->fp, "Comment UnitsPerEm %ld\n", top->sup.UnitsPerEm);
61
62 if (top->sup.flags & ABF_CID_FONT) {
63 /* Write header for cid-keyed font */
64 fprintf(h->fp, "MetricsSets 2\n");
65 writeString(h, "FontName", &top->cid.CIDFontName);
66 writeString(h, "Weight", &top->Weight);
67 fprintf(h->fp, "FontBBox %g %g %g %g\n",
68 top->FontBBox[0], top->FontBBox[1],
69 top->FontBBox[2], top->FontBBox[3]);
70 fprintf(h->fp, "Version %.3f\n", top->cid.CIDFontVersion);
71 writeString(h, "Notice", &top->Notice);
72 fprintf(h->fp, "CharacterSet %s-%s-%ld\n", top->cid.Registry.ptr,
73 top->cid.Ordering.ptr, top->cid.Supplement);
74 fprintf(h->fp, "Characters %ld\n", top->sup.nGlyphs);
75 fprintf(h->fp, "IsBaseFont true\n");
76 fprintf(h->fp, "IsCIDFont true\n");
77 fprintf(h->fp, "StartDirection 2\n");
78 fprintf(h->fp, "UnderlinePosition %g\n", top->UnderlinePosition);
79 fprintf(h->fp, "UnderlineThickness %g\n", top->UnderlineThickness);
80 fprintf(h->fp, "ItalicAngle %g\n", top->ItalicAngle);
81 fprintf(h->fp, "IsFixedPitch %s\n",
82 top->isFixedPitch ? "true" : "false");
83 fprintf(h->fp, "EndDirection\n");
84 fprintf(h->fp, "StartCharMetrics %ld\n", top->sup.nGlyphs);
85 } else {
86 /* Write header for name-keyed font */
87 writeString(h, "FontName", &top->FDArray.array[0].FontName);
88 writeString(h, "FullName", &top->FullName);
89 writeString(h, "FamilyName", &top->FamilyName);
90 writeString(h, "Weight", &top->Weight);
91 fprintf(h->fp, "ItalicAngle %g\n", top->ItalicAngle);
92 fprintf(h->fp, "IsFixedPitch %s\n",
93 top->isFixedPitch ? "true" : "false");
94 fprintf(h->fp, "FontBBox %g %g %g %g\n",
95 top->FontBBox[0], top->FontBBox[1],
96 top->FontBBox[2], top->FontBBox[3]);
97 fprintf(h->fp, "UnderlinePosition %g\n", top->UnderlinePosition);
98 fprintf(h->fp, "UnderlineThickness %g\n", top->UnderlineThickness);
99 writeString(h, "Version", &top->version);
100 writeString(h, "Notice", &top->Notice);
101 fprintf(h->fp, "StartCharMetrics %ld\n", top->sup.nGlyphs - 1L);
102 }
103
104 /* copy glyph metrics from temp file to output file */
105 rewind(h->tmp_fp);
106 while ((c = fgetc(h->tmp_fp)) != EOF) {
107 fputc(c, h->fp);
108 }
109
110 fprintf(h->fp, "EndCharMetrics\n");
111 fprintf(h->fp, "EndFontMetrics\n");
112 }
113
114 /* Begin glyph path. */
115 static int glyphBeg(abfGlyphCallbacks *cb, abfGlyphInfo *info) {
116 abfAFMCtx h = (abfAFMCtx)cb->direct_ctx;
117 cb->info = info;
118 return h->glyph_metrics.cb.beg(&h->glyph_metrics.cb, info);
119 }
120
121 /* Save glyph width. */
122 static void glyphWidth(abfGlyphCallbacks *cb, float hAdv) {
123 abfAFMCtx h = (abfAFMCtx)cb->direct_ctx;
124 h->glyph_metrics.cb.width(&h->glyph_metrics.cb, hAdv);
125 }
126
127 /* Add move to path. */
128 static void glyphMove(abfGlyphCallbacks *cb, float x0, float y0) {
129 abfAFMCtx h = (abfAFMCtx)cb->direct_ctx;
130 h->glyph_metrics.cb.move(&h->glyph_metrics.cb, x0, y0);
131 }
132
133 /* Add line to path. */
134 static void glyphLine(abfGlyphCallbacks *cb, float x1, float y1) {
135 abfAFMCtx h = (abfAFMCtx)cb->direct_ctx;
136 h->glyph_metrics.cb.line(&h->glyph_metrics.cb, x1, y1);
137 }
138
139 /* Add curve to path. */
140 static void glyphCurve(abfGlyphCallbacks *cb,
141 float x1, float y1,
142 float x2, float y2,
143 float x3, float y3) {
144 abfAFMCtx h = (abfAFMCtx)cb->direct_ctx;
145 h->glyph_metrics.cb.curve(&h->glyph_metrics.cb, x1, y1, x2, y2, x3, y3);
146 }
147
148 /* Ignore stem operator. */
149 static void glyphStem(abfGlyphCallbacks *cb,
150 int flags, float edge0, float edge1) {
151 /* Nothing to do */
152 }
153
154 /* Convert flex operator. */
155 static void glyphFlex(abfGlyphCallbacks *cb, float depth,
156 float x1, float y1,
157 float x2, float y2,
158 float x3, float y3,
159 float x4, float y4,
160 float x5, float y5,
161 float x6, float y6) {
162 glyphCurve(cb, x1, y1, x2, y2, x3, y3);
163 glyphCurve(cb, x4, y4, x5, y5, x6, y6);
164 }
165
166 /* Ignore general glyph operator. */
167 static void glyphGenop(abfGlyphCallbacks *cb,
168 int cnt, float *args, int op) {
169 /* Nothing to do */
170 }
171
172 /* Handle seac operator. */
173 static void glyphSeac(abfGlyphCallbacks *cb,
174 float adx, float ady, int bchar, int achar) {
175 abfMetricsCtx h = (abfMetricsCtx)cb->direct_ctx;
176 h->err_code = abfErrGlyphSeac;
177 }
178
179 static void updateFontBoundingBox(abfAFMCtx h) {
180 /* ignore empty glyphs */
181 if ( (h->glyph_metrics.ctx.int_mtx.left == 0)
182 && (h->glyph_metrics.ctx.int_mtx.right == 0)
183 && (h->glyph_metrics.ctx.int_mtx.top == 0)
184 && (h->glyph_metrics.ctx.int_mtx.bottom == 0))
185 return;
186
187 if (h->glyph_metrics.ctx.int_mtx.left < h->font_bbox.left)
188 h->font_bbox.left = h->glyph_metrics.ctx.int_mtx.left;
189
190 if (h->glyph_metrics.ctx.int_mtx.right > h->font_bbox.right)
191 h->font_bbox.right = h->glyph_metrics.ctx.int_mtx.right;
192
193 if (h->glyph_metrics.ctx.int_mtx.top > h->font_bbox.top)
194 h->font_bbox.top = h->glyph_metrics.ctx.int_mtx.top;
195
196 if (h->glyph_metrics.ctx.int_mtx.bottom < h->font_bbox.bottom)
197 h->font_bbox.bottom = h->glyph_metrics.ctx.int_mtx.bottom;
198 }
199
200 /* End glyph path. */
201 static void glyphEnd(abfGlyphCallbacks *cb) {
202 abfAFMCtx h = (abfAFMCtx)cb->direct_ctx;
203 abfMetricsCtx g = &h->glyph_metrics.ctx;
204 abfGlyphInfo *info = cb->info;
205 long code = (info->encoding.code == ABF_GLYPH_UNENC) ? -1 : info->encoding.code;
206 h->glyph_metrics.cb.end(&h->glyph_metrics.cb);
207
208 updateFontBoundingBox(h);
209
210 /* Print glyph metrics */
211 if (info->flags & ABF_GLYPH_CID)
212 fprintf(h->tmp_fp, "C %ld ; W0X %ld ; N %hu ; B %ld %ld %ld %ld ;\n",
213 code, g->int_mtx.hAdv, info->cid,
214 g->int_mtx.left, g->int_mtx.bottom,
215 g->int_mtx.right, g->int_mtx.top);
216 else if (info->gname.ptr != NULL) {
217 if (strcmp(info->gname.ptr, ".notdef") != 0) {
218 fprintf(h->tmp_fp, "C %ld ; WX %ld ; N %s ; B %ld %ld %ld %ld ;\n",
219 code, g->int_mtx.hAdv, info->gname.ptr,
220 g->int_mtx.left, g->int_mtx.bottom,
221 g->int_mtx.right, g->int_mtx.top);
222 }
223 } else {
224 fprintf(h->tmp_fp, "C %ld ; WX %ld ; B %ld %ld %ld %ld ;\n",
225 code, g->int_mtx.hAdv,
226 g->int_mtx.left, g->int_mtx.bottom,
227 g->int_mtx.right, g->int_mtx.top);
228 }
229 }
230
231 /* AFM callbacks template. */
232 const abfGlyphCallbacks abfGlyphAFMCallbacks =
233 {
234 NULL,
235 NULL,
236 NULL,
237 glyphBeg,
238 glyphWidth,
239 glyphMove,
240 glyphLine,
241 glyphCurve,
242 glyphStem,
243 glyphFlex,
244 glyphGenop,
245 glyphSeac,
246 glyphEnd,
247 };