10000 Fix handling of font file matching so that if two different paths point · matplotlib/matplotlib@c8ae4e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit c8ae4e2

Browse files
committed
Fix handling of font file matching so that if two different paths point
to the same file, they are treated as the same file. svn path=/trunk/matplotlib/; revision=3499
1 parent d871588 commit c8ae4e2

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from matplotlib._pylab_helpers import Gcf
2222
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
2323
FigureManagerBase, FigureCanvasBase
24-
from matplotlib.cbook import Bunch, enumerate, is_string_like, reverse_dict
24+
from matplotlib.cbook import Bunch, enumerate, is_string_like, reverse_dict, get_realpath_and_stat
2525
from matplotlib.figure import Figure
2626
from matplotlib.font_manager import fontManager
2727
from matplotlib.afm import AFM
@@ -455,8 +455,9 @@ def writeFonts(self):
455455
if filename.endswith('.afm'):
456456
fontdictObject = self._write_afm_font(filename)
457457
else:
458+
realpath, stat_key = get_realpath_and_stat(filename)
458459
fontdictObject = self.embedTTF(
459-
filename, self.used_characters[filename])
460+
*self.used_characters[stat_key])
460461
fonts[Fx] = fontdictObject
461462
#print >>sys.stderr, filename
462463
self.writeObject(self.fontObject, fonts)
@@ -927,8 +928,10 @@ def track_characters(self, font, s):
927928
fname = font
928929
else:
929930
fname = font.fname
930-
used_characters = self.used_characters.setdefault(fname, sets.Set())
931-
used_characters.update(s)
931+
realpath, stat_key = get_realpath_and_stat(fname)
932+
used_characters = self.used_characters.setdefault(
933+
stat_key, (realpath, sets.Set()))
934+
used_characters[1].update(s)
932935

933936
def draw_arc(self, gcEdge, rgbFace, x, y, width, height,
934937
angle1, angle2, rotation):

lib/matplotlib/backends/backend_ps.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
1515
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
1616
FigureManagerBase, FigureCanvasBase
1717

18-
from matplotlib.cbook import is_string_like, izip
18+
from matplotlib.cbook import is_string_like, izip, get_realpath_and_stat
1919
from matplotlib.figure import Figure
2020

2121
from matplotlib.font_manager import fontManager
@@ -149,9 +149,10 @@ def __init__(self, width, height, pswriter, dpi=72):
149149
def track_characters(self, font, s):
150150
"""Keeps track of which characters are required from
151151
each font."""
152-
fname = font.fname
153-
used_characters = self.used_characters.setdefault(fname, sets.Set())
154-
used_characters.update(s)
152+
realpath, stat_key = get_realpath_and_stat(font.fname)
153+
used_characters = self.used_characters.setdefault(
154+
stat_key, (realpath, sets.Set()))
155+
used_characters[1].update(s)
155156

156157
def set_color(self, r, g, b, store=1):
157158
if (r,g,b) != self.color:
@@ -1028,7 +1029,7 @@ def print_figure(self, outfile, dpi=72, facecolor='w', edgecolor='w',
10281029
for l in d.split('\n'):
10291030
print >>fh, l.strip()
10301031
if not rcParams['ps.useafm']:
1031-
for font_filename, chars in renderer.used_characters.items():
1032+
for font_filename, chars in renderer.used_characters.values():
10321033
font = FT2Font(font_filename)
10331034
cmap = font.get_charmap()
10341035
glyph_ids = []

lib/matplotlib/cbook.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,21 @@ def mkdirs(newdir, mode=0777):
405405
raise
406406

407407

408+
class GetRealpathAndStat:
409+
def __init__(self):
410+
self._cache = {}
411+
412+
def __call__(self, path):
413+
result = self._cache.get(path)
414+
if result is None:
415+
realpath = os.path.realpath(path)
416+
stat = os.stat(realpath)
417+
stat_key = (stat.st_ino, stat.st_dev)
418+
result = realpath, stat_key
419+
self._cache[path] = result
420+
return result
421+
get_realpath_and_stat = GetRealpathAndStat()
422+
408423
def dict_delall(d, keys):
409424
'delete all of the keys from the dict d'
410425
for key in keys:

0 commit comments

Comments
 (0)
0