|
| 1 | +.. _Font: |
| 2 | + |
| 3 | +================ |
| 4 | +Font |
| 5 | +================ |
| 6 | + |
| 7 | +*(New in v1.16.18)* This class represents a font as defined in MuPDF (*fz_font_s* structure). It is required for the new class :ref:`TextWriter` and the new :meth:`Page.writeText`. Currently, it has no connection to how fonts are used in methods ``insertText`` or insertTextbox``, respectively. |
| 8 | + |
| 9 | +A Font object also contains useful general information, like the font bbox, the number of defined glyphs, glyph names or the bbox of a single glyph. |
| 10 | + |
| 11 | +**Class API** |
| 12 | + |
| 13 | +.. class:: Font |
| 14 | + |
| 15 | + .. method:: __init__(self, fontname=None, fontfile=None, |
| 16 | + fontbuffer=None, script=0, language=None, ordering=-1, is_bold=0, |
| 17 | + is_italic=0, is_serif=0) |
| 18 | + |
| 19 | + Font constructor. The large number of parameters are used to locate font, which most closely resembles the requirements. Not all parameters are ever required -- see the below pseudo code explaining the logic how the parameters are evaluated. |
| 20 | + |
| 21 | + :arg str fontname: one of the :ref:`Base-14-Fonts` or CJK fontnames. Also possible are a select few of other names like (watch the correct spelling): "Arial", "Times", "Times Roman". |
| 22 | + :arg str filename: the filename of a fontfile somewhere on your system [#f1]_. |
| 23 | + :arg bytes,bytearray,io.BytesIO fontbuffer: a fontfile loaded in memory [#f1]_. |
| 24 | + :arg in script: the number of a UCDN script. Currently supported in PyMuPDF are numbers 24, and 32 through 35. |
| 25 | + :arg str language: one of the values "zh-Hant" (traditional Chinese), "zh-Hans" (simplified Chinese), "ja" (Japanese) and "ko" (Korean). Otherwise, all ISO 639 codes from the subsets 1, 2, 3 and 5 are also possible, but are currently documentary only. |
| 26 | + :arg int ordering: an alternative selector for one of the CJK fonts. |
| 27 | + :arg bool is_bold: look for a bold font. |
| 28 | + :arg bool is_italic: look for an italic font. |
| 29 | + :arg bool is_serif: look for a serifed font. |
| 30 | + |
| 31 | + :returns: a MuPDF font if successful. This is the overall logic, how an appropriate font is located:: |
| 32 | + |
| 33 | + if fontfile: |
| 34 | + create font from it ignoring other arguments |
| 35 | + if not successful -> exception |
| 36 | + if fonbuffer: |
| 37 | + create font from it ignoring other arguments |
| 38 | + if not successful -> exception |
| 39 | + if ordering >= 0: |
| 40 | + load **"universal"** font ignoring other parameters |
| 41 | + # this will always be successful |
| 42 | + if fontname: |
| 43 | + create a Base14 font, or resp. **"universal"** font, ignoring other parameters |
| 44 | + # note: values "Arial", "Times", "Times Roman" are also possible |
| 45 | + if not successful -> exception |
| 46 | + Finally try to load a "NOTO" font using *script* and *language* parameters. |
| 47 | + if not successful: |
| 48 | + look for fallback font |
| 49 | + |
| 50 | + .. note:: |
| 51 | + |
| 52 | + With the usual abbreviations "helv", "tiro", etc., you will create fonts with the expected names "Helvetica", "Times-Roman" and so on. |
| 53 | + |
| 54 | + Using *ordering >= 0*, or fontnames starting with "china", "japan" or "korea" will always create the same **"universal"** font **"Droid Sans Fallback Regular"**. This font supports **all CJK and all Latin characters**. |
| 55 | + |
| 56 | + Actually, you would rarely ever need another font than **"Droid Sans Fallback Regular"**. **Except** that this font file is relatively large and adds about 1.65 MB (compressed) to your PDF file size. If you do not need CJK support, stick with specifying "helv", "tiro" etc., and you will get away with about 35 KB compressed. |
| 57 | + |
| 58 | + If you **know** you have a mixture of CJK and Latin text, consider just using ``Font(ordering=0)`` because this supports everything and also significantly (by a factor of two to three) speeds up execution: MuPDF will always find any character in this single font and need not check fallbacks. |
| 59 | + |
| 60 | + But if you do specify a Base-14 fontname, you will still be able to also write CJK characters! MuPDF automatically detects this situation and silently falls back to the universal font (which will then of course also be included in your PDF). |
| 61 | + |
| 62 | + **All fonts mentioned here** also support Greek and Cyrillic letters. |
| 63 | + |
| 64 | + *Monospaced* fonts are an issue: They are written with a too large width, e.g. ``" a"`` instead of ``"a"``. This applies to "cour" variants as well as most other mono fonts. The only exception we know of so far is ``consola.ttf``. If you want to output monospaced text, we recommend using the Consolas font for the time being. |
| 65 | + |
| 66 | + .. method:: has_glyph(chr, language=None, script=0) |
| 67 | + |
| 68 | + Check whether the unicode *chr* exists in the font or some fallback. May be used to check whether any "TOFU" symbols will appear on output. |
| 69 | + |
| 70 | + :arg int chr: the unicode of the character (i.e. *ord()*). |
| 71 | + :arg str language: the language -- currently unused. |
| 72 | + :arg int script: the UCDN script number. |
| 73 | + :returns: *True* or *False*. |
| 74 | + |
| 75 | + .. method:: glyph_advance(chr, language=None, script=0, wmode=0) |
| 76 | + |
| 77 | + Calculate the "width" of the character's glyph (visual representation). |
| 78 | + |
| 79 | + :arg int chr: the unicode number of the character. Use ``ord(c)``, not the character itself. Again, this should normally work even if a character is not supported by that font, because fallback fonts will be checked where necessary. |
| 80 | + |
| 81 | + The other parameters are not in use currently. This especially means that only horizontal text writing is supported. |
| 82 | + |
| 83 | + :returns: a float representing the glyph's width relative to **fontsize 1**. |
| 84 | + |
| 85 | + .. method:: glyph_name_to_unicode(name) |
| 86 | + |
| 87 | + Return the unicode for a given glyph name. Use it in conjunction with ``chr()`` if you want to output e.g. a certain symbol. |
| 88 | + |
| 89 | + :arg str name: The name of the glyph. |
| 90 | + |
| 91 | + :returns: The unicode integer, or 65533 = 0xFFFD if the name is unknown. Examples: ``font.glyph_name_to_unicode("Sigma") = 931``, ``font.glyph_name_to_unicode("sigma") = 963``. Refer to e.g. `this <https://github.com/adobe-type-tools/agl-aglfn/blob/master/glyphlist.txt>`_ publication for a list of glyph names and their unicode numbers. |
| 92 | + |
| 93 | + .. method:: unicode_to_glyph_name(chr, language=None, script=0, wmode=0) |
| 94 | + |
| 95 | + Show the name of the character's glyph. |
| 96 | + |
| 97 | + :arg int chr: the unicode number of the character. Use ``ord(c)``, not the character itself. |
| 98 | + |
| 99 | + :returns: a string representing the glyph's name. E.g. ``font.glyph_name(ord("#")) = "numbersign"``. Depending on how this font was built, the string may be empty, ".notfound" or some generated name. |
| 100 | + |
| 101 | + .. method:: text_length(text, fontsize=11) |
| 102 | + |
| 103 | + Calculate the length of a unicode string. |
| 104 | + |
| 105 | + :arg str text: a text string -- UTF-8 encoded. For Python 2, you must use unicode here. |
| 106 | + |
| 107 | + :arg float fontsize: the fontsize. |
| 108 | + |
| 109 | + :returns: a float representing the length of the string when stored in the PDF. Internally :meth:`glyph_advance` is used on a by-character level. If the font does not have a character, it will automatically be looked up in a fallback font. |
| 110 | + |
| 111 | + .. attribute:: flags |
| 112 | + |
| 113 | + A dictionary with various font properties, each represented as bools. |
| 114 | + |
| 115 | + .. attribute:: name |
| 116 | + |
| 117 | + Name of the font. May be "" or "(null)". |
| 118 | + |
| 119 | + .. attribute:: glyph_count |
| 120 | + |
| 121 | + The number of glyphs defined in the font. |
| 122 | + |
| 123 | +.. rubric:: Footnotes |
| 124 | + |
| 125 | +.. [#f1] MuPDF does not support all fontfiles with this feature and will raise exceptions like *"mupdf: FT_New_Memory_Face((null)): unknown file format"*, if encounters issues. |
0 commit comments