2 * << Haru Free PDF Library >> -- hpdf_font_type1.c
4 * URL: http://libharu.org
6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
9 * Permission to use, copy, modify, distribute and sell this software
10 * and its documentation for any purpose is hereby granted without fee,
11 * provided that the above copyright notice appear in all copies and
12 * that both that copyright notice and this permission notice appear
13 * in supporting documentation.
14 * It is provided "as is" without express or implied warranty.
18 #include "hpdf_conf.h"
19 #include "hpdf_utils.h"
20 #include "hpdf_font.h"
23 Type1Font_OnWrite (HPDF_Dict obj,
28 Type1Font_OnFree (HPDF_Dict obj);
32 Type1Font_TextWidth (HPDF_Font font,
33 const HPDF_BYTE *text,
38 Type1Font_MeasureText (HPDF_Font font,
39 const HPDF_BYTE *text,
46 HPDF_REAL *real_width);
50 Type1Font_CreateDescriptor (HPDF_MMgr mmgr,
56 HPDF_Type1Font_New (HPDF_MMgr mmgr,
63 HPDF_Type1FontDefAttr fontdef_attr;
64 HPDF_BasicEncoderAttr encoder_attr;
68 HPDF_PTRACE ((" HPDF_Type1Font_New\n"));
70 /* check whether the fontdef object and the encoder object is valid. */
71 if (encoder->type != HPDF_ENCODER_TYPE_SINGLE_BYTE) {
72 HPDF_SetError(mmgr->error, HPDF_INVALID_ENCODER_TYPE, 0);
76 if (fontdef->type != HPDF_FONTDEF_TYPE_TYPE1) {
77 HPDF_SetError(mmgr->error, HPDF_INVALID_FONTDEF_TYPE, 0);
81 font = HPDF_Dict_New (mmgr);
85 font->header.obj_class |= HPDF_OSUBCLASS_FONT;
87 attr = HPDF_GetMem (mmgr, sizeof(HPDF_FontAttr_Rec));
89 HPDF_Dict_Free (font);
93 font->header.obj_class |= HPDF_OSUBCLASS_FONT;
94 font->write_fn = Type1Font_OnWrite;
95 font->free_fn = Type1Font_OnFree;
97 HPDF_MemSet (attr, 0, sizeof(HPDF_FontAttr_Rec));
100 attr->type = HPDF_FONT_TYPE1;
101 attr->writing_mode = HPDF_WMODE_HORIZONTAL;
102 attr->text_width_fn = Type1Font_TextWidth;
103 attr->measure_text_fn = Type1Font_MeasureText;
104 attr->fontdef = fontdef;
105 attr->encoder = encoder;
108 /* singlebyte-font has a widths-array which is an array of 256 signed
111 attr->widths = HPDF_GetMem (mmgr, sizeof(HPDF_INT16) * 256);
113 HPDF_Dict_Free (font);
117 encoder_attr = (HPDF_BasicEncoderAttr)encoder->attr;
119 HPDF_MemSet (attr->widths, 0, sizeof(HPDF_INT16) * 256);
120 for (i = encoder_attr->first_char; i <= encoder_attr->last_char; i++) {
121 HPDF_UNICODE u = encoder_attr->unicode_map[i];
123 HPDF_UINT16 w = HPDF_Type1FontDef_GetWidth (fontdef, u);
127 fontdef_attr = (HPDF_Type1FontDefAttr)fontdef->attr;
129 ret += HPDF_Dict_AddName (font, "Type", "Font");
130 ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font);
131 ret += HPDF_Dict_AddName (font, "Subtype", "Type1");
133 if (!fontdef_attr->is_base14font) {
134 if (fontdef->missing_width != 0)
135 ret += HPDF_Dict_AddNumber (font, "MissingWidth",
136 fontdef->missing_width);
138 ret += Type1Font_CreateDescriptor (mmgr, font, xref);
141 if (ret != HPDF_OK) {
142 HPDF_Dict_Free (font);
146 if (HPDF_Xref_Add (xref, font) != HPDF_OK)
154 Type1Font_CreateDescriptor (HPDF_MMgr mmgr,
158 HPDF_FontAttr font_attr = (HPDF_FontAttr)font->attr;
159 HPDF_FontDef def = font_attr->fontdef;
160 HPDF_Type1FontDefAttr def_attr = (HPDF_Type1FontDefAttr)def->attr;
162 HPDF_PTRACE ((" HPDF_Type1Font_CreateDescriptor\n"));
164 if (!font_attr->fontdef->descriptor) {
165 HPDF_Dict descriptor = HPDF_Dict_New (mmgr);
170 return HPDF_Error_GetCode (font->error);
172 ret += HPDF_Xref_Add (xref, descriptor);
173 ret += HPDF_Dict_AddName (descriptor, "Type", "FontDescriptor");
174 ret += HPDF_Dict_AddNumber (descriptor, "Ascent", def->ascent);
175 ret += HPDF_Dict_AddNumber (descriptor, "Descent", def->descent);
176 ret += HPDF_Dict_AddNumber (descriptor, "Flags", def->flags);
178 array = HPDF_Box_Array_New (mmgr, def->font_bbox);
179 ret += HPDF_Dict_Add (descriptor, "FontBBox", array);
181 ret += HPDF_Dict_AddName (descriptor, "FontName",
182 font_attr->fontdef->base_font);
183 ret += HPDF_Dict_AddNumber (descriptor, "ItalicAngle",
185 ret += HPDF_Dict_AddNumber (descriptor, "StemV", def->stemv);
186 ret += HPDF_Dict_AddNumber (descriptor, "XHeight", def->x_height);
188 if (def_attr->char_set)
189 ret += HPDF_Dict_AddName (descriptor, "CharSet",
193 return HPDF_Error_GetCode (font->error);
195 if (def_attr->font_data) {
196 HPDF_Dict font_data = HPDF_DictStream_New (mmgr, xref);
199 return HPDF_Error_GetCode (font->error);
201 if (HPDF_Stream_WriteToStream (def_attr->font_data,
202 font_data->stream, HPDF_STREAM_FILTER_NONE, NULL) != HPDF_OK)
203 return HPDF_Error_GetCode (font->error);
205 ret += HPDF_Dict_Add (descriptor, "FontFile", font_data);
206 ret += HPDF_Dict_AddNumber (font_data, "Length1",
208 ret += HPDF_Dict_AddNumber (font_data, "Length2",
210 ret += HPDF_Dict_AddNumber (font_data, "Length3",
213 font_data->filter = font->filter;
217 return HPDF_Error_GetCode (font->error);
219 font_attr->fontdef->descriptor = descriptor;
222 return HPDF_Dict_Add (font, "FontDescriptor",
223 font_attr->fontdef->descriptor);
227 static HPDF_TextWidth
228 Type1Font_TextWidth (HPDF_Font font,
229 const HPDF_BYTE *text,
232 HPDF_FontAttr attr = (HPDF_FontAttr)font->attr;
233 HPDF_TextWidth ret = {0, 0, 0, 0};
237 HPDF_PTRACE ((" HPDF_Type1Font_TextWidth\n"));
240 for (i = 0; i < len; i++) {
243 ret.width += attr->widths[b];
245 if (HPDF_IS_WHITE_SPACE(b)) {
251 HPDF_SetError (font->error, HPDF_FONT_INVALID_WIDTHS_TABLE, 0);
253 /* 2006.08.19 add. */
254 if (HPDF_IS_WHITE_SPACE(b))
264 Type1Font_MeasureText (HPDF_Font font,
265 const HPDF_BYTE *text,
269 HPDF_REAL char_space,
270 HPDF_REAL word_space,
272 HPDF_REAL *real_width)
275 HPDF_UINT tmp_len = 0;
277 HPDF_FontAttr attr = (HPDF_FontAttr)font->attr;
279 HPDF_PTRACE ((" HPDF_Type1Font_MeasureText\n"));
281 for (i = 0; i < len; i++) {
282 HPDF_BYTE b = text[i];
284 if (HPDF_IS_WHITE_SPACE(b)) {
291 } else if (!wordwrap) {
298 w += attr->widths[b] * font_size / 1000;
300 /* 2006.08.04 break when it encountered line feed */
301 if (w > width || b == 0x0A)
308 /* all of text can be put in the specified width */
317 Type1Font_OnWrite (HPDF_Dict obj,
320 HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr;
321 HPDF_Type1FontDefAttr fontdef_attr =
322 (HPDF_Type1FontDefAttr)attr->fontdef->attr;
323 HPDF_BasicEncoderAttr encoder_attr =
324 (HPDF_BasicEncoderAttr)attr->encoder->attr;
327 char buf[HPDF_TMP_BUF_SIZ];
328 char *eptr = buf + HPDF_TMP_BUF_SIZ - 1;
330 HPDF_PTRACE ((" HPDF_Font_Type1Font_OnWrite\n"));
332 /* if font is base14-font these entries is not required */
333 if (!fontdef_attr->is_base14font || encoder_attr->has_differences) {
336 pbuf = (char *)HPDF_StrCpy (buf, "/FirstChar ", eptr);
337 pbuf = HPDF_IToA (pbuf, encoder_attr->first_char, eptr);
338 HPDF_StrCpy (pbuf, "\012", eptr);
339 if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK)
342 pbuf = (char *)HPDF_StrCpy (buf, "/LastChar ", eptr);
343 pbuf = HPDF_IToA (pbuf, encoder_attr->last_char, eptr);
344 HPDF_StrCpy (pbuf, "\012", eptr);
345 if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK)
349 if ((ret = HPDF_Stream_WriteEscapeName (stream, "Widths")) != HPDF_OK)
352 if ((ret = HPDF_Stream_WriteStr (stream, " [\012")) != HPDF_OK)
356 for (i = encoder_attr->first_char; i <= encoder_attr->last_char; i++) {
358 pbuf = HPDF_IToA (pbuf, attr->widths[i], eptr);
361 if ((i + 1) % 16 == 0) {
362 HPDF_StrCpy(pbuf, "\012", eptr);
363 if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK)
369 HPDF_StrCpy (pbuf, "]\012", eptr);
371 if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK)
375 return attr->encoder->write_fn (attr->encoder, stream);
380 Type1Font_OnFree (HPDF_Dict obj)
382 HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr;
384 HPDF_PTRACE ((" HPDF_Type1Font_OnFree\n"));
388 HPDF_FreeMem (obj->mmgr, attr->widths);
390 HPDF_FreeMem (obj->mmgr, attr);