File: fonts.rst

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (311 lines) | stat: -rw-r--r-- 7,278 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
Fonts
=====

.. module:: ezdxf.fonts.fonts

The module :mod:`ezdxf.fonts.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.


.. versionadded:: 1.1

Since `ezdxf` v1.1 text rendering is done by the `fontTools`_ package.
Support for stroke fonts, these are the basic vector fonts included in CAD
applications, like .shx, .shp or .lff fonts was also added.

None of the required font files (.ttf, .ttc, .otf, .shx, .shp or .lff) are included in
`ezdxf` as they are copyrighted or, in the case of the LibreCAD font format (.lff),
licensed under the "GPL v2 and later".


Font Locations
--------------

TrueType Fonts
~~~~~~~~~~~~~~

The font manager searches the following directories recursively for .ttf, .ttc and .otf
font files.

- Windows:
    - ~/AppData/Local/Microsoft/Windows/Fonts
    - <SystemRoot>/Fonts

- Linux and other \*nix like systems:
    - /usr/share/fonts
    - /usr/local/share/fonts
    - ~/.fonts
    - ~/.local/share/fonts
    - ~/.local/share/texmf/fonts

- macOS:
    - /Library/Fonts
    - /System/Library/Fonts

The `fc-list` command on Linux shows all available fonts and their location.

The default font is selected in the following order, if none of them is available on
your system - install one of them, the open source fonts can be found in the github
repository in the folder `ezdxf/fonts`_.

- Arial.ttf
- DejaVuSansCondensed.ttf
- DejaVuSans.ttf
- LiberationSans-Regular.ttf
- OpenSans-Regular.ttf

Basic Stroke Fonts
~~~~~~~~~~~~~~~~~~

There is no universal way to find the basic stroke fonts of CAD applications on a
system, beside scanning all drives. Set the paths to the stroke fonts in your config
file manually to tell `ezdxf` where to search for them, all paths are search recursively,
see also option :attr:`ezdxf.options.support_dirs`:

.. code-block:: ini

    [core]
    support_dirs =
        "C:\Program Files\Bricsys\BricsCAD V23 en_US\Fonts",
        ~/shx_fonts,
        ~/shp_fonts,
        ~/lff_fonts,

The .shx fonts can be found on the internet but be aware that they are not free as all
websites claim. The LibreCAD font files (.llf) can be downloaded from their github
repository: https://github.com/LibreCAD/LibreCAD/tree/master/librecad/support/fonts

.. important::

    When you add new font directories to ``support_dirs`` or new fonts to one of the
    support directories, you have to rebuild the font cache to use these fonts,
    see section :ref:`Rebuilding the Font Cache` for more information.

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

The fonts available on a system are cached automatically, the cache has to be rebuild
by the :func:`build_system_font_cache` function to recognize new installed fonts or when
you add new font directories to the config file.

The cache is stored in the users home directory "~/.cache/ezdxf" or the directory
specified by the environment variable "XDG_CACHE_HOME".

.. _Rebuilding the Font Cache:

Rebuilding the Font Cache
-------------------------

When you add new fonts to your system or add a font directory to the support directories 
in the config file you have to rebuild the font-cache of `ezdxf` to
recognize these new fonts:

.. code-block:: Python

    import ezdxf
    from ezdxf.fonts import fonts

    fonts.build_system_font_cache()

or call the `ezdxf` launcher to do that::

    ezdxf --fonts

Functions
---------

.. autofunction:: make_font

.. autofunction:: get_font_face

.. autofunction:: find_font_face

.. autofunction:: find_font_file_name

.. autofunction:: find_best_match

.. autofunction:: get_entity_font_face

.. autofunction:: get_font_measurements

.. autofunction:: build_system_font_cache()

.. autofunction:: load

.. autofunction:: sideload_ttf

Classes
-------

.. autoclass:: AbstractFont

    .. attribute:: name

        The font filename e.g. "LiberationSans-Regular.ttf"

    .. attribute:: font_render_type

        The font type, see enum :class:`FontRenderType`

    .. attribute:: measurement

        The :class:`FontMeasurements` data.

    .. automethod:: text_width

    .. automethod:: text_width_ex

    .. automethod:: text_path

    .. automethod:: text_path_ex

    .. automethod:: space_width


.. autoclass:: MonospaceFont

.. autoclass:: TrueTypeFont

.. autoclass:: ShapeFileFont

.. autoclass:: LibreCadFont


.. _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
---------------

DXF to store fonts in the :class:`~ezdxf.entities.Textstyle` entity as TTF file name e.g.
"LiberationSans-Regular.ttf".

The :class:`FontFace` class can be used to specify a font in a more generic way:

family
    font name e.g. "Liberation Sans" or "Arial", may a generic font family name, either "serif",
    "sans-serif" or "monospace"

style
    "Regular", "Italic", "Oblique", "Bold", "BoldOblique", ...

width
    (usWidthClass) A numeric value in the range 0-9

    === ==================
    1   UltraCondensed
    2   ExtraCondensed
    3   Condensed
    4   SemiCondensed
    5   Normal or Medium
    6   SemiExpanded
    7   Expanded
    8   ExtraExpanded
    9   UltraExpanded
    === ==================

weight
    (usWeightClass) A numeric value in the range 0-1000

    === =============
    100 Thin
    200 ExtraLight
    300 Light
    400 Normal
    500 Medium
    600 SemiBold
    700 Bold
    800 ExtraBold
    900 Black
    === =============

.. seealso::

    - W3C: https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/

.. class:: FontRenderType

    Enumeration of font render type.

    .. attribute:: STROKE

        Basic stroke font, can only be rendered as linear paths.

    .. attribute:: OUTLINE

        TrueType or similar font, can be rendered as filled paths or as outline strokes.


.. autoclass:: FontFace

    .. attribute:: filename

        font file name without parent directories 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 "Regular"

    .. attribute:: weight

        Font weight as int in the renge from 0-1000, the default value is 400 (Normal)

    .. autoproperty:: weight_str

    .. attribute:: width

        Font width (stretch) as int in the range from 1-9, the default value is 5 (Normal)

    .. autoproperty:: width_str

    .. autoproperty:: is_italic

    .. autoproperty:: is_oblique

    .. autoproperty:: is_bold


.. 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


.. _ezdxf/fonts: https://github.com/mozman/ezdxf/tree/master/fonts
.. _fontTools: https://pypi.org/project/fonttools/