Font reregistration cleanup
authorandy_robinson
Tue, 09 Mar 2004 22:22:26 +0000
changeset 2216 aadcd9dc9480
parent 2215 e86aad9766c7
child 2217 18ff4aed511d
Font reregistration cleanup
reportlab/__init__.py
reportlab/lib/fonts.py
reportlab/pdfbase/_fontdata.py
reportlab/pdfbase/pdfmetrics.py
reportlab/pdfbase/ttfonts.py
reportlab/platypus/doctemplate.py
--- a/reportlab/__init__.py	Mon Mar 08 18:36:05 2004 +0000
+++ b/reportlab/__init__.py	Tue Mar 09 22:22:26 2004 +0000
@@ -1,8 +1,8 @@
 #copyright ReportLab Inc. 2000-2001
 #see license.txt for license details
 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/__init__.py?cvsroot=reportlab
-#$Header: /tmp/reportlab/reportlab/__init__.py,v 1.27 2004/01/21 16:20:28 rgbecker Exp $
-__version__=''' $Id: __init__.py,v 1.27 2004/01/21 16:20:28 rgbecker Exp $ '''
+#$Header: /tmp/reportlab/reportlab/__init__.py,v 1.28 2004/03/09 22:22:26 andy_robinson Exp $
+__version__=''' $Id: __init__.py,v 1.28 2004/03/09 22:22:26 andy_robinson Exp $ '''
 __doc__="""The Reportlab PDF generation library."""
 Version = "1.19"
 
@@ -16,3 +16,9 @@
     else:
         # this signals that it should revert to default processing
         return None
+
+
+def getMonitor():
+    import reportlab.monitor
+    mon = reportlab.monitor.ReportLabToolkitMonitor()
+    return mon
\ No newline at end of file
--- a/reportlab/lib/fonts.py	Mon Mar 08 18:36:05 2004 +0000
+++ b/reportlab/lib/fonts.py	Tue Mar 09 22:22:26 2004 +0000
@@ -2,8 +2,8 @@
 #copyright ReportLab Inc. 2000
 #see license.txt for license details
 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/fonts.py?cvsroot=reportlab
-#$Header: /tmp/reportlab/reportlab/lib/fonts.py,v 1.16 2004/01/20 22:50:31 andy_robinson Exp $
-__version__=''' $Id: fonts.py,v 1.16 2004/01/20 22:50:31 andy_robinson Exp $ '''
+#$Header: /tmp/reportlab/reportlab/lib/fonts.py,v 1.17 2004/03/09 22:22:26 andy_robinson Exp $
+__version__=''' $Id: fonts.py,v 1.17 2004/03/09 22:22:26 andy_robinson Exp $ '''
 import string, sys, os
 ###############################################################################
 #   A place to put useful font stuff
@@ -40,46 +40,6 @@
             ('helvetica', 0, 1) :'Helvetica-Oblique',
             ('helvetica', 1, 1) :'Helvetica-BoldOblique',
 
-            # TTF fonts
-            ('timesnew', 0, 0) :'Times New Roman',
-            ('timesnew', 1, 0) :'Times New Roman-Bold',
-            ('timesnew', 0, 1) :'Times New Roman-Italic',
-            ('timesnew', 1, 1) :'Times New Roman-BoldItalic',
-
-            ('courier new', 0, 0) :'Courier New',
-            ('courier new', 1, 0) :'Courier New-Bold',
-            ('courier new', 0, 1) :'Courier New-Italic',
-            ('courier new', 1, 1) :'Courier New-BoldItalic',
-
-            ('verdana', 0, 0) :'Verdana',
-            ('verdana', 1, 0) :'Verdana-Bold',
-            ('verdana', 0, 1) :'Verdana-Italic',
-            ('verdana', 1, 1) :'Verdana-BoldItalic',
-
-            ('arial', 0, 0) :'Arial',
-            ('arial', 1, 0) :'Arial-Bold',
-            ('arial', 0, 1) :'Arial-Italic',
-            ('arial', 1, 1) :'Arial-BoldItalic',
-
-            ('arialnarrow', 0, 0) :'Arial Narrow',
-            ('arialnarrow', 1, 0) :'Arial Narrow-Bold',
-            ('arialnarrow', 0, 1) :'Arial Narrow-Italic',
-            ('arialnarrow', 1, 1) :'Arial Narrow-BoldItalic',
-
-            ('bookmanos', 0, 0) :'Bookman Old Style',
-            ('bookmanos', 1, 0) :'Bookman Old Style-Bold',
-            ('bookmanos', 0, 1) :'Bookman Old Style-Italic',
-            ('bookmanos', 1, 1) :'Bookman Old Style-BoldItalic',
-
-            ('georgia', 0, 0) :'Georgia',
-            ('georgia', 1, 0) :'Georgia-Bold',
-            ('georgia', 0, 1) :'Georgia-Italic',
-            ('georgia', 1, 1) :'Georgia-BoldItalic',
-
-            ('trebuchet', 0, 0) :'Trebuchet MS',
-            ('trebuchet', 1, 0) :'Trebuchet MS-Bold',
-            ('trebuchet', 0, 1) :'Trebuchet MS-Italic',
-            ('trebuchet', 1, 1) :'Trebuchet MS-BoldItalic',
 
             # there is only one Symbol font
             ('symbol', 0, 0) :'Symbol',
@@ -92,6 +52,8 @@
             ('zapfdingbats', 1, 0) :'ZapfDingbats',
             ('zapfdingbats', 0, 1) :'ZapfDingbats',
             ('zapfdingbats', 1, 1) :'ZapfDingbats',
+
+
             }
 
 _ps2tt_map={}
@@ -104,7 +66,7 @@
     psfn = string.lower(psfn)
     if _ps2tt_map.has_key(psfn):
         return _ps2tt_map[psfn]
-    raise ValueError, "Can't map PS font %s" % psfn
+    raise ValueError, "Can't map determine family/bold/italic for %s" % psfn
 
 def tt2ps(fn,b,i):
     'family name + bold & italic to ps font name'
@@ -116,16 +78,13 @@
         K = fn, b1|b, i1|i
         if _tt2ps_map.has_key(K):
             return _tt2ps_map[K]
-    raise ValueError, "Can't map TT font %s" % fn
+    raise ValueError, "Can't find concrete font for family=%s, bold=%d, italic=%d" % (fn, b, i)
 
 def addMapping(face, bold, italic, psname):
-    'allow a custom font to be put in the mapping -- ONLY IF NOT ALREADY PRESENT!'
+    'allow a custom font to be put in the mapping'
     k = (string.lower(face), bold, italic)
-    if not _tt2ps_map.has_key(k):
-        _tt2ps_map[k] = psname
-        # rebuild inverse - inefficient
-        for k,v in _tt2ps_map.items():
-            if not _ps2tt_map.has_key(k):
-                _ps2tt_map[string.lower(v)] = k
-    elif _tt2ps_map[k]!=psname:
-        raise ValueError, "_tt2ps_map[%s]==%s already, not %s" % (repr(k), _tt2ps_map[k], psname)
\ No newline at end of file
+    _tt2ps_map[k] = psname
+    # rebuild inverse - inefficient
+    for k,v in _tt2ps_map.items():
+        if not _ps2tt_map.has_key(k):
+            _ps2tt_map[string.lower(v)] = k
--- a/reportlab/pdfbase/_fontdata.py	Mon Mar 08 18:36:05 2004 +0000
+++ b/reportlab/pdfbase/_fontdata.py	Tue Mar 09 22:22:26 2004 +0000
@@ -2,7 +2,7 @@
 #see license.txt for license details
 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/pdfbase/_fontdata.py?cvsroot=reportlab
 #$Header $
-__version__=''' $Id: _fontdata.py,v 1.13 2003/07/07 19:38:31 rgbecker Exp $ '''
+__version__=''' $Id: _fontdata.py,v 1.14 2004/03/09 22:22:26 andy_robinson Exp $ '''
 __doc__="""
     database of font related things
     standardFonts       tuple of the 14 standard string font names
@@ -26,6 +26,28 @@
     'Times-Roman', 'Times-Bold', 'Times-Italic', 'Times-BoldItalic',
     'Symbol','ZapfDingbats')
 
+standardFontAttributes = {
+    #family, bold, italic defined for basic ones
+    'Courier':('Courier',0,0),
+    'Courier-Bold':('Courier',1,0),
+    'Courier-Oblique':('Courier',0,1),
+    'Courier-BoldOblique':('Courier',1,1),
+    
+    'Helvetica':('Helvetica',0,0),
+    'Helvetica-Bold':('Helvetica',1,0),
+    'Helvetica-Oblique':('Helvetica',0,1),
+    'Helvetica-BoldOblique':('Helvetica',1,1),
+
+    'Times-Roman':('Times-Roman',0,0),
+    'Times-Bold':('Times-Roman',1,0),
+    'Times-Italic':('Times-Roman',0,1),
+    'Times-BoldItalic':('Times-Roman',1,1),
+
+    'Symbol':('Symbol',0,0),
+    'ZapfDingbats':('ZapfDingbats',0,0)
+
+    }
+
 #this maps fontnames to the equivalent filename root.
 _font2fnrMapWin32 = {
                     'symbol':                   'Sy______',
--- a/reportlab/pdfbase/pdfmetrics.py	Mon Mar 08 18:36:05 2004 +0000
+++ b/reportlab/pdfbase/pdfmetrics.py	Tue Mar 09 22:22:26 2004 +0000
@@ -2,7 +2,7 @@
 #see license.txt for license details
 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/pdfbase/pdfmetrics.py?cvsroot=reportlab
 #$Header $
-__version__=''' $Id: pdfmetrics.py,v 1.66 2004/01/20 22:50:31 andy_robinson Exp $ '''
+__version__=''' $Id: pdfmetrics.py,v 1.67 2004/03/09 22:22:26 andy_robinson Exp $ '''
 __doc__="""
 This provides a database of font metric information and
 efines Font, Encoding and TypeFace classes aimed at end users.
@@ -125,6 +125,15 @@
         self.glyphWidths = {}
         self.ascent = 0
         self.descent = 0
+
+
+        # all typefaces of whatever class should have these 3 attributes.
+        # these are the basis for family detection.
+        self.familyName = None  # should set on load/construction if possible
+        self.bold = 0    # bold faces should set this
+        self.italic = 0  #italic faces should set this
+
+        
         if name == 'ZapfDingbats':
             self.requiredEncoding = 'ZapfDingbatsEncoding'
         elif name == 'Symbol':
@@ -145,6 +154,10 @@
         self.glyphNames = self.glyphWidths.keys()
         self.ascent,self.descent = _fontdata.ascent_descent[name]
 
+    def getFontFiles(self):
+        "Info function, return list of the font files this depends on."
+        return []
+
     def findT1File(self, ext='.pfb'):
         possible_exts = (string.lower(ext), string.upper(ext))
         if hasattr(self,'pfbFileName'):
@@ -423,12 +436,18 @@
     Its glyph data will be embedded in the PDF file."""
     def __init__(self, afmFileName, pfbFileName):
         # ignore afm file for now
+        TypeFace.__init__(self, None)
+        #None is a hack, name will be supplied by AFM parse lower done
+        #in this __init__ method.
         self.afmFileName = os.path.abspath(afmFileName)
         self.pfbFileName = os.path.abspath(pfbFileName)
         self.requiredEncoding = None
         self._loadGlyphs(pfbFileName)
         self._loadMetrics(afmFileName)
 
+    def getFontFiles(self):
+        return [self.afmFileName, self.pfbFileName]
+    
     def _loadGlyphs(self, pfbFileName):
         """Loads in binary glyph data, and finds the four length
         measurements needed for the font descriptor"""
@@ -452,7 +471,7 @@
         (topLevel, glyphData) = parseAFMFile(afmFileName)
 
         self.name = topLevel['FontName']
-
+        self.familyName = topLevel['FamilyName']
         self.ascent = topLevel.get('Ascender', 1000)
         self.descent = topLevel.get('Descender', 0)
         self.capHeight = topLevel.get('CapHeight', 1000)
@@ -574,8 +593,10 @@
         # not found, construct it if known
         if faceName in standardFonts:
             face = TypeFace(faceName)
+            (face.familyName, face.bold, face.italic) = _fontdata.standardFontAttributes[faceName]
             registerTypeFace(face)
-            #print 'auto-constructing type face %s' % face.name
+##            print 'auto-constructing type face %s with family=%s, bold=%d, italic=%d' % (
+##                face.name, face.familyName, face.bold, face.italic)
             return face
         else:
             #try a brute force search
@@ -624,7 +645,11 @@
         registerFont(font)
         return font
 
-
+def getRegisteredFontNames():
+    "Returns what's in there"
+    reg = _fonts.keys()
+    reg.sort()
+    return reg
 
 def _slowStringWidth(text, fontName, fontSize):
     """Define this anyway so it can be tested, but whether it is used or not depends on _rl_accel"""
@@ -643,6 +668,14 @@
     Font.stringWidth = new.instancemethod(_rl_accel._instanceStringWidth,None,Font)
     stringWidth = _stringWidth
 
+    #if accelerator present, make sure we at least
+    #register Courier font, since it will fall back to Courier
+    #as its default font.
+    face = TypeFace('Courier')
+    (face.familyName, face.bold, face.italic) = _fontdata.standardFontAttributes['Courier']
+    registerTypeFace(face)
+
+
     def _SWRecover(text, fontName, fontSize, encoding):
         '''This is called when _rl_accel's database doesn't know about a font.
         Currently encoding is always a dummy.
--- a/reportlab/pdfbase/ttfonts.py	Mon Mar 08 18:36:05 2004 +0000
+++ b/reportlab/pdfbase/ttfonts.py	Tue Mar 09 22:22:26 2004 +0000
@@ -1,7 +1,7 @@
 #copyright ReportLab Inc. 2000
 #see license.txt for license details
 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/pdfbase/ttfonts.py?cvsroot=reportlab
-#$Header: /tmp/reportlab/reportlab/pdfbase/ttfonts.py,v 1.18 2004/01/21 10:09:34 rgbecker Exp $
+#$Header: /tmp/reportlab/reportlab/pdfbase/ttfonts.py,v 1.19 2004/03/09 22:22:26 andy_robinson Exp $
 """TrueType font support
 
 This defines classes to represent TrueType fonts.  They know how to calculate
@@ -58,7 +58,7 @@
 Canvas and TextObject have special support for dynamic fonts.
 """
 
-__version__ = '$Id: ttfonts.py,v 1.18 2004/01/21 10:09:34 rgbecker Exp $'
+__version__ = '$Id: ttfonts.py,v 1.19 2004/03/09 22:22:26 andy_robinson Exp $'
 
 import string
 from types import StringType
@@ -889,7 +889,7 @@
 FF_SMALLCAP     = 1 << 18-1
 FF_FORCEBOLD    = 1 << 19-1
 
-class TTFontFace(TTFontFile):
+class TTFontFace(TTFontFile, pdfmetrics.TypeFace):
     """TrueType typeface.
 
     Conceptually similar to a single byte typeface, but the glyphs are
@@ -897,6 +897,7 @@
 
     def __init__(self, filename, validate=0):
         "Loads a TrueType font from filename."
+        pdfmetrics.TypeFace.__init__(self, None)
         TTFontFile.__init__(self, filename, validate=validate)
 
     def getCharWidth(self, code):
--- a/reportlab/platypus/doctemplate.py	Mon Mar 08 18:36:05 2004 +0000
+++ b/reportlab/platypus/doctemplate.py	Tue Mar 09 22:22:26 2004 +0000
@@ -1,9 +1,9 @@
 #copyright ReportLab Inc. 2000
 #see license.txt for license details
 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/doctemplate.py?cvsroot=reportlab
-#$Header: /tmp/reportlab/reportlab/platypus/doctemplate.py,v 1.72 2004/01/20 22:50:31 andy_robinson Exp $
+#$Header: /tmp/reportlab/reportlab/platypus/doctemplate.py,v 1.73 2004/03/09 22:22:26 andy_robinson Exp $
 
-__version__=''' $Id: doctemplate.py,v 1.72 2004/01/20 22:50:31 andy_robinson Exp $ '''
+__version__=''' $Id: doctemplate.py,v 1.73 2004/03/09 22:22:26 andy_robinson Exp $ '''
 
 __doc__="""
 This module contains the core structure of platypus.
@@ -253,6 +253,10 @@
       a chapter and two more for the interior of a chapter on odd and even pages.
       If this argument is omitted then at least one pageTemplate should be provided
       using the addPageTemplates method before the document is built.
+    pageSize: a 2-tuple or a size constant from reportlab/lib/pagesizes.pu.
+     Used by the SimpleDocTemplate subclass which does NOT accept a list of
+     pageTemplates but makes one for you; ignored when using pageTemplates.
+
     showBoundary: if set draw a box around the frame boundaries.
     leftMargin:
     rightMargin: