From: <md...@us...> - 2011-02-01 17:57:29
|
Revision: 8944 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8944&view=rev Author: mdboom Date: 2011-02-01 17:57:23 +0000 (Tue, 01 Feb 2011) Log Message: ----------- Very basic first pass at rendering text using freetype-py http://code.google.com/p/freetype-py/ Modified Paths: -------------- branches/ctypes_freetype/examples/pylab_examples/simple_plot.py branches/ctypes_freetype/lib/matplotlib/backends/backend_agg.py branches/ctypes_freetype/lib/matplotlib/font_manager.py branches/ctypes_freetype/lib/matplotlib/text.py Added Paths: ----------- branches/ctypes_freetype/lib/matplotlib/freetype_util.py Modified: branches/ctypes_freetype/examples/pylab_examples/simple_plot.py =================================================================== --- branches/ctypes_freetype/examples/pylab_examples/simple_plot.py 2011-02-01 17:49:30 UTC (rev 8943) +++ branches/ctypes_freetype/examples/pylab_examples/simple_plot.py 2011-02-01 17:57:23 UTC (rev 8944) @@ -8,4 +8,8 @@ ylabel('voltage (mV)') title('About as simple as it gets, folks') grid(True) + +import sys +print 'ft2font' in str(sys.modules) + show() Modified: branches/ctypes_freetype/lib/matplotlib/backends/backend_agg.py =================================================================== --- branches/ctypes_freetype/lib/matplotlib/backends/backend_agg.py 2011-02-01 17:49:30 UTC (rev 8943) +++ branches/ctypes_freetype/lib/matplotlib/backends/backend_agg.py 2011-02-01 17:57:23 UTC (rev 8944) @@ -30,13 +30,14 @@ from matplotlib.cbook import is_string_like, maxdict from matplotlib.figure import Figure from matplotlib.font_manager import findfont -from matplotlib.ft2font import FT2Font, LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING from matplotlib.mathtext import MathTextParser from matplotlib.path import Path from matplotlib.transforms import Bbox, BboxBase from _backend_agg import RendererAgg as _RendererAgg from matplotlib import _png +from matplotlib import freetype_util +from freetype import ft_enums backend_version = 'v2.2' @@ -71,9 +72,9 @@ def _get_hinting_flag(self): if rcParams['text.hinting']: - return LOAD_FORCE_AUTOHINT + return ft_enums.FT_LOAD_FORCE_AUTOHINT else: - return LOAD_NO_HINTING + return ft_enums.FT_LOAD_NO_HINTING # for filtering to work with rasterization, methods needs to be wrapped. # maybe there is better way to do it. @@ -82,7 +83,7 @@ def draw_path_collection(self, *kl, **kw): return self._renderer.draw_path_collection(*kl, **kw) - + def _update_methods(self): #self.draw_path = self._renderer.draw_path # see below #self.draw_markers = self._renderer.draw_markers @@ -144,18 +145,20 @@ flags = self._get_hinting_flag() font = self._get_agg_font(prop) if font is None: return None - if len(s) == 1 and ord(s) > 127: - font.load_char(ord(s), flags=flags) - else: - # We pass '0' for angle here, since it will be rotated (in raster - # space) in the following call to draw_text_image). - font.set_text(s, 0, flags=flags) - font.draw_glyphs_to_bitmap() + # if len(s) == 1 and ord(s) > 127: + # font.load_char(ord(s), flags=flags) + # else: + # # We pass '0' for angle here, since it will be rotated (in raster + # # space) in the following call to draw_text_image). + # font.set_text(s, 0, flags=flags) + # font.draw_glyphs_to_bitmap() #print x, y, int(x), int(y), s - self._renderer.draw_text_image(font.get_image(), int(x), int(y) + 1, angle, gc) + Z = freetype_util.render_string(font, s) + self._renderer.draw_text_image(Z, int(x), int(y) + 1, angle, gc) + def get_text_width_height_descent(self, s, prop, ismath): """ get the width and height in display coords of the string s @@ -181,12 +184,7 @@ flags = self._get_hinting_flag() font = self._get_agg_font(prop) - font.set_text(s, 0.0, flags=flags) # the width and height of unrotated string - w, h = font.get_width_height() - d = font.get_descent() - w /= 64.0 # convert from subpixels - h /= 64.0 - d /= 64.0 + w, h, d = freetype_util.get_width_height_descent(font, s) return w, h, d @@ -221,13 +219,15 @@ fname = findfont(prop) font = self._fontd.get(fname) if font is None: - font = FT2Font(str(fname)) + font = freetype_util.Face(str(fname)) self._fontd[fname] = font self._fontd[key] = font - font.clear() + # font.clear() size = prop.get_size_in_points() - font.set_size(size, self.dpi) + font.set_char_size(height = int(size * 64), + hres = int(self.dpi), + vres = int(self.dpi)) return font Modified: branches/ctypes_freetype/lib/matplotlib/font_manager.py =================================================================== --- branches/ctypes_freetype/lib/matplotlib/font_manager.py 2011-02-01 17:49:30 UTC (rev 8943) +++ branches/ctypes_freetype/lib/matplotlib/font_manager.py 2011-02-01 17:57:23 UTC (rev 8944) @@ -49,7 +49,6 @@ from sets import Set as set import matplotlib from matplotlib import afm -from matplotlib import ft2font from matplotlib import rcParams, get_configdir from matplotlib.cbook import is_string_like from matplotlib.fontconfig_pattern import \ @@ -60,6 +59,8 @@ except ImportError: import pickle +import freetype + USE_FONTCONFIG = False verbose = matplotlib.verbose @@ -597,7 +598,7 @@ prop = afmFontProperty(fpath, font) else: try: - font = ft2font.FT2Font(str(fpath)) + font = freetype.Face(str(fpath)) except RuntimeError: verbose.report("Could not open font file %s"%fpath) continue @@ -739,7 +740,7 @@ Return the name of the font that best matches the font properties. """ - return ft2font.FT2Font(str(findfont(self))).family_name + return freetype.Face(str(findfont(self))).family_name def get_style(self): """ Added: branches/ctypes_freetype/lib/matplotlib/freetype_util.py =================================================================== --- branches/ctypes_freetype/lib/matplotlib/freetype_util.py (rev 0) +++ branches/ctypes_freetype/lib/matplotlib/freetype_util.py 2011-02-01 17:57:23 UTC (rev 8944) @@ -0,0 +1,49 @@ +""" +Higher-level functionality built on top of the Freetype wrapper. +""" + +import numpy as np +from freetype import Face + +def get_width_height_descent(face, text): + slot = face.glyph + + 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 + + return width, height, baseline + +def render_string(face, text): + slot = face.glyph + + # First pass to compute bbox + width, height, baseline = get_width_height_descent(face, text) + + Z = np.zeros((height,width), dtype=np.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] |= np.array(bitmap.buffer).reshape(h,w) + x += (slot.advance.x >> 6) + previous = c + + return Z Modified: branches/ctypes_freetype/lib/matplotlib/text.py =================================================================== --- branches/ctypes_freetype/lib/matplotlib/text.py 2011-02-01 17:49:30 UTC (rev 8943) +++ branches/ctypes_freetype/lib/matplotlib/text.py 2011-02-01 17:57:23 UTC (rev 8944) @@ -25,8 +25,9 @@ import matplotlib.nxutils as nxutils from matplotlib.path import Path -import matplotlib.font_manager as font_manager -from matplotlib.ft2font import FT2Font +# import matplotlib.font_manager as font_manager +# from matplotlib.ft2font import FT2Font +# from matplotlib import freetype from matplotlib.backend_bases import RendererBase This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |