diff options
author | Roozbeh Pournader <roozbeh@google.com> | 2015-01-13 08:50:27 -0800 |
---|---|---|
committer | James Godfrey-Kittle <jamesgk@google.com> | 2015-04-16 12:16:25 -0700 |
commit | 36623f13d922461c897fe0ae1ff550a073385329 (patch) | |
tree | 9339ab434222da3393491673ea12090de381495b /third_party/freetype-py/examples/hello-world.py | |
parent | 9fc4898c2338e4b9bb028171f5f8492f01879fbe (diff) |
Add FreeType python bindings to third_party.
Diffstat (limited to 'third_party/freetype-py/examples/hello-world.py')
-rw-r--r-- | third_party/freetype-py/examples/hello-world.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/third_party/freetype-py/examples/hello-world.py b/third_party/freetype-py/examples/hello-world.py new file mode 100644 index 0000000..72126ae --- /dev/null +++ b/third_party/freetype-py/examples/hello-world.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# ----------------------------------------------------------------------------- +# +# FreeType high-level python API - Copyright 2011 Nicolas P. Rougier +# Distributed under the terms of the new BSD license. +# +# ----------------------------------------------------------------------------- +from freetype import * + +if __name__ == '__main__': + import numpy + import matplotlib.pyplot as plt + + face = Face('./Vera.ttf') + text = 'Hello World !' + face.set_char_size( 48*64 ) + slot = face.glyph + + # First pass to compute bbox + width, height, baseline = 0, 0, 0 + previous = 0 + for i,c in enumerate(text): + face.load_char(c) + bitmap = slot.bitmap + height = max(height, + bitmap.rows + max(0,-(slot.bitmap_top-bitmap.rows))) + baseline = max(baseline, max(0,-(slot.bitmap_top-bitmap.rows))) + kerning = face.get_kerning(previous, c) + width += (slot.advance.x >> 6) + (kerning.x >> 6) + previous = c + + Z = numpy.zeros((height,width), dtype=numpy.ubyte) + + # Second pass for actual rendering + x, y = 0, 0 + previous = 0 + for c in text: + face.load_char(c) + bitmap = slot.bitmap + top = slot.bitmap_top + left = slot.bitmap_left + w,h = bitmap.width, bitmap.rows + y = height-baseline-top + kerning = face.get_kerning(previous, c) + x += (kerning.x >> 6) + Z[y:y+h,x:x+w] += numpy.array(bitmap.buffer).reshape(h,w) + x += (slot.advance.x >> 6) + previous = c + + plt.figure(figsize=(10, 10*Z.shape[0]/float(Z.shape[1]))) + plt.imshow(Z, interpolation='nearest', origin='upper', cmap=plt.cm.gray) + plt.xticks([]), plt.yticks([]) + plt.show() |