Revision: 7635
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7635&view=rev
Author: leejjoon
Date: 2009-09-04 19:11:00 +0000 (Fri, 04 Sep 2009)
Log Message:
-----------
textpath support mathtext and tex
Modified Paths:
--------------
trunk/matplotlib/CHANGELOG
trunk/matplotlib/examples/pylab_examples/demo_text_path.py
trunk/matplotlib/lib/matplotlib/mathtext.py
trunk/matplotlib/lib/matplotlib/text.py
trunk/matplotlib/src/ft2font.cpp
trunk/matplotlib/src/ft2font.h
Added Paths:
-----------
trunk/matplotlib/lib/matplotlib/textpath.py
Modified: trunk/matplotlib/CHANGELOG
===================================================================
--- trunk/matplotlib/CHANGELOG 2009-09-04 04:12:16 UTC (rev 7634)
+++ trunk/matplotlib/CHANGELOG 2009-09-04 19:11:00 UTC (rev 7635)
@@ -1,3 +1,6 @@
+2009-09-04 Make the textpath class as a separate moduel
+ (textpath.py). Add support for mathtext and tex.- JJL
+
2009-09-01 Added support for Gouraud interpolated triangles.
pcolormesh now accepts shading='gouraud' as an option. - MGD
Modified: trunk/matplotlib/examples/pylab_examples/demo_text_path.py
===================================================================
--- trunk/matplotlib/examples/pylab_examples/demo_text_path.py 2009-09-04
04:12:16 UTC (rev 7634)
+++ trunk/matplotlib/examples/pylab_examples/demo_text_path.py 2009-09-04
19:11:00 UTC (rev 7635)
@@ -76,23 +76,52 @@
# make anchored offset box
ao = AnchoredOffsetbox(loc=2, child=offsetbox, frameon=True, borderpad=0.2)
-
ax.add_artist(ao)
+ # another text
+ from matplotlib.patches import PathPatch
+ text_path = TextPath((0, 0), r"\mbox{textpath supports mathtext \& \TeX}",
+ size=20, usetex=True)
+ p1 = PathPatch(text_path, ec="w", lw=3, fc="w", alpha=0.9,
+ transform=IdentityTransform())
+ p2 = PathPatch(text_path, ec="none", fc="k",
+ transform=IdentityTransform())
+ offsetbox2 = AuxTransformBox(IdentityTransform())
+ offsetbox2.add_artist(p1)
+ offsetbox2.add_artist(p2)
+ ab = AnnotationBbox(offsetbox2, (0.95, 0.05),
+ xycoords='axes fraction',
+ boxcoords="offset points",
+ box_alignment=(1.,0.),
+ frameon=False
+ )
+ ax.add_artist(ab)
+
+ ax.imshow([[0,1,2],[1,2,3]], cmap=plt.cm.gist_gray_r,
+ interpolation="bilinear",
+ aspect="auto")
+
+
+
# EXAMPLE 2
ax = plt.subplot(212)
arr = np.arange(256).reshape(1,256)/256.
- text_path = TextPath((0, 0), "TextPath", size=70)
+ usetex = plt.rcParams["text.usetex"]
+ if usetex:
+ s =
r"$\displaystyle\left[\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}\right]$!"
+ else:
+ s = r"$\left[\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}\right]$!"
+ text_path = TextPath((0, 0), s, size=40, usetex=usetex)
text_patch = PathClippedImagePatch(text_path, arr, ec="none",
transform=IdentityTransform())
- shadow1 = mpatches.Shadow(text_patch, 3, -2, props=dict(fc="none",
ec="0.6", lw=3))
- shadow2 = mpatches.Shadow(text_patch, 3, -2, props=dict(fc="0.3",
ec="none"))
+ shadow1 = mpatches.Shadow(text_patch, 1, -1, props=dict(fc="none",
ec="0.6", lw=3))
+ shadow2 = mpatches.Shadow(text_patch, 1, -1, props=dict(fc="0.3",
ec="none"))
# make offset box
Modified: trunk/matplotlib/lib/matplotlib/mathtext.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/mathtext.py 2009-09-04 04:12:16 UTC (rev
7634)
+++ trunk/matplotlib/lib/matplotlib/mathtext.py 2009-09-04 19:11:00 UTC (rev
7635)
@@ -336,6 +336,34 @@
svg_elements,
self.fonts_object.get_used_characters())
+class MathtextBackendPath(MathtextBackend):
+ """
+ Store information to write a mathtext rendering to the Cairo
+ backend.
+ """
+
+ def __init__(self):
+ self.glyphs = []
+ self.rects = []
+
+ def render_glyph(self, ox, oy, info):
+ oy = self.height - oy + info.offset
+ thetext = unichr(info.num)
+ self.glyphs.append(
+ (info.font, info.fontsize, thetext, ox, oy))
+
+ def render_rect_filled(self, x1, y1, x2, y2):
+ self.rects.append(
+ (x1, self.height-y2 , x2 - x1, y2 - y1))
+
+ def get_results(self, box):
+ ship(0, -self.depth, box)
+ return (self.width,
+ self.height + self.depth,
+ self.depth,
+ self.glyphs,
+ self.rects)
+
class MathtextBackendCairo(MathtextBackend):
"""
Store information to write a mathtext rendering to the Cairo
@@ -2751,6 +2779,7 @@
'ps' : MathtextBackendPs,
'pdf' : MathtextBackendPdf,
'svg' : MathtextBackendSvg,
+ 'path' : MathtextBackendPath,
'cairo' : MathtextBackendCairo,
'macosx': MathtextBackendAgg,
}
Modified: trunk/matplotlib/lib/matplotlib/text.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/text.py 2009-09-04 04:12:16 UTC (rev
7634)
+++ trunk/matplotlib/lib/matplotlib/text.py 2009-09-04 19:11:00 UTC (rev
7635)
@@ -1770,173 +1770,4 @@
docstring.interpd.update(Annotation=Annotation.__init__.__doc__)
-class TextPath(Path):
- """
- Create a path from the text.
- """
-
- # TODO : math text is currently not supported, but it would not be easy.
-
- FONT_SCALE = 100.
-
- def __init__(self, xy, s, size=None, prop=None,
- _interpolation_steps=1,
- *kl, **kwargs):
- """
- Create a path from the text. No support for TeX yet. Note that
- it simply is a path, not an artist. You need to use the
- PathPatch (or other artists) to draw this path onto the
- canvas.
-
- xy : position of the text.
- s : text
- size : font size
- prop : font property
- """
-
-
- if prop is None:
- prop = FontProperties()
-
- if size is None:
- size = prop.get_size_in_points()
-
-
- self._xy = xy
- self.set_size(size)
-
- self._cached_vertices = None
-
- self._vertices, self._codes = self.text_get_vertices_codes(prop, s)
-
- self.should_simplify = False
- self.simplify_threshold = rcParams['path.simplify_threshold']
- self.has_nonfinite = False
- self._interpolation_steps = _interpolation_steps
-
-
- def set_size(self, size):
- """
- set the size of the text
- """
- self._size = size
- self._invalid = True
-
- def get_size(self):
- """
- get the size of the text
- """
- return self._size
-
- def _get_vertices(self):
- """
- Return the cached path after updating it if necessary.
- """
- self._revalidate_path()
- return self._cached_vertices
-
- def _get_codes(self):
- """
- Return the codes
- """
- return self._codes
-
- vertices = property(_get_vertices)
- codes = property(_get_codes)
-
- def _revalidate_path(self):
- """
- update the path if necessary.
-
- The path for the text is initially create with the font size
- of FONT_SCALE, and this path is rescaled to other size when
- necessary.
-
- """
- if self._invalid or \
- (self._cached_vertices is None):
- tr = Affine2D().scale(self._size/self.FONT_SCALE,
-
self._size/self.FONT_SCALE).translate(*self._xy)
- self._cached_vertices = tr.transform(self._vertices)
- self._invalid = False
-
-
- def glyph_char_path(self, glyph, currx=0.):
- """
- convert the glyph to vertices and codes. Mostly copied from
- backend_svg.py.
- """
- verts, codes = [], []
- for step in glyph.path:
- if step[0] == 0: # MOVE_TO
- verts.append((step[1], step[2]))
- codes.append(Path.MOVETO)
- elif step[0] == 1: # LINE_TO
- verts.append((step[1], step[2]))
- codes.append(Path.LINETO)
- elif step[0] == 2: # CURVE3
- verts.extend([(step[1], step[2]),
- (step[3], step[4])])
- codes.extend([Path.CURVE3, Path.CURVE3])
- elif step[0] == 3: # CURVE4
- verts.extend([(step[1], step[2]),
- (step[3], step[4]),
- (step[5], step[6])])
- codes.extend([Path.CURVE4, Path.CURVE4, Path.CURVE4])
- elif step[0] == 4: # ENDPOLY
- verts.append((0, 0,))
- codes.append(Path.CLOSEPOLY)
-
- verts = [(x+currx, y) for (x,y) in verts]
-
- return verts, codes
-
-
- def text_get_vertices_codes(self, prop, s):
- """
- convert the string *s* to vertices and codes using the
- provided font property *prop*. Mostly copied from
- backend_svg.py.
- """
-
- fname = font_manager.findfont(prop)
- font = FT2Font(str(fname))
-
- font.set_size(self.FONT_SCALE, 72)
-
- cmap = font.get_charmap()
- lastgind = None
-
- currx = 0
-
- verts, codes = [], []
-
-
- # I'm not sure if I get kernings right. Needs to be verified. -JJL
-
- for c in s:
-
- ccode = ord(c)
- gind = cmap.get(ccode)
- if gind is None:
- ccode = ord('?')
- gind = 0
- glyph = font.load_char(ccode, flags=LOAD_NO_HINTING)
-
-
- if lastgind is not None:
- kern = font.get_kerning(lastgind, gind, KERNING_DEFAULT)
- else:
- kern = 0
- currx += (kern / 64.0) #/ (self.FONT_SCALE)
-
- verts1, codes1 = self.glyph_char_path(glyph, currx)
- verts.extend(verts1)
- codes.extend(codes1)
-
-
- currx += (glyph.linearHoriAdvance / 65536.0) #/ (self.FONT_SCALE)
- lastgind = gind
-
- return verts, codes
-
+from matplotlib.textpath import TextPath
Added: trunk/matplotlib/lib/matplotlib/textpath.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/textpath.py (rev 0)
+++ trunk/matplotlib/lib/matplotlib/textpath.py 2009-09-04 19:11:00 UTC (rev
7635)
@@ -0,0 +1,463 @@
+# -*- coding: utf-8 -*-
+
+import urllib
+from matplotlib.path import Path
+import matplotlib.font_manager as font_manager
+
+from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING,
LOAD_TARGET_LIGHT
+
+from matplotlib.mathtext import MathTextParser
+
+import matplotlib.dviread as dviread
+from matplotlib.texmanager import TexManager
+
+import numpy as np
+
+class TextToPath(object):
+ """
+ A class that convert a given text to a path using ttf fonts.
+ """
+
+ FONT_SCALE = 50.
+ DPI = 72
+
+ def __init__(self):
+ """
+ Initialization
+ """
+ self.mathtext_parser = MathTextParser('path')
+ self.tex_font_map = None
+
+ from matplotlib.cbook import maxdict
+ self._ps_fontd = maxdict(50)
+
+
+ def _get_font(self, prop):
+ """
+ find a ttf font.
+ """
+ fname = font_manager.findfont(prop)
+ font = FT2Font(str(fname))
+ font.set_size(self.FONT_SCALE, self.DPI)
+
+ return font
+
+
+ def _get_char_id(self, font, ccode):
+ """
+ Return a unique id for the given font and character-code set.
+ """
+ ps_name = font.get_sfnt()[(1,0,0,6)]
+ char_id = urllib.quote('%s-%d' % (ps_name, ccode))
+ return char_id
+
+ def _get_char_id_ps(self, font, ccode):
+ """
+ Return a unique id for the given font and character-code set (for tex).
+ """
+ ps_name = font.get_ps_font_info()[2]
+ char_id = urllib.quote('%s-%d' % (ps_name, ccode))
+ return char_id
+
+
+ def glyph_to_path(self, glyph, currx=0.):
+ """
+ convert the ft2font glyph to vertices and codes.
+ """
+ #Mostly copied from backend_svg.py.
+
+ verts, codes = [], []
+ for step in glyph.path:
+ if step[0] == 0: # MOVE_TO
+ verts.append((step[1], step[2]))
+ codes.append(Path.MOVETO)
+ elif step[0] == 1: # LINE_TO
+ verts.append((step[1], step[2]))
+ codes.append(Path.LINETO)
+ elif step[0] == 2: # CURVE3
+ verts.extend([(step[1], step[2]),
+ (step[3], step[4])])
+ codes.extend([Path.CURVE3, Path.CURVE3])
+ elif step[0] == 3: # CURVE4
+ verts.extend([(step[1], step[2]),
+ (step[3], step[4]),
+ (step[5], step[6])])
+ codes.extend([Path.CURVE4, Path.CURVE4, Path.CURVE4])
+ elif step[0] == 4: # ENDPOLY
+ verts.append((0, 0,))
+ codes.append(Path.CLOSEPOLY)
+
+ verts = [(x+currx, y) for (x,y) in verts]
+ return verts, codes
+
+
+ def get_text_path(self, prop, s, ismath=False, usetex=False):
+ """
+ convert text *s* to path (a tuple of vertices and codes for
matplotlib.math.Path).
+
+ *prop*
+ font property
+
+ *s*
+ text to be converted
+
+ *usetex*
+ If True, use matplotlib usetex mode.
+
+ *ismath*
+ If True, use mathtext parser. Effective only if usetex == False.
+
+
+ """
+ if usetex==False:
+ if ismath == False:
+ font = self._get_font(prop)
+ glyph_info, glyph_map, rects = self.get_glyphs_with_font(font,
s)
+ else:
+ glyph_info, glyph_map, rects = self.get_glyphs_mathtext(prop,
s)
+ else:
+ glyph_info, glyph_map, rects = self.get_glyphs_tex(prop, s)
+
+ verts, codes = [], []
+
+ for glyph_id, xposition, yposition, scale in glyph_info:
+ verts1, codes1 = glyph_map[glyph_id]
+ if verts1:
+ verts1 = np.array(verts1)*scale + [xposition, yposition]
+ verts.extend(verts1)
+ codes.extend(codes1)
+
+ for verts1, codes1 in rects:
+ verts.extend(verts1)
+ codes.extend(codes1)
+
+ return verts, codes
+
+
+ def get_glyphs_with_font(self, font, s, glyph_map=None):
+ """
+ convert the string *s* to vertices and codes using the
+ provided ttf font.
+ """
+
+ # Mostly copied from backend_svg.py.
+
+ cmap = font.get_charmap()
+ lastgind = None
+
+ currx = 0
+ xpositions = []
+ glyph_ids = []
+
+ if glyph_map is None:
+ glyph_map = dict()
+
+ # I'm not sure if I get kernings right. Needs to be verified. -JJL
+
+ for c in s:
+
+
+ ccode = ord(c)
+ gind = cmap.get(ccode)
+ if gind is None:
+ ccode = ord('?')
+ gind = 0
+
+ if lastgind is not None:
+ kern = font.get_kerning(lastgind, gind, KERNING_DEFAULT)
+ else:
+ kern = 0
+
+
+ glyph = font.load_char(ccode, flags=LOAD_NO_HINTING)
+ horiz_advance = (glyph.linearHoriAdvance / 65536.0)
+
+ char_id = self._get_char_id(font, ccode)
+ if not char_id in glyph_map:
+ glyph_map[char_id] = self.glyph_to_path(glyph)
+
+ currx += (kern / 64.0)
+
+ xpositions.append(currx)
+ glyph_ids.append(char_id)
+
+ currx += horiz_advance
+
+ lastgind = gind
+
+ ypositions = [0] * len(xpositions)
+ sizes = [1.] * len(xpositions)
+
+ rects = []
+
+ return zip(glyph_ids, xpositions, ypositions, sizes), glyph_map, rects
+
+
+
+
+ def get_glyphs_mathtext(self, prop, s):
+ """
+ convert the string *s* to vertices and codes by parsing it with
mathtext.
+ """
+
+ prop = prop.copy()
+ prop.set_size(self.FONT_SCALE)
+
+ width, height, descent, glyphs, rects = self.mathtext_parser.parse(
+ s, self.DPI, prop)
+
+
+ glyph_map = dict()
+
+ xpositions = []
+ ypositions = []
+ glyph_ids = []
+ sizes = []
+
+ currx, curry = 0, 0
+ for font, fontsize, s, ox, oy in glyphs:
+
+ ccode = ord(s)
+ char_id = self._get_char_id(font, ccode)
+ if not char_id in glyph_map:
+ font.clear()
+ font.set_size(self.FONT_SCALE, self.DPI)
+ glyph = font.load_char(ccode, flags=LOAD_NO_HINTING)
+ glyph_map[char_id] = self.glyph_to_path(glyph)
+
+ xpositions.append(ox)
+ ypositions.append(oy)
+ glyph_ids.append(char_id)
+ size = fontsize / self.FONT_SCALE
+ sizes.append(size)
+
+ myrects = []
+ for ox, oy, w, h in rects:
+ vert1=[(ox, oy), (ox, oy+h), (ox+w, oy+h), (ox+w, oy), (ox, oy),
(0,0)]
+ code1 = [Path.MOVETO,
+ Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO,
+ Path.CLOSEPOLY]
+ myrects.append((vert1, code1))
+
+
+ return zip(glyph_ids, xpositions, ypositions, sizes), glyph_map,
myrects
+
+
+
+ def get_glyphs_tex(self, prop, s):
+ """
+ convert the string *s* to vertices and codes using matplotlib's usetex
mode.
+ """
+
+ # codes are modstly borrowed from pdf backend.
+
+ texmanager = TexManager()
+
+ if self.tex_font_map is None:
+ self.tex_font_map =
dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))
+
+ fontsize = prop.get_size_in_points()
+ if hasattr(texmanager, "get_dvi"): #
+ dvifilelike = texmanager.get_dvi(s, self.FONT_SCALE)
+ dvi = dviread.DviFromFileLike(dvifilelike, self.DPI)
+ else:
+ dvifile = texmanager.make_dvi(s, self.FONT_SCALE)
+ dvi = dviread.Dvi(dvifile, self.DPI)
+ page = iter(dvi).next()
+ dvi.close()
+
+ glyph_ids, xpositions, ypositions, sizes = [], [], [], []
+ glyph_map = dict()
+
+ # Gather font information and do some setup for combining
+ # characters into strings.
+ #oldfont, seq = None, []
+ for x1, y1, dvifont, glyph, width in page.text:
+ font_and_encoding = self._ps_fontd.get(dvifont.texname)
+
+ if font_and_encoding is None:
+ font_bunch = self.tex_font_map[dvifont.texname]
+ font = FT2Font(font_bunch.filename)
+ if font_bunch.encoding:
+ enc = dviread.Encoding(font_bunch.encoding)
+ else:
+ enc = None
+ self._ps_fontd[dvifont.texname] = font, enc
+
+ else:
+ font, enc = font_and_encoding
+
+ ft2font_flag = LOAD_TARGET_LIGHT
+ if enc:
+ ng = font.get_name_index(enc.encoding[glyph])
+ else:
+ ng = glyph
+
+ char_id = self._get_char_id_ps(font, ng)
+
+ if not char_id in glyph_map:
+ font.clear()
+ font.set_size(self.FONT_SCALE, self.DPI)
+
+ if ng == 0:
+ # While 0 is a valid index (e.g., "-", "\Gamma"),
+ # font.load_glyph(0) does not seem to work. This
+ # may not be a general solution.
+ glyph0 = font.load_glyph(128, flags=ft2font_flag)
+ else:
+ glyph0 = font.load_glyph(ng, flags=ft2font_flag)
+
+ glyph_map[char_id] = self.glyph_to_path(glyph0)
+
+ glyph_ids.append(char_id)
+ xpositions.append(x1)
+ ypositions.append(y1)
+ sizes.append(dvifont.size/self.FONT_SCALE)
+
+ myrects = []
+
+ for ox, oy, h, w in page.boxes:
+ vert1=[(ox, oy), (ox+w, oy), (ox+w, oy+h), (ox, oy+h), (ox, oy),
(0,0)]
+ code1 = [Path.MOVETO,
+ Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO,
+ Path.CLOSEPOLY]
+ myrects.append((vert1, code1))
+
+
+ return zip(glyph_ids, xpositions, ypositions, sizes), glyph_map,
myrects
+
+
+
+
+
+
+from matplotlib.font_manager import FontProperties
+from matplotlib import rcParams
+from matplotlib.transforms import Affine2D
+
+text_to_path = TextToPath()
+
+class TextPath(Path):
+ """
+ Create a path from the text.
+ """
+
+ def __init__(self, xy, s, size=None, prop=None,
+ _interpolation_steps=1, usetex=False,
+ *kl, **kwargs):
+ """
+ Create a path from the text. No support for TeX yet. Note that
+ it simply is a path, not an artist. You need to use the
+ PathPatch (or other artists) to draw this path onto the
+ canvas.
+
+ xy : position of the text.
+ s : text
+ size : font size
+ prop : font property
+ """
+
+
+ if prop is None:
+ prop = FontProperties()
+
+ if size is None:
+ size = prop.get_size_in_points()
+
+
+ self._xy = xy
+ self.set_size(size)
+
+ self._cached_vertices = None
+
+ self._vertices, self._codes = self.text_get_vertices_codes(prop, s,
usetex=usetex)
+
+ self.should_simplify = False
+ self.simplify_threshold = rcParams['path.simplify_threshold']
+ self.has_nonfinite = False
+ self._interpolation_steps = _interpolation_steps
+
+
+ def set_size(self, size):
+ """
+ set the size of the text
+ """
+ self._size = size
+ self._invalid = True
+
+ def get_size(self):
+ """
+ get the size of the text
+ """
+ return self._size
+
+ def _get_vertices(self):
+ """
+ Return the cached path after updating it if necessary.
+ """
+ self._revalidate_path()
+ return self._cached_vertices
+
+ def _get_codes(self):
+ """
+ Return the codes
+ """
+ return self._codes
+
+ vertices = property(_get_vertices)
+ codes = property(_get_codes)
+
+ def _revalidate_path(self):
+ """
+ update the path if necessary.
+
+ The path for the text is initially create with the font size
+ of FONT_SCALE, and this path is rescaled to other size when
+ necessary.
+
+ """
+ if self._invalid or \
+ (self._cached_vertices is None):
+ tr = Affine2D().scale(self._size/text_to_path.FONT_SCALE,
+
self._size/text_to_path.FONT_SCALE).translate(*self._xy)
+ self._cached_vertices = tr.transform(self._vertices)
+ self._invalid = False
+
+
+ def is_math_text(self, s):
+ """
+ Returns True if the given string *s* contains any mathtext.
+ """
+ # copied from Text.is_math_text -JJL
+
+ # Did we find an even number of non-escaped dollar signs?
+ # If so, treat is as math text.
+ dollar_count = s.count(r'$') - s.count(r'\$')
+ even_dollars = (dollar_count > 0 and dollar_count % 2 == 0)
+
+ if rcParams['text.usetex']:
+ return s, 'TeX'
+
+ if even_dollars:
+ return s, True
+ else:
+ return s.replace(r'\$', '$'), False
+
+ def text_get_vertices_codes(self, prop, s, usetex):
+ """
+ convert the string *s* to vertices and codes using the
+ provided font property *prop*. Mostly copied from
+ backend_svg.py.
+ """
+
+ if usetex:
+ verts, codes = text_to_path.get_text_path(prop, s, usetex=True)
+ else:
+ clean_line, ismath = self.is_math_text(s)
+ verts, codes = text_to_path.get_text_path(prop, clean_line,
ismath=ismath)
+
+ return verts, codes
+
+
+
+
Modified: trunk/matplotlib/src/ft2font.cpp
===================================================================
--- trunk/matplotlib/src/ft2font.cpp 2009-09-04 04:12:16 UTC (rev 7634)
+++ trunk/matplotlib/src/ft2font.cpp 2009-09-04 19:11:00 UTC (rev 7635)
@@ -1105,6 +1105,51 @@
return Py::asObject(gm);
}
+
+char FT2Font::load_glyph__doc__[] =
+"load_glyph(glyphindex, flags=LOAD_FORCE_AUTOHINT)\n"
+"\n"
+"Load character with glyphindex in current fontfile and set glyph.\n"
+"The flags argument can be a bitwise-or of the LOAD_XXX constants.\n"
+"Return value is a Glyph object, with attributes\n"
+" width # glyph width\n"
+" height # glyph height\n"
+" bbox # the glyph bbox (xmin, ymin, xmax, ymax)\n"
+" horiBearingX # left side bearing in horizontal layouts\n"
+" horiBearingY # top side bearing in horizontal layouts\n"
+" horiAdvance # advance width for horizontal layout\n"
+" vertBearingX # left side bearing in vertical layouts\n"
+" vertBearingY # top side bearing in vertical layouts\n"
+" vertAdvance # advance height for vertical layout\n"
+;
+Py::Object
+FT2Font::load_glyph(const Py::Tuple & args, const Py::Dict & kwargs) {
+ _VERBOSE("FT2Font::load_glyph");
+ //load a char using the unsigned long charcode
+
+ args.verify_length(1);
+ long glyph_index = Py::Long(args[0]), flags =
Py::Long(FT_LOAD_FORCE_AUTOHINT);
+ if (kwargs.hasKey("flags"))
+ flags = Py::Long(kwargs["flags"]);
+
+ int error = FT_Load_Glyph( face, glyph_index, flags );
+
+ if (error)
+ throw Py::RuntimeError(Printf("Could not load glyph index %d",
glyph_index).str());
+
+ FT_Glyph thisGlyph;
+ error = FT_Get_Glyph( face->glyph, &thisGlyph );
+
+ if (error)
+ throw Py::RuntimeError(Printf("Could not get glyph for glyph index %d",
glyph_index).str());
+
+ size_t num = glyphs.size(); //the index into the glyphs list
+ glyphs.push_back(thisGlyph);
+ Glyph* gm = new Glyph(face, thisGlyph, num);
+ return Py::asObject(gm);
+}
+
+
char FT2Font::get_width_height__doc__[] =
"w, h = get_width_height()\n"
"\n"
@@ -1782,6 +1827,8 @@
FT2Font::get_num_glyphs__doc__);
add_keyword_method("load_char", &FT2Font::load_char,
FT2Font::load_char__doc__);
+ add_keyword_method("load_glyph", &FT2Font::load_glyph,
+ FT2Font::load_glyph__doc__);
add_keyword_method("set_text", &FT2Font::set_text,
FT2Font::set_text__doc__);
add_varargs_method("set_size", &FT2Font::set_size,
Modified: trunk/matplotlib/src/ft2font.h
===================================================================
--- trunk/matplotlib/src/ft2font.h 2009-09-04 04:12:16 UTC (rev 7634)
+++ trunk/matplotlib/src/ft2font.h 2009-09-04 19:11:00 UTC (rev 7635)
@@ -99,6 +99,7 @@
Py::Object get_kerning(const Py::Tuple & args);
Py::Object get_num_glyphs(const Py::Tuple & args);
Py::Object load_char(const Py::Tuple & args, const Py::Dict & kws);
+ Py::Object load_glyph(const Py::Tuple & args, const Py::Dict & kws);
Py::Object get_width_height(const Py::Tuple & args);
Py::Object get_descent(const Py::Tuple & args);
Py::Object draw_rect_filled(const Py::Tuple & args);
@@ -140,6 +141,7 @@
static char get_glyph__doc__ [];
static char get_num_glyphs__doc__ [];
static char load_char__doc__ [];
+ static char load_glyph__doc__ [];
static char get_width_height__doc__ [];
static char get_descent__doc__ [];
static char get_kerning__doc__ [];
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins