*** texmanager.py.ORIG	2007-05-17 18:17:28.000000000 +0200
--- texmanager.py	2007-05-18 17:53:07.000000000 +0200
***************
*** 33,38 ****
  
  """
! 
! import glob, md5, os, shutil, sys, warnings
  from tempfile import gettempdir
  from matplotlib import get_configdir, get_home, get_data_path, \
--- 33,37 ----
  
  """
! import glob, md5, os, shutil, sys, warnings, copy
  from tempfile import gettempdir
  from matplotlib import get_configdir, get_home, get_data_path, \
***************
*** 47,51 ****
  else: cmd_split = ';'
  
- 
  def get_dvipng_version():
      stdin, stdout = os.popen4('dvipng -version')
--- 46,49 ----
***************
*** 92,96 ****
      cursive = ('pzc', r'\usepackage{chancery}')
      font_family = 'serif'
!     
      font_info = {'new century schoolbook': ('pnc', r'\renewcommand{\rmdefault}{pnc}'),
                  'bookman': ('pbk', r'\renewcommand{\rmdefault}{pbk}'),
--- 90,94 ----
      cursive = ('pzc', r'\usepackage{chancery}')
      font_family = 'serif'
!     font_families  = ('serif', 'sans-serif', 'cursive', 'monospace')    
      font_info = {'new century schoolbook': ('pnc', r'\renewcommand{\rmdefault}{pnc}'),
                  'bookman': ('pbk', r'\renewcommand{\rmdefault}{pbk}'),
***************
*** 108,155 ****
                  'computer modern sans serif': ('cmss', ''),
                  'computer modern typewriter': ('cmtt', '')}
! 
      def __init__(self):
!         
          if not os.path.isdir(self.texcache):
              os.mkdir(self.texcache)
!         if rcParams['font.family'].lower() in ('serif', 'sans-serif', 'cursive', 'monospace'):
!             self.font_family = rcParams['font.family'].lower()
          else:
              warnings.warn('The %s font family is not compatible with LaTeX. serif will be used by default.' % ff)
              self.font_family = 'serif'
!         self._fontconfig = self.font_family
!         for font in rcParams['font.serif']:
!             try:
!                 self.serif = self.font_info[font.lower()]
!             except KeyError:
!                 continue
!             else:
!                 break
!         self._fontconfig += self.serif[0]
!         for font in rcParams['font.sans-serif']:
!             try:
!                 self.sans_serif = self.font_info[font.lower()]
!             except KeyError:
!                 continue
              else:
!                 break
!         self._fontconfig += self.sans_serif[0]
!         for font in rcParams['font.monospace']:
!             try:
!                 self.monospace = self.font_info[font.lower()]
!             except KeyError:
!                 continue
!             else:
!                 break                
!         self._fontconfig += self.monospace[0]
!         for font in rcParams['font.cursive']:
!             try:
!                 self.cursive = self.font_info[font.lower()]
!             except KeyError:
!                 continue
!             else:
!                 break                
!         self._fontconfig += self.cursive[0]
!         
          # The following packages and commands need to be included in the latex 
          # file's preamble:
--- 106,135 ----
                  'computer modern sans serif': ('cmss', ''),
                  'computer modern typewriter': ('cmtt', '')}
!     _rc_cache = None # XXX checking text.latex.preamble might be overkill OTOH it's typically cheap
!     _rc_cache_keys = ('text.latex.preamble',)+tuple('font.' + n for n in ('family',) + font_families)
      def __init__(self):
!         global CUR; CUR = self #DEBUG
          if not os.path.isdir(self.texcache):
              os.mkdir(self.texcache)
!         ff = rcParams['font.family'].lower()
!         if ff in self.font_families:
!             self.font_family = ff
          else:
              warnings.warn('The %s font family is not compatible with LaTeX. serif will be used by default.' % ff)
              self.font_family = 'serif'
!         fontconfig = [self.font_family]
!         for font_family, font_family_attr in ((ff, ff.replace('-','_')) for ff in self.font_families):
!             assert not isinstance(rcParams['font.' + font_family], basestring), '\
! BUG, rcParams[%s] treated as list but is comma-sep string: %s' % (
! 'font.' + font_family, rcParams['font.' + font_family])
!             for font in rcParams['font.' + font_family]:
!                 print '####DEBUG family: %s font: %s info: %s' % (font_family, font, self.font_info.get(font.lower()))
!                 if font.lower() in self.font_info:
!                     setattr(self, font_family_attr, self.font_info[font.lower()])
!                     break
              else:
!                 print >> sys.stderr, "Don't know how to use font %s in LaTeX" % font
!             fontconfig.append(getattr(self, font_family_attr)[0])
!         self._fontconfig = "".join(fontconfig)
          # The following packages and commands need to be included in the latex 
          # file's preamble:
***************
*** 160,172 ****
          cmd = '\n'.join(cmd)
          self._font_preamble = '\n'.join([r'\usepackage{type1cm}',
!                              cmd,
!                              r'\usepackage{textcomp}'])
!         
      def get_basefile(self, tex, fontsize, dpi=None):
!         s = tex + self._fontconfig + ('%f'%fontsize) + self.get_custom_preamble()
!         if dpi: s += ('%s'%dpi)
          return os.path.join(self.texcache, md5.md5(s).hexdigest())
          
      def get_font_config(self):
          return self._fontconfig
          
--- 140,163 ----
          cmd = '\n'.join(cmd)
          self._font_preamble = '\n'.join([r'\usepackage{type1cm}',
!                                          cmd,
!                                          r'\usepackage{textcomp}'])
      def get_basefile(self, tex, fontsize, dpi=None):
!         s = "".join([tex, self.get_font_config(), ('%f'%fontsize), self.get_custom_preamble(), str(dpi or '')])
          return os.path.join(self.texcache, md5.md5(s).hexdigest())
          
      def get_font_config(self):
+         "Reinitializes self if rcParams self depends on have changed."
+         if self._rc_cache is None:
+             self._rc_cache = dict((k,None) for k in self._rc_cache_keys)
+         changed = [par for par in self._rc_cache_keys if rcParams[par] != self._rc_cache[par]]
+         if changed:
+             print '###DEBUG following keys changed:', changed
+             for k in changed:
+                 print '###DEBUG %-20s: %-10s -> %-10s' % (k, self._rc_cache[k], rcParams[k])
+                 # XXX deepcopy is likely not necessary, but feels more future-proof
+                 self._rc_cache[k] = copy.deepcopy(rcParams[k])
+             print '###DEBUG ABOUT TO RE-INIT\n     old fontconfig:', self._fontconfig
+             self.__init__()
+         print '###DEBUG fontconfig:', self._fontconfig
          return self._fontconfig
          
***************
*** 175,179 ****
      
      def get_custom_preamble(self):
!         return '\n'.join(rcParams['text.latex.preamble'])
          
      def get_shell_cmd(self, *args):
--- 166,173 ----
      
      def get_custom_preamble(self):
!         p=rcParams['text.latex.preamble']
!         assert not isinstance(p, basestring), \
!                "latex.preamble must be a list of strings, not a string!:%r" % p
!         return '\n'.join(p)
          
      def get_shell_cmd(self, *args):
***************
*** 208,211 ****
--- 202,206 ----
  \end{document}
  """ % (self._font_preamble, custom_preamble, fontsize, fontsize*1.25, tex)
+         print "###DEBUG {%s}", s
          fh.write(s)
          fh.close()
***************
*** 310,314 ****
          # When you set dvipng -bg Transparent, it actually makes the
          # alpha channel 1 and does the background compositing and
!         # antialiasing itself and puts the blended data in the rgb
          # channels.  So what we do is extract the alpha information
          # from the red channel, which is a blend of the default dvipng
--- 305,309 ----
          # When you set dvipng -bg Transparent, it actually makes the
          # alpha channel 1 and does the background compositing and
!         # antialiasing itself and puts the bolended data in the rgb
          # channels.  So what we do is extract the alpha information
          # from the red channel, which is a blend of the default dvipng
***************
*** 328,335 ****
          if not dpi: dpi = rcParams['savefig.dpi']
          r,g,b = rgb
-         key = tex, fontsize, dpi, tuple(rgb)
-         Z = self.arrayd.get(key)
  
          if Z is None:
              # force=True to skip cacheing while debugging
              pngfile = self.make_png(tex, fontsize, dpi, force=False)
--- 323,332 ----
          if not dpi: dpi = rcParams['savefig.dpi']
          r,g,b = rgb
  
+         key = tex, self.get_font_config(), fontsize, dpi, tuple(rgb)
+         Z = self.arrayd.get(key)
+         print '''###DEBUG looking for Z...''', key
          if Z is None:
+             print '''###DEBUG  ... no right Z, computing'''
              # force=True to skip cacheing while debugging
              pngfile = self.make_png(tex, fontsize, dpi, force=False)
