File: test_ttfautohint.py

package info (click to toggle)
python-ttfautohint-py 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 536 kB
  • sloc: python: 1,978; makefile: 109; sh: 62
file content (85 lines) | stat: -rw-r--r-- 2,303 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
from glob import glob
from io import BytesIO

from fontTools.ttLib import TTFont

from ttfautohint import ttfautohint

import pytest


DATA = os.path.join(os.path.dirname(__file__), "data")
UNHINTED_TTFS = glob(os.path.join(DATA, "*.ttf"))

GLOBAL_HINTING_TABLES = ["fpgm", "prep", "cvt ", "gasp"]


def autohint_font(ttfont, **options):
    buf = BytesIO()
    ttfont.save(buf)
    data = ttfautohint(in_buffer=buf.getvalue(), **options)
    return TTFont(BytesIO(data))


@pytest.fixture(
    params=UNHINTED_TTFS,
    ids=lambda p: os.path.basename(p)
)
def unhinted(request):
    return TTFont(request.param)


class TestTTFAutohint(object):

    def test_simple(self, unhinted):
        for tag in GLOBAL_HINTING_TABLES:
            assert tag not in unhinted
        assert not unhinted["glyf"]["a"].program
        nameID5 = unhinted["name"].getName(5, 3, 1).toUnicode()
        assert u"; ttfautohint" not in nameID5

        hinted = autohint_font(unhinted)

        for tag in GLOBAL_HINTING_TABLES:
            assert tag in hinted
        assert hinted["glyf"]["a"].program

        nameID5 = hinted["name"].getName(5, 3, 1).toUnicode()
        assert u"; ttfautohint" in nameID5

    def test_in_and_out_file_paths(self, tmpdir, unhinted):
        in_file = tmpdir / "unhinted.ttf"
        out_file = tmpdir / "hinted.ttf"

        with in_file.open("wb") as f:
            unhinted.save(f)

        n = ttfautohint(in_file=str(in_file), out_file=str(out_file))

        assert n > 0

    def test_no_info(self, unhinted):
        hinted = autohint_font(unhinted, no_info=True)

        nameID5 = hinted["name"].getName(5, 3, 1).toUnicode()
        assert u"; ttfautohint" not in nameID5

    def test_detailed_info(self, unhinted):
        hinted = autohint_font(unhinted, detailed_info=True)

        nameID5 = hinted["name"].getName(5, 3, 1).toUnicode()
        assert u"; ttfautohint" in nameID5

        assert (
            u' -l 8 -r 50 -G 200 -x 14 -D latn -f none -a qsq -X ""'
            in nameID5)

    def test_family_suffix(self, unhinted):
        suffix = " Hinted"
        hinted = autohint_font(unhinted, family_suffix=suffix)

        nametable = hinted["name"]
        nameID1 = nametable.getName(1, 3, 1).toUnicode()

        assert nameID1.endswith(suffix)