author  robin 
Tue, 19 Nov 2013 13:50:34 +0000  
branch  py33 
changeset 3794  398ea04239b5 
parent 3721  0c93dd8ff567 
child 3796  c9a86549dee7 
permissions  rwrr 
3617  1 
#Copyright ReportLab Europe Ltd. 20002012 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

2 
#see license.txt for license details 
2332  3 
#history http://www.reportlab.co.uk/cgibin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/codecharts.py 
1361
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 ''' 
3029  6 
__doc__="""Routines to print code page (character set) drawings. Predates unicode. 
1361
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. """ 
2619  12 
import codecs 
1361
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 

2619  21 
adobe2codec = { 
22 
'WinAnsiEncoding':'winansi', 

23 
'MacRomanEncoding':'macroman', 

24 
'MacExpert':'macexpert', 

25 
'PDFDoc':'pdfdoc', 

26 

27 
} 

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

28 

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

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

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

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

32 
into boxes.""" 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

33 

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

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

35 
"Work out x and y positions for drawing" 
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 

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

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

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

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

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

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

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

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

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

46 

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

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

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

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

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

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

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

53 
self.xlist.append(col * self.boxSize) 
1683  54 

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

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

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

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

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

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

60 

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

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

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

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

64 
extraNeeded = (self.rows * self.charsPerRow  len(charList)) 
2192  65 
for i in range(extraNeeded): 
66 
charList.append(None) 

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

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

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

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

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

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

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

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

74 
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

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

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

77 
else: 
2337  78 
try: 
79 
self.canv.drawCentredString( 

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

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

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

83 
) 
2337  84 
except: 
85 
self.canv.setFillGray(0.9) 

86 
self.canv.rect((1+col) * self.boxSize, (self.rows  row  1) * self.boxSize, 

87 
self.boxSize, self.boxSize, stroke=0, fill=1) 

88 
self.canv.drawCentredString( 

89 
(col+1.5) * self.boxSize, 

90 
(self.rows  row  0.875) * self.boxSize, 

91 
'?', 

92 
) 

93 
self.canv.setFillGray(0.0) 

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

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

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

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

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

98 

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

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

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

102 
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

103 
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

104 
self.canv.setFillGray(0.0) 
1683  105 

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

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

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

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

109 
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

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

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

112 
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

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

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

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

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

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

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

121 
self.formatByte(col) 
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 

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

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

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

128 
topLeft 
1362  129 
) 
1683  130 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

146 

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

147 
self.calcLayout() 
1683  148 

149 

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

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

151 
self.drawLabels() 
3721  152 
charList = [None] * 32 + list(map(chr, list(range(32, 256)))) 
2619  153 

154 
#we need to convert these to Unicode, since ReportLab 

155 
#2.0 can only draw in Unicode. 

156 

157 
encName = self.encodingName 

158 
#apply some common translations 

159 
encName = adobe2codec.get(encName, encName) 

160 
decoder = codecs.lookup(encName)[1] 

161 
def decodeFunc(txt): 

162 
if txt is None: 

163 
return None 

164 
else: 

165 
return decoder(txt, errors='replace')[0] 

166 

167 
charList = [decodeFunc(ch) for ch in charList] 

168 

169 

170 

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

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

172 
self.canv.grid(self.xlist, self.ylist) 
1683  173 

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

174 

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

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

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

177 

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

178 
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

179 
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

180 
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

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

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

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

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

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

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

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

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

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

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

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

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

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

194 

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

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

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

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

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

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

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

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

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

203 
self.fontName = self.faceName + '' + self.encodingName 
1683  204 
self.calcLayout() 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

205 

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

206 
def makeRow(self, row): 
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

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

208 
cells = [] 
3794  209 
if self.encodingName.find('EUC') > 1: 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

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

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

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

213 
cells.append(ch) 
3794  214 
## elif self.encodingName.find('GB') > 1: 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

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

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

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

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

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

220 
return cells 
1683  221 

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

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

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

224 

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

225 
# work out which characters we need for the row 
3794  226 
#assert self.encodingName.find('EUC') > 1, 'Only handles EUC encoding today, you gave me %s!' % self.encodingName 
1683  227 

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

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

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

231 
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

232 

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

233 

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

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

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

236 

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

237 
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

238 
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

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

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

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

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

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

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

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

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

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

248 
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

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

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

251 
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

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

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

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

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

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

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

259 

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

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

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

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

263 
cells = [] 
3794  264 
if self.encodingName.find('B5') > 1: 
1363
92e3fb40a784
Asian fonts now all working in major encodings, and partly documented
andy_robinson
parents:
1362
diff
changeset

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

266 
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

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

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

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

270 
cells.append(ch) 
1683  271 

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

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

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

274 
return cells 
1683  275 

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

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

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

278 

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

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

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

282 

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

283 

2630  284 
def hBoxText(msg, canvas, x, y, fontName): 
1378
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

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

286 

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

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

288 
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

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

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

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

292 
except KeyError: 
2630  293 
font = cidfonts.UnicodeCIDFont(fontName) 
1378
796ce78d458a
Fixed font registry entries, tidied up stringWidth tests
andy_robinson
parents:
1363
diff
changeset

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

295 

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

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

297 
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

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

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

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

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

302 

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

303 

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

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

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

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

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

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

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

310 
self.height = 160 
1683  311 

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

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

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

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

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

316 
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

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

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

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

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

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

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

323 
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

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

325 
return g 
1683  326 

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

327 

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

328 

1683  329 

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

330 

1683  331 

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

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

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

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

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

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

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

338 

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

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

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

341 

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

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

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

344 

2192  345 
## c.showPage() 
346 
## 

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

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

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

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

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

352 
## 

353 
## c.showPage() 

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

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

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

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

357 

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

358 
c.save() 
3721  359 
print('saved codecharts.pdf') 
1361
35586d769319
Enhancements/fixes to Asian fonts; codecharts utility to
andy_robinson
parents:
diff
changeset

360 

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

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

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

363 

1683  364 