Fonts¶
The module ezdxf.fonts.fonts
manages the internal usage of fonts and
has no relation how the DXF formats manages text styles.
See also
The Textstyle
entity, the DXF way to define fonts.
Added in version 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 ezdxf.options.support_dirs
:
[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 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 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¶
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:
import ezdxf
from ezdxf.fonts import fonts
fonts.build_system_font_cache()
or call the ezdxf launcher to do that:
ezdxf --fonts
Functions¶
- ezdxf.fonts.fonts.make_font(font_name: str, cap_height: float, width_factor: float = 1.0) AbstractFont ¶
Returns a font abstraction based on class
AbstractFont
.Supported font types:
.ttf, .ttc and .otf - TrueType fonts
.shx, .shp - Autodesk® shapefile fonts
.lff - LibreCAD font format
The special name “*monospace” returns the fallback font
MonospaceFont
for testing and basic measurements.Note
The font definition files are not included in ezdxf.
- Parameters:
font_name – font file name as stored in the
Textstyle
entity e.g. “OpenSans-Regular.ttf”cap_height – desired cap height in drawing units.
width_factor – horizontal text stretch factor
- ezdxf.fonts.fonts.get_font_face(font_name: str, map_shx=True) FontFace ¶
Returns the
FontFace
definition for the given font filename e.g. “LiberationSans-Regular.ttf”.This function translates a DXF font definition by the TTF font file name into a
FontFace
object. Returns theFontFace
of the default font when a font is not available on the current system.- Parameters:
font_name – raw font file name as stored in the
Textstyle
entitymap_shx – maps SHX font names to TTF replacement fonts, e.g. “TXT” -> “txt_____.ttf”
- ezdxf.fonts.fonts.find_font_face(font_name: str) FontFace ¶
Returns the
FontFace
definition for the given font filename e.g. “LiberationSans-Regular.ttf”.
- ezdxf.fonts.fonts.find_font_file_name(font_face: FontFace) str ¶
Returns the true type font file name without parent directories e.g. “Arial.ttf”.
- ezdxf.fonts.fonts.find_best_match(*, family: str = 'sans-serif', style: str = 'Regular', weight: int = 400, width: int = 5, italic: bool | None = False) FontFace | None ¶
Returns a
FontFace
that matches the given properties best. The search is based the descriptive properties and not on comparing glyph shapes. ReturnsNone
if no font was found.- Parameters:
family – font family name e.g. “sans-serif”, “Liberation Sans”
style – font style e.g. “Regular”, “Italic”, “Bold”
weight – weight in the range from 1-1000 (usWeightClass)
width – width in the range from 1-9 (usWidthClass)
italic –
True
,False
orNone
to ignore this flag
- ezdxf.fonts.fonts.get_entity_font_face(entity: DXFEntity, doc: Drawing | None = None) FontFace ¶
Returns the
FontFace
defined by the associated text style. Returns the default font face if the entity does not have or support the DXF attribute “style”. Supports the extended font information stored inTextstyle
table entries.Pass a DXF document as argument doc to resolve text styles for virtual entities which are not assigned to a DXF document. The argument doc always overrides the DXF document to which the entity is assigned to.
- ezdxf.fonts.fonts.get_font_measurements(font_name: str, map_shx=True) FontMeasurements ¶
Get
FontMeasurements
for the given font filename e.g. “LiberationSans-Regular.ttf”.- Parameters:
font_name – raw font file name as stored in the
Textstyle
entitymap_shx – maps SHX font names to TTF replacement fonts, e.g. “TXT” -> “txt_____.ttf”
- ezdxf.fonts.fonts.build_system_font_cache()¶
Builds or rebuilds the font manager cache. The font manager cache has a fixed location in the cache directory of the users home directory “~/.cache/ezdxf” or the directory specified by the environment variable “XDG_CACHE_HOME”.
- ezdxf.fonts.fonts.load()¶
Reload all cache files. The cache files are loaded automatically at the import of ezdxf.
- ezdxf.fonts.fonts.sideload_ttf(font_path: str | PathLike, cap_height) AbstractFont ¶
This function bypasses the FontManager and loads the TrueType font straight from the file system, requires the absolute font file path e.g. “C:/Windows/Fonts/Arial.ttf”.
Warning
Expert feature, use with care: no fallback font and no error handling.
Classes¶
- class ezdxf.fonts.fonts.AbstractFont(measurements: FontMeasurements)¶
The ezdxf font abstraction for text measurement and text path rendering.
- name¶
The font filename e.g. “LiberationSans-Regular.ttf”
- font_render_type¶
The font type, see enum
FontRenderType
- measurement¶
The
FontMeasurements
data.
- abstract text_width(text: str) float ¶
Returns the text width in drawing units for the given text string.
- abstract text_width_ex(text: str, cap_height: float, width_factor: float = 1.0) float ¶
Returns the text width in drawing units, bypasses the stored cap_height and width_factor.
- abstract text_path(text: str) NumpyPath2d ¶
Returns the 2D text path for the given text.
- abstract text_path_ex(text: str, cap_height: float, width_factor: float = 1.0) NumpyPath2d ¶
Returns the 2D text path for the given text, bypasses the stored cap_height and width_factor.
- abstract space_width() float ¶
Returns the width of a “space” character a.k.a. word spacing.
- class ezdxf.fonts.fonts.MonospaceFont(cap_height: float, width_factor: float = 1.0, baseline: float = 0, descender_factor: float = DESCENDER_FACTOR, x_height_factor: float = X_HEIGHT_FACTOR)¶
Represents a monospaced font where each letter has the same cap- and descender height and the same width. The given cap height and width factor are the default values for measurements and rendering. The extended methods can override these default values.
This font exists only for generic text measurement in tests and does not render any glyphs!
- class ezdxf.fonts.fonts.TrueTypeFont(font_name: str, cap_height: float, width_factor: float = 1.0)¶
Represents a TrueType font. Font measurement and glyph rendering is done by the fontTools package. The given cap height and width factor are the default values for measurements and glyph rendering. The extended methods can override these default values.
- class ezdxf.fonts.fonts.ShapeFileFont(font_name: str, cap_height: float, width_factor: float = 1.0)¶
Represents a shapefile font (.shx, .shp). Font measurement and glyph rendering is done by the ezdxf.fonts.shapefile module. The given cap height and width factor are the default values for measurements and glyph rendering. The extended methods can override these default values.
- class ezdxf.fonts.fonts.LibreCadFont(font_name: str, cap_height: float, width_factor: float = 1.0)¶
Represents a LibreCAD font (.shx, .shp). Font measurement and glyph rendering is done by the ezdxf.fonts.lff module. The given cap height and width factor are the default values for measurements and glyph rendering. The extended methods can override these default values.
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¶
DXF to store fonts in the Textstyle
entity as TTF file name e.g.
“LiberationSans-Regular.ttf”.
The 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
See also
- class ezdxf.fonts.fonts.FontRenderType¶
Enumeration of font render type.
- STROKE¶
Basic stroke font, can only be rendered as linear paths.
- OUTLINE¶
TrueType or similar font, can be rendered as filled paths or as outline strokes.
- class ezdxf.fonts.fonts.FontFace(filename, family, style, weight, width)¶
- filename¶
font file name without parent directories as string, e.g. “arial.ttf”
- family¶
Family name as string, the default value is “sans-serif”
- style¶
Font style as string, the default value is “Regular”
- weight¶
Font weight as int in the renge from 0-1000, the default value is 400 (Normal)
- width¶
Font width (stretch) as int in the range from 1-9, the default value is 5 (Normal)
- property is_italic: bool¶
Returns
True
if font face is italic.
- property is_oblique: bool¶
Returns
True
if font face is oblique.
- property is_bold: bool¶
Returns
True
if font face weight > 400.
- class ezdxf.fonts.fonts.FontMeasurements¶
See Font Anatomy for more information.
- baseline¶
- cap_height¶
- x_height¶
- descender_height¶
- scale(factor: float = 1.0) FontMeasurements ¶
- scale_from_baseline(desired_cap_height: float) FontMeasurements ¶
- shift(distance: float = 0.0) FontMeasurements ¶
- property cap_top: float¶
- property x_top: float¶
- property bottom: float¶
- property total_height: float¶