libutee: add text rendering routines
* Adds routines to render text from raw bitmap fonts
* Adds script to render raw bitmap fonts from True Type Fonts
* Adds rendered raw bitmaps of the Amble TTF
Needed by Trusted UI.
Reviewed-by: Joakim Bech <joakim.bech@linaro.org>
Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/scripts/render_font.py b/scripts/render_font.py
new file mode 100755
index 0000000..f49edf1
--- /dev/null
+++ b/scripts/render_font.py
@@ -0,0 +1,146 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2016, Linaro Limited
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+from wand.image import Image
+from wand.drawing import Drawing
+from wand.color import Color
+import sys
+
+def get_args():
+ from argparse import ArgumentParser
+
+ parser = ArgumentParser()
+ parser.add_argument('--font_file', required=True, \
+ help='Name of font file')
+ parser.add_argument('--font_size', type=int, default=40, \
+ help='Size of font')
+ parser.add_argument('--font_name', required=True, \
+ help='Name of font in program')
+ parser.add_argument('--out_dir', required=True, \
+ help='Out directory')
+ return parser.parse_args()
+
+def c_hex_print(f, str):
+ n = 0
+ for x in str:
+ if n % 8 == 0:
+ f.write("\t")
+ else:
+ f.write(" ")
+ f.write("0x" + ("0" + (hex(ord(x)))[2:])[-2:] + ",")
+ n = n + 1
+ if n % 8 == 0:
+ f.write("\n")
+ if n % 8 != 0:
+ f.write("\n")
+
+def write_comment(f):
+ f.write("/*\n * This file is auto generated with\n")
+ f.write(" *")
+ for x in sys.argv:
+ f.write(" " + x);
+ f.write("\n * do not edit.\n */\n")
+
+
+def main():
+ args = get_args()
+
+ draw = Drawing()
+ draw.font = args.font_file
+ draw.font_size = args.font_size
+
+ font_name = args.font_name
+ out_dir = args.out_dir
+
+ img_ref = Image(width=1000, height=1000)
+
+ print "Writing " + out_dir + "/" + font_name + ".c"
+ f = open(out_dir + "/" + font_name + ".c", 'wb+')
+ write_comment(f)
+ f.write("#include \"font.h\"\n\n")
+
+ font_height = 0
+ range_first = 0x20
+ range_last = 0x7d
+ font_width = []
+ max_width = 0
+ for x in range(range_first, range_last + 1):
+ letter = chr(x)
+ metrics = draw.get_font_metrics(img_ref, letter)
+ text_height = int(round(metrics.text_height + 2))
+ if font_height == 0:
+ font_height = text_height
+ assert (font_height == text_height), "font height changed!"
+ if max_width == 0:
+ max_width = metrics.maximum_horizontal_advance + 2
+ assert (max_width == metrics.maximum_horizontal_advance + 2), \
+ "font advance width changed!"
+ text_width = int(round(metrics.text_width + 2))
+ font_width.append(text_width)
+ img = Image(width=text_width, height=text_height)
+ d = draw.clone()
+ d.text(0, int(metrics.ascender), letter)
+ d(img)
+
+ img.depth = 1;
+
+ f.write("static const unsigned char ")
+ f.write("letter_" + str(hex(x)[2:]) + "[] = {\n")
+ c_hex_print(f, img.make_blob(format='A'))
+ f.write("};\n\n")
+ img.close()
+
+ f.write("static const struct font_letter letters[] = {\n")
+ for x in range(range_first, range_last + 1):
+ letter_var_name = "letter_" + str(hex(x)[2:])
+ f.write("\t{ " + letter_var_name + ", ")
+ f.write("sizeof(" + letter_var_name + "), ")
+ f.write(str(font_width[x - range_first]) + "},\n")
+ f.write("};\n\n")
+
+ f.write("const struct font font_" + font_name + " = {\n")
+ f.write("\t.first = " + str(hex(range_first)) + ",\n")
+ f.write("\t.last = " + str(hex(range_last)) + ",\n")
+ f.write("\t.letters = letters,\n")
+ f.write("\t.height = " + str(font_height) + ",\n")
+ f.write("\t.max_width = " + str(max_width) + ",\n")
+ f.write("};\n")
+ f.close()
+
+ print "Writing " + out_dir + "/" + font_name + ".h"
+ f = open(out_dir + "/" + font_name + ".h", 'wb+')
+ write_comment(f)
+ f.write("#ifndef __" + font_name.upper() + "_H\n");
+ f.write("#define __" + font_name.upper() + "_H\n");
+ f.write("#include \"font.h\"\n")
+ f.write("extern const struct font font_" + font_name + ";\n")
+ f.write("#endif /*__" + font_name.upper() + "_H*/\n");
+ f.close()
+
+if __name__ == "__main__":
+ main()