File: fonts.rst

package info (click to toggle)
ezdxf 0.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 46,952 kB
  • sloc: python: 158,141; javascript: 166; cpp: 138; makefile: 116; lisp: 20
file content (190 lines) | stat: -rw-r--r-- 4,725 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
Fonts
=====

.. module:: ezdxf.tools.fonts

The module :mod:`ezdxf.tools.fonts` manages the internal usage of fonts and
has no relation how the DXF formats manages text styles.

.. seealso::

    The :class:`~ezdxf.entities.Textstyle` entity, the DXF way to define fonts.

The tools in this module provide abstractions to get font measurements with and
without the optional `Matplotlib` package.

For a proper text rendering the font measurements are required. `Ezdxf` has a
lean approach to package dependencies, therefore the rendering results without
support from the optional Matplotlib package are not very good.

.. hint::

    If `Matplotlib` does not find an installed font and rebuilding the matplotlib
    font cache does not help, deleting the cache file ``~/.matplotlib/fontlist-v330.json``
    may help.

Font Classes
------------

.. autofunction:: make_font

.. autoclass:: AbstractFont

    .. attribute:: measurement

        The :class:`FontMeasurements` data.

    .. automethod:: text_width

    .. automethod:: space_width


.. autoclass:: MonospaceFont

    .. automethod:: text_width

    .. automethod:: space_width


.. autoclass:: MatplotlibFont

    .. automethod:: text_width

    .. automethod:: space_width

.. _font_anatomy:

Font Anatomy
------------

- A Visual Guide to the Anatomy of Typography: https://visme.co/blog/type-anatomy/
- Anatomy of a Character: https://www.fonts.com/content/learning/fontology/level-1/type-anatomy/anatomy

.. _font_properties:

Font Properties
---------------

The default way of DXF to store fonts in the :class:`~ezdxf.entities.Textstyle`
entity by using the raw TTF file name is not the way how most render backends
select fonts. 

The render backends and web technologies select the fonts by their properties.
This list shows the Matplotlib properties:

family
    List of font names in decreasing order of priority.
    The items may include a generic font family name, either
    "serif", "sans-serif", "cursive", "fantasy", or "monospace".

style
    "normal" ("regular"), "italic" or "oblique"

stretch
    A numeric value in the range 0-1000 or one of
    "ultra-condensed", "extra-condensed", "condensed",
    "semi-condensed", "normal", "semi-expanded", "expanded",
    "extra-expanded" or "ultra-expanded"

weight
    A numeric value in the range 0-1000 or one of
    "ultralight", "light", "normal", "regular", "book", "medium",
    "roman", "semibold", "demibold", "demi", "bold", "heavy",
    "extra bold", "black".

This way the backend can choose a similar font if the original font is not
available.

.. seealso::

    - Matplotlib: https://matplotlib.org/stable/api/font_manager_api.html
    - PyQt: https://doc.qt.io/archives/qtforpython-5.12/PySide2/QtGui/QFont.html
    - W3C: https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/


.. autoclass:: FontFace

    This is the equivalent to the Matplotlib :class:`FontProperties` class.

    .. attribute:: ttf

        Raw font file name as string, e.g. "arial.ttf"

    .. attribute:: family

        Family name as string, the default value is "sans-serif"

    .. attribute:: style

        Font style as string, the default value is "normal"

    .. attribute:: stretch

        Font stretch as string, the default value is "normal"

    .. attribute:: weight

        Font weight as string, the default value is "normal"

    .. autoproperty:: is_italic

        Returns ``True`` if font face is italic

    .. autoproperty:: is_oblique

        Returns ``True`` if font face is oblique

    .. autoproperty:: is_bold

        Returns ``True`` if font face weight > 400

.. class:: FontMeasurements

    See `Font Anatomy`_ for more information.

    .. attribute:: baseline

    .. attribute:: cap_height

    .. attribute:: x_height

    .. attribute:: descender_height

    .. automethod:: scale

    .. automethod:: scale_from_baseline

    .. automethod:: shift

    .. autoproperty:: cap_top

    .. autoproperty:: x_top

    .. autoproperty:: bottom

    .. autoproperty:: total_height

Font Caching
------------

`Ezdxf` uses Matplotlib to manage fonts and caches the collected information.
The default installation of `ezdxf` provides a basic set of font properties.
It is possible to create your own font cache specific for your system:
see :attr:`ezdxf.options.font_cache_directory`

The font cache is loaded automatically at startup, if not disabled by setting
config variable ``auto_load_fonts`` in ``[core]`` section to ``False`` :
see :ref:`environment_variables`

.. autofunction:: get_font_face

.. autofunction:: get_entity_font_face

.. autofunction:: get_font_measurements

.. autofunction:: build_system_font_cache

.. autofunction:: load

.. autofunction:: save