Skip to content

Instantly share code, notes, and snippets.

@qistoph
Last active May 3, 2019 09:29
Show Gist options
  • Save qistoph/5e93a8aff5680f29a79dd9b897ec14ff to your computer and use it in GitHub Desktop.
Save qistoph/5e93a8aff5680f29a79dd9b897ec14ff to your computer and use it in GitHub Desktop.
Character table generator
encodings = ['cp437', 'iso-8859-15', 'windows-1252']
def chr_str(ch, show_class = False, name = ''):
    outstr = ""
    if ch == '':
        outstr += " NA "
        if show_class:
            outstr += "(--) "
    else:
        cls = unicodedata.category(ch)
        if cls == 'Cc':
            outstr += "{0:3s} ".format(name)
        else:
            outstr += " {0}  ".format(ch)
        if show_class:
            outstr += "("+cls+") "
    return outstr
import unicodedata
import curses.ascii

def get_row(n):
    cu = chr(n)

    outstr = chr_str(cu)

    for idx, enc in enumerate(encodings):
        cv = bytes([n]).decode(enc, errors="ignore")
        name = ''
        if enc == 'cp437' and n < len(curses.ascii.controlnames):
            name = curses.ascii.controlnames[n]
        outstr += chr_str(cv, name=name)
        same[idx] = same[idx] and (cv == cu)

    return "{0:3d} {0:2x} {0:3o} {1}".format(n, outstr)

print("dec hx oct ")
same = [True] * len(encodings)
for i in range(0, 256):
    print(get_row(i))
dec hx oct 
  0  0   0     NUL         
  1  1   1     SOH         
  2  2   2     STX         
  3  3   3     ETX         
  4  4   4     EOT         
  5  5   5     ENQ         
  6  6   6     ACK         
  7  7   7     BEL         
  8  8  10     BS          
  9  9  11     HT          
 10  a  12     LF          
 11  b  13     VT          
 12  c  14     FF          
 13  d  15     CR          
 14  e  16     SO          
 15  f  17     SI          
 16 10  20     DLE         
 17 11  21     DC1         
 18 12  22     DC2         
 19 13  23     DC3         
 20 14  24     DC4         
 21 15  25     NAK         
 22 16  26     SYN         
 23 17  27     ETB         
 24 18  30     CAN         
 25 19  31     EM          
 26 1a  32     SUB         
 27 1b  33     ESC         
 28 1c  34     FS          
 29 1d  35     GS          
 30 1e  36     RS          
 31 1f  37     US          
 32 20  40                 
 33 21  41  !   !   !   !  
 34 22  42  "   "   "   "  
 35 23  43  #   #   #   #  
 36 24  44  $   $   $   $  
 37 25  45  %   %   %   %  
 38 26  46  &   &   &   &  
 39 27  47  '   '   '   '  
 40 28  50  (   (   (   (  
 41 29  51  )   )   )   )  
 42 2a  52  *   *   *   *  
 43 2b  53  +   +   +   +  
 44 2c  54  ,   ,   ,   ,  
 45 2d  55  -   -   -   -  
 46 2e  56  .   .   .   .  
 47 2f  57  /   /   /   /  
 48 30  60  0   0   0   0  
 49 31  61  1   1   1   1  
 50 32  62  2   2   2   2  
 51 33  63  3   3   3   3  
 52 34  64  4   4   4   4  
 53 35  65  5   5   5   5  
 54 36  66  6   6   6   6  
 55 37  67  7   7   7   7  
 56 38  70  8   8   8   8  
 57 39  71  9   9   9   9  
 58 3a  72  :   :   :   :  
 59 3b  73  ;   ;   ;   ;  
 60 3c  74  <   <   <   <  
 61 3d  75  =   =   =   =  
 62 3e  76  >   >   >   >  
 63 3f  77  ?   ?   ?   ?  
 64 40 100  @   @   @   @  
 65 41 101  A   A   A   A  
 66 42 102  B   B   B   B  
 67 43 103  C   C   C   C  
 68 44 104  D   D   D   D  
 69 45 105  E   E   E   E  
 70 46 106  F   F   F   F  
 71 47 107  G   G   G   G  
 72 48 110  H   H   H   H  
 73 49 111  I   I   I   I  
 74 4a 112  J   J   J   J  
 75 4b 113  K   K   K   K  
 76 4c 114  L   L   L   L  
 77 4d 115  M   M   M   M  
 78 4e 116  N   N   N   N  
 79 4f 117  O   O   O   O  
 80 50 120  P   P   P   P  
 81 51 121  Q   Q   Q   Q  
 82 52 122  R   R   R   R  
 83 53 123  S   S   S   S  
 84 54 124  T   T   T   T  
 85 55 125  U   U   U   U  
 86 56 126  V   V   V   V  
 87 57 127  W   W   W   W  
 88 58 130  X   X   X   X  
 89 59 131  Y   Y   Y   Y  
 90 5a 132  Z   Z   Z   Z  
 91 5b 133  [   [   [   [  
 92 5c 134  \   \   \   \  
 93 5d 135  ]   ]   ]   ]  
 94 5e 136  ^   ^   ^   ^  
 95 5f 137  _   _   _   _  
 96 60 140  `   `   `   `  
 97 61 141  a   a   a   a  
 98 62 142  b   b   b   b  
 99 63 143  c   c   c   c  
100 64 144  d   d   d   d  
101 65 145  e   e   e   e  
102 66 146  f   f   f   f  
103 67 147  g   g   g   g  
104 68 150  h   h   h   h  
105 69 151  i   i   i   i  
106 6a 152  j   j   j   j  
107 6b 153  k   k   k   k  
108 6c 154  l   l   l   l  
109 6d 155  m   m   m   m  
110 6e 156  n   n   n   n  
111 6f 157  o   o   o   o  
112 70 160  p   p   p   p  
113 71 161  q   q   q   q  
114 72 162  r   r   r   r  
115 73 163  s   s   s   s  
116 74 164  t   t   t   t  
117 75 165  u   u   u   u  
118 76 166  v   v   v   v  
119 77 167  w   w   w   w  
120 78 170  x   x   x   x  
121 79 171  y   y   y   y  
122 7a 172  z   z   z   z  
123 7b 173  {   {   {   {  
124 7c 174  |   |   |   |  
125 7d 175  }   }   }   }  
126 7e 176  ~   ~   ~   ~  
127 7f 177                 
128 80 200      Ç       €  
129 81 201      ü       NA 
130 82 202      é       ‚  
131 83 203      â       ƒ  
132 84 204      ä       „  
133 85 205      à       …  
134 86 206      å       †  
135 87 207      ç       ‡  
136 88 210      ê       ˆ  
137 89 211      ë       ‰  
138 8a 212      è       Š  
139 8b 213      ï       ‹  
140 8c 214      î       Œ  
141 8d 215      ì       NA 
142 8e 216      Ä       Ž  
143 8f 217      Å       NA 
144 90 220      É       NA 
145 91 221      æ       ‘  
146 92 222      Æ       ’  
147 93 223      ô       “  
148 94 224      ö       ”  
149 95 225      ò       •  
150 96 226      û       –  
151 97 227      ù       —  
152 98 230      ÿ       ˜  
153 99 231      Ö       ™  
154 9a 232      Ü       š  
155 9b 233      ¢       ›  
156 9c 234      £       œ  
157 9d 235      ¥       NA 
158 9e 236      ₧       ž  
159 9f 237      ƒ       Ÿ  
160 a0 240      á          
161 a1 241  ¡   í   ¡   ¡  
162 a2 242  ¢   ó   ¢   ¢  
163 a3 243  £   ú   £   £  
164 a4 244  ¤   ñ   €   ¤  
165 a5 245  ¥   Ñ   ¥   ¥  
166 a6 246  ¦   ª   Š   ¦  
167 a7 247  §   º   §   §  
168 a8 250  ¨   ¿   š   ¨  
169 a9 251  ©   ⌐   ©   ©  
170 aa 252  ª   ¬   ª   ª  
171 ab 253  «   ½   «   «  
172 ac 254  ¬   ¼   ¬   ¬  
173 ad 255  ­   ¡   ­   ­  
174 ae 256  ®   «   ®   ®  
175 af 257  ¯   »   ¯   ¯  
176 b0 260  °   ░   °   °  
177 b1 261  ±   ▒   ±   ±  
178 b2 262  ²   ▓   ²   ²  
179 b3 263  ³   │   ³   ³  
180 b4 264  ´   ┤   Ž   ´  
181 b5 265  µ   ╡   µ   µ  
182 b6 266  ¶   ╢   ¶   ¶  
183 b7 267  ·   ╖   ·   ·  
184 b8 270  ¸   ╕   ž   ¸  
185 b9 271  ¹   ╣   ¹   ¹  
186 ba 272  º   ║   º   º  
187 bb 273  »   ╗   »   »  
188 bc 274  ¼   ╝   Œ   ¼  
189 bd 275  ½   ╜   œ   ½  
190 be 276  ¾   ╛   Ÿ   ¾  
191 bf 277  ¿   ┐   ¿   ¿  
192 c0 300  À   └   À   À  
193 c1 301  Á   ┴   Á   Á  
194 c2 302  Â   ┬   Â   Â  
195 c3 303  Ã   ├   Ã   Ã  
196 c4 304  Ä   ─   Ä   Ä  
197 c5 305  Å   ┼   Å   Å  
198 c6 306  Æ   ╞   Æ   Æ  
199 c7 307  Ç   ╟   Ç   Ç  
200 c8 310  È   ╚   È   È  
201 c9 311  É   ╔   É   É  
202 ca 312  Ê   ╩   Ê   Ê  
203 cb 313  Ë   ╦   Ë   Ë  
204 cc 314  Ì   ╠   Ì   Ì  
205 cd 315  Í   ═   Í   Í  
206 ce 316  Î   ╬   Î   Î  
207 cf 317  Ï   ╧   Ï   Ï  
208 d0 320  Ð   ╨   Ð   Ð  
209 d1 321  Ñ   ╤   Ñ   Ñ  
210 d2 322  Ò   ╥   Ò   Ò  
211 d3 323  Ó   ╙   Ó   Ó  
212 d4 324  Ô   ╘   Ô   Ô  
213 d5 325  Õ   ╒   Õ   Õ  
214 d6 326  Ö   ╓   Ö   Ö  
215 d7 327  ×   ╫   ×   ×  
216 d8 330  Ø   ╪   Ø   Ø  
217 d9 331  Ù   ┘   Ù   Ù  
218 da 332  Ú   ┌   Ú   Ú  
219 db 333  Û   █   Û   Û  
220 dc 334  Ü   ▄   Ü   Ü  
221 dd 335  Ý   ▌   Ý   Ý  
222 de 336  Þ   ▐   Þ   Þ  
223 df 337  ß   ▀   ß   ß  
224 e0 340  à   α   à   à  
225 e1 341  á   ß   á   á  
226 e2 342  â   Γ   â   â  
227 e3 343  ã   π   ã   ã  
228 e4 344  ä   Σ   ä   ä  
229 e5 345  å   σ   å   å  
230 e6 346  æ   µ   æ   æ  
231 e7 347  ç   τ   ç   ç  
232 e8 350  è   Φ   è   è  
233 e9 351  é   Θ   é   é  
234 ea 352  ê   Ω   ê   ê  
235 eb 353  ë   δ   ë   ë  
236 ec 354  ì   ∞   ì   ì  
237 ed 355  í   φ   í   í  
238 ee 356  î   ε   î   î  
239 ef 357  ï   ∩   ï   ï  
240 f0 360  ð   ≡   ð   ð  
241 f1 361  ñ   ±   ñ   ñ  
242 f2 362  ò   ≥   ò   ò  
243 f3 363  ó   ≤   ó   ó  
244 f4 364  ô   ⌠   ô   ô  
245 f5 365  õ   ⌡   õ   õ  
246 f6 366  ö   ÷   ö   ö  
247 f7 367  ÷   ≈   ÷   ÷  
248 f8 370  ø   °   ø   ø  
249 f9 371  ù   ∙   ù   ù  
250 fa 372  ú   ·   ú   ú  
251 fb 373  û   √   û   û  
252 fc 374  ü   ⁿ   ü   ü  
253 fd 375  ý   ²   ý   ý  
254 fe 376  þ   ■   þ   þ  
255 ff 377  ÿ       ÿ   ÿ  
from PIL import Image, ImageDraw, ImageFont
import IPython.display

pages = 2
columns = 4
char_start = 0
char_end = 256
rows_per_column = (char_end-char_start)//pages//columns

#ttf = '/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf'
ttf = '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf'
font = ImageFont.truetype(ttf, 12)
textcolor = (0,0,0,255)

(col_width, _) = font.getsize(get_row(0))
ascent, descent = font.getmetrics()
row_height = ascent+descent
#(width, baseline), (offset_x, offset_y) = font.font.getsize("AsdfjgxQ^")
#print("ascent:", ascent, "descent:", descent, "width:", width, "baseline:",baseline, "offset_x:",offset_x, "offset_y:", offset_y)

def draw_page(pagenr, start, end):
    additional_page_rows = 3 # 1 for header, 2 for footer
    im = Image.new('RGBA', (col_width * columns, (rows_per_column+additional_page_rows)*row_height), (255,255,255,255))

    draw = ImageDraw.Draw(im)

    draw.line((0, row_height, im.size[0], row_height), fill=(32,32,32,128))
        
    for c in range(0, columns):
        x_off = c * col_width
        draw.text((x_off, 1), "dec hx oct  U   C   I   W", font=font, fill=textcolor)
        draw.line((x_off - 4, 0, x_off - 4, row_height*(rows_per_column+1)), fill=(32,32,32,128))

    for i in range(start, end):
        x_off = int((i-start)//rows_per_column) * col_width
        y_off = ((i-start) % rows_per_column + 1) * row_height
        draw.text((x_off, y_off), get_row(i), font=font, fill=textcolor)

    draw.text((1, (rows_per_column+2) * row_height), "U = Unicode / ISO-8895-1 / Latin 1, C = Codepage 437 (DOS Latin US), I = ISO-8895-15, W = Windows-1252", font=font, fill=textcolor)
        
    return im

from IPython.display import display # to display images
for page in range(pages):
    page_size = (char_end - char_start)//pages
    page_start = char_start + page_size * page
    page_end = page_start + page_size
    im_page = draw_page(page, page_start, page_end)
    display(im_page)
    im_page.save(f"page_{page}.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment