reportlab/lib/codecharts.py
author andy_robinson
Sun, 21 Oct 2001 17:05:01 +0000
changeset 1361 35586d769319
child 1362 9771eaa21b0c
permissions -rw-r--r--
Enhancements/fixes to Asian fonts; codecharts utility to facilitate displaying characters in fonts; enhancements to user guide to describe fonts; work-in-progress on pdfimages towards an XObject.
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
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    76
    def drawLabels(self):
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):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    87
            startByte = row * self.charsPerRow
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    88
            self.canv.drawCentredString(0.5 * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    89
                                        (self.rows - row - 0.75) * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    90
                                        self.formatByte(startByte)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    91
                                        )
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    92
        for col in range(self.charsPerRow):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    93
            self.canv.drawCentredString((col + 1.5) * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    94
                                        (self.rows + 0.25) * self.boxSize,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    95
                                        self.formatByte(col)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    96
                                        )
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    97
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    98
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
    99
class SingleByteEncodingChart(CodeChartBase):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   100
    def __init__(self, faceName='Helvetica', encodingName='WinAnsiEncoding',
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   101
                 charsPerRow=16, boxSize=14, hex=1):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   102
        self.codePoints = 256
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   103
        self.faceName = faceName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   104
        self.encodingName = encodingName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   105
        self.fontName = self.faceName + '-' + self.encodingName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   106
        self.charsPerRow = charsPerRow
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   107
        self.boxSize = boxSize
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   108
        self.hex = hex
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   109
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   110
        pdfmetrics.registerFont(pdfmetrics.Font(self.fontName,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   111
                                                self.faceName,
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   112
                                                self.encodingName)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   113
                                )
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   114
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   115
        self.calcLayout()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   116
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   117
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   118
    def draw(self):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   119
        self.drawLabels()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   120
        charList = [None] * 32 + map(chr, range(32, 256))
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   121
        self.drawChars(charList)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   122
        self.canv.grid(self.xlist, self.ylist)
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
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   125
class KutenRowCodeChart(CodeChartBase):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   126
    """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
   127
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   128
    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
   129
    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
   130
    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
   131
    but rather output a sequence of these."""
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   132
    #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
   133
    #was to draw the boxes, but never mind...
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   134
    def __init__(self, row, faceName, encodingName):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   135
        self.row = row
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   136
        self.codePoints = 94
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   137
        self.boxSize = 18
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   138
        self.charsPerRow = 20
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   139
        self.rows = 5
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   140
        self.hex = 0
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   141
        self.faceName = faceName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   142
        self.encodingName = encodingName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   143
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   144
        try:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   145
            # the dependent files might not be available
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   146
            font = cidfonts.CIDFont(self.faceName, self.encodingName)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   147
            pdfmetrics.registerFont(font)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   148
        except:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   149
            # 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
   150
            self.faceName = 'Helvetica'
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   151
            self.encodingName = 'WinAnsiEncoding'
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   152
        self.fontName = self.faceName + '-' + self.encodingName
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   153
        self.calcLayout()        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   154
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   155
    def makeRow(self, row):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   156
        """Works out the character values for this kuten row"""
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   157
        cells = []
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   158
        if string.find(self.encodingName, 'EUC') > -1:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   159
            # it is an EUC family encoding.
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   160
            for col in range(1, 95):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   161
                ch = chr(row + 160) + chr(col+160)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   162
                cells.append(ch)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   163
        else:
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   164
            cells.append(None)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   165
        return cells
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   166
                
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   167
    def draw(self):
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   168
        self.drawLabels()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   169
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   170
        # work out which characters we need for the row
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   171
        assert string.find(self.encodingName, 'EUC') > -1, 'Only handles EUC encoding today!'
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   172
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   173
        # pad out by 1 to match Ken Lunde's tables
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   174
        charList = [None] + self.makeRow(self.row) 
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   175
        self.drawChars(charList)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   176
        self.canv.grid(self.xlist, self.ylist)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   177
        
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   178
def test():
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   179
    c = Canvas('codecharts.pdf')
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   180
    c.setFont('Helvetica-Bold', 24)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   181
    c.drawString(72, 750, 'Testing code page charts')
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   182
    cc1 = SingleByteEncodingChart()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   183
    cc1.drawOn(c, 72, 500)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   184
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   185
    cc2 = SingleByteEncodingChart(charsPerRow=32)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   186
    cc2.drawOn(c, 72, 300)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   187
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   188
    cc3 = SingleByteEncodingChart(charsPerRow=25, hex=0)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   189
    cc3.drawOn(c, 72, 100)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   190
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   191
    c.showPage()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   192
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   193
    c.setFont('Helvetica-Bold', 24)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   194
    c.drawString(72, 750, 'Multi-byte code chart examples')
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   195
    KutenRowCodeChart(1, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 600)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   196
    
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   197
    KutenRowCodeChart(16, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 450)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   198
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   199
    KutenRowCodeChart(84, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 300)
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   200
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   201
    c.save()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   202
    print 'saved codecharts.pdf'
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   203
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   204
if __name__=='__main__':
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   205
    test()
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   206
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   207
    
35586d769319 Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff changeset
   208