kiva.fonttools.font module

Defines the Kiva Font class and a utility method to parse free-form font specification strings into Font instances.

class kiva.fonttools.font.Font(face_name='', size=12, family=1, weight=400, style=0, underline=0, encoding=0)[source]

Bases: object

Font class for device independent font specification.

It is primarily based on wxPython, but looks to be similar to the needs of Mac OS X, etc.

The family defaults to SWISS so that font rotation will work correctly under wxPython. Revisit as we get more platforms defined.

copy()[source]

Returns a copy of the font object.

familymap = {0: 'serif', 1: 'sans-serif', 2: 'serif', 3: 'sans-serif', 4: 'fantasy', 5: 'cursive', 6: 'monospace'}
findfont(language=None)[source]

Returns the file name and face index of the font that most closely matches our font properties.

Parameters

language (str [optional]) – If provided, attempt to find a font which supports language.

findfontname(language=None)[source]

Returns the name of the font that most closely matches our font properties.

Parameters

language (str [optional]) – If provided, attempt to find a font which supports language.

is_bold()[source]

Is the font considered bold or not?

This is a convenience method for backends which don’t fully support font weights. We consider a font to be bold if its weight is more than medium.

property name
exception kiva.fonttools.font.FontParseError[source]

Bases: ValueError

An exception raised when font parsing fails.

kiva.fonttools.font.simple_parser(description)[source]

An extremely simple font description parser.

The parser is simple, and works by splitting the description on whitespace and examining each resulting token for understood terms:

Size

The first numeric term is treated as the font size.

Weight

The following weight terms are accepted: ‘thin’, ‘extra-light’, ‘light’, ‘regular’, ‘medium’, ‘semi-bold’, ‘bold’, ‘extra-bold’, ‘heavy’, ‘extra-heavy’.

Style

The following style terms are accepted: ‘italic’, ‘oblique’.

Decorations

The following decoration term is accepted: ‘underline’

Generic Families

The following generic family terms are accepted: ‘default’, ‘cursive’, ‘decorative’, ‘fantasy’, ‘modern’, ‘monospace’, ‘roman’, ‘sans-serif’, ‘script’, ‘serif’, ‘swiss’, ‘teletype’, ‘typewriter’.

In addition, the parser ignores the terms ‘pt’, ‘point’, ‘px’, and ‘family’. Any remaining terms are combined into the typeface name. There is no expected order to the terms.

This parser is roughly compatible with the various ad-hoc parsers in TraitsUI and Kiva, allowing for the slight differences between them and adding support for additional options supported by Pyface fonts, such as stretch and variants.

Parameters

description (str) – The font description to be parsed.

Returns

properties – Font properties suitable for use in creating a Pyface Font.

Return type

dict

Notes

This is not a particularly good parser, as it will fail to properly parse something like “10 pt times new roman” or “14 pt computer modern” since they have generic font names as part of the font face name.

This is derived from Pyface’s equivalent simple_parser. Eventually both will be replaced by better parsers that can parse something closer to a CSS font definition.

kiva.fonttools.font.str_to_font(fontspec, parser=<function simple_parser>)[source]

Converts a string specification of a font into a Font instance. string specifications are of the form: “modern 12”, “9 roman italic”, and so on.