blob: c6e5510a662c91504445d61e593f5348e9f5ad93 [file] [log] [blame]
Jens Wiklanderf789aa02016-04-14 20:37:01 +02001#!/usr/bin/env python
2#
3# Copyright (c) 2016, Linaro Limited
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9# 1. Redistributions of source code must retain the above copyright notice,
10# this list of conditions and the following disclaimer.
11#
12# 2. Redistributions in binary form must reproduce the above copyright notice,
13# this list of conditions and the following disclaimer in the documentation
14# and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28
29from wand.image import Image
30from wand.drawing import Drawing
31from wand.color import Color
32import sys
33
34def get_args():
35 from argparse import ArgumentParser
36
37 parser = ArgumentParser()
38 parser.add_argument('--font_file', required=True, \
39 help='Name of font file')
40 parser.add_argument('--font_size', type=int, default=40, \
41 help='Size of font')
42 parser.add_argument('--font_name', required=True, \
43 help='Name of font in program')
44 parser.add_argument('--out_dir', required=True, \
45 help='Out directory')
Jerome Forissierd9b94642016-06-07 09:41:57 +020046 parser.add_argument('--verbose', default=False, \
47 help='Print informational messages')
Jens Wiklanderf789aa02016-04-14 20:37:01 +020048 return parser.parse_args()
49
50def c_hex_print(f, str):
51 n = 0
Joakim Bechba4f1fc2016-07-29 16:47:59 +020052 s = ""
Jens Wiklanderf789aa02016-04-14 20:37:01 +020053 for x in str:
54 if n % 8 == 0:
Joakim Bechba4f1fc2016-07-29 16:47:59 +020055 s += "\t"
Jens Wiklanderf789aa02016-04-14 20:37:01 +020056 else:
Joakim Bechba4f1fc2016-07-29 16:47:59 +020057 s += " "
58 # Hack to satisfy both Python 2.x and 3.x. In 2.x the variable x
59 # is string, while it in Python 3.x it's considered as a class
60 # int.
61 if sys.version_info > (3, 0):
62 s += "0x%02x," % x
63 else:
64 s += "0x%02x," % ord(x)
Jens Wiklanderf789aa02016-04-14 20:37:01 +020065 n = n + 1
66 if n % 8 == 0:
Joakim Bechba4f1fc2016-07-29 16:47:59 +020067 f.write(s + "\n")
68 s = ""
Jens Wiklanderf789aa02016-04-14 20:37:01 +020069 if n % 8 != 0:
Joakim Bechba4f1fc2016-07-29 16:47:59 +020070 f.write(s + "\n")
71 s = ""
Jens Wiklanderf789aa02016-04-14 20:37:01 +020072
73def write_comment(f):
74 f.write("/*\n * This file is auto generated with\n")
75 f.write(" *")
76 for x in sys.argv:
77 f.write(" " + x);
78 f.write("\n * do not edit.\n */\n")
79
80
81def main():
82 args = get_args()
83
84 draw = Drawing()
85 draw.font = args.font_file
86 draw.font_size = args.font_size
87
88 font_name = args.font_name
89 out_dir = args.out_dir
90
91 img_ref = Image(width=1000, height=1000)
92
Jerome Forissierd9b94642016-06-07 09:41:57 +020093 if args.verbose:
Joakim Bechba4f1fc2016-07-29 16:47:59 +020094 print("Writing " + out_dir + "/" + font_name + ".c")
95 f = open(out_dir + "/" + font_name + ".c", 'w+')
Jens Wiklanderf789aa02016-04-14 20:37:01 +020096 write_comment(f)
97 f.write("#include \"font.h\"\n\n")
98
99 font_height = 0
100 range_first = 0x20
101 range_last = 0x7d
102 font_width = []
103 max_width = 0
104 for x in range(range_first, range_last + 1):
105 letter = chr(x)
106 metrics = draw.get_font_metrics(img_ref, letter)
107 text_height = int(round(metrics.text_height + 2))
108 if font_height == 0:
109 font_height = text_height
110 assert (font_height == text_height), "font height changed!"
111 if max_width == 0:
112 max_width = metrics.maximum_horizontal_advance + 2
113 assert (max_width == metrics.maximum_horizontal_advance + 2), \
114 "font advance width changed!"
115 text_width = int(round(metrics.text_width + 2))
116 font_width.append(text_width)
117 img = Image(width=text_width, height=text_height)
118 d = draw.clone()
119 d.text(0, int(metrics.ascender), letter)
120 d(img)
121
122 img.depth = 1;
123
124 f.write("static const unsigned char ")
125 f.write("letter_" + str(hex(x)[2:]) + "[] = {\n")
126 c_hex_print(f, img.make_blob(format='A'))
127 f.write("};\n\n")
128 img.close()
129
130 f.write("static const struct font_letter letters[] = {\n")
131 for x in range(range_first, range_last + 1):
132 letter_var_name = "letter_" + str(hex(x)[2:])
133 f.write("\t{ " + letter_var_name + ", ")
134 f.write("sizeof(" + letter_var_name + "), ")
135 f.write(str(font_width[x - range_first]) + "},\n")
136 f.write("};\n\n")
137
138 f.write("const struct font font_" + font_name + " = {\n")
139 f.write("\t.first = " + str(hex(range_first)) + ",\n")
140 f.write("\t.last = " + str(hex(range_last)) + ",\n")
141 f.write("\t.letters = letters,\n")
142 f.write("\t.height = " + str(font_height) + ",\n")
143 f.write("\t.max_width = " + str(max_width) + ",\n")
144 f.write("};\n")
145 f.close()
146
Jerome Forissierd9b94642016-06-07 09:41:57 +0200147 if args.verbose:
Joakim Bechba4f1fc2016-07-29 16:47:59 +0200148 print("Writing " + out_dir + "/" + font_name + ".h")
149 f = open(out_dir + "/" + font_name + ".h", 'w+')
Jens Wiklanderf789aa02016-04-14 20:37:01 +0200150 write_comment(f)
151 f.write("#ifndef __" + font_name.upper() + "_H\n");
152 f.write("#define __" + font_name.upper() + "_H\n");
153 f.write("#include \"font.h\"\n")
154 f.write("extern const struct font font_" + font_name + ";\n")
155 f.write("#endif /*__" + font_name.upper() + "_H*/\n");
156 f.close()
157
158if __name__ == "__main__":
159 main()