File: update_icon_font.py

package info (click to toggle)
copyq 13.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,964 kB
  • sloc: cpp: 63,306; sh: 992; xml: 452; python: 293; ruby: 152; makefile: 27; javascript: 25
file content (165 lines) | stat: -rwxr-xr-x 4,762 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
'''
Updates icon font and header files for CopyQ repository.

First argument is path to unpacked Font Awesome archive (https://fontawesome.com/).
'''
import json
import os
import sys

from textwrap import dedent

from fontTools.merge import Merger
from fontTools.ttLib import TTFont

FONTS = [
    'fa-solid-900.ttf',
    'fa-brands-400.ttf',
]
FONT_FILENAME = 'fontawesome.ttf'

SOLID_STYLE = 'solid'
BRANDS_STYLE = 'brands'


def read_icons(icons_json):
    with open(icons_json, 'r') as icons_file:
        icons_content = icons_file.read()
        return json.loads(icons_content)


def write_header_file_preamble(header_file):
    script = os.path.realpath(__file__)
    script_name = os.path.basename(script)
    comment = (
            f'// This file is generated with "{script_name}"'
            + ' from FontAwesome\'s metadata.\n#pragma once\n\n')
    header_file.write(comment)


def write_icon_list_header_file(header_icon_list, icons):
    with open(header_icon_list, 'w') as header_file:
        items = []
        for style in [SOLID_STYLE, BRANDS_STYLE]:
            is_brand = 'true' if style == BRANDS_STYLE else 'false'
            for name, icon in icons.items():
                if style in icon['styles']:
                    code = icon['unicode']
                    search_terms = [icon['label'].lower()] + (icon['search']['terms'] or [])
                    search_terms_list = '|'.join(search_terms)
                    items.append('{0x%s, %s, "%s"}' % (code, is_brand, search_terms_list))

        item_list_content = ',\n'.join(items)
        content = dedent('''\
            struct Icon {
                unsigned short unicode;
                bool isBrand;
                const char *searchTerms;
            };

            constexpr Icon iconList[] = {
            %s
            };
        ''') % item_list_content

        write_header_file_preamble(header_file)
        header_file.write(content)


def write_icons_header_file(header_icons, icons):
    with open(header_icons, 'w') as header_file:
        write_header_file_preamble(header_file)
        header_file.write('#ifndef ICONS_H\n')
        header_file.write('#define ICONS_H\n')
        header_file.write('\n')
        header_file.write('enum IconId {\n')

        for name, icon in icons.items():
            label = name.title().replace('-', '')
            code = icon['unicode']
            header_file.write(f'    Icon{label} = 0x{code},' + '\n')

        header_file.write('};\n')
        header_file.write('\n')
        header_file.write('#endif // ICONS_H\n')


def rename_font_family(path):
    """
    Adds suffix to font family it doesn't conflict with font installed on
    system, which could be in incorrect version.

    See: https://github.com/fonttools/fonttools/blob/master/Snippets/rename-fonts.py
    """

    font = TTFont(path)
    name_table = font['name']

    FAMILY_RELATED_IDS = dict(
        LEGACY_FAMILY=1,
        TRUETYPE_UNIQUE_ID=3,
        FULL_NAME=4,
        POSTSCRIPT_NAME=6,
        PREFERRED_FAMILY=16,
        WWS_FAMILY=21,
    )

    for rec in name_table.names:
        if rec.nameID not in FAMILY_RELATED_IDS.values():
            continue

        name = rec.toUnicode()
        if name.startswith('Font Awesome'):
            rec.string = name + ' (CopyQ)'
        elif name.startswith('FontAwesome'):
            rec.string = name + '(CopyQ)'

        assert rec.toUnicode().endswith('(CopyQ)')

    font.save(path)


def copy_fonts(font_awesome_src, target_font_dir):
    font_dir = os.path.join(font_awesome_src, 'webfonts')
    fonts = [
        os.path.join(font_dir, src_name)
        for src_name in FONTS
    ]
    dest_path = os.path.join(target_font_dir, FONT_FILENAME)

    print(f'Merging fonts: {fonts} -> {dest_path}')
    merger = Merger()
    with merger.merge(fonts) as font:
        font.save(dest_path)

    rename_font_family(dest_path)


def main():
    font_awesome_src = sys.argv[1]

    script = os.path.realpath(__file__)
    utils_dir = os.path.dirname(script)
    project_dir = os.path.dirname(utils_dir)
    src_dir = os.path.join(project_dir, 'src')

    header_icon_list = os.path.join(src_dir, 'gui', 'icon_list.h')
    header_icons = os.path.join(src_dir, 'gui', 'icons.h')

    target_font_dir = os.path.join(src_dir, 'images')
    copy_fonts(font_awesome_src, target_font_dir)

    icons_json = os.path.join(
            font_awesome_src, 'metadata', 'icons.json')
    icons = read_icons(icons_json)

    write_icon_list_header_file(header_icon_list, icons)
    print(f'Header file "{header_icon_list}" updated.')

    write_icons_header_file(header_icons, icons)
    print(f'Header file "{header_icons}" updated.')


if __name__ == '__main__':
    main()