Tensorfont - Turn font glyphs into numpy arrays¶
-
class
tensorfont.
Font
(filename, x_height_in_px=None)¶ The Font module is your entry point for using Tensorfont. Use this to load up a font and begin exploring it.
-
ascender_px
= None¶ The font’s ascender height, in font units and pixels.
-
descender_px
= None¶ The font’s descender height, in font units and pixels (usually negative).
-
full_height_px
= None¶ The font’s full (descender + ascender) height, in font units and pixels.
-
baseline_ratio
= None¶ The ascender-to-descender ratio.
-
get_xheight
()¶
-
glyph
(g)¶ Access a glyph by name. Returns a Glyph object.
-
italic_angle
¶ The italic angle of the font, in degrees.
-
m_width
¶ The width of the ‘m’ glyph, in font units.
-
x_height
¶ The height of the ‘x’ glyph, in font units.
-
pair_kerning
(left, right)¶ The kerning between two glyphs (specified by name), in font units.
-
pair_distance
(left, right, with_kerning=True)¶ The ink distance between two named glyphs, in font units. This is formed by adding the right sidebearing of the left glyph to the left sidebearing of the right glyph, plus a kerning correction. To turn off kerning, use with_kerning=False.
-
minimum_ink_distance
(left, right)¶ The distance, in pixels, between the ink of the left glyph and the ink of the right glyph, when sidebearings are discarded. For many pairs, this will be zero, as the shapes bump up against each other (consider “nn” and “oo”). However, pairs like “VA” and “xT” will have a large minimum ink distance.
-
maximum_ink_distance
(left, right)¶ The maximum distance, in pixels, between the ink of the left glyph and the ink of the right glyph, when sidebearings are discarded. In other words, the size of the “hole” in the glyph (LV has a large hole).
-
shift_distances
(l, r, dist)¶ Returns two distances, for which the left glyph matrix and the right glyph matrix should be translated such that, when the translations are done, the pair is set at a distance dist pixels apart.
(Inputs l and r are glyph names, not GlyphRendering objects.)
-
set_string
(s, pair_distance_dict={})¶ Returns a matrix containing a representation of the given string. If a dictionary is passed to pair_distance_dict, then each pair name (l,r) will be looked up in the directionary and the result will be used as a distance in pixel units at which to set the pair. If no entry is found or no dictionary is passed, then the font will be queried for the appropriate distance.
Hint: If you want to use glyph names which are not single characters, then pass an array of glyph names instead of a string.
-
-
class
tensorfont.
Glyph
(font, g)¶ A representation of a glyph and its metrics.
-
name
= None¶ The name of the glyph.
-
width
= None¶ The width of the glyph in font units (including sidebearings).
-
height
= None¶ The height of the glyph in font units.
-
lsb
= None¶ The left sidebearing in font units.
-
rsb
= None¶ The right sidebearing in font units.
-
tsb
= None¶ The top sidebearing (distance from ascender to ink top) in font units.
-
as_matrix
¶ Renders the glyph as a matrix. By default, the matrix values are integer pixel greyscale values in the range 0 to 255, but they can be normalized or turned into binary values with the appropriate keyword arguments. The matrix is returned as a GlyphRendering object which can be further manipulated.
-
-
class
tensorfont.
GlyphRendering
¶ -
classmethod
init_from_numpy
(glyph, matrix)¶
-
transform
(func)¶
-
with_padding
(left_padding, right_padding)¶ Returns a new GlyphRendering object, left and right zero-padding to the glyph image. If the padding is negative, the matrix will be trimmed appropriately.
-
with_padding_to_constant_box_width
(box_width)¶
-
with_padding_to_size
(box_height, box_width)¶
-
with_sidebearings
()¶ Returns a new GlyphRendering object, extending the image to add the glyph’s sidebearings. If the sidebearings are negative, the matrix will be trimmed appropriately.
-
mask_to_x_height
()¶ Returns a new GlyphRendering object, cropping the glyph image from the baseline to the x-height. (Assuming that the input GlyphRendering is full height.)
-
crop_descender
()¶ Returns a new GlyphRendering object, cropping the glyph image from the baseline to the ascender. (Assuming that the input GlyphRendering is full height.)
-
scale_to_height
(height)¶ Returns a new GlyphRendering object, scaling the glyph image to the given height. (The new width is calculated proportionally.)
-
left_contour
(cutoff=30, max_depth=10000)¶ Returns the left contour of the matrix; ie, the ‘sidebearing array’ from the edge of the matrix to the leftmost ink pixel. If no ink is found at a given scanline, the value of max_depth is used instead.
-
right_contour
(cutoff=30, max_depth=10000)¶ Returns the right contour of the matrix; ie, the ‘sidebearing array’ from the edge of the matrix to the rightmost ink pixel. If no ink is found at a given scanline, the value of max_depth is used instead.
-
apply_flexible_distance_kernel
(strength)¶ Transforms the matrix by applying a flexible distance kernel, with given strength.
-
gradients
()¶ Returns a pair of images representing the horizontal and vertical gradients.
-
impose
(other, distance=0)¶ Returns a new GlyphRendering object made up of two GlyphRendering objects placed side by side at the given distance.
-
set_at_distance
(other, distance=0)¶ Similar to impose but returns a pair of GlyphRendering objects separately, padded at the correct distance.
-
mask_ink_to_edge
()¶ Returns two GlyphRendering objects representing the left and right “edges” of the glyph: the first has positive values in the space between the left-hand contour and the left edge of the matrix and zero values elsewhere, and the second has positive values between the right-hand contour and the right edge of the matrix and zero values elsewhere. In other words this gives you the “white” at the edge of the glyph, without any interior counters.
-
discontinuity
(contour='left', tolerance=0.05)¶ Provides a measure, from zero to one, of the “jumpiness” or discontinuity of a contour. By default it looks at the left contour; pass contour=”right” to look at the right. You can feed this value to reduce_concavity. The tolerance parameter determines the size of “jumps” that are significance, measured in fractions of an em. By default, anything less that 0.05em is considered smooth and continuous.
-
right_face
(epsilon=0.2)¶
-
left_face
(epsilon=0.2)¶
-
reduce_concavity
(percent)¶ Smooths out concavities in glyphs like ‘T’, ‘L’, ‘E’ by interpolating between the glyph and its convex hull.
-
classmethod