PcfFontFile.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #
  2. # THIS IS WORK IN PROGRESS
  3. #
  4. # The Python Imaging Library
  5. # $Id$
  6. #
  7. # portable compiled font file parser
  8. #
  9. # history:
  10. # 1997-08-19 fl created
  11. # 2003-09-13 fl fixed loading of unicode fonts
  12. #
  13. # Copyright (c) 1997-2003 by Secret Labs AB.
  14. # Copyright (c) 1997-2003 by Fredrik Lundh.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18. import io
  19. from . import FontFile, Image
  20. from ._binary import i8, i16be as b16, i16le as l16, i32be as b32, i32le as l32
  21. # --------------------------------------------------------------------
  22. # declarations
  23. PCF_MAGIC = 0x70636601 # "\x01fcp"
  24. PCF_PROPERTIES = 1 << 0
  25. PCF_ACCELERATORS = 1 << 1
  26. PCF_METRICS = 1 << 2
  27. PCF_BITMAPS = 1 << 3
  28. PCF_INK_METRICS = 1 << 4
  29. PCF_BDF_ENCODINGS = 1 << 5
  30. PCF_SWIDTHS = 1 << 6
  31. PCF_GLYPH_NAMES = 1 << 7
  32. PCF_BDF_ACCELERATORS = 1 << 8
  33. BYTES_PER_ROW = [
  34. lambda bits: ((bits + 7) >> 3),
  35. lambda bits: ((bits + 15) >> 3) & ~1,
  36. lambda bits: ((bits + 31) >> 3) & ~3,
  37. lambda bits: ((bits + 63) >> 3) & ~7,
  38. ]
  39. def sz(s, o):
  40. return s[o : s.index(b"\0", o)]
  41. ##
  42. # Font file plugin for the X11 PCF format.
  43. class PcfFontFile(FontFile.FontFile):
  44. name = "name"
  45. def __init__(self, fp):
  46. magic = l32(fp.read(4))
  47. if magic != PCF_MAGIC:
  48. raise SyntaxError("not a PCF file")
  49. FontFile.FontFile.__init__(self)
  50. count = l32(fp.read(4))
  51. self.toc = {}
  52. for i in range(count):
  53. type = l32(fp.read(4))
  54. self.toc[type] = l32(fp.read(4)), l32(fp.read(4)), l32(fp.read(4))
  55. self.fp = fp
  56. self.info = self._load_properties()
  57. metrics = self._load_metrics()
  58. bitmaps = self._load_bitmaps(metrics)
  59. encoding = self._load_encoding()
  60. #
  61. # create glyph structure
  62. for ch in range(256):
  63. ix = encoding[ch]
  64. if ix is not None:
  65. x, y, l, r, w, a, d, f = metrics[ix]
  66. glyph = (w, 0), (l, d - y, x + l, d), (0, 0, x, y), bitmaps[ix]
  67. self.glyph[ch] = glyph
  68. def _getformat(self, tag):
  69. format, size, offset = self.toc[tag]
  70. fp = self.fp
  71. fp.seek(offset)
  72. format = l32(fp.read(4))
  73. if format & 4:
  74. i16, i32 = b16, b32
  75. else:
  76. i16, i32 = l16, l32
  77. return fp, format, i16, i32
  78. def _load_properties(self):
  79. #
  80. # font properties
  81. properties = {}
  82. fp, format, i16, i32 = self._getformat(PCF_PROPERTIES)
  83. nprops = i32(fp.read(4))
  84. # read property description
  85. p = []
  86. for i in range(nprops):
  87. p.append((i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))))
  88. if nprops & 3:
  89. fp.seek(4 - (nprops & 3), io.SEEK_CUR) # pad
  90. data = fp.read(i32(fp.read(4)))
  91. for k, s, v in p:
  92. k = sz(data, k)
  93. if s:
  94. v = sz(data, v)
  95. properties[k] = v
  96. return properties
  97. def _load_metrics(self):
  98. #
  99. # font metrics
  100. metrics = []
  101. fp, format, i16, i32 = self._getformat(PCF_METRICS)
  102. append = metrics.append
  103. if (format & 0xFF00) == 0x100:
  104. # "compressed" metrics
  105. for i in range(i16(fp.read(2))):
  106. left = i8(fp.read(1)) - 128
  107. right = i8(fp.read(1)) - 128
  108. width = i8(fp.read(1)) - 128
  109. ascent = i8(fp.read(1)) - 128
  110. descent = i8(fp.read(1)) - 128
  111. xsize = right - left
  112. ysize = ascent + descent
  113. append((xsize, ysize, left, right, width, ascent, descent, 0))
  114. else:
  115. # "jumbo" metrics
  116. for i in range(i32(fp.read(4))):
  117. left = i16(fp.read(2))
  118. right = i16(fp.read(2))
  119. width = i16(fp.read(2))
  120. ascent = i16(fp.read(2))
  121. descent = i16(fp.read(2))
  122. attributes = i16(fp.read(2))
  123. xsize = right - left
  124. ysize = ascent + descent
  125. append((xsize, ysize, left, right, width, ascent, descent, attributes))
  126. return metrics
  127. def _load_bitmaps(self, metrics):
  128. #
  129. # bitmap data
  130. bitmaps = []
  131. fp, format, i16, i32 = self._getformat(PCF_BITMAPS)
  132. nbitmaps = i32(fp.read(4))
  133. if nbitmaps != len(metrics):
  134. raise IOError("Wrong number of bitmaps")
  135. offsets = []
  136. for i in range(nbitmaps):
  137. offsets.append(i32(fp.read(4)))
  138. bitmapSizes = []
  139. for i in range(4):
  140. bitmapSizes.append(i32(fp.read(4)))
  141. # byteorder = format & 4 # non-zero => MSB
  142. bitorder = format & 8 # non-zero => MSB
  143. padindex = format & 3
  144. bitmapsize = bitmapSizes[padindex]
  145. offsets.append(bitmapsize)
  146. data = fp.read(bitmapsize)
  147. pad = BYTES_PER_ROW[padindex]
  148. mode = "1;R"
  149. if bitorder:
  150. mode = "1"
  151. for i in range(nbitmaps):
  152. x, y, l, r, w, a, d, f = metrics[i]
  153. b, e = offsets[i], offsets[i + 1]
  154. bitmaps.append(Image.frombytes("1", (x, y), data[b:e], "raw", mode, pad(x)))
  155. return bitmaps
  156. def _load_encoding(self):
  157. # map character code to bitmap index
  158. encoding = [None] * 256
  159. fp, format, i16, i32 = self._getformat(PCF_BDF_ENCODINGS)
  160. firstCol, lastCol = i16(fp.read(2)), i16(fp.read(2))
  161. firstRow, lastRow = i16(fp.read(2)), i16(fp.read(2))
  162. i16(fp.read(2)) # default
  163. nencoding = (lastCol - firstCol + 1) * (lastRow - firstRow + 1)
  164. for i in range(nencoding):
  165. encodingOffset = i16(fp.read(2))
  166. if encodingOffset != 0xFFFF:
  167. try:
  168. encoding[i + firstCol] = encodingOffset
  169. except IndexError:
  170. break # only load ISO-8859-1 glyphs
  171. return encoding