campy.graphics.gfont module

Friendly fonts.

A GFont provides a convenient way to define fonts.

To get the current platform’s default font:

font = GFont.default()

To get a font from a text description in the form FAMILY-STYLE or FAMILY-STYLE-MODIFIERS:

font1 = GFont.parse('Helvetica-12')
font2 = GFont.parse('Times New Roman-24-bold')
font3 = GFont.parse('Verdana-11-bold-italic')

For finer grained control, use the constructor directly.

font1 = GFont.parse(‘Helvetica’, 12) font2 = GFont.parse(‘Times New Roman’, 24, weight=True) font3 = GFont.parse(‘Verdana’, 11, weight=True, slant=True)

To access a system font by name, such as the macOS alert header:

font = GFont.system('systemAlertHeaderFont')

Note that a given system font may not be accessible on all platforms.

Once you have a GFont, you can access its family, size, weight, and slant. You can also access font metrics directly, such as the font’s ascent, the font’s descent, the font’s line spacing, and whether the font is fixed-width. These are properties that apply to the font as a whole, and not to any particular text rendered in this font.

To get the width of a particular string, use GFont.measure():

font = GFont.default()
width = font.measure('Hello World!')
print(width)  # => 77
class campy.graphics.gfont.GFont(family, size, weight=False, slant=False)[source]

Bases: object

ascent
classmethod default()[source]
descent
family
fixed
linespace
measure(text)[source]
classmethod parse(description)[source]

Parse a description into a font.

A description can be either FAMILY-SIZE or FAMILY-SIZE-MODIFIERS, where FAMILY is the name of a font family available on the current system, SIZE is a positive integer representing the font size (in points, not in pixels), and MODIFIERS is either the string “bold”, “bold-italic”, or “italic”. Examples of valid font descriptions include:

Helvetica-12
Dialog-13
Courier-18
Times New Roman-24-bold
Times-16-italic
Verdana-11-bold-italic

This method returns None if no font could be parsed.

size
slant
classmethod system(font_name)[source]
weight