author  andy_robinson 
Wed, 07 Jan 2004 22:58:54 +0000  
changeset 2192  955d4bf3b9d2 
parent 1683  7fa753e4420a 
child 2332  2a7ab4405e18 
permissions  rwrr 
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/cgibin/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 multibyte 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 
1679
e77be1edcd61
Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents:
1675
diff
changeset

17 
from reportlab.graphics.shapes import Drawing, Group, String, Circle, Rect 
1675
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

18 
from reportlab.graphics.widgetbase import Widget 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

19 
from reportlab.lib import colors 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

20 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

21 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

22 
class CodeChartBase(Flowable): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

23 
"""Basic bits of drawing furniture used by 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

24 
single and multibyte versions: ability to put letters 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

25 
into boxes.""" 
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 
def calcLayout(self): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

28 
"Work out x and y positions for drawing" 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

29 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

30 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

31 
rows = self.codePoints * 1.0 / self.charsPerRow 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

32 
if rows == int(rows): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

33 
self.rows = int(rows) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

34 
else: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

35 
self.rows = int(rows) + 1 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

36 
# size allows for a gray column of labels 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

37 
self.width = self.boxSize * (1+self.charsPerRow) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

38 
self.height = self.boxSize * (1+self.rows) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

39 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

40 
#handy lists 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

41 
self.ylist = [] 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

42 
for row in range(self.rows + 2): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

43 
self.ylist.append(row * self.boxSize) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

44 
self.xlist = [] 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

45 
for col in range(self.charsPerRow + 2): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

46 
self.xlist.append(col * self.boxSize) 
1683  47 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

48 
def formatByte(self, byt): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

49 
if self.hex: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

50 
return '%02X' % byt 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

51 
else: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

52 
return '%d' % byt 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

53 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

54 
def drawChars(self, charList): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

55 
"""Fills boxes in order. None means skip a box. 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

56 
Empty boxes at end get filled with gray""" 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

57 
extraNeeded = (self.rows * self.charsPerRow  len(charList)) 
2192  58 
for i in range(extraNeeded): 
59 
charList.append(None) 

60 
#charList.extend([None] * extraNeeded) 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

61 
row = 0 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

62 
col = 0 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

63 
self.canv.setFont(self.fontName, self.boxSize * 0.75) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

64 
for ch in charList: # may be 2 bytes or 1 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

65 
if ch is None: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

66 
self.canv.setFillGray(0.9) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

67 
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

68 
self.boxSize, self.boxSize, stroke=0, fill=1) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

69 
self.canv.setFillGray(0.0) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

70 
else: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

71 
self.canv.drawCentredString( 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

72 
(col+1.5) * self.boxSize, 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

73 
(self.rows  row  0.875) * self.boxSize, 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

74 
ch 
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 
col = col + 1 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

77 
if col == self.charsPerRow: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

78 
row = row + 1 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

79 
col = 0 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

80 

1362  81 
def drawLabels(self, topLeft = ''): 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

82 
"""Writes little labels in the top row and first column""" 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

83 
self.canv.setFillGray(0.8) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

84 
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

85 
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

86 
self.canv.setFillGray(0.0) 
1683  87 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

88 
#label each row and column 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

89 
self.canv.setFont('HelveticaOblique',0.375 * self.boxSize) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

90 
byt = 0 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

91 
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

92 
if self.rowLabels: 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

93 
label = self.rowLabels[row] 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

94 
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

95 
label = self.formatByte(row * self.charsPerRow) 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

96 
self.canv.drawCentredString(0.5 * self.boxSize, 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

97 
(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

98 
label 
1361
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 
for col in range(self.charsPerRow): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

101 
self.canv.drawCentredString((col + 1.5) * self.boxSize, 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

102 
(self.rows + 0.25) * self.boxSize, 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

103 
self.formatByte(col) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

104 
) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

105 

1362  106 
if topLeft: 
107 
self.canv.setFont('HelveticaBoldOblique',0.5 * self.boxSize) 

108 
self.canv.drawCentredString(0.5 * self.boxSize, 

109 
(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

110 
topLeft 
1362  111 
) 
1683  112 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

113 
class SingleByteEncodingChart(CodeChartBase): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

114 
def __init__(self, faceName='Helvetica', encodingName='WinAnsiEncoding', 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

115 
charsPerRow=16, boxSize=14, hex=1): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

116 
self.codePoints = 256 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

117 
self.faceName = faceName 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

118 
self.encodingName = encodingName 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

119 
self.fontName = self.faceName + '' + self.encodingName 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

120 
self.charsPerRow = charsPerRow 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

121 
self.boxSize = boxSize 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

122 
self.hex = hex 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

123 
self.rowLabels = None 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

124 
pdfmetrics.registerFont(pdfmetrics.Font(self.fontName, 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

125 
self.faceName, 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

126 
self.encodingName) 
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 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

129 
self.calcLayout() 
1683  130 

131 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

132 
def draw(self): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

133 
self.drawLabels() 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

134 
charList = [None] * 32 + map(chr, range(32, 256)) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

135 
self.drawChars(charList) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

136 
self.canv.grid(self.xlist, self.ylist) 
1683  137 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

138 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

139 
class KutenRowCodeChart(CodeChartBase): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

140 
"""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

141 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

142 
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

143 
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

144 
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

145 
but rather output a sequence of these.""" 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

146 
#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

147 
#was to draw the boxes, but never mind... 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

148 
def __init__(self, row, faceName, encodingName): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

149 
self.row = row 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

150 
self.codePoints = 94 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

151 
self.boxSize = 18 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

152 
self.charsPerRow = 20 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

153 
self.rows = 5 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

154 
self.rowLabels = ['00','20','40','60','80'] 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

155 
self.hex = 0 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

156 
self.faceName = faceName 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

157 
self.encodingName = encodingName 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

158 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

159 
try: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

160 
# the dependent files might not be available 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

161 
font = cidfonts.CIDFont(self.faceName, self.encodingName) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

162 
pdfmetrics.registerFont(font) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

163 
except: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

164 
# 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

165 
self.faceName = 'Helvetica' 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

166 
self.encodingName = 'WinAnsiEncoding' 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

167 
self.fontName = self.faceName + '' + self.encodingName 
1683  168 
self.calcLayout() 
1361
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 
def makeRow(self, row): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

171 
"""Works out the character values for this kuten row""" 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

172 
cells = [] 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

173 
if string.find(self.encodingName, 'EUC') > 1: 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

174 
# it is an EUC family encoding. 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

175 
for col in range(1, 95): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

176 
ch = chr(row + 160) + chr(col+160) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

177 
cells.append(ch) 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

178 
## 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

179 
## # it is an EUC family encoding. 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

180 
## for col in range(1, 95): 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

181 
## ch = chr(row + 160) + chr(col+160) 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

182 
else: 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

183 
cells.append([None] * 94) 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

184 
return cells 
1683  185 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

186 
def draw(self): 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

187 
self.drawLabels(topLeft= 'R%d' % self.row) 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

188 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

189 
# 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

190 
#assert string.find(self.encodingName, 'EUC') > 1, 'Only handles EUC encoding today, you gave me %s!' % self.encodingName 
1683  191 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

192 
# pad out by 1 to match Ken Lunde's tables 
1683  193 
charList = [None] + self.makeRow(self.row) 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

194 
self.drawChars(charList) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

195 
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

196 

92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

197 

92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

198 
class Big5CodeChart(CodeChartBase): 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

199 
"""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

200 

92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

201 
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

202 
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

203 
def __init__(self, row, faceName, encodingName): 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

204 
self.row = row 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

205 
self.codePoints = 160 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

206 
self.boxSize = 18 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

207 
self.charsPerRow = 16 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

208 
self.rows = 10 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

209 
self.hex = 1 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

210 
self.faceName = faceName 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

211 
self.encodingName = encodingName 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

212 
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

213 
try: 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

214 
# 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

215 
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

216 
pdfmetrics.registerFont(font) 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

217 
except: 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

218 
# 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

219 
self.faceName = 'Helvetica' 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

220 
self.encodingName = 'WinAnsiEncoding' 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

221 
self.fontName = self.faceName + '' + self.encodingName 
1683  222 
self.calcLayout() 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

223 

92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

224 
def makeRow(self, row): 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

225 
"""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

226 
Rows start at 0xA1""" 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

227 
cells = [] 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

228 
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

229 
# big 5, different row size 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

230 
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

231 
for x in range(16): 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

232 
col = y*16+x 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

233 
ch = chr(row) + chr(col) 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

234 
cells.append(ch) 
1683  235 

1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

236 
else: 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

237 
cells.append([None] * 160) 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

238 
return cells 
1683  239 

1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

240 
def draw(self): 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

241 
self.drawLabels(topLeft='%02X' % self.row) 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

242 

1683  243 
charList = self.makeRow(self.row) 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

244 
self.drawChars(charList) 
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

245 
self.canv.grid(self.xlist, self.ylist) 
1378
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

246 

796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

247 

796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

248 
def hBoxText(msg, canvas, x, y, faceName, encName): 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

249 
"""Helper for stringwidth tests on Asian fonts. 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

250 

796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

251 
Registers font if needed. Then draws the string, 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

252 
and a box around it derived from the stringWidth function""" 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

253 
canvas.saveState() 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

254 
fontName = faceName + '' + encName 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

255 
try: 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

256 
font = pdfmetrics.getFont(fontName) 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

257 
except KeyError: 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

258 
font = cidfonts.CIDFont(faceName, encName) 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

259 
pdfmetrics.registerFont(font) 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

260 

796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

261 
canvas.setFillGray(0.8) 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

262 
canvas.rect(x,y,pdfmetrics.stringWidth(msg, fontName, 16),16,stroke=0,fill=1) 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

263 
canvas.setFillGray(0) 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

264 
canvas.setFont(fontName, 16,16) 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

265 
canvas.drawString(x,y,msg) 
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

266 
canvas.restoreState() 
1675
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

267 

92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

268 

92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

269 
class CodeWidget(Widget): 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

270 
"""Block showing all the characters""" 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

271 
def __init__(self): 
1679
e77be1edcd61
Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents:
1675
diff
changeset

272 
self.x = 0 
e77be1edcd61
Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents:
1675
diff
changeset

273 
self.y = 0 
1675
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

274 
self.width = 160 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

275 
self.height = 160 
1683  276 

1675
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

277 
def draw(self): 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

278 
dx = self.width / 16.0 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

279 
dy = self.height / 16.0 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

280 
g = Group() 
1679
e77be1edcd61
Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents:
1675
diff
changeset

281 
g.add(Rect(self.x, self.y, self.width, self.height, 
e77be1edcd61
Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents:
1675
diff
changeset

282 
fillColor=None, strokeColor=colors.black)) 
1675
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

283 
for x in range(16): 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

284 
for y in range(16): 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

285 
charValue = y * 16 + x 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

286 
if charValue > 32: 
1679
e77be1edcd61
Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents:
1675
diff
changeset

287 
s = String(self.x + x * dx, 
e77be1edcd61
Fixed page stream filter order (Thanks Bernhard)
andy_robinson
parents:
1675
diff
changeset

288 
self.y + (self.height  y*dy), chr(charValue)) 
1675
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

289 
g.add(s) 
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

290 
return g 
1683  291 

1675
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

292 

92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

293 

1683  294 

1675
92d3b3c2397d
Fixed standard fonts demo, added a basic code page graphic
andy_robinson
parents:
1378
diff
changeset

295 

1683  296 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

297 
def test(): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

298 
c = Canvas('codecharts.pdf') 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

299 
c.setFont('HelveticaBold', 24) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

300 
c.drawString(72, 750, 'Testing code page charts') 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

301 
cc1 = SingleByteEncodingChart() 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

302 
cc1.drawOn(c, 72, 500) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

303 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

304 
cc2 = SingleByteEncodingChart(charsPerRow=32) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

305 
cc2.drawOn(c, 72, 300) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

306 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

307 
cc3 = SingleByteEncodingChart(charsPerRow=25, hex=0) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

308 
cc3.drawOn(c, 72, 100) 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

309 

2192  310 
## c.showPage() 
311 
## 

312 
## c.setFont('HelveticaBold', 24) 

313 
## c.drawString(72, 750, 'Multibyte Kuten code chart examples') 

314 
## KutenRowCodeChart(1, 'HeiseiMinW3','EUCH').drawOn(c, 72, 600) 

315 
## KutenRowCodeChart(16, 'HeiseiMinW3','EUCH').drawOn(c, 72, 450) 

316 
## KutenRowCodeChart(84, 'HeiseiMinW3','EUCH').drawOn(c, 72, 300) 

317 
## 

318 
## c.showPage() 

319 
## c.setFont('HelveticaBold', 24) 

320 
## c.drawString(72, 750, 'Big5 Code Chart Examples') 

321 
## #Big5CodeChart(0xA1, 'MSungStdLightAcro','ETenmsB5H').drawOn(c, 72, 500) 

1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

322 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

323 
c.save() 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

324 
print 'saved codecharts.pdf' 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

325 

35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

326 
if __name__=='__main__': 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

327 
test() 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

328 

1683  329 