Text Properties

font(fontName, fontSize=None)

Set a font with the name of the font. If a font path is given the font will be installed and used directly. Optionally a fontSize can be set directly. The default font, also used as fallback font, is ‘LucidaGrande’. The default fontSize is 10pt.

The name of the font relates to the font’s postscript name.

The font name is returned, which is handy when the font was loaded from a path.

font("Times-Italic")
fontSize(fontSize)

Set the font size in points. The default fontSize is 10pt.

fontSize(30)
fallbackFont(fontName)

Set a fallback font, this is used whenever a glyph is not available in the current font.

fallbackFont("Times")
hyphenation(value)

Set hyphenation, True or False.

txt = '''Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.'''
# enable hyphenation
hyphenation(True)
# set font size
fontSize(50)
# draw text in a box
textBox(txt, (100, 100, 800, 800))
lineHeight(value)

Set the line height.

# set line height
lineHeight(150)
# set font size
fontSize(60)
# draw text in a box
textBox("Hello World " * 10, (100, 100, 800, 800))
tracking(value)

Set the tracking between characters.

size(1000, 350)
# set tracking
tracking(100)
# set font size
fontSize(100)
# draw some text
text("hello", (100, 200))
# disable tracking
tracking(None)
# draw some text
text("world", (100, 100))
baselineShift(value)

Set the shift of the baseline.

openTypeFeatures(frac=True, case=True, ...)

Enable OpenType features.

Supported OpenType tags:

c2pc, c2sc, calt, case, cpsp, cswh, dlig, frac, liga, lnum, onum, ordn, pnum, rlig, sinf, smcp, ss01, ss02, ss03, ss04, ss05, ss06, ss07, ss08, ss09, ss10, ss11, ss12, ss13, ss14, ss15, ss16, ss17, ss18, ss19, ss20, subs, sups, swsh, titl, tnum
size(1000, 300)
# set a font
font("ACaslonPro-Regular")
# set the font size
fontSize(50)
# draw a string
text("aabcde1234567890", (100, 200))
# enable some OpenType features
openTypeFeatures(onum=True, smcp=True)
# draw the same string
text("aabcde1234567890", (100, 100))
listOpenTypeFeatures(fontName=None)

List all OpenType feature tags for the current font.

Optionally a fontName can be given. If a font path is given the font will be installed and used directly.

fontVariations(wdth=0.6, wght=0.1, ...)

Pick a variation by axes values.

size(1000, 500)
# pick a font
font("Skia")
# pick a font size
fontSize(200)
# list all axis from the current font
for axis, data in listFontVariations().items():
    print((axis, data))
# pick a variation from the current font
fontVariations(wght=.6)
# draw text!!
text("Hello Q", (100, 100))
# pick a variation from the current font
fontVariations(wght=3, wdth=1.2)
# draw text!!
text("Hello Q", (100, 300))
listFontVariations(fontName=None)

List all variation axes for the current font.

Optionally a fontName can be given. If a font path is given the font will be installed and used directly.

tabs(*tabs)

Set tabs, tuples of (float, alignment) Aligment can be “left”, “center”, “right” or any other character. If a character is provided the alignment will be right and centered on the specified character.

t = " hello w o r l d"
# replace all spaces by tabs
t = t.replace(" ", "\t")
# set some tabs
tabs((85, "center"), (232, "right"), (300, "left"))
# draw the string
text(t, (10, 10))
# reset all tabs
tabs(None)
# draw the same string
text(t, (10, 50))
language(language)

Set the preferred language as language tag or None to use the default language.

Support is depending on local OS.

size(1000, 600)
# a long dutch word
word = "paardenkop"
# a box where we draw in
box = (100, 50, 400, 500)
# set font size
fontSize(118)
# enable hyphenation
hyphenation(True)
# draw the text with no language set
textBox(word, box)
# set language to dutch (nl)
language("nl")
# shift up a bit
translate(500, 0)
# darw the text again with a language set
textBox(word, box)

Font Properties

fontContainsCharacters(characters)

Return a bool if the current font contains the provided characters. Characters is a string containing one or more characters.

fontContainsGlyph(glyphName)

Return a bool if the current font contains a provided glyph name.

fontFilePath()

Return the path to the file of the current font.

listFontGlyphNames()

Return a list of glyph names supported by the current font.

fontDescender()

Returns the current font descender, based on the current font and fontSize.

fontAscender()

Returns the current font ascender, based on the current font and fontSize.

fontXHeight()

Returns the current font x-height, based on the current font and fontSize.

fontCapHeight()

Returns the current font cap height, based on the current font and fontSize.

fontLeading()

Returns the current font leading, based on the current font and fontSize.

fontLineHeight()

Returns the current line height, based on the current font and fontSize. If a lineHeight is set, this value will be returned.

txt = "Hello World"
x, y = 10, 100

# set a font
font("Helvetica")
# set a font size
fontSize(100)
# draw the text
text(txt, (x, y))

# calculate the size of the text
textWidth, textHeight = textSize(txt)

# set a red stroke color
stroke(1, 0, 0)
# loop over all font metrics
for metric in (0, fontDescender(), fontAscender(), fontXHeight(), fontCapHeight()):
    # draw a red line with the size of the drawn text
    line((x, y+metric), (x+textWidth, y+metric))