# Glyph object

Use the bdfparser Glyph object in Python to inspect character metrics and draw individual pixel glyphs as bitmaps.

## `Glyph()`

#### Syntax

```python
Glyph(meta_dict, font)
```

#### Parameters

| Name        | <abbr title='required'>R</abbr>/<abbr title='optional'>O</abbr> | 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)](../font/#glyph) and do not need to use `Glyph()`.
:::

## `.meta`

#### Syntax

```python
.meta
```

#### Examples

```python
# `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](../../bdf_spec/file_format/individual_glyph_info/):

- `'glyphname'`: (_string_) see [`STARTCHAR`](../../bdf_spec/file_format/individual_glyph_info/#startchar)
- `'codepoint'`: (_integer_) see [`ENCODING`](../../bdf_spec/file_format/individual_glyph_info/#encoding)
- `'bbw'`: (_integer_) see [`BBX`](../../bdf_spec/file_format/individual_glyph_info/#bbx)
- `'bbh'`: (_integer_) see [`BBX`](../../bdf_spec/file_format/individual_glyph_info/#bbx)
- `'bbxoff'`: (_integer_) see [`BBX`](../../bdf_spec/file_format/individual_glyph_info/#bbx)
- `'bbyoff'`: (_integer_) see [`BBX`](../../bdf_spec/file_format/individual_glyph_info/#bbx)
- `'swx0'`: (_integer_) see [`SWIDTH`](../../bdf_spec/file_format/individual_glyph_info/#swidth)
- `'swy0'`: (_integer_) see [`SWIDTH`](../../bdf_spec/file_format/individual_glyph_info/#swidth)
- `'dwx0'`: (_integer_) see [`DWIDTH`](../../bdf_spec/file_format/individual_glyph_info/#dwidth)
- `'dwy0'`: (_integer_) see [`DWIDTH`](../../bdf_spec/file_format/individual_glyph_info/#dwidth)
- `'swx1'`: (_integer_) see [`SWIDTH1`](../../bdf_spec/file_format/individual_glyph_info/#swidth1)
- `'swy1'`: (_integer_) see [`SWIDTH1`](../../bdf_spec/file_format/individual_glyph_info/#swidth1)
- `'dwx1'`: (_integer_) see [`DWIDTH1`](../../bdf_spec/file_format/individual_glyph_info/#dwidth1)
- `'dwy1'`: (_integer_) see [`DWIDTH1`](../../bdf_spec/file_format/individual_glyph_info/#dwidth1)
- `'vvectorx'`: (_integer_) see [`VVECTOR`](../../bdf_spec/file_format/individual_glyph_info/#vvector)
- `'vvectory'`: (_integer_) see [`VVECTOR`](../../bdf_spec/file_format/individual_glyph_info/#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()](#chr) instead.

Consider using .[draw()](#draw).[todata(4)](../bitmap/#todata) instead of `.meta['hexdata']`.
:::

## `.font`

#### Syntax

```python
.font
```

#### Examples

```python
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

```python
.cp()
```

#### Examples

```python
glyph.cp()
# 97
```

#### Parameters

No parameters

#### Return value

(_integer_) codepoint of the glyph

#### Description

Get the codepoint of the glyph.

## `.chr()`

#### Syntax

```python
.chr()
```

#### Examples

```python
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

```python
.draw(mode, bb)
```

#### Examples

In these examples, we use [the "quoteright" (`'`) glyph in the BDF spec's example figure 3](../../bdf_spec/examples/#figure-3).

```python
font = Font('tests/fonts/spec_example_fixed.bdf')
quoteright = font.glyph("'")
print(quoteright.draw())
```

<details>
<summary>Click to see the output of <code>quoteright.draw()</code> (with default mode 0)</summary>

```
.....###.
.....###.
.....###.
.....##..
....###..
....##...
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
```

</details>

---

```python
print(quoteright.draw(1))
```

<details>
<summary>Click to see the output of <code>quoteright.draw(1)</code></summary>

```
.###
.###
.###
.##.
###.
##..
```

</details>

---

```python
print(quoteright.draw(2))
```

<details>
<summary>Click to see the output of <code>quoteright.draw(2)</code></summary>

```
.###....
.###....
.###....
.##.....
###.....
##......
```

</details>

---

```python
print(quoteright.draw(-1, (6, 17, 1, 1)))
```

<details>
<summary>Click to see the output of <code>quoteright.draw(-1, (6, 17, 1, 1))</code></summary>

```
..###.
..###.
..###.
..##..
.###..
.##...
......
......
......
......
......
......
......
......
......
......
......
```

</details>

#### Parameters

| Name   | <abbr title='required'>R</abbr>/<abbr title='optional'>O</abbr> | 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 to [`FONTBOUNDINGBOX`](../../bdf_spec/file_format/global_font_info/#fontboundingbox) (the font's global bounding box)
- `1`: area represented by the bitmap hex data, resized (cropped) according to [`BBX`](../../bdf_spec/file_format/individual_glyph_info/#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](../../bdf_spec/examples/#figure-3) `'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`](../../bdf_spec/file_format/global_font_info/#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_](../bitmap/) object

#### Description

Draw the glyph to a _Bitmap_ object.

## `.origin()`

#### Syntax

```python
.origin(mode, fromorigin, xoff, yoff)
```

#### Examples

```python
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         | <abbr title='required'>R</abbr>/<abbr title='optional'>O</abbr> | Type       | Default Value | Description                                         |
| :----------- | :-------------------------------------------------------------- | :--------- | :------------ | :-------------------------------------------------- |
| `mode`       | Optional                                                        | _integer_  | `0`           | Same as [`.draw()`](#draw)'s `mode` parameter       |
| `fromorigin` | Optional                                                        | _boolean_  | `False`       | `False`: to the origin<br />`True`: from the origin |
| `xoff`       | Optional                                                        | _integers_ | `None`        | See below                                           |
| `yoff`       | Optional                                                        | _integers_ | `None`        | See below                                           |

Similar to [`.draw()`](#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()`](#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()`](#draw), or vice versa (i.e. displacement of the left bottom corner of the bitmap from the origin).

## `str()` and `print()`

#### Syntax

```python
str(glyph)
print(glyph)
```

#### Examples

```python
print(quoteright)
# or `print(str(quoteright))`
```

<details>
<summary>Click to see the output of <code>str(quoteright)</code></summary>

```
.....###.
.....###.
.....###.
.....##..
....###..
....##...
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
.........
```

</details>

#### 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](../bitmap/#str-and-print)

## `repr()`

#### Syntax

```python
repr(glyph)
```

#### Examples

```python
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.
