| 1 | /* |
| 2 | * << Haru Free PDF Library >> -- hpdf_font_tt.c |
| 3 | * |
| 4 | * URL: http://libharu.org |
| 5 | * |
| 6 | * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp> |
| 7 | * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org> |
| 8 | * |
| 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. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include "hpdf_conf.h" |
| 19 | #include "hpdf_utils.h" |
| 20 | #include "hpdf_font.h" |
| 21 | |
| 22 | static HPDF_STATUS |
| 23 | OnWrite (HPDF_Dict obj, |
| 24 | HPDF_Stream stream); |
| 25 | |
| 26 | static HPDF_STATUS |
| 27 | BeforeWrite (HPDF_Dict obj); |
| 28 | |
| 29 | |
| 30 | static void |
| 31 | OnFree (HPDF_Dict obj); |
| 32 | |
| 33 | |
| 34 | static HPDF_INT |
| 35 | CharWidth (HPDF_Font font, |
| 36 | HPDF_BYTE code); |
| 37 | |
| 38 | static HPDF_TextWidth |
| 39 | TextWidth (HPDF_Font font, |
| 40 | const HPDF_BYTE *text, |
| 41 | HPDF_UINT len); |
| 42 | |
| 43 | |
| 44 | static HPDF_STATUS |
| 45 | CreateDescriptor (HPDF_Font font); |
| 46 | |
| 47 | |
| 48 | static HPDF_UINT |
| 49 | MeasureText (HPDF_Font font, |
| 50 | const HPDF_BYTE *text, |
| 51 | HPDF_UINT len, |
| 52 | HPDF_REAL width, |
| 53 | HPDF_REAL font_size, |
| 54 | HPDF_REAL char_space, |
| 55 | HPDF_REAL word_space, |
| 56 | HPDF_BOOL wordwrap, |
| 57 | HPDF_REAL *real_width); |
| 58 | |
| 59 | |
| 60 | HPDF_Font |
| 61 | HPDF_TTFont_New (HPDF_MMgr mmgr, |
| 62 | HPDF_FontDef fontdef, |
| 63 | HPDF_Encoder encoder, |
| 64 | HPDF_Xref xref) |
| 65 | { |
| 66 | HPDF_Dict font; |
| 67 | HPDF_FontAttr attr; |
| 68 | HPDF_TTFontDefAttr fontdef_attr; |
| 69 | HPDF_BasicEncoderAttr encoder_attr; |
| 70 | HPDF_STATUS ret = 0; |
| 71 | |
| 72 | HPDF_PTRACE ((" HPDF_TTFont_New\n" )); |
| 73 | |
| 74 | font = HPDF_Dict_New (mmgr); |
| 75 | if (!font) |
| 76 | return NULL; |
| 77 | |
| 78 | font->header.obj_class |= HPDF_OSUBCLASS_FONT; |
| 79 | |
| 80 | /* check whether the fontdef object and the encoder object is valid. */ |
| 81 | if (encoder->type != HPDF_ENCODER_TYPE_SINGLE_BYTE) { |
| 82 | HPDF_SetError(font->error, HPDF_INVALID_ENCODER_TYPE, 0); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | if (fontdef->type != HPDF_FONTDEF_TYPE_TRUETYPE) { |
| 87 | HPDF_SetError(font->error, HPDF_INVALID_FONTDEF_TYPE, 0); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | attr = HPDF_GetMem (mmgr, sizeof(HPDF_FontAttr_Rec)); |
| 92 | if (!attr) { |
| 93 | HPDF_Dict_Free (font); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | HPDF_MemSet (attr, 0, sizeof(HPDF_FontAttr_Rec)); |
| 98 | |
| 99 | font->header.obj_class |= HPDF_OSUBCLASS_FONT; |
| 100 | font->write_fn = OnWrite; |
| 101 | font->before_write_fn = BeforeWrite; |
| 102 | font->free_fn = OnFree; |
| 103 | font->attr = attr; |
| 104 | |
| 105 | attr->type = HPDF_FONT_TRUETYPE; |
| 106 | attr->writing_mode = HPDF_WMODE_HORIZONTAL; |
| 107 | attr->text_width_fn = TextWidth; |
| 108 | attr->measure_text_fn = MeasureText; |
| 109 | attr->fontdef = fontdef; |
| 110 | attr->encoder = encoder; |
| 111 | attr->xref = xref; |
| 112 | |
| 113 | /* singlebyte-font has a widths-array which is an array of 256 signed |
| 114 | * short integer. |
| 115 | * in the case of type1-font, widths-array for all letters is made in |
| 116 | * constructer. but in the case of true-type-font, the array is |
| 117 | * initialized at 0, and set when the corresponding character was used |
| 118 | * for the first time. |
| 119 | */ |
| 120 | attr->widths = HPDF_GetMem (mmgr, sizeof(HPDF_INT16) * 256); |
| 121 | if (!attr->widths) { |
| 122 | HPDF_Dict_Free (font); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | HPDF_MemSet (attr->widths, 0, sizeof(HPDF_INT16) * 256); |
| 127 | |
| 128 | attr->used = HPDF_GetMem (mmgr, sizeof(HPDF_BYTE) * 256); |
| 129 | if (!attr->used) { |
| 130 | HPDF_Dict_Free (font); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | HPDF_MemSet (attr->used, 0, sizeof(HPDF_BYTE) * 256); |
| 135 | |
| 136 | fontdef_attr = (HPDF_TTFontDefAttr)fontdef->attr; |
| 137 | |
| 138 | ret += HPDF_Dict_AddName (font, "Type" , "Font" ); |
| 139 | ret += HPDF_Dict_AddName (font, "BaseFont" , fontdef_attr->base_font); |
| 140 | ret += HPDF_Dict_AddName (font, "Subtype" , "TrueType" ); |
| 141 | |
| 142 | encoder_attr = (HPDF_BasicEncoderAttr)encoder->attr; |
| 143 | |
| 144 | ret += HPDF_Dict_AddNumber (font, "FirstChar" , encoder_attr->first_char); |
| 145 | ret += HPDF_Dict_AddNumber (font, "LastChar" , encoder_attr->last_char); |
| 146 | if (fontdef->missing_width != 0) |
| 147 | ret += HPDF_Dict_AddNumber (font, "MissingWidth" , |
| 148 | fontdef->missing_width); |
| 149 | |
| 150 | if (ret != HPDF_OK) { |
| 151 | HPDF_Dict_Free (font); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | if (HPDF_Xref_Add (xref, font) != HPDF_OK) |
| 156 | return NULL; |
| 157 | |
| 158 | return font; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static HPDF_STATUS |
| 163 | CreateDescriptor (HPDF_Font font) |
| 164 | { |
| 165 | HPDF_FontAttr font_attr = (HPDF_FontAttr)font->attr; |
| 166 | HPDF_FontDef def = font_attr->fontdef; |
| 167 | HPDF_TTFontDefAttr def_attr = (HPDF_TTFontDefAttr)def->attr; |
| 168 | |
| 169 | HPDF_PTRACE ((" HPDF_TTFont_CreateDescriptor\n" )); |
| 170 | |
| 171 | if (!font_attr->fontdef->descriptor) { |
| 172 | HPDF_Dict descriptor = HPDF_Dict_New (font->mmgr); |
| 173 | HPDF_STATUS ret = 0; |
| 174 | HPDF_Array array; |
| 175 | |
| 176 | if (!descriptor) |
| 177 | return HPDF_Error_GetCode (font->error); |
| 178 | |
| 179 | ret += HPDF_Xref_Add (font_attr->xref, descriptor); |
| 180 | ret += HPDF_Dict_AddName (descriptor, "Type" , "FontDescriptor" ); |
| 181 | ret += HPDF_Dict_AddNumber (descriptor, "Ascent" , def->ascent); |
| 182 | ret += HPDF_Dict_AddNumber (descriptor, "Descent" , def->descent); |
| 183 | ret += HPDF_Dict_AddNumber (descriptor, "CapHeight" , def->cap_height); |
| 184 | ret += HPDF_Dict_AddNumber (descriptor, "Flags" , def->flags); |
| 185 | |
| 186 | array = HPDF_Box_Array_New (font->mmgr, def->font_bbox); |
| 187 | ret += HPDF_Dict_Add (descriptor, "FontBBox" , array); |
| 188 | |
| 189 | ret += HPDF_Dict_AddName (descriptor, "FontName" , def_attr->base_font); |
| 190 | ret += HPDF_Dict_AddNumber (descriptor, "ItalicAngle" , |
| 191 | def->italic_angle); |
| 192 | ret += HPDF_Dict_AddNumber (descriptor, "StemV" , def->stemv); |
| 193 | ret += HPDF_Dict_AddNumber (descriptor, "XHeight" , def->x_height); |
| 194 | |
| 195 | if (def_attr->char_set) |
| 196 | ret += HPDF_Dict_AddName (descriptor, "CharSet" , |
| 197 | def_attr->char_set); |
| 198 | |
| 199 | if (ret != HPDF_OK) |
| 200 | return HPDF_Error_GetCode (font->error); |
| 201 | |
| 202 | if (def_attr->embedding) { |
| 203 | HPDF_Dict font_data = HPDF_DictStream_New (font->mmgr, |
| 204 | font_attr->xref); |
| 205 | |
| 206 | if (!font_data) |
| 207 | return HPDF_Error_GetCode (font->error); |
| 208 | |
| 209 | if (HPDF_TTFontDef_SaveFontData (font_attr->fontdef, |
| 210 | font_data->stream) != HPDF_OK) |
| 211 | return HPDF_Error_GetCode (font->error); |
| 212 | |
| 213 | ret += HPDF_Dict_Add (descriptor, "FontFile2" , font_data); |
| 214 | ret += HPDF_Dict_AddNumber (font_data, "Length1" , |
| 215 | def_attr->length1); |
| 216 | ret += HPDF_Dict_AddNumber (font_data, "Length2" , 0); |
| 217 | ret += HPDF_Dict_AddNumber (font_data, "Length3" , 0); |
| 218 | |
| 219 | font_data->filter = font->filter; |
| 220 | } |
| 221 | |
| 222 | if (ret != HPDF_OK) |
| 223 | return HPDF_Error_GetCode (font->error); |
| 224 | |
| 225 | font_attr->fontdef->descriptor = descriptor; |
| 226 | } |
| 227 | |
| 228 | return HPDF_Dict_Add (font, "FontDescriptor" , |
| 229 | font_attr->fontdef->descriptor); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | static HPDF_INT |
| 234 | CharWidth (HPDF_Font font, |
| 235 | HPDF_BYTE code) |
| 236 | { |
| 237 | HPDF_FontAttr attr = (HPDF_FontAttr)font->attr; |
| 238 | |
| 239 | if (attr->used[code] == 0) { |
| 240 | HPDF_UNICODE unicode = HPDF_Encoder_ToUnicode (attr->encoder, code); |
| 241 | |
| 242 | attr->used[code] = 1; |
| 243 | attr->widths[code] = HPDF_TTFontDef_GetCharWidth(attr->fontdef, |
| 244 | unicode); |
| 245 | } |
| 246 | |
| 247 | return attr->widths[code]; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | static HPDF_TextWidth |
| 252 | TextWidth (HPDF_Font font, |
| 253 | const HPDF_BYTE *text, |
| 254 | HPDF_UINT len) |
| 255 | { |
| 256 | HPDF_FontAttr attr = (HPDF_FontAttr)font->attr; |
| 257 | HPDF_TextWidth ret = {0, 0, 0, 0}; |
| 258 | HPDF_UINT i; |
| 259 | HPDF_BYTE b = 0; |
| 260 | |
| 261 | HPDF_PTRACE ((" HPDF_TTFont_TextWidth\n" )); |
| 262 | |
| 263 | if (attr->widths) { |
| 264 | for (i = 0; i < len; i++) { |
| 265 | b = text[i]; |
| 266 | ret.numchars++; |
| 267 | ret.width += CharWidth (font, b); |
| 268 | |
| 269 | if (HPDF_IS_WHITE_SPACE(b)) { |
| 270 | ret.numspace++; |
| 271 | ret.numwords++; |
| 272 | } |
| 273 | } |
| 274 | } else |
| 275 | HPDF_SetError (font->error, HPDF_FONT_INVALID_WIDTHS_TABLE, 0); |
| 276 | |
| 277 | /* 2006.08.19 add. */ |
| 278 | if (HPDF_IS_WHITE_SPACE(b)) |
| 279 | ; /* do nothing. */ |
| 280 | else |
| 281 | ret.numwords++; |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | static HPDF_UINT |
| 288 | MeasureText (HPDF_Font font, |
| 289 | const HPDF_BYTE *text, |
| 290 | HPDF_UINT len, |
| 291 | HPDF_REAL width, |
| 292 | HPDF_REAL font_size, |
| 293 | HPDF_REAL char_space, |
| 294 | HPDF_REAL word_space, |
| 295 | HPDF_BOOL wordwrap, |
| 296 | HPDF_REAL *real_width) |
| 297 | { |
| 298 | HPDF_DOUBLE w = 0; |
| 299 | HPDF_UINT tmp_len = 0; |
| 300 | HPDF_UINT i; |
| 301 | |
| 302 | HPDF_PTRACE ((" HPDF_TTFont_MeasureText\n" )); |
| 303 | |
| 304 | for (i = 0; i < len; i++) { |
| 305 | HPDF_BYTE b = text[i]; |
| 306 | |
| 307 | if (HPDF_IS_WHITE_SPACE(b)) { |
| 308 | tmp_len = i + 1; |
| 309 | |
| 310 | if (real_width) |
| 311 | *real_width = (HPDF_REAL)w; |
| 312 | |
| 313 | w += word_space; |
| 314 | } else if (!wordwrap) { |
| 315 | tmp_len = i; |
| 316 | |
| 317 | if (real_width) |
| 318 | *real_width = (HPDF_REAL)w; |
| 319 | } |
| 320 | |
| 321 | w += (HPDF_DOUBLE)CharWidth (font, b) * font_size / 1000; |
| 322 | |
| 323 | /* 2006.08.04 break when it encountered line feed */ |
| 324 | if (w > width || b == 0x0A) |
| 325 | return tmp_len; |
| 326 | |
| 327 | if (i > 0) |
| 328 | w += char_space; |
| 329 | } |
| 330 | |
| 331 | /* all of text can be put in the specified width */ |
| 332 | if (real_width) |
| 333 | *real_width = (HPDF_REAL)w; |
| 334 | return len; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | static HPDF_STATUS |
| 339 | OnWrite (HPDF_Dict obj, |
| 340 | HPDF_Stream stream) |
| 341 | { |
| 342 | HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr; |
| 343 | HPDF_BasicEncoderAttr encoder_attr = |
| 344 | (HPDF_BasicEncoderAttr)attr->encoder->attr; |
| 345 | HPDF_UINT i; |
| 346 | HPDF_STATUS ret; |
| 347 | char buf[128]; |
| 348 | char *pbuf = buf; |
| 349 | char *eptr = buf + 127; |
| 350 | |
| 351 | HPDF_PTRACE ((" HPDF_Font_OnWrite\n" )); |
| 352 | |
| 353 | /* Widths entry */ |
| 354 | if ((ret = HPDF_Stream_WriteEscapeName (stream, "Widths" )) != HPDF_OK) |
| 355 | return ret; |
| 356 | |
| 357 | if ((ret = HPDF_Stream_WriteStr (stream, " [\012" )) != HPDF_OK) |
| 358 | return ret; |
| 359 | |
| 360 | for (i = encoder_attr->first_char; i <= encoder_attr->last_char; i++) { |
| 361 | |
| 362 | pbuf = HPDF_IToA (pbuf, attr->widths[i], eptr); |
| 363 | *pbuf++ = ' '; |
| 364 | |
| 365 | if ((i + 1) % 16 == 0) { |
| 366 | HPDF_StrCpy(pbuf, "\012" , eptr); |
| 367 | if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK) |
| 368 | return ret; |
| 369 | pbuf = buf; |
| 370 | } |
| 371 | |
| 372 | } |
| 373 | |
| 374 | HPDF_StrCpy (pbuf, "]\012" , eptr); |
| 375 | |
| 376 | if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK) |
| 377 | return ret; |
| 378 | |
| 379 | return attr->encoder->write_fn (attr->encoder, stream); |
| 380 | } |
| 381 | |
| 382 | static HPDF_STATUS |
| 383 | BeforeWrite (HPDF_Dict obj) |
| 384 | { |
| 385 | HPDF_PTRACE ((" HPDF_TTFont_BeforeWrite\n" )); |
| 386 | |
| 387 | return CreateDescriptor (obj); |
| 388 | } |
| 389 | |
| 390 | static void |
| 391 | OnFree (HPDF_Dict obj) |
| 392 | { |
| 393 | HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr; |
| 394 | |
| 395 | HPDF_PTRACE ((" HPDF_TTFont_OnFree\n" )); |
| 396 | |
| 397 | if (attr) { |
| 398 | if (attr->widths) { |
| 399 | HPDF_FreeMem (obj->mmgr, attr->widths); |
| 400 | } |
| 401 | |
| 402 | if (attr->used) { |
| 403 | HPDF_FreeMem (obj->mmgr, attr->used); |
| 404 | } |
| 405 | |
| 406 | HPDF_FreeMem (obj->mmgr, attr); |
| 407 | } |
| 408 | } |
| 409 | |