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