1 #ifndef MUPDF_FITZ_FONT_H 2 #define MUPDF_FITZ_FONT_H 3 4 #include "mupdf/fitz/system.h" 5 #include "mupdf/fitz/context.h" 6 #include "mupdf/fitz/geometry.h" 7 #include "mupdf/fitz/buffer.h" 8 9 /* forward declaration for circular dependency */ 10 struct fz_device; 11 12 /* Various font encoding tables and lookup functions */ 13 14 extern const char *fz_glyph_name_from_adobe_standard[256]; 15 extern const char *fz_glyph_name_from_iso8859_7[256]; 16 extern const char *fz_glyph_name_from_koi8u[256]; 17 extern const char *fz_glyph_name_from_mac_expert[256]; 18 extern const char *fz_glyph_name_from_mac_roman[256]; 19 extern const char *fz_glyph_name_from_win_ansi[256]; 20 extern const char *fz_glyph_name_from_windows_1252[256]; 21 22 extern const unsigned short fz_unicode_from_iso8859_1[256]; 23 extern const unsigned short fz_unicode_from_iso8859_7[256]; 24 extern const unsigned short fz_unicode_from_koi8u[256]; 25 extern const unsigned short fz_unicode_from_pdf_doc_encoding[256]; 26 extern const unsigned short fz_unicode_from_windows_1250[256]; 27 extern const unsigned short fz_unicode_from_windows_1251[256]; 28 extern const unsigned short fz_unicode_from_windows_1252[256]; 29 30 int fz_iso8859_1_from_unicode(int u); 31 int fz_iso8859_7_from_unicode(int u); 32 int fz_koi8u_from_unicode(int u); 33 int fz_windows_1250_from_unicode(int u); 34 int fz_windows_1251_from_unicode(int u); 35 int fz_windows_1252_from_unicode(int u); 36 37 int fz_unicode_from_glyph_name(const char *name); 38 int fz_unicode_from_glyph_name_strict(const char *name); 39 const char **fz_duplicate_glyph_names_from_unicode(int unicode); 40 const char *fz_glyph_name_from_unicode_sc(int unicode); 41 42 /** 43 An abstract font handle. 44 */ 45 typedef struct fz_font fz_font; 46 47 /** 48 Fonts come in two variants: 49 Regular fonts are handled by FreeType. 50 Type 3 fonts have callbacks to the interpreter. 51 */ 52 53 /** 54 Retrieve the FT_Face handle 55 for the font. 56 57 font: The font to query 58 59 Returns the FT_Face handle for the font, or NULL 60 if not a freetype handled font. (Cast to void * 61 to avoid nasty header exposure). 62 */ 63 void *fz_font_ft_face(fz_context *ctx, fz_font *font); 64 65 /** 66 Retrieve the Type3 procs 67 for a font. 68 69 font: The font to query 70 71 Returns the t3_procs pointer. Will be NULL for a 72 non type-3 font. 73 */ 74 fz_buffer **fz_font_t3_procs(fz_context *ctx, fz_font *font); 75 76 /* common CJK font collections */ 77 enum { FZ_ADOBE_CNS, FZ_ADOBE_GB, FZ_ADOBE_JAPAN, FZ_ADOBE_KOREA }; 78 79 /** 80 Every fz_font carries a set of flags 81 within it, in a fz_font_flags_t structure. 82 */ 83 typedef struct 84 { 85 unsigned int is_mono : 1; 86 unsigned int is_serif : 1; 87 unsigned int is_bold : 1; 88 unsigned int is_italic : 1; 89 unsigned int ft_substitute : 1; /* use substitute metrics */ 90 unsigned int ft_stretch : 1; /* stretch to match PDF metrics */ 91 92 unsigned int fake_bold : 1; /* synthesize bold */ 93 unsigned int fake_italic : 1; /* synthesize italic */ 94 unsigned int has_opentype : 1; /* has opentype shaping tables */ 95 unsigned int invalid_bbox : 1; 96 } fz_font_flags_t; 97 98 /** 99 Retrieve a pointer to the font flags 100 for a given font. These can then be updated as required. 101 102 font: The font to query 103 104 Returns a pointer to the flags structure (or NULL, if 105 the font is NULL). 106 */ 107 fz_font_flags_t *fz_font_flags(fz_font *font); 108 109 /** 110 In order to shape a given font, we need to 111 declare it to a shaper library (harfbuzz, by default, but others 112 are possible). To avoid redeclaring it every time we need to 113 shape, we hold a shaper handle and the destructor for it within 114 the font itself. The handle is initialised by the caller when 115 first required and the destructor is called when the fz_font is 116 destroyed. 117 */ 118 typedef struct 119 { 120 void *shaper_handle; 121 void (*destroy)(fz_context *ctx, void *); /* Destructor for shape_handle */ 122 } fz_shaper_data_t; 123 124 /** 125 Retrieve a pointer to the shaper data 126 structure for the given font. 127 128 font: The font to query. 129 130 Returns a pointer to the shaper data structure (or NULL if 131 font is NULL). 132 */ 133 fz_shaper_data_t *fz_font_shaper_data(fz_context *ctx, fz_font *font); 134 135 /** 136 Retrieve a pointer to the name of the font. 137 138 font: The font to query. 139 140 Returns a pointer to an internal copy of the font name. 141 Will never be NULL, but may be the empty string. 142 */ 143 const char *fz_font_name(fz_context *ctx, fz_font *font); 144 145 /** 146 Query whether the font flags say that this font is bold. 147 */ 148 int fz_font_is_bold(fz_context *ctx, fz_font *font); 149 150 /** 151 Query whether the font flags say that this font is italic. 152 */ 153 int fz_font_is_italic(fz_context *ctx, fz_font *font); 154 155 /** 156 Query whether the font flags say that this font is serif. 157 */ 158 int fz_font_is_serif(fz_context *ctx, fz_font *font); 159 160 /** 161 Query whether the font flags say that this font is monospaced. 162 */ 163 int fz_font_is_monospaced(fz_context *ctx, fz_font *font); 164 165 /** 166 Retrieve a pointer to the font bbox. 167 168 font: The font to query. 169 170 Returns a pointer to the font bbox (or NULL if the 171 font is NULL). 172 */ 173 fz_rect fz_font_bbox(fz_context *ctx, fz_font *font); 174 175 /** 176 Type for user supplied system font loading hook. 177 178 name: The name of the font to load. 179 180 bold: 1 if a bold font desired, 0 otherwise. 181 182 italic: 1 if an italic font desired, 0 otherwise. 183 needs_exact_metrics: 1 if an exact metric match is required for 184 the font requested. 185 186 Returns a new font handle, or NULL if no font found (or on error). 187 */ 188 typedef fz_font *(fz_load_system_font_fn)(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics); 189 190 /** 191 Type for user supplied cjk font loading hook. 192 193 name: The name of the font to load. 194 195 ordering: The ordering for which to load the font (e.g. 196 FZ_ADOBE_KOREA) 197 198 serif: 1 if a serif font is desired, 0 otherwise. 199 200 Returns a new font handle, or NULL if no font found (or on error). 201 */ 202 typedef fz_font *(fz_load_system_cjk_font_fn)(fz_context *ctx, const char *name, int ordering, int serif); 203 204 /** 205 Type for user supplied fallback font loading hook. 206 207 name: The name of the font to load. 208 209 script: UCDN script enum. 210 211 language: FZ_LANG enum. 212 213 serif, bold, italic: boolean style flags. 214 215 Returns a new font handle, or NULL if no font found (or on error). 216 */ 217 typedef fz_font *(fz_load_system_fallback_font_fn)(fz_context *ctx, int script, int language, int serif, int bold, int italic); 218 219 /** 220 Install functions to allow MuPDF to request fonts from the 221 system. 222 223 Only one set of hooks can be in use at a time. 224 */ 225 void fz_install_load_system_font_funcs(fz_context *ctx, 226 fz_load_system_font_fn *f, 227 fz_load_system_cjk_font_fn *f_cjk, 228 fz_load_system_fallback_font_fn *f_fallback); 229 230 /** 231 Attempt to load a given font from the system. 232 233 name: The name of the desired font. 234 235 bold: 1 if bold desired, 0 otherwise. 236 237 italic: 1 if italic desired, 0 otherwise. 238 239 needs_exact_metrics: 1 if an exact metrical match is required, 240 0 otherwise. 241 242 Returns a new font handle, or NULL if no matching font was found 243 (or on error). 244 */ 245 fz_font *fz_load_system_font(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics); 246 247 /** 248 Attempt to load a given font from 249 the system. 250 251 name: The name of the desired font. 252 253 ordering: The ordering to load the font from (e.g. FZ_ADOBE_KOREA) 254 255 serif: 1 if serif desired, 0 otherwise. 256 257 Returns a new font handle, or NULL if no matching font was found 258 (or on error). 259 */ 260 fz_font *fz_load_system_cjk_font(fz_context *ctx, const char *name, int ordering, int serif); 261 262 /** 263 Search the builtin fonts for a match. 264 Whether a given font is present or not will depend on the 265 configuration in which MuPDF is built. 266 267 name: The name of the font desired. 268 269 bold: 1 if bold desired, 0 otherwise. 270 271 italic: 1 if italic desired, 0 otherwise. 272 273 len: Pointer to a place to receive the length of the discovered 274 font buffer. 275 276 Returns a pointer to the font file data, or NULL if not present. 277 */ 278 const unsigned char *fz_lookup_builtin_font(fz_context *ctx, const char *name, int bold, int italic, int *len); 279 280 /** 281 Search the builtin base14 fonts for a match. 282 Whether a given font is present or not will depend on the 283 configuration in which MuPDF is built. 284 285 name: The name of the font desired. 286 287 len: Pointer to a place to receive the length of the discovered 288 font buffer. 289 290 Returns a pointer to the font file data, or NULL if not present. 291 */ 292 const unsigned char *fz_lookup_base14_font(fz_context *ctx, const char *name, int *len); 293 294 /** 295 Search the builtin cjk fonts for a match. 296 Whether a font is present or not will depend on the 297 configuration in which MuPDF is built. 298 299 ordering: The desired ordering of the font (e.g. FZ_ADOBE_KOREA). 300 301 len: Pointer to a place to receive the length of the discovered 302 font buffer. 303 304 Returns a pointer to the font file data, or NULL if not present. 305 */ 306 const unsigned char *fz_lookup_cjk_font(fz_context *ctx, int ordering, int *len, int *index); 307 308 /** 309 Search the builtin cjk fonts for a match for a given language. 310 Whether a font is present or not will depend on the 311 configuration in which MuPDF is built. 312 313 lang: Pointer to a (case sensitive) language string (e.g. 314 "ja", "ko", "zh-Hant" etc). 315 316 len: Pointer to a place to receive the length of the discovered 317 font buffer. 318 319 subfont: Pointer to a place to store the subfont index of the 320 discovered font. 321 322 Returns a pointer to the font file data, or NULL if not present. 323 */ 324 const unsigned char *fz_lookup_cjk_font_by_language(fz_context *ctx, const char *lang, int *len, int *subfont); 325 326 /** 327 Return the matching FZ_ADOBE_* ordering 328 for the given language tag, such as "zh-Hant", "zh-Hans", "ja", or "ko". 329 */ 330 int fz_lookup_cjk_ordering_by_language(const char *name); 331 332 /** 333 Search the builtin noto fonts for a match. 334 Whether a font is present or not will depend on the 335 configuration in which MuPDF is built. 336 337 script: The script desired (e.g. UCDN_SCRIPT_KATAKANA). 338 339 lang: The language desired (e.g. FZ_LANG_ja). 340 341 len: Pointer to a place to receive the length of the discovered 342 font buffer. 343 344 Returns a pointer to the font file data, or NULL if not present. 345 */ 346 const unsigned char *fz_lookup_noto_font(fz_context *ctx, int script, int lang, int *len, int *subfont); 347 348 /** 349 Search the builtin noto fonts specific symbol fonts. 350 Whether a font is present or not will depend on the 351 configuration in which MuPDF is built. 352 */ 353 const unsigned char *fz_lookup_noto_math_font(fz_context *ctx, int *len); 354 const unsigned char *fz_lookup_noto_music_font(fz_context *ctx, int *len); 355 const unsigned char *fz_lookup_noto_symbol1_font(fz_context *ctx, int *len); 356 const unsigned char *fz_lookup_noto_symbol2_font(fz_context *ctx, int *len); 357 const unsigned char *fz_lookup_noto_emoji_font(fz_context *ctx, int *len); 358 359 /** 360 Try to load a fallback font for the 361 given combination of font attributes. Whether a font is 362 present or not will depend on the configuration in which 363 MuPDF is built. 364 365 script: The script desired (e.g. UCDN_SCRIPT_KATAKANA). 366 367 language: The language desired (e.g. FZ_LANG_ja). 368 369 serif: 1 if serif desired, 0 otherwise. 370 371 bold: 1 if bold desired, 0 otherwise. 372 373 italic: 1 if italic desired, 0 otherwise. 374 375 Returns a new font handle, or NULL if not available. 376 */ 377 fz_font *fz_load_fallback_font(fz_context *ctx, int script, int language, int serif, int bold, int italic); 378 379 /** 380 Create a new (empty) type3 font. 381 382 name: Name of font (or NULL). 383 384 matrix: Font matrix. 385 386 Returns a new font handle, or throws exception on 387 allocation failure. 388 */ 389 fz_font *fz_new_type3_font(fz_context *ctx, const char *name, fz_matrix matrix); 390 391 /** 392 Create a new font from a font 393 file in memory. 394 395 name: Name of font (leave NULL to use name from font). 396 397 data: Pointer to the font file data. 398 399 len: Length of the font file data. 400 401 index: Which font from the file to load (0 for default). 402 403 use_glyph_box: 1 if we should use the glyph bbox, 0 otherwise. 404 405 Returns new font handle, or throws exception on error. 406 */ 407 fz_font *fz_new_font_from_memory(fz_context *ctx, const char *name, const unsigned char *data, int len, int index, int use_glyph_bbox); 408 409 /** 410 Create a new font from a font file in a fz_buffer. 411 412 name: Name of font (leave NULL to use name from font). 413 414 buffer: Buffer to load from. 415 416 index: Which font from the file to load (0 for default). 417 418 use_glyph_box: 1 if we should use the glyph bbox, 0 otherwise. 419 420 Returns new font handle, or throws exception on error. 421 */ 422 fz_font *fz_new_font_from_buffer(fz_context *ctx, const char *name, fz_buffer *buffer, int index, int use_glyph_bbox); 423 424 /** 425 Create a new font from a font file. 426 427 name: Name of font (leave NULL to use name from font). 428 429 path: File path to load from. 430 431 index: Which font from the file to load (0 for default). 432 433 use_glyph_box: 1 if we should use the glyph bbox, 0 otherwise. 434 435 Returns new font handle, or throws exception on error. 436 */ 437 fz_font *fz_new_font_from_file(fz_context *ctx, const char *name, const char *path, int index, int use_glyph_bbox); 438 439 /** 440 Create a new font from one of the built-in fonts. 441 */ 442 fz_font *fz_new_base14_font(fz_context *ctx, const char *name); 443 fz_font *fz_new_cjk_font(fz_context *ctx, int ordering); 444 fz_font *fz_new_builtin_font(fz_context *ctx, const char *name, int is_bold, int is_italic); 445 446 /** 447 Add a reference to an existing fz_font. 448 449 font: The font to add a reference to. 450 451 Returns the same font. 452 */ 453 fz_font *fz_keep_font(fz_context *ctx, fz_font *font); 454 455 /** 456 Drop a reference to a fz_font, destroying the 457 font when the last reference is dropped. 458 459 font: The font to drop a reference to. 460 */ 461 void fz_drop_font(fz_context *ctx, fz_font *font); 462 463 /** 464 Set the font bbox. 465 466 font: The font to set the bbox for. 467 468 xmin, ymin, xmax, ymax: The bounding box. 469 */ 470 void fz_set_font_bbox(fz_context *ctx, fz_font *font, float xmin, float ymin, float xmax, float ymax); 471 472 /** 473 Return a bbox for a given glyph in a font. 474 475 font: The font to look for the glyph in. 476 477 gid: The glyph to bound. 478 479 trm: The matrix to apply to the glyph before bounding. 480 481 r: Pointer to a fz_rect to use for storage. 482 483 Returns r, after filling it in with the bounds of the given 484 glyph. 485 */ 486 fz_rect fz_bound_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm); 487 488 /** 489 Determine if a given glyph in a font 490 is cacheable. Certain glyphs in a type 3 font cannot safely 491 be cached, as their appearance depends on the enclosing 492 graphic state. 493 494 font: The font to look for the glyph in. 495 496 gif: The glyph to query. 497 498 Returns non-zero if cacheable, 0 if not. 499 */ 500 int fz_glyph_cacheable(fz_context *ctx, fz_font *font, int gid); 501 502 /** 503 Run a glyph from a Type3 font to 504 a given device. 505 506 font: The font to find the glyph in. 507 508 gid: The glyph to run. 509 510 trm: The transform to apply. 511 512 dev: The device to render onto. 513 */ 514 void fz_run_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, struct fz_device *dev); 515 516 /** 517 Return the advance for a given glyph. 518 519 font: The font to look for the glyph in. 520 521 glyph: The glyph to find the advance for. 522 523 wmode: 1 for vertical mode, 0 for horizontal. 524 525 Returns the advance for the glyph. 526 */ 527 float fz_advance_glyph(fz_context *ctx, fz_font *font, int glyph, int wmode); 528 529 /** 530 Find the glyph id for a given unicode 531 character within a font. 532 533 font: The font to look for the unicode character in. 534 535 unicode: The unicode character to encode. 536 537 Returns the glyph id for the given unicode value, or 0 if 538 unknown. 539 */ 540 int fz_encode_character(fz_context *ctx, fz_font *font, int unicode); 541 542 /** 543 Encode character, preferring small-caps variant if available. 544 545 font: The font to look for the unicode character in. 546 547 unicode: The unicode character to encode. 548 549 Returns the glyph id for the given unicode value, or 0 if 550 unknown. 551 */ 552 int fz_encode_character_sc(fz_context *ctx, fz_font *font, int unicode); 553 554 /** 555 Encode character. 556 557 Either by direct lookup of glyphname within a font, or, failing 558 that, by mapping glyphname to unicode and thence to the glyph 559 index within the given font. 560 561 Returns zero for type3 fonts. 562 */ 563 int fz_encode_character_by_glyph_name(fz_context *ctx, fz_font *font, const char *glyphname); 564 565 /** 566 Find the glyph id for 567 a given unicode character within a font, falling back to 568 an alternative if not found. 569 570 font: The font to look for the unicode character in. 571 572 unicode: The unicode character to encode. 573 574 script: The script in use. 575 576 language: The language in use. 577 578 out_font: The font handle in which the given glyph represents 579 the requested unicode character. The caller does not own the 580 reference it is passed, so should call fz_keep_font if it is 581 not simply to be used immediately. 582 583 Returns the glyph id for the given unicode value in the supplied 584 font (and sets *out_font to font) if it is present. Otherwise 585 an alternative fallback font (based on script/language) is 586 searched for. If the glyph is found therein, *out_font is set 587 to this reference, and the glyph reference is returned. If it 588 cannot be found anywhere, the function returns 0. 589 */ 590 int fz_encode_character_with_fallback(fz_context *ctx, fz_font *font, int unicode, int script, int language, fz_font **out_font); 591 592 /** 593 Find the name of a glyph 594 595 font: The font to look for the glyph in. 596 597 glyph: The glyph id to look for. 598 599 buf: Pointer to a buffer for the name to be inserted into. 600 601 size: The size of the buffer. 602 603 If a font contains a name table, then the name of the glyph 604 will be returned in the supplied buffer. Otherwise a name 605 is synthesised. The name will be truncated to fit in 606 the buffer. 607 */ 608 void fz_get_glyph_name(fz_context *ctx, fz_font *font, int glyph, char *buf, int size); 609 610 /** 611 Retrieve font ascender in ems. 612 */ 613 float fz_font_ascender(fz_context *ctx, fz_font *font); 614 615 /** 616 Retrieve font descender in ems. 617 */ 618 float fz_font_descender(fz_context *ctx, fz_font *font); 619 620 /** 621 Retrieve the MD5 digest for the font's data. 622 */ 623 void fz_font_digest(fz_context *ctx, fz_font *font, unsigned char digest[16]); 624 625 /* Implementation details: subject to change. */ 626 627 void fz_decouple_type3_font(fz_context *ctx, fz_font *font, void *t3doc); 628 629 /** 630 map an FT error number to a 631 static string. 632 633 err: The error number to lookup. 634 635 Returns a pointer to a static textual representation 636 of a freetype error. 637 */ 638 const char *ft_error_string(int err); 639 int ft_char_index(void *face, int cid); 640 int ft_name_index(void *face, const char *name); 641 642 /** 643 Internal functions for our Harfbuzz integration 644 to work around the lack of thread safety. 645 */ 646 647 /** 648 Lock against Harfbuzz being called 649 simultaneously in several threads. This reuses 650 FZ_LOCK_FREETYPE. 651 */ 652 void fz_hb_lock(fz_context *ctx); 653 654 /** 655 Unlock after a Harfbuzz call. This reuses 656 FZ_LOCK_FREETYPE. 657 */ 658 void fz_hb_unlock(fz_context *ctx); 659 660 struct fz_font 661 { 662 int refs; 663 char name[32]; 664 fz_buffer *buffer; 665 666 fz_font_flags_t flags; 667 668 void *ft_face; /* has an FT_Face if used */ 669 fz_shaper_data_t shaper_data; 670 671 fz_matrix t3matrix; 672 void *t3resources; 673 fz_buffer **t3procs; /* has 256 entries if used */ 674 struct fz_display_list **t3lists; /* has 256 entries if used */ 675 float *t3widths; /* has 256 entries if used */ 676 unsigned short *t3flags; /* has 256 entries if used */ 677 void *t3doc; /* a pdf_document for the callback */ 678 void (*t3run)(fz_context *ctx, void *doc, void *resources, fz_buffer *contents, struct fz_device *dev, fz_matrix ctm, void *gstate, fz_default_colorspaces *default_cs); 679 void (*t3freeres)(fz_context *ctx, void *doc, void *resources); 680 681 fz_rect bbox; /* font bbox is used only for t3 fonts */ 682 683 int glyph_count; 684 685 /* per glyph bounding box cache */ 686 fz_rect *bbox_table; 687 688 /* substitute metrics */ 689 int width_count; 690 short width_default; /* in 1000 units */ 691 short *width_table; /* in 1000 units */ 692 693 /* cached glyph metrics */ 694 float *advance_cache; 695 696 /* cached encoding lookup */ 697 uint16_t *encoding_cache[256]; 698 699 /* cached md5sum for caching */ 700 int has_digest; 701 unsigned char digest[16]; 702 }; 703 704 #endif 705