codecharts.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #Copyright ReportLab Europe Ltd. 2000-2017
  2. #see license.txt for license details
  3. #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/lib/codecharts.py
  4. #$Header $
  5. __version__='3.3.0'
  6. __doc__="""Routines to print code page (character set) drawings. Predates unicode.
  7. To be sure we can accurately represent characters in various encodings
  8. and fonts, we need some routines to display all those characters.
  9. These are defined herein. The idea is to include flowable, drawable
  10. and graphic objects for single and multi-byte fonts. """
  11. import codecs
  12. from reportlab.pdfgen.canvas import Canvas
  13. from reportlab.platypus import Flowable
  14. from reportlab.pdfbase import pdfmetrics, cidfonts
  15. from reportlab.graphics.shapes import Drawing, Group, String, Circle, Rect
  16. from reportlab.graphics.widgetbase import Widget
  17. from reportlab.lib import colors
  18. from reportlab.lib.utils import int2Byte
  19. adobe2codec = {
  20. 'WinAnsiEncoding':'winansi',
  21. 'MacRomanEncoding':'macroman',
  22. 'MacExpert':'macexpert',
  23. 'PDFDoc':'pdfdoc',
  24. }
  25. class CodeChartBase(Flowable):
  26. """Basic bits of drawing furniture used by
  27. single and multi-byte versions: ability to put letters
  28. into boxes."""
  29. def calcLayout(self):
  30. "Work out x and y positions for drawing"
  31. rows = self.codePoints * 1.0 / self.charsPerRow
  32. if rows == int(rows):
  33. self.rows = int(rows)
  34. else:
  35. self.rows = int(rows) + 1
  36. # size allows for a gray column of labels
  37. self.width = self.boxSize * (1+self.charsPerRow)
  38. self.height = self.boxSize * (1+self.rows)
  39. #handy lists
  40. self.ylist = []
  41. for row in range(self.rows + 2):
  42. self.ylist.append(row * self.boxSize)
  43. self.xlist = []
  44. for col in range(self.charsPerRow + 2):
  45. self.xlist.append(col * self.boxSize)
  46. def formatByte(self, byt):
  47. if self.hex:
  48. return '%02X' % byt
  49. else:
  50. return '%d' % byt
  51. def drawChars(self, charList):
  52. """Fills boxes in order. None means skip a box.
  53. Empty boxes at end get filled with gray"""
  54. extraNeeded = (self.rows * self.charsPerRow - len(charList))
  55. for i in range(extraNeeded):
  56. charList.append(None)
  57. #charList.extend([None] * extraNeeded)
  58. row = 0
  59. col = 0
  60. self.canv.setFont(self.fontName, self.boxSize * 0.75)
  61. for ch in charList: # may be 2 bytes or 1
  62. if ch is None:
  63. self.canv.setFillGray(0.9)
  64. self.canv.rect((1+col) * self.boxSize, (self.rows - row - 1) * self.boxSize,
  65. self.boxSize, self.boxSize, stroke=0, fill=1)
  66. self.canv.setFillGray(0.0)
  67. else:
  68. try:
  69. self.canv.drawCentredString(
  70. (col+1.5) * self.boxSize,
  71. (self.rows - row - 0.875) * self.boxSize,
  72. ch,
  73. )
  74. except:
  75. self.canv.setFillGray(0.9)
  76. self.canv.rect((1+col) * self.boxSize, (self.rows - row - 1) * self.boxSize,
  77. self.boxSize, self.boxSize, stroke=0, fill=1)
  78. self.canv.drawCentredString(
  79. (col+1.5) * self.boxSize,
  80. (self.rows - row - 0.875) * self.boxSize,
  81. '?',
  82. )
  83. self.canv.setFillGray(0.0)
  84. col = col + 1
  85. if col == self.charsPerRow:
  86. row = row + 1
  87. col = 0
  88. def drawLabels(self, topLeft = ''):
  89. """Writes little labels in the top row and first column"""
  90. self.canv.setFillGray(0.8)
  91. self.canv.rect(0, self.ylist[-2], self.width, self.boxSize, fill=1, stroke=0)
  92. self.canv.rect(0, 0, self.boxSize, self.ylist[-2], fill=1, stroke=0)
  93. self.canv.setFillGray(0.0)
  94. #label each row and column
  95. self.canv.setFont('Helvetica-Oblique',0.375 * self.boxSize)
  96. byt = 0
  97. for row in range(self.rows):
  98. if self.rowLabels:
  99. label = self.rowLabels[row]
  100. else: # format start bytes as hex or decimal
  101. label = self.formatByte(row * self.charsPerRow)
  102. self.canv.drawCentredString(0.5 * self.boxSize,
  103. (self.rows - row - 0.75) * self.boxSize,
  104. label
  105. )
  106. for col in range(self.charsPerRow):
  107. self.canv.drawCentredString((col + 1.5) * self.boxSize,
  108. (self.rows + 0.25) * self.boxSize,
  109. self.formatByte(col)
  110. )
  111. if topLeft:
  112. self.canv.setFont('Helvetica-BoldOblique',0.5 * self.boxSize)
  113. self.canv.drawCentredString(0.5 * self.boxSize,
  114. (self.rows + 0.25) * self.boxSize,
  115. topLeft
  116. )
  117. class SingleByteEncodingChart(CodeChartBase):
  118. def __init__(self, faceName='Helvetica', encodingName='WinAnsiEncoding',
  119. charsPerRow=16, boxSize=14, hex=1):
  120. self.codePoints = 256
  121. self.faceName = faceName
  122. self.encodingName = encodingName
  123. self.fontName = self.faceName + '-' + self.encodingName
  124. self.charsPerRow = charsPerRow
  125. self.boxSize = boxSize
  126. self.hex = hex
  127. self.rowLabels = None
  128. pdfmetrics.registerFont(pdfmetrics.Font(self.fontName,
  129. self.faceName,
  130. self.encodingName)
  131. )
  132. self.calcLayout()
  133. def draw(self):
  134. self.drawLabels()
  135. charList = [None] * 32 + list(map(int2Byte, list(range(32, 256))))
  136. #we need to convert these to Unicode, since ReportLab
  137. #2.0 can only draw in Unicode.
  138. encName = self.encodingName
  139. #apply some common translations
  140. encName = adobe2codec.get(encName, encName)
  141. decoder = codecs.lookup(encName)[1]
  142. def decodeFunc(txt):
  143. if txt is None:
  144. return None
  145. else:
  146. return decoder(txt, errors='replace')[0]
  147. charList = [decodeFunc(ch) for ch in charList]
  148. self.drawChars(charList)
  149. self.canv.grid(self.xlist, self.ylist)
  150. class KutenRowCodeChart(CodeChartBase):
  151. """Formats one 'row' of the 94x94 space used in many Asian encodings.aliases
  152. These deliberately resemble the code charts in Ken Lunde's "Understanding
  153. CJKV Information Processing", to enable manual checking. Due to the large
  154. numbers of characters, we don't try to make one graphic with 10,000 characters,
  155. but rather output a sequence of these."""
  156. #would be cleaner if both shared one base class whose job
  157. #was to draw the boxes, but never mind...
  158. def __init__(self, row, faceName, encodingName):
  159. self.row = row
  160. self.codePoints = 94
  161. self.boxSize = 18
  162. self.charsPerRow = 20
  163. self.rows = 5
  164. self.rowLabels = ['00','20','40','60','80']
  165. self.hex = 0
  166. self.faceName = faceName
  167. self.encodingName = encodingName
  168. try:
  169. # the dependent files might not be available
  170. font = cidfonts.CIDFont(self.faceName, self.encodingName)
  171. pdfmetrics.registerFont(font)
  172. except:
  173. # fall back to English and at least shwo we can draw the boxes
  174. self.faceName = 'Helvetica'
  175. self.encodingName = 'WinAnsiEncoding'
  176. self.fontName = self.faceName + '-' + self.encodingName
  177. self.calcLayout()
  178. def makeRow(self, row):
  179. """Works out the character values for this kuten row"""
  180. cells = []
  181. if self.encodingName.find('EUC') > -1:
  182. # it is an EUC family encoding.
  183. for col in range(1, 95):
  184. ch = int2Byte(row + 160) + int2Byte(col+160)
  185. cells.append(ch)
  186. ## elif self.encodingName.find('GB') > -1:
  187. ## # it is an EUC family encoding.
  188. ## for col in range(1, 95):
  189. ## ch = int2Byte(row + 160) + int2Byte(col+160)
  190. else:
  191. cells.append([None] * 94)
  192. return cells
  193. def draw(self):
  194. self.drawLabels(topLeft= 'R%d' % self.row)
  195. # work out which characters we need for the row
  196. #assert self.encodingName.find('EUC') > -1, 'Only handles EUC encoding today, you gave me %s!' % self.encodingName
  197. # pad out by 1 to match Ken Lunde's tables
  198. charList = [None] + self.makeRow(self.row)
  199. self.drawChars(charList)
  200. self.canv.grid(self.xlist, self.ylist)
  201. class Big5CodeChart(CodeChartBase):
  202. """Formats one 'row' of the 94x160 space used in Big 5
  203. These deliberately resemble the code charts in Ken Lunde's "Understanding
  204. CJKV Information Processing", to enable manual checking."""
  205. def __init__(self, row, faceName, encodingName):
  206. self.row = row
  207. self.codePoints = 160
  208. self.boxSize = 18
  209. self.charsPerRow = 16
  210. self.rows = 10
  211. self.hex = 1
  212. self.faceName = faceName
  213. self.encodingName = encodingName
  214. self.rowLabels = ['4','5','6','7','A','B','C','D','E','F']
  215. try:
  216. # the dependent files might not be available
  217. font = cidfonts.CIDFont(self.faceName, self.encodingName)
  218. pdfmetrics.registerFont(font)
  219. except:
  220. # fall back to English and at least shwo we can draw the boxes
  221. self.faceName = 'Helvetica'
  222. self.encodingName = 'WinAnsiEncoding'
  223. self.fontName = self.faceName + '-' + self.encodingName
  224. self.calcLayout()
  225. def makeRow(self, row):
  226. """Works out the character values for this Big5 row.
  227. Rows start at 0xA1"""
  228. cells = []
  229. if self.encodingName.find('B5') > -1:
  230. # big 5, different row size
  231. for y in [4,5,6,7,10,11,12,13,14,15]:
  232. for x in range(16):
  233. col = y*16+x
  234. ch = int2Byte(row) + int2Byte(col)
  235. cells.append(ch)
  236. else:
  237. cells.append([None] * 160)
  238. return cells
  239. def draw(self):
  240. self.drawLabels(topLeft='%02X' % self.row)
  241. charList = self.makeRow(self.row)
  242. self.drawChars(charList)
  243. self.canv.grid(self.xlist, self.ylist)
  244. def hBoxText(msg, canvas, x, y, fontName):
  245. """Helper for stringwidth tests on Asian fonts.
  246. Registers font if needed. Then draws the string,
  247. and a box around it derived from the stringWidth function"""
  248. canvas.saveState()
  249. try:
  250. font = pdfmetrics.getFont(fontName)
  251. except KeyError:
  252. font = cidfonts.UnicodeCIDFont(fontName)
  253. pdfmetrics.registerFont(font)
  254. canvas.setFillGray(0.8)
  255. canvas.rect(x,y,pdfmetrics.stringWidth(msg, fontName, 16),16,stroke=0,fill=1)
  256. canvas.setFillGray(0)
  257. canvas.setFont(fontName, 16,16)
  258. canvas.drawString(x,y,msg)
  259. canvas.restoreState()
  260. class CodeWidget(Widget):
  261. """Block showing all the characters"""
  262. def __init__(self):
  263. self.x = 0
  264. self.y = 0
  265. self.width = 160
  266. self.height = 160
  267. def draw(self):
  268. dx = self.width / 16.0
  269. dy = self.height / 16.0
  270. g = Group()
  271. g.add(Rect(self.x, self.y, self.width, self.height,
  272. fillColor=None, strokeColor=colors.black))
  273. for x in range(16):
  274. for y in range(16):
  275. charValue = y * 16 + x
  276. if charValue > 32:
  277. s = String(self.x + x * dx,
  278. self.y + (self.height - y*dy), int2Byte(charValue))
  279. g.add(s)
  280. return g
  281. def test():
  282. c = Canvas('codecharts.pdf')
  283. c.setFont('Helvetica-Bold', 24)
  284. c.drawString(72, 750, 'Testing code page charts')
  285. cc1 = SingleByteEncodingChart()
  286. cc1.drawOn(c, 72, 500)
  287. cc2 = SingleByteEncodingChart(charsPerRow=32)
  288. cc2.drawOn(c, 72, 300)
  289. cc3 = SingleByteEncodingChart(charsPerRow=25, hex=0)
  290. cc3.drawOn(c, 72, 100)
  291. ## c.showPage()
  292. ##
  293. ## c.setFont('Helvetica-Bold', 24)
  294. ## c.drawString(72, 750, 'Multi-byte Kuten code chart examples')
  295. ## KutenRowCodeChart(1, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 600)
  296. ## KutenRowCodeChart(16, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 450)
  297. ## KutenRowCodeChart(84, 'HeiseiMin-W3','EUC-H').drawOn(c, 72, 300)
  298. ##
  299. ## c.showPage()
  300. ## c.setFont('Helvetica-Bold', 24)
  301. ## c.drawString(72, 750, 'Big5 Code Chart Examples')
  302. ## #Big5CodeChart(0xA1, 'MSungStd-Light-Acro','ETenms-B5-H').drawOn(c, 72, 500)
  303. c.save()
  304. print('saved codecharts.pdf')
  305. if __name__=='__main__':
  306. test()