mcfonts.utils.exporting.opentype
¶
Functions for exporting into OpenType XML-representable data (TTX).
Contains functions for transforming and handling OpenType font XML data..
Module Contents¶
Functions¶
|
Set a namerecord in a font. |
|
Allocate a Unicode character in a font. |
|
Given a set of characters, return a list of ulUnicodeRanges, with bits set appropriately. |
|
Include glyph into the font XML. |
|
Insert cmap subtables in font_xml. These are used for mapping glyph IDs to Unicode codepoints. |
|
Ensure |
|
Set the font's appropriate CFF name in 'CFF ' tables. |
|
Set the font's name to font_xml in all appropriate places. |
|
Set the font's created and modified times, in the format of |
|
Set the |
|
Set a program to a character. |
|
Add/set a space character to the font, only defining its width. |
|
Generate the ulUnicodeRanges corresponding to characters and set them in font_xml. |
Attributes¶
A mapping of (block codepoint start, block codepoint end) to ulUnicodeRange bit. |
|
Bytes of a blank TTF XML, with a |
- mcfonts.utils.exporting.opentype.EXCLUDED_FONT_NAME_CHARS¶
- mcfonts.utils.exporting.opentype.ULUNICODERANGE_BIT_MAP : dict[tuple[int, int], int]¶
A mapping of (block codepoint start, block codepoint end) to ulUnicodeRange bit. It is best to store a single 128 bit number and then split it up for processing later. See https://learn.microsoft.com/en-us/typography/opentype/spec/os2#ur.
-
mcfonts.utils.exporting.opentype.XML_FONT_TEMPLATE : bytes =
b'<?xml version="1.0" encoding="UTF-8"?>\n<ttFont sfntVersion="OTTO" ttLibVersion="4.34">\n ...
¶ Bytes of a blank TTF XML, with a
.notdef
glyph already embedded. You must calllxml.etree.XML()
with this.
- mcfonts.utils.exporting.opentype.add_namerecord_to_font(font_xml, data, name_id)¶
Set a namerecord in a font.
This doesn't check if such a namerecord already exists; this will add new namerecords.
0 -> Copyright
1 -> Font family
2 -> Font subfamily
3 -> Unique font ID
4 -> Full font name, ID 1 + 2
5 -> Version: "Version maj.min"
6 -> PostScript name
7 -> Trademark
8 -> Manufacturer
9 -> Designer
10 -> Descriptions
... see more at https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-ids.
All are encoded at (0, 4) and (3, 1), Unicode 2.0+ full.
- mcfonts.utils.exporting.opentype.allocate_character(font_xml, character)¶
Allocate a Unicode character in a font.
This doesn't assign any character data or widths.
- mcfonts.utils.exporting.opentype.generate_ulunicoderange(characters)¶
Given a set of characters, return a list of ulUnicodeRanges, with bits set appropriately.
See https://learn.microsoft.com/en-us/typography/opentype/spec/os2#ur.
>>> generate_ulunicoderange({"A", "B", "☀", "᯿", "�"}) [ '00000000 00000000 00000000 00000000', '00000000 00000000 00000000 00100000', '00000000 00000000 01000000 00000000', '00000000 00000000 00000000 00000001', ]
- mcfonts.utils.exporting.opentype.include_glyph(font_xml, glyph, character, allocated_characters)¶
Include glyph into the font XML.
Different Glyph subtypes have different methods of being imported; this function centralizes those varying methods.
-
mcfonts.utils.exporting.opentype.make_cmap(font_xml, format_4=
True
, format_12=True
)¶ Insert cmap subtables in font_xml. These are used for mapping glyph IDs to Unicode codepoints.
If format_4, cmap_format_4 will be added. If format_12, cmap_format_12 will be added.
Format 12: https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-12-segmented-coverage
- Parameters:¶
- font_xml : lxml.etree._Element¶
A font XML.
- format_4 : bool¶
Whether to insert a cmap format 4 subtable. See https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-4-segment-mapping-to-delta-values.
- format_12 : bool¶
Whether to insert a cmap format 12 subtable. See https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-12-segmented-coverage.
- Return type:¶
None
- mcfonts.utils.exporting.opentype.sanitize_font_name(font_name)¶
Ensure
sanitized_font_name
is a valid PostScript font name.A PostScript font name can't:
Contain
(){}[]<;>%/
or spaceBe longer than 63 characters
Have non-ASCII characters
>>> sanitize_font_name("☀ This is a really long name!") '_This_is_a_really_long_name!' >>> sanitize_font_name("(secret info) [do not leak]") '_secret_info___do_not_leak_' >>> sanitize_font_name("a really long name with over 63 characters I guarantee it you won't believe it") 'a_really_long_name_with_over_63_characters_I_guarantee_it_you_w'
- mcfonts.utils.exporting.opentype.set_cfffont_name(font_xml, font_name, family_name)¶
Set the font's appropriate CFF name in 'CFF ' tables.
This is different from changing the 'name' tables, see
add_namerecord_to_font()
instead.
- mcfonts.utils.exporting.opentype.set_font_name(font_xml, font_name)¶
Set the font's name to font_xml in all appropriate places.
This is different from
set_cfffont_name()
, which sets the name for only CFF tables.
- mcfonts.utils.exporting.opentype.set_font_times(font_xml)¶
Set the font's created and modified times, in the format of
%a %b %d %X %Y
.
- mcfonts.utils.exporting.opentype.set_notdef_in_font(font_xml, program)¶
Set the
.notdef
character of the font.Note
The default font template already has a default notdef, so use this for setting it to something else.
-
mcfonts.utils.exporting.opentype.set_program_to_character(font_xml, program, character, replace=
True
)¶ Set a program to a character.
This is how character data is added to the font. The character must be allocated already; that isn't done in this function.
If the character isn't in the font, add it. If it's and
replace
is True, set it to the new character data. Otherwise, do nothing.- Parameters:¶
- Returns:¶
Nothing, font is modified in-place.
- Raises:¶
GlyphLimitError -- If there are more than 65,535 glyphs in the font; can't add more.
-
mcfonts.utils.exporting.opentype.set_space_to_character(font_xml, character, width, replace=
True
)¶ Add/set a space character to the font, only defining its width.
The character must be allocated already. That isn't done in this function.
If the glyph isn't in the font, add it. If it's and
replace
is True, set it to the new value. Otherwise, do nothing.- Parameters:¶
- Returns:¶
Nothing, font is modified in-place.
- Raises:¶
GlyphLimitError -- If there are more than 65,535 glyphs in the font; can't add more.
- Return type:¶
None
- mcfonts.utils.exporting.opentype.set_ulunicoderange(font_xml, characters)¶
Generate the ulUnicodeRanges corresponding to characters and set them in font_xml.