Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Very basic first pass at rendering text using freetype-py http://code…

….google.com/p/freetype-py/

svn path=/branches/ctypes_freetype/; revision=8944
  • Loading branch information...
commit 6448288571c8d7b73704683868ca8742e4749322 1 parent aabb275
@mdboom mdboom authored
View
4 examples/pylab_examples/simple_plot.py
@@ -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()
View
42 lib/matplotlib/backends/backend_agg.py
@@ -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 __init__(self, width, height, dpi):
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_markers(self, *kl, **kw):
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,17 +145,19 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
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):
"""
@@ -181,12 +184,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
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 @@ def _get_agg_font(self, prop):
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
View
7 lib/matplotlib/font_manager.py
@@ -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 @@ def createFontList(fontfiles, fontext='ttf'):
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 @@ def get_name(self):
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):
"""
View
49 lib/matplotlib/freetype_util.py
@@ -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
View
5 lib/matplotlib/text.py
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.