home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2130 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.2 KB  |  108 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import Image
  6. import marshal
  7.  
  8. try:
  9.     import zlib
  10. except ImportError:
  11.     zlib = None
  12.  
  13. WIDTH = 800
  14.  
  15. def puti16(fp, values):
  16.     for v in values:
  17.         if v < 0:
  18.             v = v + 65536
  19.         
  20.         fp.write(chr(v >> 8 & 255) + chr(v & 255))
  21.     
  22.  
  23.  
  24. class FontFile:
  25.     bitmap = None
  26.     
  27.     def __init__(self):
  28.         self.info = { }
  29.         self.glyph = [
  30.             None] * 256
  31.  
  32.     
  33.     def __getitem__(self, ix):
  34.         return self.glyph[ix]
  35.  
  36.     
  37.     def compile(self):
  38.         if self.bitmap:
  39.             return None
  40.         lines = 1
  41.         for glyph in self:
  42.             if glyph:
  43.                 (d, dst, src, im) = glyph
  44.                 h = max(h, src[3] - src[1])
  45.                 w = w + (src[2] - src[0])
  46.                 maxwidth = max(maxwidth, w)
  47.                 continue
  48.             None if w > WIDTH else self.bitmap
  49.         
  50.         xsize = maxwidth
  51.         ysize = lines * h
  52.         if xsize == 0 and ysize == 0:
  53.             return ''
  54.         self.ysize = h
  55.         self.bitmap = Image.new('1', (xsize, ysize))
  56.         self.metrics = [
  57.             None] * 256
  58.         for i in range(256):
  59.             glyph = self[i]
  60.             if glyph:
  61.                 (d, dst, src, im) = glyph
  62.                 xx = src[2] - src[0]
  63.                 yy = src[3] - src[1]
  64.                 x0 = x
  65.                 y0 = y
  66.                 x = x + xx
  67.                 s = (src[0] + x0, src[1] + y0, src[2] + x0, src[3] + y0)
  68.                 self.bitmap.paste(im.crop(src), s)
  69.                 self.metrics[i] = (d, dst, s)
  70.                 continue
  71.             None if x > WIDTH else ysize == 0
  72.         
  73.  
  74.     
  75.     def save1(self, filename):
  76.         self.compile()
  77.         self.bitmap.save(os.path.splitext(filename)[0] + '.pbm', 'PNG')
  78.         fp = open(os.path.splitext(filename)[0] + '.pil', 'wb')
  79.         fp.write('PILfont\n')
  80.         fp.write(';;;;;;%d;\n' % self.ysize)
  81.         fp.write('DATA\n')
  82.         for id in range(256):
  83.             m = self.metrics[id]
  84.             if not m:
  85.                 puti16(fp, [
  86.                     0] * 10)
  87.                 continue
  88.             puti16(fp, m[0] + m[1] + m[2])
  89.         
  90.         fp.close()
  91.  
  92.     
  93.     def save2(self, filename):
  94.         self.compile()
  95.         data = marshal.dumps((self.metrics, self.info))
  96.         if zlib:
  97.             data = 'z' + zlib.compress(data, 9)
  98.         else:
  99.             data = 'u' + data
  100.         fp = open(os.path.splitext(filename)[0] + '.pil', 'wb')
  101.         fp.write('PILfont2\n' + self.name + '\n' + 'DATA\n')
  102.         fp.write(data)
  103.         self.bitmap.save(fp, 'PNG')
  104.         fp.close()
  105.  
  106.     save = save1
  107.  
  108.