From: <md...@us...> - 2007-07-20 15:47:03
|
Revision: 3593 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3593&view=rev Author: mdboom Date: 2007-07-20 08:47:00 -0700 (Fri, 20 Jul 2007) Log Message: ----------- Fix bug where some characters would be missing from the font on subsequent Ps or Pdf plots. Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py trunk/matplotlib/lib/matplotlib/backends/backend_ps.py trunk/matplotlib/lib/matplotlib/mathtext.py Modified: trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py 2007-07-20 15:42:09 UTC (rev 3592) +++ trunk/matplotlib/lib/matplotlib/backends/backend_pdf.py 2007-07-20 15:47:00 UTC (rev 3593) @@ -17,7 +17,7 @@ from cStringIO import StringIO from datetime import datetime from math import ceil, cos, floor, pi, sin -import sets +from sets import Set from matplotlib import __version__, rcParams, agg, get_data_path from matplotlib._pylab_helpers import Gcf @@ -931,9 +931,15 @@ fname = font.fname realpath, stat_key = get_realpath_and_stat(fname) used_characters = self.used_characters.setdefault( - stat_key, (realpath, sets.Set())) + stat_key, (realpath, Set())) used_characters[1].update(s) + def merge_used_characters(self, other): + for stat_key, (realpath, set) in other.items(): + used_characters = self.used_characters.setdefault( + stat_key, (realpath, Set())) + used_characters[1].update(set) + def draw_arc(self, gcEdge, rgbFace, x, y, width, height, angle1, angle2, rotation): """ @@ -1087,8 +1093,10 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): # TODO: fix positioning and encoding fontsize = prop.get_size_in_points() - width, height, pswriter = math_parse_s_pdf(s, 72, fontsize, 0, self.track_characters) - + width, height, pswriter, used_characters = \ + math_parse_s_pdf(s, 72, fontsize, 0) + self.merge_used_characters(used_characters) + self.check_gc(gc, gc._rgb) self.file.output(Op.begin_text) prev_font = None, None @@ -1201,8 +1209,8 @@ if ismath: fontsize = prop.get_size_in_points() - w, h, pswriter = math_parse_s_pdf( - s, 72, fontsize, 0, self.track_characters) + w, h, pswriter, used_characters = math_parse_s_pdf( + s, 72, fontsize, 0) elif rcParams['pdf.use14corefonts']: font = self._get_font_afm(prop) Modified: trunk/matplotlib/lib/matplotlib/backends/backend_ps.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_ps.py 2007-07-20 15:42:09 UTC (rev 3592) +++ trunk/matplotlib/lib/matplotlib/backends/backend_ps.py 2007-07-20 15:47:00 UTC (rev 3593) @@ -29,7 +29,7 @@ import numpy as npy import binascii import re -import sets +from sets import Set if sys.platform.startswith('win'): cmd_split = '&' else: cmd_split = ';' @@ -150,9 +150,15 @@ each font.""" realpath, stat_key = get_realpath_and_stat(font.fname) used_characters = self.used_characters.setdefault( - stat_key, (realpath, sets.Set())) + stat_key, (realpath, Set())) used_characters[1].update(s) + def merge_used_characters(self, other): + for stat_key, (realpath, set) in other.items(): + used_characters = self.used_characters.setdefault( + stat_key, (realpath, Set())) + used_characters[1].update(set) + def set_color(self, r, g, b, store=1): if (r,g,b) != self.color: if r==g and r==b: @@ -271,8 +277,8 @@ return w, h if ismath: - width, height, pswriter = math_parse_s_ps( - s, 72, prop.get_size_in_points(), 0, self.track_characters) + width, height, pswriter, used_characters = math_parse_s_ps( + s, 72, prop.get_size_in_points(), 0) return width, height if rcParams['ps.useafm']: @@ -808,7 +814,10 @@ self._pswriter.write("% mathtext\n") fontsize = prop.get_size_in_points() - width, height, pswriter = math_parse_s_ps(s, 72, fontsize, angle, self.track_characters) + width, height, pswriter, used_characters = \ + math_parse_s_ps(s, 72, fontsize, angle) + self.merge_used_characters(used_characters) + self.set_color(*gc.get_rgb()) thetext = pswriter.getvalue() ps = """gsave Modified: trunk/matplotlib/lib/matplotlib/mathtext.py =================================================================== --- trunk/matplotlib/lib/matplotlib/mathtext.py 2007-07-20 15:42:09 UTC (rev 3592) +++ trunk/matplotlib/lib/matplotlib/mathtext.py 2007-07-20 15:47:00 UTC (rev 3593) @@ -131,6 +131,7 @@ from __future__ import division import os, sys from cStringIO import StringIO +from sets import Set from matplotlib import verbose from matplotlib.pyparsing import Literal, Word, OneOrMore, ZeroOrMore, \ @@ -138,7 +139,7 @@ StringStart, StringEnd, ParseException, FollowedBy, Regex from matplotlib.afm import AFM -from matplotlib.cbook import enumerate, iterable, Bunch +from matplotlib.cbook import enumerate, iterable, Bunch, get_realpath_and_stat from matplotlib.ft2font import FT2Font from matplotlib.font_manager import fontManager, FontProperties from matplotlib._mathtext_data import latex_to_bakoma, cmkern, \ @@ -704,7 +705,7 @@ None : 'cmmi10', } - def __init__(self, character_tracker=None): + def __init__(self): self.glyphd = {} self.fonts = dict( [ (name, FT2Font(os.path.join(self.basepath, name) + '.ttf')) @@ -717,8 +718,9 @@ for charcode, glyphind in charmap.items()]) for font in self.fonts.values(): font.clear() - self.character_tracker = character_tracker + self.used_characters = {} + def _get_info (self, font, sym, fontsize, dpi): 'load the cmfont, metrics and glyph with caching' key = font, sym, fontsize, dpi @@ -745,8 +747,10 @@ head = cmfont.get_sfnt_table('head') glyph = cmfont.load_char(num) - if self.character_tracker: - self.character_tracker(cmfont, unichr(num)) + realpath, stat_key = get_realpath_and_stat(cmfont.fname) + used_characters = self.used_characters.setdefault( + stat_key, (realpath, Set())) + used_characters[1].update(unichr(num)) xmin, ymin, xmax, ymax = [val/64.0 for val in glyph.bbox] if basename == 'cmex10': @@ -817,8 +821,6 @@ fontname, metrics, glyphname, offset = \ self._get_info(font, sym, fontsize, dpi) filename, num = self._get_filename_and_num(font, sym, fontsize, dpi) - if self.character_tracker: - self.character_tracker(filename, unichr(num)) if fontname.lower() == 'cmex10': oy += offset - 512/2048.*10. @@ -1545,12 +1547,11 @@ self.output = output self.cache = {} - def __call__(self, s, dpi, fontsize, angle=0, character_tracker=None): + def __call__(self, s, dpi, fontsize, angle=0): cacheKey = (s, dpi, fontsize, angle) s = s[1:-1] # strip the $ from front and back if self.cache.has_key(cacheKey): - w, h, fontlike = self.cache[cacheKey] - return w, h, fontlike + return self.cache[cacheKey] if self.output == 'SVG': self.font_object = BakomaTrueTypeFonts(useSVG=True) #self.font_object = MyUnicodeFonts(output='SVG') @@ -1564,11 +1565,11 @@ self.font_object = StandardPSFonts() Element.fonts = self.font_object else: - self.font_object = BakomaPSFonts(character_tracker) + self.font_object = BakomaPSFonts() #self.font_object = MyUnicodeFonts(output='PS') Element.fonts = self.font_object elif self.output == 'PDF': - self.font_object = BakomaPDFFonts(character_tracker) + self.font_object = BakomaPDFFonts() Element.fonts = self.font_object handler.clear() @@ -1608,13 +1609,11 @@ svg_elements = Bunch(svg_glyphs=self.font_object.svg_glyphs, svg_lines=[]) self.cache[cacheKey] = w, h, svg_elements - return w, h, svg_elements elif self.output == 'Agg': self.cache[cacheKey] = w, h, self.font_object.fonts.values() - return w, h, self.font_object.fonts.values() elif self.output in ('PS', 'PDF'): - self.cache[cacheKey] = w, h, pswriter - return w, h, pswriter + self.cache[cacheKey] = w, h, pswriter, self.font_object.used_characters + return self.cache[cacheKey] if rcParams["mathtext.mathtext2"]: from matplotlib.mathtext2 import math_parse_s_ft2font This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |