matplotlib.ft2font#

class matplotlib.ft2font.FT2Font(filename, hinting_factor=8, *, _fallback_list=None, _kerning_factor=0)#

Bases: object

Create a new FT2Font object.

Parameters:
filenamestr or file-like

The source of the font data in a format (ttf or ttc) that FreeType can read

hinting_factorint, optional

Must be positive. Used to scale the hinting in the x-direction

_fallback_listlist of FT2Font, optional

A list of FT2Font objects used to find missing glyphs.

Warning

This API is both private and provisional: do not use it directly

_kerning_factorint, optional

Used to adjust the degree of kerning.

Warning

This API is private: do not use it directly

Attributes:
num_facesint

Number of faces in file.

face_flags, style_flagsint

Face and style flags; see the ft2font constants.

num_glyphsint

Number of glyphs in the face.

family_name, style_namestr

Face family and style name.

num_fixed_sizesint

Number of bitmap in the face.

scalablebool

Whether face is scalable; attributes after this one are only defined for scalable faces.

bboxtuple[int, int, int, int]

Face global bounding box (xmin, ymin, xmax, ymax).

units_per_EMint

Number of font units covered by the EM.

ascender, descenderint

Ascender and descender in 26.6 units.

heightint

Height in 26.6 units; used to compute a default line spacing (baseline-to-baseline distance).

max_advance_width, max_advance_heightint

Maximum horizontal and vertical cursor advance for all glyphs.

underline_position, underline_thicknessint

Vertical position and thickness of the underline bar.

postscript_namestr

PostScript name of the font.

ascender#
bbox#
clear(self)#

Clear all the glyphs, reset for a new call to set_text.

descender#
draw_glyph_to_bitmap(self, image, x, y, glyph, antialiased=True)#

Draw a single glyph to the bitmap at pixel locations x, y Note it is your responsibility to set up the bitmap manually with set_bitmap_size(w, h) before this call is made.

If you want automatic layout, use set_text in combinations with draw_glyphs_to_bitmap. This function is instead intended for people who want to render individual glyphs (e.g., returned by load_char) at precise locations.

draw_glyphs_to_bitmap(self, antialiased=True)#

Draw the glyphs that were loaded by set_text to the bitmap. The bitmap size will be automatically set to include the glyphs.

face_flags#
family_name#
fname#
get_bitmap_offset(self)#

Get the (x, y) offset in 26.6 subpixels for the bitmap if ink hangs left or below (0, 0). Since Matplotlib only supports left-to-right text, y is always 0.

get_char_index(self, codepoint)#

Return the glyph index corresponding to a character codepoint.

get_charmap(self)#

Return a dict that maps the character codes of the selected charmap (Unicode by default) to their corresponding glyph indices.

get_descent(self)#

Get the descent in 26.6 subpixels of the current string set by set_text. The rotation of the string is accounted for. To get the descent in pixels, divide this value by 64.

get_glyph_name(self, index)#

Retrieve the ASCII name of a given glyph index in a face.

Due to Matplotlib's internal design, for fonts that do not contain glyph names (per FT_FACE_FLAG_GLYPH_NAMES), this returns a made-up name which does not roundtrip through get_name_index.

get_image(self)#

Return the underlying image buffer for this font object.

get_kerning(self, left, right, mode)#

Get the kerning between left and right glyph indices. mode is a kerning mode constant:

  • KERNING_DEFAULT - Return scaled and grid-fitted kerning distances

  • KERNING_UNFITTED - Return scaled but un-grid-fitted kerning distances

  • KERNING_UNSCALED - Return the kerning vector in original font units

get_name_index(self, name)#

Return the glyph index of a given glyph name. The glyph index 0 means 'undefined character code'.

get_num_glyphs(self)#

Return the number of loaded glyphs.

get_path(self)#

Get the path data from the currently loaded glyph as a tuple of vertices, codes.

get_ps_font_info(self)#

Return the information in the PS Font Info structure.

get_sfnt(self)#

Load the entire SFNT names table, as a dict whose keys are (platform-ID, ISO-encoding-scheme, language-code, and description) tuples.

get_sfnt_table(self, name)#

Return one of the following SFNT tables: head, maxp, OS/2, hhea, vhea, post, or pclt.

get_width_height(self)#

Get the width and height in 26.6 subpixels of the current string set by set_text. The rotation of the string is accounted for. To get width and height in pixels, divide these values by 64.

get_xys(self, antialiased=True)#

Get the xy locations of the current glyphs.

Deprecated since version 3.8.

height#
load_char(self, charcode, flags=32)#

Load character with charcode in current fontfile and set glyph. flags can be a bitwise-or of the LOAD_XXX constants; the default value is LOAD_FORCE_AUTOHINT. Return value is a Glyph object, with attributes

  • width: glyph width

  • height: glyph height

  • bbox: the glyph bbox (xmin, ymin, xmax, ymax)

  • horiBearingX: left side bearing in horizontal layouts

  • horiBearingY: top side bearing in horizontal layouts

  • horiAdvance: advance width for horizontal layout

  • vertBearingX: left side bearing in vertical layouts

  • vertBearingY: top side bearing in vertical layouts

  • vertAdvance: advance height for vertical layout

load_glyph(self, glyphindex, flags=32)#

Load character with glyphindex in current fontfile and set glyph. flags can be a bitwise-or of the LOAD_XXX constants; the default value is LOAD_FORCE_AUTOHINT. Return value is a Glyph object, with attributes

  • width: glyph width

  • height: glyph height

  • bbox: the glyph bbox (xmin, ymin, xmax, ymax)

  • horiBearingX: left side bearing in horizontal layouts

  • horiBearingY: top side bearing in horizontal layouts

  • horiAdvance: advance width for horizontal layout

  • vertBearingX: left side bearing in vertical layouts

  • vertBearingY: top side bearing in vertical layouts

  • vertAdvance: advance height for vertical layout

max_advance_height#
max_advance_width#
num_charmaps#
num_faces#
num_fixed_sizes#
num_glyphs#
postscript_name#
scalable#
select_charmap(self, i)#

Select a charmap by its FT_Encoding number.

set_charmap(self, i)#

Make the i-th charmap current.

set_size(self, ptsize, dpi)#

Set the point size and dpi of the text.

set_text(self, string, angle=0.0, flags=32)#

Set the text string and angle. flags can be a bitwise-or of the LOAD_XXX constants; the default value is LOAD_FORCE_AUTOHINT. You must call this before draw_glyphs_to_bitmap. A sequence of x,y positions is returned.

style_flags#
style_name#
underline_position#
underline_thickness#
units_per_EM#
class matplotlib.ft2font.FT2Image#

Bases: object

draw_rect(self, x0, y0, x1, y1)#

Draw an empty rectangle to the image.

Deprecated since version 3.8.

draw_rect_filled(self, x0, y0, x1, y1)#

Draw a filled rectangle to the image.