File: gftools-list-widthclass.py

package info (click to toggle)
gftools 0.5.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 3,252 kB
  • sloc: python: 9,619; xml: 432; sh: 8; makefile: 4
file content (113 lines) | stat: -rwxr-xr-x 3,356 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
# Copyright 2016 The Fontbakery Authors
# Copyright 2017 The Google Font Tools Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import print_function
import argparse
import csv
import os
import sys
import tabulate
from fontTools import ttLib

parser = argparse.ArgumentParser(description='Print out'
                                             ' usWidthClass of the fonts')
parser.add_argument('font', nargs="+")
parser.add_argument('--csv', default=False, action='store_true')
parser.add_argument('--set', type=int, default=0)
parser.add_argument('--autofix', default=False, action='store_true')

def print_info(fonts, print_csv=False):
    headers = ['filename', 'usWidthClass']
    rows = []
    warnings = []
    for font in fonts:
        ttfont = ttLib.TTFont(font)
        usWidthClass = ttfont['OS/2'].usWidthClass
        rows.append([os.path.basename(font), usWidthClass])
        if usWidthClass != 5:
            warning = "WARNING: {} is {}, expected 5"
            warnings.append(warning.format(font, usWidthClass))

    def as_csv(rows):
        writer = csv.writer(sys.stdout)
        writer.writerows([headers])
        writer.writerows(rows)
        sys.exit(0)

    if print_csv:
        as_csv(rows)

    print(tabulate.tabulate(rows, headers, tablefmt="pipe"))
    for warn in warnings:
        print(warn, file=sys.stderr)


def getFromFilename(filename):
    if "UltraCondensed-" in filename:
        usWidthClass = 1
    elif "ExtraCondensed-" in filename:
        usWidthClass = 2
    elif "SemiCondensed-" in filename:
        usWidthClass = 4
    elif "Condensed-" in filename:
        usWidthClass = 3
    elif "SemiExpanded-" in filename:
        usWidthClass = 6
    elif "ExtraExpanded-" in filename:
        usWidthClass = 8
    elif "UltraExpanded-" in filename:
        usWidthClass = 9
    elif "Expanded-" in filename:
        usWidthClass = 7
    else:
        usWidthClass = 5
    return usWidthClass


def fix(fonts, value=None):
    rows = []
    headers = ['filename', 'usWidthClass was', 'usWidthClass now']

    for font in fonts:
        row = [font]
        ttfont = ttLib.TTFont(font)
        if not value:
            usWidthClass = getFromFilename(font)
        else:
            usWidthClass = value
        row.append(ttfont['OS/2'].usWidthClass)
        ttfont['OS/2'].usWidthClass = usWidthClass
        row.append(ttfont['OS/2'].usWidthClass)
        ttfont.save(font + '.fix')
        rows.append(row)

    if rows:
        print(tabulate.tabulate(rows, headers, tablefmt="pipe"))


def main():
    args = parser.parse_args()
    if args.autofix:
        fix(args.font)
        sys.exit(0)
    if args.set:
        fix(args.font, value=int(args.set))
        sys.exit(0)
    print_info(args.font, print_csv=args.csv)

if __name__ == '__main__':
  main()