reportlab/lib/codecharts.py
author andy_robinson
Mon, 22 Oct 2001 05:43:18 +0000
changeset 1363 92e3fb40a784
parent 1362 9771eaa21b0c
child 1378 796ce78d458a
permissions -rw-r--r--
Asian fonts now all working in major encodings, and partly documented
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     1
#copyright ReportLab Inc. 2001
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     2
#see license.txt for license details
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/codecharts?cvsroot=reportlab
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     4
#$Header $
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     5
__version__=''' $Id '''
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     6
__doc__="""Routines to print code page (character set) drawings.
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     7
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     8
To be sure we can accurately represent characters in various encodings
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
     9
and fonts, we need some routines to display all those characters.
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    10
These are defined herein.  The idea is to include flowable, drawable
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    11
and graphic objects for single and multi-byte fonts. """
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    12
import string
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    13
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    14
from reportlab.pdfgen.canvas import Canvas
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    15
from reportlab.platypus import Flowable
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    16
from reportlab.pdfbase import pdfmetrics, cidfonts
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    17
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    18
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    19
class CodeChartBase(Flowable):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    20
    """Basic bits of drawing furniture used by
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    21
    single and multi-byte versions: ability to put letters
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    22
    into boxes."""
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    23
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    24
    def calcLayout(self):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    25
        "Work out x and y positions for drawing"
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    26
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    27
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    28
        rows = self.codePoints * 1.0 / self.charsPerRow
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    29
        if rows == int(rows):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    30
            self.rows = int(rows)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    31
        else:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    32
            self.rows = int(rows) + 1
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    33
        # size allows for a gray column of labels
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    34
        self.width = self.boxSize * (1+self.charsPerRow)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    35
        self.height = self.boxSize * (1+self.rows)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    36
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    37
        #handy lists
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    38
        self.ylist = []
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    39
        for row in range(self.rows + 2):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    40
            self.ylist.append(row * self.boxSize)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    41
        self.xlist = []
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    42
        for col in range(self.charsPerRow + 2):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    43
            self.xlist.append(col * self.boxSize)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    44
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    45
    def formatByte(self, byt):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    46
        if self.hex:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    47
            return '%02X' % byt
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    48
        else:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    49
            return '%d' % byt
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    50
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    51
    def drawChars(self, charList):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    52
        """Fills boxes in order.  None means skip a box.
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    53
        Empty boxes at end get filled with gray"""
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    54
        extraNeeded = (self.rows * self.charsPerRow - len(charList))
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    55
        charList = charList + [None] * extraNeeded
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    56
        row = 0
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    57
        col = 0
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    58
        self.canv.setFont(self.fontName, self.boxSize * 0.75)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    59
        for ch in charList:  # may be 2 bytes or 1
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    60
            if ch is None:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    61
                self.canv.setFillGray(0.9)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    62
                self.canv.rect((1+col) * self.boxSize, (self.rows - row - 1) * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    63
                    self.boxSize, self.boxSize, stroke=0, fill=1)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    64
                self.canv.setFillGray(0.0)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    65
            else:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    66
                self.canv.drawCentredString(
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    67
                            (col+1.5) * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    68
                            (self.rows - row - 0.875) * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    69
                            ch
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    70
                            )
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    71
            col = col + 1
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    72
            if col == self.charsPerRow:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    73
                row = row + 1
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    74
                col = 0
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    75
1362
9771eaa21b0c Japanese test now does full EUC kuten table
andy_robinson
parents: 1361
diff changeset
    76
    def drawLabels(self, topLeft = ''):
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    77
        """Writes little labels in the top row and first column"""
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    78
        self.canv.setFillGray(0.8)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    79
        self.canv.rect(0, self.ylist[-2], self.width, self.boxSize, fill=1, stroke=0)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    80
        self.canv.rect(0, 0, self.boxSize, self.ylist[-2], fill=1, stroke=0)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    81
        self.canv.setFillGray(0.0)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    82
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    83
        #label each row and column
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    84
        self.canv.setFont('Helvetica-Oblique',0.375 * self.boxSize)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    85
        byt = 0
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    86
        for row in range(self.rows):
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
    87
            if self.rowLabels:
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
    88
                label = self.rowLabels[row]
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
    89
            else: # format start bytes as hex or decimal
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
    90
                label = self.formatByte(row * self.charsPerRow)
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    91
            self.canv.drawCentredString(0.5 * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    92
                                        (self.rows - row - 0.75) * self.boxSize,
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
    93
                                        label
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    94
                                        )
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    95
        for col in range(self.charsPerRow):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    96
            self.canv.drawCentredString((col + 1.5) * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    97
                                        (self.rows + 0.25) * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    98
                                        self.formatByte(col)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    99
                                        )
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   100
1362
9771eaa21b0c Japanese test now does full EUC kuten table
andy_robinson
parents: 1361
diff changeset
   101
        if topLeft:
9771eaa21b0c Japanese test now does full EUC kuten table
andy_robinson
parents: 1361
diff changeset
   102
            self.canv.setFont('Helvetica-BoldOblique',0.5 * self.boxSize)
9771eaa21b0c Japanese test now does full EUC kuten table
andy_robinson
parents: 1361
diff changeset
   103
            self.canv.drawCentredString(0.5 * self.boxSize,
9771eaa21b0c Japanese test now does full EUC kuten table
andy_robinson
parents: 1361
diff changeset
   104
                                        (self.rows + 0.25) * self.boxSize,
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   105
                                        topLeft
1362
9771eaa21b0c Japanese test now does full EUC kuten table
andy_robinson
parents: 1361
diff changeset
   106
                                        )
9771eaa21b0c Japanese test now does full EUC kuten table
andy_robinson
parents: 1361
diff changeset
   107
            
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   108
class SingleByteEncodingChart(CodeChartBase):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   109
    def __init__(self, faceName='Helvetica', encodingName='WinAnsiEncoding',
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   110
                 charsPerRow=16, boxSize=14, hex=1):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   111
        self.codePoints = 256
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   112
        self.faceName = faceName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   113
        self.encodingName = encodingName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   114
        self.fontName = self.faceName + '-' + self.encodingName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   115
        self.charsPerRow = charsPerRow
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   116
        self.boxSize = boxSize
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   117
        self.hex = hex
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   118
        self.rowLabels = None
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   119
        pdfmetrics.registerFont(pdfmetrics.Font(self.fontName,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   120
                                                self.faceName,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   121
                                                self.encodingName)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   122
                                )
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   123
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   124
        self.calcLayout()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   125
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   126
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   127
    def draw(self):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   128
        self.drawLabels()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   129
        charList = [None] * 32 + map(chr, range(32, 256))
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   130
        self.drawChars(charList)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   131
        self.canv.grid(self.xlist, self.ylist)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   132
                    
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   133
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   134
class KutenRowCodeChart(CodeChartBase):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   135
    """Formats one 'row' of the 94x94 space used in many Asian encodings.aliases
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   136
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   137
    These deliberately resemble the code charts in Ken Lunde's "Understanding
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   138
    CJKV Information Processing", to enable manual checking.  Due to the large
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   139
    numbers of characters, we don't try to make one graphic with 10,000 characters,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   140
    but rather output a sequence of these."""
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   141
    #would be cleaner if both shared one base class whose job
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   142
    #was to draw the boxes, but never mind...
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   143
    def __init__(self, row, faceName, encodingName):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   144
        self.row = row
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   145
        self.codePoints = 94
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   146
        self.boxSize = 18
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   147
        self.charsPerRow = 20
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   148
        self.rows = 5
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   149
        self.rowLabels = ['00','20','40','60','80']
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   150
        self.hex = 0
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   151
        self.faceName = faceName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   152
        self.encodingName = encodingName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   153
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   154
        try:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   155
            # the dependent files might not be available
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   156
            font = cidfonts.CIDFont(self.faceName, self.encodingName)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   157
            pdfmetrics.registerFont(font)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   158
        except:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   159
            # fall back to English and at least shwo we can draw the boxes
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   160
            self.faceName = 'Helvetica'
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   161
            self.encodingName = 'WinAnsiEncoding'
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   162
        self.fontName = self.faceName + '-' + self.encodingName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   163
        self.calcLayout()        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   164
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   165
    def makeRow(self, row):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   166
        """Works out the character values for this kuten row"""
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   167
        cells = []
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   168
        if string.find(self.encodingName, 'EUC') > -1:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   169
            # it is an EUC family encoding.
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   170
            for col in range(1, 95):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   171
                ch = chr(row + 160) + chr(col+160)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   172
                cells.append(ch)
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   173
##        elif string.find(self.encodingName, 'GB') > -1:
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   174
##            # it is an EUC family encoding.
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   175
##            for col in range(1, 95):
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   176
##                ch = chr(row + 160) + chr(col+160)
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   177
        else:
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   178
            cells.append([None] * 94)
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   179
        return cells
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   180
                
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   181
    def draw(self):
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   182
        self.drawLabels(topLeft= 'R%d' % self.row)
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   183
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   184
        # work out which characters we need for the row
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   185
        #assert string.find(self.encodingName, 'EUC') > -1, 'Only handles EUC encoding today, you gave me %s!' % self.encodingName
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   186
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   187
        # pad out by 1 to match Ken Lunde's tables
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   188
        charList = [None] + self.makeRow(self.row) 
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   189
        self.drawChars(charList)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   190
        self.canv.grid(self.xlist, self.ylist)
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   191
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   192
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   193
class Big5CodeChart(CodeChartBase):
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   194
    """Formats one 'row' of the 94x160 space used in Big 5
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   195
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   196
    These deliberately resemble the code charts in Ken Lunde's "Understanding
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   197
    CJKV Information Processing", to enable manual checking."""
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   198
    def __init__(self, row, faceName, encodingName):
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   199
        self.row = row
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   200
        self.codePoints = 160
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   201
        self.boxSize = 18
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   202
        self.charsPerRow = 16
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   203
        self.rows = 10
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   204
        self.hex = 1
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   205
        self.faceName = faceName
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   206
        self.encodingName = encodingName
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   207
        self.rowLabels = ['4','5','6','7','A','B','C','D','E','F']
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   208
        try:
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   209
            # the dependent files might not be available
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   210
            font = cidfonts.CIDFont(self.faceName, self.encodingName)
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   211
            pdfmetrics.registerFont(font)
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   212
        except:
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   213
            # fall back to English and at least shwo we can draw the boxes
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   214
            self.faceName = 'Helvetica'
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   215
            self.encodingName = 'WinAnsiEncoding'
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   216
        self.fontName = self.faceName + '-' + self.encodingName
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   217
        self.calcLayout()        
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   218
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   219
    def makeRow(self, row):
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   220
        """Works out the character values for this Big5 row.
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   221
        Rows start at 0xA1"""
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   222
        cells = []
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   223
        if string.find(self.encodingName, 'B5') > -1:
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   224
            # big 5, different row size
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   225
            for y in [4,5,6,7,10,11,12,13,14,15]:
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   226
                for x in range(16):
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   227
                    col = y*16+x
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   228
                    ch = chr(row) + chr(col)
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   229
                    cells.append(ch)
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   230
                
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   231
        else:
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   232
            cells.append([None] * 160)
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   233
        return cells
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   234
                
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   235
    def draw(self):
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   236
        self.drawLabels(topLeft='%02X' % self.row)
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   237
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   238
        charList = self.makeRow(self.row) 
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   239
        self.drawChars(charList)
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   240
        self.canv.grid(self.xlist, self.ylist)
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   241
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   242
def test():
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   243
    c = Canvas('codecharts.pdf')
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   244
    c.setFont('Helvetica-Bold', 24)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   245
    c.drawString(72, 750, 'Testing code page charts')
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   246
    cc1 = SingleByteEncodingChart()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   247
    cc1.drawOn(c, 72, 500)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   248
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   249
    cc2 = SingleByteEncodingChart(charsPerRow=32)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   250
    cc2.drawOn(c, 72, 300)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   251
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   252
    cc3 = SingleByteEncodingChart(charsPerRow=25, hex=0)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   253
    cc3.drawOn(c, 72, 100)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   254
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   255
    c.showPage()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   256
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   257
    c.setFont('Helvetica-Bold', 24)
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   258
    c.drawString(72, 750, 'Multi-byte Kuten code chart examples')
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   259
    KutenRowCodeChart(1, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 600)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   260
    KutenRowCodeChart(16, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 450)
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   261
    KutenRowCodeChart(84, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 300)
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   262
1363
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   263
    c.showPage()
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   264
    c.setFont('Helvetica-Bold', 24)
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   265
    c.drawString(72, 750, 'Big5 Code Chart Examples')
92e3fb40a784 Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents: 1362
diff changeset
   266
    Big5CodeChart(0xA1, 'MSungStd-Light-Acro','ETenms-B5-H').drawOn(c, 72, 500)
1361
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   267
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   268
    c.save()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   269
    print 'saved codecharts.pdf'
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   270
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   271
if __name__=='__main__':
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   272
    test()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   273
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   274
    
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   275