forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhpdf_font_type1.c
More file actions
391 lines (301 loc) · 11.3 KB
/
hpdf_font_type1.c
File metadata and controls
391 lines (301 loc) · 11.3 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* << Haru Free PDF Library 2.0.3 >> -- hpdf_font_type1.c
*
* Copyright (c) 1999-2006 Takeshi Kanno <[email protected]>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
* It is provided "as is" without express or implied warranty.
*
*/
#include "hpdf_conf.h"
#include "hpdf_utils.h"
#include "hpdf_font.h"
static HPDF_STATUS
Type1Font_OnWrite (HPDF_Dict obj,
HPDF_Stream stream);
static void
Type1Font_OnFree (HPDF_Dict obj);
static HPDF_TextWidth
Type1Font_TextWidth (HPDF_Font font,
const HPDF_BYTE *text,
HPDF_UINT len);
static HPDF_UINT
Type1Font_MeasureText (HPDF_Font font,
const HPDF_BYTE *text,
HPDF_UINT len,
HPDF_REAL width,
HPDF_REAL font_size,
HPDF_REAL char_space,
HPDF_REAL word_space,
HPDF_BOOL wordwrap,
HPDF_REAL *real_width);
static HPDF_STATUS
Type1Font_CreateDescriptor (HPDF_MMgr mmgr,
HPDF_Font font,
HPDF_Xref xref);
HPDF_Font
HPDF_Type1Font_New (HPDF_MMgr mmgr,
HPDF_FontDef fontdef,
HPDF_Encoder encoder,
HPDF_Xref xref)
{
HPDF_Dict font;
HPDF_FontAttr attr;
HPDF_Type1FontDefAttr fontdef_attr;
HPDF_BasicEncoderAttr encoder_attr;
HPDF_STATUS ret = 0;
HPDF_UINT i;
HPDF_PTRACE ((" HPDF_Type1Font_New\n"));
/* check whether the fontdef object and the encoder object is valid. */
if (encoder->type != HPDF_ENCODER_TYPE_SINGLE_BYTE) {
HPDF_SetError(mmgr->error, HPDF_INVALID_ENCODER_TYPE, 0);
return NULL;
}
if (fontdef->type != HPDF_FONTDEF_TYPE_TYPE1) {
HPDF_SetError(mmgr->error, HPDF_INVALID_FONTDEF_TYPE, 0);
return NULL;
}
font = HPDF_Dict_New (mmgr);
if (!font)
return NULL;
font->header.obj_class |= HPDF_OSUBCLASS_FONT;
attr = HPDF_GetMem (mmgr, sizeof(HPDF_FontAttr_Rec));
if (!attr) {
HPDF_Dict_Free (font);
return NULL;
}
font->header.obj_class |= HPDF_OSUBCLASS_FONT;
font->write_fn = Type1Font_OnWrite;
font->free_fn = Type1Font_OnFree;
HPDF_MemSet (attr, 0, sizeof(HPDF_FontAttr_Rec));
font->attr = attr;
attr->type = HPDF_FONT_TYPE1;
attr->writing_mode = HPDF_WMODE_HORIZONTAL;
attr->text_width_fn = Type1Font_TextWidth;
attr->measure_text_fn = Type1Font_MeasureText;
attr->fontdef = fontdef;
attr->encoder = encoder;
attr->xref = xref;
/* singlebyte-font has a widths-array which is an array of 256 signed
* short integer.
*/
attr->widths = HPDF_GetMem (mmgr, sizeof(HPDF_INT16) * 256);
if (!attr->widths) {
HPDF_Dict_Free (font);
return NULL;
}
encoder_attr = (HPDF_BasicEncoderAttr)encoder->attr;
HPDF_MemSet (attr->widths, 0, sizeof(HPDF_INT16) * 256);
for (i = encoder_attr->first_char; i <= encoder_attr->last_char; i++) {
HPDF_UNICODE u = encoder_attr->unicode_map[i];
HPDF_UINT16 w = HPDF_Type1FontDef_GetWidth (fontdef, u);
attr->widths[i] = w;
}
fontdef_attr = (HPDF_Type1FontDefAttr)fontdef->attr;
ret += HPDF_Dict_AddName (font, "Type", "Font");
ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font);
ret += HPDF_Dict_AddName (font, "Subtype", "Type1");
if (!fontdef_attr->is_base14font) {
if (fontdef->missing_width != 0)
ret += HPDF_Dict_AddNumber (font, "MissingWidth",
fontdef->missing_width);
ret += Type1Font_CreateDescriptor (mmgr, font, xref);
}
if (ret != HPDF_OK) {
HPDF_Dict_Free (font);
return NULL;
}
if (HPDF_Xref_Add (xref, font) != HPDF_OK)
return NULL;
return font;
}
static HPDF_STATUS
Type1Font_CreateDescriptor (HPDF_MMgr mmgr,
HPDF_Font font,
HPDF_Xref xref)
{
HPDF_FontAttr font_attr = (HPDF_FontAttr)font->attr;
HPDF_FontDef def = font_attr->fontdef;
HPDF_Type1FontDefAttr def_attr = (HPDF_Type1FontDefAttr)def->attr;
HPDF_PTRACE ((" HPDF_Type1Font_CreateDescriptor\n"));
if (!font_attr->fontdef->descriptor) {
HPDF_Dict descriptor = HPDF_Dict_New (mmgr);
HPDF_STATUS ret = 0;
HPDF_Array array;
if (!descriptor)
return HPDF_Error_GetCode (font->error);
ret += HPDF_Xref_Add (xref, descriptor);
ret += HPDF_Dict_AddName (descriptor, "Type", "FontDescriptor");
ret += HPDF_Dict_AddNumber (descriptor, "Ascent", def->ascent);
ret += HPDF_Dict_AddNumber (descriptor, "Descent", def->descent);
ret += HPDF_Dict_AddNumber (descriptor, "Flags", def->flags);
array = HPDF_Box_Array_New (mmgr, def->font_bbox);
ret += HPDF_Dict_Add (descriptor, "FontBBox", array);
ret += HPDF_Dict_AddName (descriptor, "FontName",
font_attr->fontdef->base_font);
ret += HPDF_Dict_AddNumber (descriptor, "ItalicAngle",
def->italic_angle);
ret += HPDF_Dict_AddNumber (descriptor, "StemV", def->stemv);
ret += HPDF_Dict_AddNumber (descriptor, "XHeight", def->x_height);
if (def_attr->char_set)
ret += HPDF_Dict_AddName (descriptor, "CharSet",
def_attr->char_set);
if (ret != HPDF_OK)
return HPDF_Error_GetCode (font->error);
if (def_attr->font_data) {
HPDF_Dict font_data = HPDF_DictStream_New (mmgr, xref);
if (!font_data)
return HPDF_Error_GetCode (font->error);
if (HPDF_Stream_WriteToStream (def_attr->font_data,
font_data->stream, HPDF_STREAM_FILTER_NONE, NULL) != HPDF_OK)
return HPDF_Error_GetCode (font->error);
ret += HPDF_Dict_Add (descriptor, "FontFile", font_data);
ret += HPDF_Dict_AddNumber (font_data, "Length1",
def_attr->length1);
ret += HPDF_Dict_AddNumber (font_data, "Length2",
def_attr->length2);
ret += HPDF_Dict_AddNumber (font_data, "Length3",
def_attr->length3);
font_data->filter = font->filter;
}
if (ret != HPDF_OK)
return HPDF_Error_GetCode (font->error);
font_attr->fontdef->descriptor = descriptor;
}
return HPDF_Dict_Add (font, "FontDescriptor",
font_attr->fontdef->descriptor);
}
static HPDF_TextWidth
Type1Font_TextWidth (HPDF_Font font,
const HPDF_BYTE *text,
HPDF_UINT len)
{
HPDF_FontAttr attr = (HPDF_FontAttr)font->attr;
HPDF_TextWidth ret = {0, 0, 0, 0};
HPDF_UINT i;
HPDF_BYTE b = 0;
HPDF_PTRACE ((" HPDF_Type1Font_TextWidth\n"));
if (attr->widths) {
for (i = 0; i < len; i++) {
b = text[i];
ret.numchars++;
ret.width += attr->widths[b];
if (HPDF_IS_WHITE_SPACE(b)) {
ret.numspace++;
ret.numwords++;
}
}
} else
HPDF_SetError (font->error, HPDF_FONT_INVALID_WIDTHS_TABLE, 0);
/* 2006.08.19 add. */
if (HPDF_IS_WHITE_SPACE(b))
; /* do nothing. */
else
ret.numwords++;
return ret;
}
static HPDF_UINT
Type1Font_MeasureText (HPDF_Font font,
const HPDF_BYTE *text,
HPDF_UINT len,
HPDF_REAL width,
HPDF_REAL font_size,
HPDF_REAL char_space,
HPDF_REAL word_space,
HPDF_BOOL wordwrap,
HPDF_REAL *real_width)
{
HPDF_REAL w = 0;
HPDF_UINT tmp_len = 0;
HPDF_UINT i;
HPDF_FontAttr attr = (HPDF_FontAttr)font->attr;
HPDF_PTRACE ((" HPDF_Type1Font_MeasureText\n"));
for (i = 0; i < len; i++) {
HPDF_BYTE b = text[i];
if (HPDF_IS_WHITE_SPACE(b)) {
tmp_len = i + 1;
if (real_width)
*real_width = w;
w += word_space;
} else if (!wordwrap) {
tmp_len = i;
if (real_width)
*real_width = w;
}
w += attr->widths[b] * font_size / 1000;
/* 2006.08.04 break when it encountered line feed */
if (w > width || b == 0x0A)
return tmp_len;
if (i > 0)
w += char_space;
}
/* all of text can be put in the specified width */
if (real_width)
*real_width = w;
return len;
}
static HPDF_STATUS
Type1Font_OnWrite (HPDF_Dict obj,
HPDF_Stream stream)
{
HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr;
HPDF_Type1FontDefAttr fontdef_attr =
(HPDF_Type1FontDefAttr)attr->fontdef->attr;
HPDF_BasicEncoderAttr encoder_attr =
(HPDF_BasicEncoderAttr)attr->encoder->attr;
HPDF_UINT i;
HPDF_STATUS ret;
char buf[HPDF_TMP_BUF_SIZ];
char *eptr = buf + HPDF_TMP_BUF_SIZ - 1;
HPDF_PTRACE ((" HPDF_Font_Type1Font_OnWrite\n"));
/* if font is base14-font these entries is not required */
if (!fontdef_attr->is_base14font || encoder_attr->has_differences) {
char *pbuf;
pbuf = HPDF_StrCpy (buf, "/FirstChar ", eptr);
pbuf = HPDF_IToA (pbuf, encoder_attr->first_char, eptr);
HPDF_StrCpy (pbuf, "\012", eptr);
if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK)
return ret;
pbuf = HPDF_StrCpy (buf, "/LastChar ", eptr);
pbuf = HPDF_IToA (pbuf, encoder_attr->last_char, eptr);
HPDF_StrCpy (pbuf, "\012", eptr);
if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK)
return ret;
/* Widths entry */
if ((ret = HPDF_Stream_WriteEscapeName (stream, "Widths")) != HPDF_OK)
return ret;
if ((ret = HPDF_Stream_WriteStr (stream, " [\012")) != HPDF_OK)
return ret;
pbuf = buf;
for (i = encoder_attr->first_char; i <= encoder_attr->last_char; i++) {
pbuf = HPDF_IToA (pbuf, attr->widths[i], eptr);
*pbuf++ = ' ';
if ((i + 1) % 16 == 0) {
HPDF_StrCpy(pbuf, "\012", eptr);
if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK)
return ret;
pbuf = buf;
}
}
HPDF_StrCpy (pbuf, "]\012", eptr);
if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK)
return ret;
}
return attr->encoder->write_fn (attr->encoder, stream);
}
static void
Type1Font_OnFree (HPDF_Dict obj)
{
HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr;
HPDF_PTRACE ((" HPDF_Type1Font_OnFree\n"));
if (attr) {
if (attr->widths) {
HPDF_FreeMem (obj->mmgr, attr->widths);
}
HPDF_FreeMem (obj->mmgr, attr);
}
}