Extract information from a TrueType font file. Parameters ---------- font : `.FT2Font` The TrueType font file from which information will be extracted. Returns ------- `FontEntry` The extracted font properties.
(font)
| 422 | |
| 423 | |
| 424 | def ttfFontProperty(font): |
| 425 | """ |
| 426 | Extract information from a TrueType font file. |
| 427 | |
| 428 | Parameters |
| 429 | ---------- |
| 430 | font : `.FT2Font` |
| 431 | The TrueType font file from which information will be extracted. |
| 432 | |
| 433 | Returns |
| 434 | ------- |
| 435 | `FontEntry` |
| 436 | The extracted font properties. |
| 437 | |
| 438 | """ |
| 439 | name = font.family_name |
| 440 | |
| 441 | # Styles are: italic, oblique, and normal (default) |
| 442 | |
| 443 | sfnt = font.get_sfnt() |
| 444 | mac_key = (1, # platform: macintosh |
| 445 | 0, # id: roman |
| 446 | 0) # langid: english |
| 447 | ms_key = (3, # platform: microsoft |
| 448 | 1, # id: unicode_cs |
| 449 | 0x0409) # langid: english_united_states |
| 450 | |
| 451 | # These tables are actually mac_roman-encoded, but mac_roman support may be |
| 452 | # missing in some alternative Python implementations and we are only going |
| 453 | # to look for ASCII substrings, where any ASCII-compatible encoding works |
| 454 | # - or big-endian UTF-16, since important Microsoft fonts use that. |
| 455 | sfnt2 = (sfnt.get((*mac_key, 2), b'').decode('latin-1').lower() or |
| 456 | sfnt.get((*ms_key, 2), b'').decode('utf_16_be').lower()) |
| 457 | sfnt4 = (sfnt.get((*mac_key, 4), b'').decode('latin-1').lower() or |
| 458 | sfnt.get((*ms_key, 4), b'').decode('utf_16_be').lower()) |
| 459 | |
| 460 | if sfnt4.find('oblique') >= 0: |
| 461 | style = 'oblique' |
| 462 | elif sfnt4.find('italic') >= 0: |
| 463 | style = 'italic' |
| 464 | elif sfnt2.find('regular') >= 0: |
| 465 | style = 'normal' |
| 466 | elif ft2font.StyleFlags.ITALIC in font.style_flags: |
| 467 | style = 'italic' |
| 468 | else: |
| 469 | style = 'normal' |
| 470 | |
| 471 | # Variants are: small-caps and normal (default) |
| 472 | |
| 473 | # !!!! Untested |
| 474 | if name.lower() in ['capitals', 'small-caps']: |
| 475 | variant = 'small-caps' |
| 476 | else: |
| 477 | variant = 'normal' |
| 478 | |
| 479 | # The weight-guessing algorithm is directly translated from fontconfig |
| 480 | # 2.13.1's FcFreeTypeQueryFaceInternal (fcfreetype.c). |
| 481 | wws_subfamily = 22 |
searching dependent graphs…