| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Copyright (C) 2009-2022 Jerry Casiano |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. |
| 18 |
# |
| 19 |
# If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 20 |
|
| 21 |
import sys |
| 22 |
|
| 23 |
from glob import glob |
| 24 |
from io import StringIO |
| 25 |
from os import path, remove |
| 26 |
from pprint import pprint |
| 27 |
|
| 28 |
NOTICE = """/* Do not edit directly. See build-aux directory */""" |
| 29 |
|
| 30 |
HEADER = """ |
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include <glib.h> |
| 34 |
|
| 35 |
#ifndef __GTK_DOC_IGNORE__ |
| 36 |
#define FONT_MANAGER_MAX_VENDOR_ID_LENGTH 4 |
| 37 |
#define FONT_MANAGER_MAX_VENDOR_LENGTH 100 |
| 38 |
#endif |
| 39 |
|
| 40 |
static const struct |
| 41 |
{ |
| 42 |
const gchar vendor[FONT_MANAGER_MAX_VENDOR_LENGTH]; |
| 43 |
const gchar vendor_id[FONT_MANAGER_MAX_VENDOR_LENGTH]; |
| 44 |
} |
| 45 |
/* Order is significant. */ |
| 46 |
FontManagerNoticeData [] = |
| 47 |
{ |
| 48 |
/* Notice data sourced from fcfreetype.c - http://www.freetype.org/ */ |
| 49 |
{"Adobe", "adobe"}, |
| 50 |
{"Adobe", "Adobe"}, |
| 51 |
{"Bigelow & Holmes", "b&h"}, |
| 52 |
{"Bigelow & Holmes", "Bigelow & Holmes"}, |
| 53 |
{"Bitstream", "Bitstream"}, |
| 54 |
{"Font21", "hwan"}, |
| 55 |
{"Font21", "Hwan"}, |
| 56 |
{"Gnat", "culmus"}, |
| 57 |
{"HanYang System", "hanyang"}, |
| 58 |
{"HanYang System", "HanYang Information & Communication"}, |
| 59 |
{"IBM", "IBM"}, |
| 60 |
{"ITC", "itc"}, |
| 61 |
{"ITC", "ITC"}, |
| 62 |
{"ITC", "International Typeface Corporation"}, |
| 63 |
{"Larabiefonts", "Larabie"}, |
| 64 |
{"Linotype", "linotype"}, |
| 65 |
{"Linotype", "Linotype GmbH"}, |
| 66 |
{"Linotype", "LINOTYPE-HELL"}, |
| 67 |
{"Microsoft", "microsoft"}, |
| 68 |
{"Microsoft", "Microsoft Corporation"}, |
| 69 |
{"Monotype", "Monotype Imaging"}, |
| 70 |
{"Monotype", "Monotype Corporation"}, |
| 71 |
{"Monotype", "Monotype Typography"}, |
| 72 |
{"Omega", "omega"}, |
| 73 |
{"Omega", "Omega"}, |
| 74 |
{"Tiro Typeworks", "Tiro Typeworks"}, |
| 75 |
{"URW", "URW"}, |
| 76 |
{"XFree86", "XFree86"}, |
| 77 |
{"Xorg", "xorg"} |
| 78 |
}; |
| 79 |
|
| 80 |
static const struct |
| 81 |
{ |
| 82 |
const gchar vendor_id[FONT_MANAGER_MAX_VENDOR_ID_LENGTH]; |
| 83 |
const gchar vendor[FONT_MANAGER_MAX_VENDOR_LENGTH]; |
| 84 |
} |
| 85 |
FontManagerVendorData[] = |
| 86 |
{ |
| 87 |
""" |
| 88 |
|
| 89 |
FOOTER = """}; |
| 90 |
|
| 91 |
#ifndef __GTK_DOC_IGNORE__ |
| 92 |
#define FONT_MANAGER_NOTICE_ENTRIES G_N_ELEMENTS(FontManagerNoticeData) |
| 93 |
#define FONT_MANAGER_VENDOR_ENTRIES G_N_ELEMENTS(FontManagerVendorData) |
| 94 |
#endif |
| 95 |
|
| 96 |
""" |
| 97 |
|
| 98 |
vendor_dir = path.dirname(path.realpath(__file__)) |
| 99 |
|
| 100 |
def get_vendor_entries () : |
| 101 |
sys.path.append(vendor_dir) |
| 102 |
module_names = [path.splitext(p)[0] for p in glob("*.py") if p != "genheader.py"] |
| 103 |
resources = map(__import__, module_names) |
| 104 |
tmp = StringIO() |
| 105 |
for module in resources: |
| 106 |
name = module.__name__ |
| 107 |
try: |
| 108 |
if module.CREDIT is not None: |
| 109 |
tmp.write("\n /* {} */\n".format(module.CREDIT)) |
| 110 |
else: |
| 111 |
tmp.write("\n") |
| 112 |
try: |
| 113 |
vendor_list = list(module.list_vendors()) |
| 114 |
except: |
| 115 |
vendor_list = [] |
| 116 |
if len(vendor_list) == 0: |
| 117 |
with open(path.join(vendor_dir, "{}.cache".format(name))) as cache: |
| 118 |
vendor_list = eval(cache.read()) |
| 119 |
print("Using cached vendor information for {}".format(name)) |
| 120 |
for vendor_id, vendor in iter(vendor_list): |
| 121 |
if len(vendor) > 50: |
| 122 |
vendor = "{}…".format(vendor[:47]) |
| 123 |
tmp.write(" {{\"{0}\", \"{1}\"}},\n".format(vendor_id.replace('"', "'"), vendor.replace('"', "'"))) |
| 124 |
tmp.write("\n") |
| 125 |
if name != "Example": |
| 126 |
try: |
| 127 |
with open(path.join(vendor_dir, "{}.cache".format(name)), "w") as cache: |
| 128 |
pprint(list(vendor_list), cache) |
| 129 |
except: |
| 130 |
pass |
| 131 |
except: |
| 132 |
print("Failed to load vendor resource : {0} : Skipping...".format(name)) |
| 133 |
contents = tmp.getvalue() |
| 134 |
tmp.close() |
| 135 |
return contents |
| 136 |
|
| 137 |
|
| 138 |
if __name__ == "__main__": |
| 139 |
with open(path.join(sys.argv[1], "font-manager-vendor.h"), "w") as header_file: |
| 140 |
header_file.write(NOTICE) |
| 141 |
header_file.write(HEADER) |
| 142 |
header_file.write(get_vendor_entries()) |
| 143 |
header_file.write(FOOTER) |
| 144 |
build_cache = path.join(vendor_dir, "__pycache__") |
| 145 |
if path.exists(build_cache): |
| 146 |
import shutil |
| 147 |
shutil.rmtree(build_cache) |
| 148 |
for f in glob("*.pyc"): |
| 149 |
remove(f) |