Glyph object

Glyph()#

Syntax#

Glyph(meta_dict, font)

Parameters#

NameR/OTypeDefault ValueDescription
meta_dictRequireddictionary (string as keys, integer or string as values)N/Adictionary of the meta information
fontRequiredFont objectN/AThe font the glyph belongs to

Return value#

Glyph object

Description#

Initialize a Glyph object. Load a dictionary of meta information and the font the glyph belongs.

note

Usually you get the glyph object from font.glyph(character) and do not need to use Glyph().

.meta#

Syntax#

.meta

Examples#

# `glyph` here is the Glyph object of "a" in Unifont v13.0.04
glyph.meta
# {'glyphname': 'U+0061', 'codepoint': 97, 'bbw': 8, 'bbh': 16, 'bbxoff': 0, 'bbyoff': -2, 'swx0': 500, 'swy0': 0, 'dwx0': 8, 'dwy0': 0, 'swx1': None, 'swy1': None, 'dwx1': None, 'dwy1': None, 'vvectorx': None, 'vvectory': None, 'hexdata': ['00', '00', '00', '00', '00', '00', '3C', '42', '02', '3E', '42', '42', '46', '3A', '00', '00']}
glyph.meta['bbw']
# 8

Type#

dictionary (string as keys, integer or string or list as values)

Description#

The glyph's meta data.

Glyph meta information's names (keys), value types, and their descriptions in the BDF spec:

  • 'glyphname': (string) see STARTCHAR
  • 'codepoint': (integer) see ENCODING
  • 'bbw': (integer) see BBX
  • 'bbh': (integer) see BBX
  • 'bbxoff': (integer) see BBX
  • 'bbyoff': (integer) see BBX
  • 'swx0': (integer) see SWIDTH
  • 'swy0': (integer) see SWIDTH
  • 'dwx0': (integer) see DWIDTH
  • 'dwy0': (integer) see DWIDTH
  • 'swx1': (integer) see SWIDTH1
  • 'swy1': (integer) see SWIDTH1
  • 'dwx1': (integer) see DWIDTH1
  • 'dwy1': (integer) see DWIDTH1
  • 'vvectorx': (integer) see VVECTOR
  • 'vvectory': (integer) see VVECTOR
  • 'hexdata': (list of strings) the glyph's shape data in the form of list of hexadecimal-encoded string
note

Same character or glyph's 'glyphname' (STARTCHAR) information may vary in different fonts, therefore it is unreliable to use it. Use the API method .chr() instead.

Consider using .draw().todata(4) instead of .meta['hexdata'].

.font#

Syntax#

.font

Examples#

glyph.font.headers['fontname']
# the name of the font the glyph belongs to, e.g. '-gnu-Unifont-Medium-R-Normal-Sans-16-160-75-75-c-80-iso10646-1'

Type#

Font object

Description#

It's a reference to the glyph's font object.

.cp()#

Syntax#

.cp()

Examples#

glyph.cp()
# 97

Parameters#

No parameters

Return value#

(integer) codepoint of the glyph

Description#

Get the codepoint of the glyph.

.chr()#

Syntax#

.chr()

Examples#

glyph.chr()
# 'a'

Parameters#

No parameters

Return value#

(string) character (one character string) of the glyph

Description#

Get the character of the glyph.

.draw()#

Syntax#

.draw(mode, bb)

Examples#

In these examples, we use the "quoteright" (') glyph in the BDF spec's example figure 3.

font = Font('tests/fonts/spec_example_fixed.bdf')
quoteright = font.glyph("'")
print(quoteright.draw())
Click to see the output of quoteright.draw() (with default mode 0)
.....###.
.....###.
.....###.
.....##..
....###..
....##...
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........

print(quoteright.draw(1))
Click to see the output of quoteright.draw(1)
.###
.###
.###
.##.
###.
##..

print(quoteright.draw(2))
Click to see the output of quoteright.draw(2)
.###....
.###....
.###....
.##.....
###.....
##......

print(quoteright.draw(-1, (6, 17, 1, 1)))
Click to see the output of quoteright.draw(-1, (6, 17, 1, 1))
..###.
..###.
..###.
..##..
.###..
.##...
......
......
......
......
......
......
......
......
......
......
......

Parameters#

NameR/OTypeDefault ValueDescription
modeOptionalinteger0See below
bbOptionaltuple of four integersNoneSee below

mode can be:

  • 0 (default): area represented by the bitmap hex data, positioned and resized (cropped) (fbbx × fbby) according to FONTBOUNDINGBOX (the font's global bounding box)
  • 1: area represented by the bitmap hex data, resized (cropped) according to BBX (bbw × bbh), which is the individual glyph bounding box, without unnecessary blank margin (but still possible to have blank margin sometimes)
  • 2: area represented by the bitmap hex data, original, without removing the right-padded '0's
  • -1: user specified area.
    • bb parameter is useless when mode -1 is not chosen. But if mode -1 is chosen, you MUST specify bb parameter, which is a tuple (bbx, bby, bbxoff, bbyoff) representing your customized font bounding box. Similar to FONTBOUNDINGBOX, bbx and bby represent the size, bbxoff and bbyoff represent the relative position (displacement) of the starting (bottom-left) point from the origin

Return value#

Bitmap object

Description#

Draw the glyph to a Bitmap object.

.origin()#

Syntax#

.origin(mode, fromorigin, xoff, yoff)

Examples#

quoteright.origin() # (2, 6)
quoteright.origin(mode=1) # (-2, -12)
quoteright.origin(mode=2) # (-2, -12)
quoteright.origin(mode=-1, xoff=1, yoff=1) # (-1, -1)
quoteright.origin(fromorigin=True) # (-2, -6)
quoteright.origin(mode=1, fromorigin=True) # (2, 12)

Parameters#

NameR/OTypeDefault ValueDescription
modeOptionalinteger0Same as .draw()'s mode parameter
fromoriginOptionalbooleanFalseFalse: to the origin
True: from the origin
xoffOptionalintegersNoneSee below
yoffOptionalintegersNoneSee below

Similar to .draw(), xoff and yoff parameters are useless when mode -1 is not chosen, but if mode=-1 in .origin(), you MUST specify xoff and yoff, which are equivalent to bb[2] ("bbxoff") and bb[3] ("bbyoff") in the method .draw().

Return value#

(tuple of two integers) The relative position (displacement) represented by (x, y) tuple (where right and top directions are positive)

Description#

Get the relative position (displacement) of the origin from the left bottom corner of the bitmap drawn by the method .draw(), or vice versa (i.e. displacement of the left bottom corner of the bitmap from the origin).

str() and print()#

Syntax#

str(glyph)
print(glyph)

Examples#

print(quoteright)
# or `print(str(quoteright))`
Click to see the output of str(quoteright)
.....###.
.....###.
.....###.
.....##..
....###..
....##...
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........

Description#

(string) str() gets a human-readable (multi-line) string representation of the Glyph object. It's actually the string converted from .draw() with default parameters.

print() prints a human-readable (multi-line) string representation of the Glyph object.

See also str() and print() for Bitmap object

repr()#

Syntax#

repr(glyph)

Examples#

print(repr(quoteright))
# Glyph({'glyphname': 'quoteright', 'codepoint': 39, 'bbw': 4, 'bbh': 6, 'bbxoff': 2, 'bbyoff': 12, 'swx0': 223, 'swy0': 0, 'dwx0': 5, 'dwy0': 0, 'swx1': None, 'swy1': None, 'dwx1': None, 'dwy1': None, 'vvectorx': None, 'vvectory': None, 'hexdata': ['70', '70', '70', '60', 'E0', 'C0']}, <bdfparser.Font object at 0x0000015314D3E490>)

Description#

(string) It gets a programmer-readable string representation of the Glyph object.