blob: 12c13e86a8a1f961e92b617114a50584ed59d771 [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
52 for x in str:
53 if n % 8 == 0:
54 f.write("\t")
55 else:
56 f.write(" ")
57 f.write("0x" + ("0" + (hex(ord(x)))[2:])[-2:] + ",")
58 n = n + 1
59 if n % 8 == 0:
60 f.write("\n")
61 if n % 8 != 0:
62 f.write("\n")
63
64def write_comment(f):
65 f.write("/*\n * This file is auto generated with\n")
66 f.write(" *")
67 for x in sys.argv:
68 f.write(" " + x);
69 f.write("\n * do not edit.\n */\n")
70
71
72def main():
73 args = get_args()
74
75 draw = Drawing()
76 draw.font = args.font_file
77 draw.font_size = args.font_size
78
79 font_name = args.font_name
80 out_dir = args.out_dir
81
82 img_ref = Image(width=1000, height=1000)
83
Jerome Forissierd9b94642016-06-07 09:41:57 +020084 if args.verbose:
85 print "Writing " + out_dir + "/" + font_name + ".c"
Jens Wiklanderf789aa02016-04-14 20:37:01 +020086 f = open(out_dir + "/" + font_name + ".c", 'wb+')
87 write_comment(f)
88 f.write("#include \"font.h\"\n\n")
89
90 font_height = 0
91 range_first = 0x20
92 range_last = 0x7d
93 font_width = []
94 max_width = 0
95 for x in range(range_first, range_last + 1):
96 letter = chr(x)
97 metrics = draw.get_font_metrics(img_ref, letter)
98 text_height = int(round(metrics.text_height + 2))
99 if font_height == 0:
100 font_height = text_height
101 assert (font_height == text_height), "font height changed!"
102 if max_width == 0:
103 max_width = metrics.maximum_horizontal_advance + 2
104 assert (max_width == metrics.maximum_horizontal_advance + 2), \
105 "font advance width changed!"
106 text_width = int(round(metrics.text_width + 2))
107 font_width.append(text_width)
108 img = Image(width=text_width, height=text_height)
109 d = draw.clone()
110 d.text(0, int(metrics.ascender), letter)
111 d(img)
112
113 img.depth = 1;
114
115 f.write("static const unsigned char ")
116 f.write("letter_" + str(hex(x)[2:]) + "[] = {\n")
117 c_hex_print(f, img.make_blob(format='A'))
118 f.write("};\n\n")
119 img.close()
120
121 f.write("static const struct font_letter letters[] = {\n")
122 for x in range(range_first, range_last + 1):
123 letter_var_name = "letter_" + str(hex(x)[2:])
124 f.write("\t{ " + letter_var_name + ", ")
125 f.write("sizeof(" + letter_var_name + "), ")
126 f.write(str(font_width[x - range_first]) + "},\n")
127 f.write("};\n\n")
128
129 f.write("const struct font font_" + font_name + " = {\n")
130 f.write("\t.first = " + str(hex(range_first)) + ",\n")
131 f.write("\t.last = " + str(hex(range_last)) + ",\n")
132 f.write("\t.letters = letters,\n")
133 f.write("\t.height = " + str(font_height) + ",\n")
134 f.write("\t.max_width = " + str(max_width) + ",\n")
135 f.write("};\n")
136 f.close()
137
Jerome Forissierd9b94642016-06-07 09:41:57 +0200138 if args.verbose:
139 print "Writing " + out_dir + "/" + font_name + ".h"
Jens Wiklanderf789aa02016-04-14 20:37:01 +0200140 f = open(out_dir + "/" + font_name + ".h", 'wb+')
141 write_comment(f)
142 f.write("#ifndef __" + font_name.upper() + "_H\n");
143 f.write("#define __" + font_name.upper() + "_H\n");
144 f.write("#include \"font.h\"\n")
145 f.write("extern const struct font font_" + font_name + ";\n")
146 f.write("#endif /*__" + font_name.upper() + "_H*/\n");
147 f.close()
148
149if __name__ == "__main__":
150 main()