home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / PIL / FontFile.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.5 KB  |  111 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.         '''Create metrics and bitmap'''
  39.         if self.bitmap:
  40.             return None
  41.         lines = 1
  42.         for glyph in self:
  43.             if glyph:
  44.                 (d, dst, src, im) = glyph
  45.                 h = max(h, src[3] - src[1])
  46.                 w = w + (src[2] - src[0])
  47.                 maxwidth = max(maxwidth, w)
  48.                 continue
  49.             None if w > WIDTH else self.bitmap
  50.         
  51.         xsize = maxwidth
  52.         ysize = lines * h
  53.         if xsize == 0 and ysize == 0:
  54.             return ''
  55.         self.ysize = h
  56.         self.bitmap = Image.new('1', (xsize, ysize))
  57.         self.metrics = [
  58.             None] * 256
  59.         for i in range(256):
  60.             glyph = self[i]
  61.             if glyph:
  62.                 (d, dst, src, im) = glyph
  63.                 xx = src[2] - src[0]
  64.                 yy = src[3] - src[1]
  65.                 x0 = x
  66.                 y0 = y
  67.                 x = x + xx
  68.                 s = (src[0] + x0, src[1] + y0, src[2] + x0, src[3] + y0)
  69.                 self.bitmap.paste(im.crop(src), s)
  70.                 self.metrics[i] = (d, dst, s)
  71.                 continue
  72.             None if x > WIDTH else ysize == 0
  73.         
  74.  
  75.     
  76.     def save1(self, filename):
  77.         '''Save font in version 1 format'''
  78.         self.compile()
  79.         self.bitmap.save(os.path.splitext(filename)[0] + '.pbm', 'PNG')
  80.         fp = open(os.path.splitext(filename)[0] + '.pil', 'wb')
  81.         fp.write('PILfont\n')
  82.         fp.write(';;;;;;%d;\n' % self.ysize)
  83.         fp.write('DATA\n')
  84.         for id in range(256):
  85.             m = self.metrics[id]
  86.             if not m:
  87.                 puti16(fp, [
  88.                     0] * 10)
  89.                 continue
  90.             puti16(fp, m[0] + m[1] + m[2])
  91.         
  92.         fp.close()
  93.  
  94.     
  95.     def save2(self, filename):
  96.         '''Save font in version 2 format'''
  97.         self.compile()
  98.         data = marshal.dumps((self.metrics, self.info))
  99.         if zlib:
  100.             data = 'z' + zlib.compress(data, 9)
  101.         else:
  102.             data = 'u' + data
  103.         fp = open(os.path.splitext(filename)[0] + '.pil', 'wb')
  104.         fp.write('PILfont2\n' + self.name + '\n' + 'DATA\n')
  105.         fp.write(data)
  106.         self.bitmap.save(fp, 'PNG')
  107.         fp.close()
  108.  
  109.     save = save1
  110.  
  111.