Font

class Font

Font controls options applied when drawing and measuring text.

Classes

Edging

Whether edge pixels draw opaque or with partial transparency.

Methods

__init__

Overloaded function.

countText

Returns number of glyphs represented by text.

dump

Dumps fields of the font to SkDebugf.

getBounds

Retrieves the bounds for each glyph in glyphs.

getEdging

Whether edge pixels draw opaque or with partial transparency.

getHinting

Returns level of glyph outline adjustment.

getMetrics

Returns FontMetrics associated with Typeface.

getPath

Modifies path to be the outline of the glyph.

getPaths

Returns path corresponding to glyph array.

getPos

Retrieves the positions for each glyph, beginning at the specified origin.

getScaleX

Returns text scale on x-axis.

getSize

Returns text size in points.

getSkewX

Returns text skew on x-axis.

getSpacing

Returns the recommended spacing between lines: the sum of metrics descent, ascent, and leading.

getTypeface

Returns Typeface if set, or nullptr.

getWidths

Retrieves the advance and bounds for each glyph in glyphs.

getWidthsBounds

Retrieves the advance and bounds for each glyph in glyphs.

getXPos

Retrieves the x-positions for each glyph, beginning at the specified origin.

isBaselineSnap

Returns true if baselines will be snapped to pixel positions when the current transformation matrix is axis aligned.

isEmbeddedBitmaps

Returns true if font engine may return glyphs from font bitmaps instead of from outlines.

isEmbolden

Returns true if bold is approximated by increasing the stroke width when creating glyph bitmaps from outlines.

isForceAutoHinting

If true, instructs the font manager to always hint glyphs.

isLinearMetrics

Returns true if font and glyph metrics are requested to be linearly scalable.

isSubpixel

Returns true if glyphs may be drawn at sub-pixel offsets.

makeWithSize

Returns a font with the same attributes of this font, but with the specified size.

measureText

Returns the advance width of text.

refTypeface

Increases Typeface RefCnt by one.

setBaselineSnap

Requests that baselines be snapped to pixels when the current transformation matrix is axis aligned.

setEdging

Requests, but does not require, that edge pixels draw opaque or with partial transparency.

setEmbeddedBitmaps

Requests, but does not require, to use bitmaps in fonts instead of outlines.

setEmbolden

Increases stroke width when creating glyph bitmaps to approximate a bold typeface.

setForceAutoHinting

Sets whether to always hint glyphs.

setHinting

Sets level of glyph outline adjustment.

setLinearMetrics

Requests, but does not require, linearly scalable font and glyph metrics.

setScaleX

Sets text scale on x-axis.

setSize

Sets text size in points.

setSkewX

Sets text skew on x-axis.

setSubpixel

Requests, but does not require, that glyphs respect sub-pixel positioning.

setTypeface

Sets Typeface to typeface, decreasing RefCnt of the previous Typeface.

textToGlyphs

Converts text into glyph indices.

unicharToGlyph

Returns glyph index for Unicode character.

unicharsToGlyphs

Attributes

kAlias

kAntiAlias

kSubpixelAntiAlias

Methods

Font.__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: skia.Font) -> None

    Constructs Font with default values.

  2. __init__(self: skia.Font, typeface: object, size: float) -> None

    Constructs Font with default values with Typeface and size in points.

    typeface:

    font and style used to draw and measure text

    size:

    typographic height of text

  3. __init__(self: skia.Font, typeface: object) -> None

    Constructs Font with default values with Typeface.

    typeface:

    font and style used to draw and measure text

  4. __init__(self: skia.Font, typeface: object, size: float, scaleX: float, skewX: float) -> None

    Constructs Font with default values with Typeface and size in points, horizontal scale, and horizontal skew.

    Horizontal scale emulates condensed and expanded fonts. Horizontal skew emulates oblique fonts.

    typeface:

    font and style used to draw and measure text

    size:

    typographic height of text

    scaleX:

    text horizontal scale

    skewX:

    additional shear on x-axis relative to y-axis

Font.countText(self: skia.Font, text: str, encoding: skia.TextEncoding = skia.TextEncoding.kUTF8) int

Returns number of glyphs represented by text.

If encoding is TextEncoding.kUTF8, TextEncoding.kUTF16, or TextEncoding.kUTF32; then each Unicode codepoint is mapped to a single glyph.

Parameters:
Font.dump(self: skia.Font) None

Dumps fields of the font to SkDebugf.

May change its output over time, so clients should not rely on this for anything specific. Used to aid in debugging.

Font.getBounds(self: skia.Font, glyphs: list[int], paint: SkPaint = None) list[skia.Rect]

Retrieves the bounds for each glyph in glyphs.

If paint is not nullptr, its stroking, PathEffect, and MaskFilter fields are respected.

Parameters:
Returns:

bounds for each glyph relative to (0, 0)

Return type:

List[skia.Rect]

Font.getEdging(self: skia.Font) skia.Font.Edging

Whether edge pixels draw opaque or with partial transparency.

Font.getHinting(self: skia.Font) skia.FontHinting

Returns level of glyph outline adjustment.

Font.getMetrics(self: skia.Font) SkFontMetrics

Returns FontMetrics associated with Typeface.

Results are scaled by text size but does not take into account dimensions required by text scale, text skew, fake bold, style stroke, and PathEffect.

Return type:

FontMetrics

Font.getPath(self: skia.Font, glyphID: int) object

Modifies path to be the outline of the glyph.

If the glyph has an outline, modifies path to be the glyph’s outline and returns true. The glyph outline may be empty. Degenerate contours in the glyph outline will be skipped. If glyph is described by a bitmap, returns false and ignores path parameter.

Parameters:

glyphID (int) – index of glyph

Returns:

true if glyphID is described by path

Font.getPaths(self: skia.Font, glyphIDs: list[int]) object

Returns path corresponding to glyph array.

Parameters:

glyphIDs – array of glyph indices

Returns:

list of Path

Font.getPos(self: skia.Font, glyphs: list[int], origin: skia.Point = skia.Point(0, 0)) list[skia.Point]

Retrieves the positions for each glyph, beginning at the specified origin.

Parameters:
  • glyphs (List[int]) – array of glyph indices to be positioned

  • origin (skia.Point) – location of the first glyph. Defaults to (0, 0).

Returns:

glyphs positions

Font.getScaleX(self: skia.Font) float

Returns text scale on x-axis.

Default value is 1.

Returns:

text horizontal scale

Font.getSize(self: skia.Font) float

Returns text size in points.

Returns:

typographic height of text

Font.getSkewX(self: skia.Font) float

Returns text skew on x-axis.

Default value is zero.

Returns:

additional shear on x-axis relative to y-axis

Font.getSpacing(self: skia.Font) float

Returns the recommended spacing between lines: the sum of metrics descent, ascent, and leading.

Result is scaled by text size but does not take into account dimensions required by stroking and PathEffect. Returns the same result as getMetrics().

Returns:

recommended spacing between lines

Font.getTypeface(self: skia.Font) skia.Typeface

Returns Typeface if set, or nullptr.

Returns:

Typeface if previously set, nullptr otherwise

Font.getWidths(self: skia.Font, glyphs: list[int]) list[float]

Retrieves the advance and bounds for each glyph in glyphs.

Parameters:

glyphs (List[int]) – array of glyph indices to be measured

Return type:

List[float]

Font.getWidthsBounds(self: skia.Font, glyphs: list[int], paint: SkPaint = None) tuple

Retrieves the advance and bounds for each glyph in glyphs.

Parameters:
Returns:

Widths and bounds array

Return type:

Tuple[List[float],List[skia.Rect]]

Font.getXPos(self: skia.Font, glyphs: list[int], origin: float = 0) list[float]

Retrieves the x-positions for each glyph, beginning at the specified origin.

Parameters:
  • glyphs (List[int]) – array of glyph indices to be positioned

  • origin (skia.Point) – location of the first glyph. Defaults to 0.

Returns:

glyphs x-positions

Font.isBaselineSnap(self: skia.Font) bool

Returns true if baselines will be snapped to pixel positions when the current transformation matrix is axis aligned.

Returns:

baselines may be snapped to pixels

Font.isEmbeddedBitmaps(self: skia.Font) bool

Returns true if font engine may return glyphs from font bitmaps instead of from outlines.

Returns:

true if glyphs may be font bitmaps

Font.isEmbolden(self: skia.Font) bool

Returns true if bold is approximated by increasing the stroke width when creating glyph bitmaps from outlines.

Returns:

bold is approximated through stroke width

Font.isForceAutoHinting(self: skia.Font) bool

If true, instructs the font manager to always hint glyphs.

Returned value is only meaningful if platform uses FreeType as the font manager.

Returns:

true if all glyphs are hinted

Font.isLinearMetrics(self: skia.Font) bool

Returns true if font and glyph metrics are requested to be linearly scalable.

Returns:

true if font and glyph metrics are requested to be linearly scalable.

Font.isSubpixel(self: skia.Font) bool

Returns true if glyphs may be drawn at sub-pixel offsets.

Returns:

true if glyphs may be drawn at sub-pixel offsets.

Font.makeWithSize(self: skia.Font, size: float) skia.Font

Returns a font with the same attributes of this font, but with the specified size.

Returns nullptr if size is less than zero, infinite, or NaN.

Parameters:

size (float) – typographic height of text

Font.measureText(self: skia.Font, text: str, encoding: skia.TextEncoding = skia.TextEncoding.kUTF8, bounds: skia.Rect = None, paint: SkPaint = None) float

Returns the advance width of text.

The advance is the normal distance to move before drawing additional text. Returns the bounding box of text if bounds is not nullptr. paint stroke width or PathEffect may modify the advance with.

Parameters:
Returns:

the advance width of text

Font.refTypeface(self: skia.Font) skia.Typeface

Increases Typeface RefCnt by one.

Returns:

Typeface if previously set, nullptr otherwise

Font.setBaselineSnap(self: skia.Font, baselineSnap: bool) None

Requests that baselines be snapped to pixels when the current transformation matrix is axis aligned.

Parameters:

baselineSnap (bool) – setting for baseline snapping to pixels

Font.setEdging(self: skia.Font, edging: skia.Font.Edging) None

Requests, but does not require, that edge pixels draw opaque or with partial transparency.

Font.setEmbeddedBitmaps(self: skia.Font, embeddedBitmaps: bool) None

Requests, but does not require, to use bitmaps in fonts instead of outlines.

:param bool embeddedBitmaps setting to use bitmaps in fonts

Font.setEmbolden(self: skia.Font, embolden: bool) None

Increases stroke width when creating glyph bitmaps to approximate a bold typeface.

Parameters:

embolden (bool) – setting for bold approximation

Font.setForceAutoHinting(self: skia.Font, forceAutoHinting: bool) None

Sets whether to always hint glyphs.

If forceAutoHinting is set, instructs the font manager to always hint glyphs.

Only affects platforms that use FreeType as the font manager.

Parameters:

forceAutoHinting (bool) – setting to always hint glyphs

Font.setHinting(self: skia.Font, hintingLevel: skia.FontHinting) None

Sets level of glyph outline adjustment.

Does not check for valid values of hintingLevel.

Font.setLinearMetrics(self: skia.Font, linearMetrics: bool) None

Requests, but does not require, linearly scalable font and glyph metrics.

For outline fonts ‘true’ means font and glyph metrics should ignore hinting and rounding. Note that some bitmap formats may not be able to scale linearly and will ignore this flag.

Parameters:

linearMetrics (bool) – setting for linearly scalable font and glyph metrics.

Font.setScaleX(self: skia.Font, scaleX: float) None

Sets text scale on x-axis.

Default value is 1.

Parameters:

scaleX (float) – text horizontal scale

Font.setSize(self: skia.Font, textSize: float) None

Sets text size in points.

Has no effect if textSize is not greater than or equal to zero.

Parameters:

textSize (float) – typographic height of text

Font.setSkewX(self: skia.Font, skewX: float) None

Sets text skew on x-axis.

Default value is zero.

Parameters:

skewX (float) – additional shear on x-axis relative to y-axis

Font.setSubpixel(self: skia.Font, subpixel: bool) None

Requests, but does not require, that glyphs respect sub-pixel positioning.

Parameters:

subpixel (bool) – setting for sub-pixel positioning

Font.setTypeface(self: skia.Font, typeface: skia.Typeface) None

Sets Typeface to typeface, decreasing RefCnt of the previous Typeface.

Pass nullptr to clear Typeface and use the default typeface. Increments tf RefCnt by one.

Parameters:

typeface – font and style used to draw text

Font.textToGlyphs(self: skia.Font, text: str, encoding: skia.TextEncoding = skia.TextEncoding.kUTF8) list[int]

Converts text into glyph indices.

Returns the number of glyph indices represented by text. TextEncoding specifies how text represents characters or glyphs.

Does not check text for valid character codes or valid glyph indices.

If encoding is TextEncoding.kUTF8 and text contains an invalid UTF-8 sequence, empty array is returned.

When encoding is TextEncoding.kUTF8, TextEncoding.kUTF16, or TextEncoding.kUTF32; then each Unicode codepoint is mapped to a single glyph. This function uses the default character-to-glyph mapping from the Typeface and maps characters not found in the Typeface to zero.

Parameters:
Returns:

glyphs represented by text

Font.unicharToGlyph(self: skia.Font, uni: int) int

Returns glyph index for Unicode character.

If the character is not supported by the Typeface, returns 0.

Parameters:

uni (int) – Unicode character

Font.unicharsToGlyphs(self: skia.Font, uni: list[int]) list[int]

Attributes

Font.kAlias = <Edging.kAlias: 0>
Font.kAntiAlias = <Edging.kAntiAlias: 1>
Font.kSubpixelAntiAlias = <Edging.kSubpixelAntiAlias: 2>