Glyph object
Glyph()
Syntax
Glyph(meta_dict, font)
Parameters
| Name | R/O | Type | Default Value | Description |
|---|---|---|---|---|
meta_dict | Required | dictionary (string as keys, integer or string as values) | N/A | dictionary of the meta information |
font | Required | Font object | N/A | The 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) seeSTARTCHAR'codepoint': (integer) seeENCODING'bbw': (integer) seeBBX'bbh': (integer) seeBBX'bbxoff': (integer) seeBBX'bbyoff': (integer) seeBBX'swx0': (integer) seeSWIDTH'swy0': (integer) seeSWIDTH'dwx0': (integer) seeDWIDTH'dwy0': (integer) seeDWIDTH'swx1': (integer) seeSWIDTH1'swy1': (integer) seeSWIDTH1'dwx1': (integer) seeDWIDTH1'dwy1': (integer) seeDWIDTH1'vvectorx': (integer) seeVVECTOR'vvectory': (integer) seeVVECTOR'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
| Name | R/O | Type | Default Value | Description |
|---|---|---|---|---|
mode | Optional | integer | 0 | See below |
bb | Optional | tuple of four integers | None | See below |
mode can be:
0(default): area represented by the bitmap hex data, positioned and resized (cropped) (fbbx×fbby) according toFONTBOUNDINGBOX(the font’s global bounding box)1: area represented by the bitmap hex data, resized (cropped) according toBBX(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.bbparameter is useless when mode-1is not chosen. But if mode-1is chosen, you MUST specifybbparameter, which is a tuple(bbx, bby, bbxoff, bbyoff)representing your customized font bounding box. Similar toFONTBOUNDINGBOX,bbxandbbyrepresent the size,bbxoffandbbyoffrepresent 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
| Name | R/O | Type | Default Value | Description |
|---|---|---|---|---|
mode | Optional | integer | 0 | Same as .draw()’s mode parameter |
fromorigin | Optional | boolean | False | False: to the originTrue: from the origin |
xoff | Optional | integers | None | See below |
yoff | Optional | integers | None | See 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.