Added font encoding support and changed default encoding to WinAnsi
authorandy_robinson
Fri, 28 Apr 2000 17:33:44 +0000
changeset 181 62d7a443cbc4
parent 180 f92b4f73b1b5
child 182 78ae2b716a06
Added font encoding support and changed default encoding to WinAnsi
reportlab/demos/stdfonts/stdfonts.py
reportlab/pdfbase/pdfdoc.py
reportlab/pdfgen/canvas.py
--- a/reportlab/demos/stdfonts/stdfonts.py	Fri Apr 28 17:04:28 2000 +0000
+++ b/reportlab/demos/stdfonts/stdfonts.py	Fri Apr 28 17:33:44 2000 +0000
@@ -31,9 +31,12 @@
 #
 ###############################################################################
 #	$Log: stdfonts.py,v $
+#	Revision 1.5  2000/04/28 17:33:44  andy_robinson
+#	Added font encoding support and changed default encoding to WinAnsi
+#
 #	Revision 1.4  2000/02/17 02:06:28  rgbecker
 #	Docstring & other fixes
-#
+#	
 #	Revision 1.3  2000/02/16 09:42:50  rgbecker
 #	Conversion to reportlab package
 #	
@@ -43,7 +46,7 @@
 #	Revision 1.1.1.1  2000/02/15 15:15:57  rgbecker
 #	Initial setup of demos directory and contents.
 #	
-__version__=''' $Id: stdfonts.py,v 1.4 2000/02/17 02:06:28 rgbecker Exp $ '''
+__version__=''' $Id: stdfonts.py,v 1.5 2000/04/28 17:33:44 andy_robinson Exp $ '''
 __doc__="""
 standardfonts.py
 shows the 14 standard fonts in our encoding
@@ -54,33 +57,41 @@
 
 def run():
 
-    canv = canvas.Canvas('standardfonts.pdf')
-    canv.setPageCompression(0)
-    
-    for fontname in pdfmetrics.StandardEnglishFonts:
-        canv.setFont('Times-Bold', 18)
-        canv.drawString(80, 744, fontname)
+    for enc in ['MacRoman', 'WinAnsi']:
+        canv = canvas.Canvas(
+                'StandardFonts_%s.pdf' % enc,
+                encoding=enc
+                )
+        canv.setPageCompression(0)
         
-        #for dingbats, we need to use another font for the numbers.
-        #do two parallel text objects.
-        if fontname == 'ZapfDingbats':
-            labelfont = 'Helvetica'
-        else:
-            labelfont = fontname
+        for fontname in pdfmetrics.StandardEnglishFonts:
+            if fontname in ['Symbol', 'ZapfDingbats']:
+                encLabel = 'only available as MacRoman'
+            else:
+                encLabel = enc
+            canv.setFont('Times-Bold', 18)
+            canv.drawString(80, 744, fontname + '-' + encLabel)
+            
+            #for dingbats, we need to use another font for the numbers.
+            #do two parallel text objects.
+            if fontname == 'ZapfDingbats':
+                labelfont = 'Helvetica'
+            else:
+                labelfont = fontname
 
-        for byt in range(32, 256):
-            col, row = divmod(byt - 32, 32)
-            x = 72 + (66*col)
-            y = 720 - (18*row)
-            canv.setFont(labelfont, 14)
-            canv.drawString(x, y, '%d =' % byt)
-            canv.setFont(fontname, 14)
-            canv.drawString(x + 44, y , chr(byt))
+            for byt in range(32, 256):
+                col, row = divmod(byt - 32, 32)
+                x = 72 + (66*col)
+                y = 720 - (18*row)
+                canv.setFont(labelfont, 14)
+                canv.drawString(x, y, '%d =' % byt)
+                canv.setFont(fontname, 14)
+                canv.drawString(x + 44, y , chr(byt))
 
-        canv.showPage()            
-            
+            canv.showPage()            
+                
 
-    canv.save()
+        canv.save()
 
 if __name__ == '__main__':
     run()
--- a/reportlab/pdfbase/pdfdoc.py	Fri Apr 28 17:04:28 2000 +0000
+++ b/reportlab/pdfbase/pdfdoc.py	Fri Apr 28 17:33:44 2000 +0000
@@ -31,9 +31,12 @@
 #
 ###############################################################################
 #	$Log: pdfdoc.py,v $
+#	Revision 1.18  2000/04/28 17:33:44  andy_robinson
+#	Added font encoding support and changed default encoding to WinAnsi
+#
 #	Revision 1.17  2000/04/28 09:08:42  rgbecker
 #	Fix typo in SaveToFile
-#
+#	
 #	Revision 1.16  2000/04/27 18:11:56  rgbecker
 #	Dinu's SaveFile patch
 #	
@@ -73,7 +76,7 @@
 #	Revision 1.2  2000/02/15 15:47:09  rgbecker
 #	Added license, __version__ and Logi comment
 #	
-__version__=''' $Id: pdfdoc.py,v 1.17 2000/04/28 09:08:42 rgbecker Exp $ '''
+__version__=''' $Id: pdfdoc.py,v 1.18 2000/04/28 17:33:44 andy_robinson Exp $ '''
 __doc__=""" 
 PDFgen is a library to generate PDF files containing text and graphics.  It is the 
 foundation for a complete reporting solution in Python.  
@@ -116,6 +119,8 @@
 #
 ##############################################################
 
+DEFAULT_ENCODING = 'WinAnsiEncoding' #hack here for a system wide change
+ALLOWED_ENCODINGS = ('WinAnsiEncoding', 'MacRomanEncoding')
 
 
 StandardEnglishFonts = [
@@ -138,7 +143,7 @@
     another object is, or raises a KeyError if not found.  The rule is that
     objects should only refer ones previously written to file.
     """
-    def __init__(self):
+    def __init__(self, encoding=DEFAULT_ENCODING):
         self.objects = []
         self.objectPositions = {}
         # named object references (may be unbound)
@@ -146,8 +151,15 @@
         self.inObject = None # mark for whether in page or form or possibly others...
         self.thispageposition = None # record if in page
         self.thisformposition = None # record if in form
-        
-        self.fonts = MakeType1Fonts()
+
+        #check encoding legal - they can omit the suffix 'Encoding
+        if encoding[-8:] <> 'Encoding':
+            encoding = encoding + 'Encoding'
+        assert encoding in ALLOWED_ENCODINGS, \
+               "Unknown encoding %s. Allowed values are %s" % (
+                encoding, ALLOWED_ENCODINGS)
+                        
+        self.fonts = MakeType1Fonts(encoding)
 
         #mapping of Postscriptfont names to internal ones;
         #needs to be dynamically built once we start adding
@@ -1203,20 +1215,23 @@
                 ], LINEEND) + LINEEND)
 
 class PDFType1Font(PDFObject):
-    def __init__(self, key, font):
+    def __init__(self, key, font, encoding):
         self.fontname = font
         self.keyname = key
+        self.encoding = encoding
         self.template = string.join([
                     '<<',
                     '/Type /Font',
                     '/Subtype /Type1',
                     '/Name /%s',
                     '/BaseFont /%s',
-                    '/Encoding /MacRomanEncoding',
+                    '/Encoding /%s',
                     '>>'],
                     LINEEND)
     def save(self, file):
-        file.write(self.template % (self.keyname, self.fontname) + LINEEND)
+        file.write(self.template %
+                   (self.keyname, self.fontname, self.encoding)
+                   + LINEEND)
 
 
        
@@ -1231,12 +1246,17 @@
 #
 ##############################################################
 
-def MakeType1Fonts():
+def MakeType1Fonts(encoding):
     "returns a list of all the standard font objects"
     fonts = []
     pos = 1
     for fontname in StandardEnglishFonts:
-        font = PDFType1Font('F'+str(pos), fontname)
+        #Symbol is almost empty in WinAnsi, no choice!
+        if fontname in ['Symbol', 'ZapfDingbats']:
+            encUsed = 'MacRomanEncoding'
+        else:
+            encUsed = encoding
+        font = PDFType1Font('F'+str(pos), fontname, encUsed)
         fonts.append(font)
         pos = pos + 1
     return fonts
--- a/reportlab/pdfgen/canvas.py	Fri Apr 28 17:04:28 2000 +0000
+++ b/reportlab/pdfgen/canvas.py	Fri Apr 28 17:33:44 2000 +0000
@@ -31,9 +31,12 @@
 #
 ###############################################################################
 #	$Log: canvas.py,v $
+#	Revision 1.34  2000/04/28 17:33:44  andy_robinson
+#	Added font encoding support and changed default encoding to WinAnsi
+#
 #	Revision 1.33  2000/04/28 14:18:16  rgbecker
 #	Use str(filename) not '<Unknown>'
-#
+#	
 #	Revision 1.32  2000/04/28 13:37:40  rgbecker
 #	Fix verbose filename print when it's actually a file type object
 #	
@@ -129,7 +132,7 @@
 #	Revision 1.2  2000/02/15 15:47:09  rgbecker
 #	Added license, __version__ and Logi comment
 #	
-__version__=''' $Id: canvas.py,v 1.33 2000/04/28 14:18:16 rgbecker Exp $ '''
+__version__=''' $Id: canvas.py,v 1.34 2000/04/28 17:33:44 andy_robinson Exp $ '''
 __doc__=""" 
 PDFgen is a library to generate PDF files containing text and graphics.  It is the 
 foundation for a complete reporting solution in Python.  It is also the
@@ -217,15 +220,18 @@
                  pagesize=(595.27,841.89),
                  bottomup = 1,
                  pageCompression=0,
+				 encoding=pdfdoc.DEFAULT_ENCODING,
                  verbosity=1):
         """Most of the attributes are private - we will use set/get methods
         as the preferred interface.  Default page size is A4."""
         self._filename = filename
-        self._doc = pdfdoc.PDFDocument()
+
+        self._doc = pdfdoc.PDFDocument(encoding)
 
         #this only controls whether it prints 'saved ...' - 0 disables
         self._verbosity = verbosity
-        
+
+		
         self._pagesize = pagesize
         #self._currentPageHasImages = 0
         self._pageTransitionString = ''