File: font_indexing.py

package info (click to toggle)
matplotlib 3.10.1%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 78,352 kB
  • sloc: python: 147,118; cpp: 62,988; objc: 1,679; ansic: 1,426; javascript: 786; makefile: 104; sh: 53
file content (36 lines) | stat: -rw-r--r-- 1,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
=============
Font indexing
=============

This example shows how the font tables relate to one another.
"""

import os

import matplotlib
from matplotlib.ft2font import FT2Font, Kerning

font = FT2Font(
    os.path.join(matplotlib.get_data_path(), 'fonts/ttf/DejaVuSans.ttf'))
font.set_charmap(0)

codes = font.get_charmap().items()

# make a charname to charcode and glyphind dictionary
coded = {}
glyphd = {}
for ccode, glyphind in codes:
    name = font.get_glyph_name(glyphind)
    coded[name] = ccode
    glyphd[name] = glyphind
    # print(glyphind, ccode, hex(int(ccode)), name)

code = coded['A']
glyph = font.load_char(code)
print(glyph.bbox)
print(glyphd['A'], glyphd['V'], coded['A'], coded['V'])
print('AV', font.get_kerning(glyphd['A'], glyphd['V'], Kerning.DEFAULT))
print('AV', font.get_kerning(glyphd['A'], glyphd['V'], Kerning.UNFITTED))
print('AV', font.get_kerning(glyphd['A'], glyphd['V'], Kerning.UNSCALED))
print('AT', font.get_kerning(glyphd['A'], glyphd['T'], Kerning.UNSCALED))