-
Notifications
You must be signed in to change notification settings - Fork 167
/
absfont_afm.c
215 lines (191 loc) · 6.14 KB
/
absfont_afm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
This software is licensed as OpenSource, under the Apache License, Version 2.0. This license is available at: http://opensource.org/licenses/Apache-2.0. */
/*
* Adobe Font Metrics (AFM) file support.
*/
#include "absfont.h"
#if PLAT_SUN4
#include "sun4/fixtime.h"
#else /* PLAT_SUN4 */
#include <time.h>
#endif /* PLAT_SUN4 */
#include <string.h>
/* Print key name and string if set. */
static void writeString(abfAFMCtx h, char *key, abfString *value)
{
if (value->ptr == ABF_UNSET_PTR)
return;
fprintf(h->fp, "%s %s\n", key, value->ptr);
}
void abfAFMBegFont(abfAFMCtx h, abfTopDict *top)
{
time_t now = time(NULL);
/* Initialize context */
h->err_code = abfSuccess;
h->metrics.cb = abfGlyphMetricsCallbacks;
h->metrics.cb.direct_ctx = &h->metrics.ctx;
h->metrics.ctx.flags = 0;
if (top->sup.flags & ABF_CID_FONT)
fprintf(h->fp, "StartFontMetrics 4.1\n");
else
fprintf(h->fp, "StartFontMetrics 2.0\n");
fprintf(h->fp, "Comment Copyright %d Adobe Systems Incorporated. "
"All Rights Reserved.\n", localtime(&now)->tm_year + 1900);
fprintf(h->fp, "Comment Creation Date: %s", ctime(&now));
if (top->UniqueID != ABF_UNSET_INT)
fprintf(h->fp, "Comment UniqueID %ld\n", top->UniqueID);
if (top->sup.UnitsPerEm != 1000)
fprintf(h->fp, "Comment UnitsPerEm %ld\n", top->sup.UnitsPerEm);
if (top->sup.flags & ABF_CID_FONT)
{
/* Write header for cid-keyed font */
fprintf(h->fp, "MetricsSets 2\n");
writeString(h, "FontName", &top->cid.CIDFontName);
writeString(h, "Weight", &top->Weight);
fprintf(h->fp, "FontBBox %g %g %g %g\n",
top->FontBBox[0], top->FontBBox[1],
top->FontBBox[2], top->FontBBox[3]);
fprintf(h->fp, "Version %.3f\n", top->cid.CIDFontVersion);
writeString(h, "Notice", &top->Notice);
fprintf(h->fp, "CharacterSet %s-%s-%ld\n", top->cid.Registry.ptr,
top->cid.Ordering.ptr, top->cid.Supplement);
fprintf(h->fp, "Characters %ld\n", top->sup.nGlyphs);
fprintf(h->fp, "IsBaseFont true\n");
fprintf(h->fp, "IsCIDFont true\n");
fprintf(h->fp, "StartDirection 2\n");
fprintf(h->fp, "UnderlinePosition %g\n", top->UnderlinePosition);
fprintf(h->fp, "UnderlineThickness %g\n", top->UnderlineThickness);
fprintf(h->fp, "ItalicAngle %g\n", top->ItalicAngle);
fprintf(h->fp, "IsFixedPitch %s\n",
top->isFixedPitch? "true": "false");
fprintf(h->fp, "EndDirection\n");
fprintf(h->fp, "StartCharMetrics %ld\n", top->sup.nGlyphs);
}
else
{
/* Write header for name-keyed font */
writeString(h, "FontName", &top->FDArray.array[0].FontName);
writeString(h, "FullName", &top->FullName);
writeString(h, "FamilyName", &top->FamilyName);
writeString(h, "Weight", &top->Weight);
fprintf(h->fp, "ItalicAngle %g\n", top->ItalicAngle);
fprintf(h->fp, "IsFixedPitch %s\n",
top->isFixedPitch? "true": "false");
fprintf(h->fp, "FontBBox %g %g %g %g\n",
top->FontBBox[0], top->FontBBox[1],
top->FontBBox[2], top->FontBBox[3]);
fprintf(h->fp, "UnderlinePosition %g\n", top->UnderlinePosition);
fprintf(h->fp, "UnderlineThickness %g\n", top->UnderlineThickness);
writeString(h, "Version", &top->version);
writeString(h, "Notice", &top->Notice);
fprintf(h->fp, "StartCharMetrics %ld\n", top->sup.nGlyphs - 1L);
}
}
void abfAFMEndFont(abfAFMCtx h)
{
fprintf(h->fp, "EndCharMetrics\n");
fprintf(h->fp, "EndFontMetrics\n");
}
/* Begin glyph path. */
static int glyphBeg(abfGlyphCallbacks *cb, abfGlyphInfo *info)
{
abfAFMCtx h = cb->direct_ctx;
cb->info = info;
return h->metrics.cb.beg(&h->metrics.cb, info);
}
/* Save glyph width. */
static void glyphWidth(abfGlyphCallbacks *cb, float hAdv)
{
abfAFMCtx h = cb->direct_ctx;
h->metrics.cb.width(&h->metrics.cb, hAdv);
}
/* Add move to path. */
static void glyphMove(abfGlyphCallbacks *cb, float x0, float y0)
{
abfAFMCtx h = cb->direct_ctx;
h->metrics.cb.move(&h->metrics.cb, x0, y0);
}
/* Add line to path. */
static void glyphLine(abfGlyphCallbacks *cb, float x1, float y1)
{
abfAFMCtx h = cb->direct_ctx;
h->metrics.cb.line(&h->metrics.cb, x1, y1);
}
/* Add curve to path. */
static void glyphCurve(abfGlyphCallbacks *cb,
float x1, float y1,
float x2, float y2,
float x3, float y3)
{
abfAFMCtx h = cb->direct_ctx;
h->metrics.cb.curve(&h->metrics.cb, x1, y1, x2, y2, x3, y3);
}
/* Ignore stem operator. */
static void glyphStem(abfGlyphCallbacks *cb,
int flags, float edge0, float edge1)
{
/* Nothing to do */
}
/* Convert flex operator. */
static void glyphFlex(abfGlyphCallbacks *cb, float depth,
float x1, float y1,
float x2, float y2,
float x3, float y3,
float x4, float y4,
float x5, float y5,
float x6, float y6)
{
glyphCurve(cb, x1, y1, x2, y2, x3, y3);
glyphCurve(cb, x4, y4, x5, y5, x6, y6);
}
/* Ignore general glyph operator. */
static void glyphGenop(abfGlyphCallbacks *cb,
int cnt, float *args, int op)
{
/* Nothing to do */
}
/* Handle seac operator. */
static void glyphSeac(abfGlyphCallbacks *cb,
float adx, float ady, int bchar, int achar)
{
abfMetricsCtx h = cb->direct_ctx;
h->err_code = abfErrGlyphSeac;
}
/* End glyph path. */
static void glyphEnd(abfGlyphCallbacks *cb)
{
abfAFMCtx h = cb->direct_ctx;
abfMetricsCtx g = &h->metrics.ctx;
abfGlyphInfo *info = cb->info;
unsigned long code = (info->encoding.code == ABF_GLYPH_UNENC)?
0xFFFFFFFF: (unsigned long)info->encoding.code;
h->metrics.cb.end(&h->metrics.cb);
/* Print glyph metrics */
if (info->flags & ABF_GLYPH_CID)
fprintf(h->fp, "C %ld ; W0X %ld ; N %hu ; B %ld %ld %ld %ld ;\n",
code, g->int_mtx.hAdv, info->cid,
g->int_mtx.left, g->int_mtx.bottom,
g->int_mtx.right, g->int_mtx.top);
else if (strcmp(info->gname.ptr, ".notdef") != 0)
fprintf(h->fp, "C %ld ; WX %ld ; N %s ; B %ld %ld %ld %ld ;\n",
code, g->int_mtx.hAdv, info->gname.ptr,
g->int_mtx.left, g->int_mtx.bottom,
g->int_mtx.right, g->int_mtx.top);
}
/* AFM callbacks template. */
const abfGlyphCallbacks abfGlyphAFMCallbacks =
{
NULL,
NULL,
NULL,
glyphBeg,
glyphWidth,
glyphMove,
glyphLine,
glyphCurve,
glyphStem,
glyphFlex,
glyphGenop,
glyphSeac,
glyphEnd,
};