PcfFontFile.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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
  21. from ._binary import i16be as b16
  22. from ._binary import i16le as l16
  23. from ._binary import i32be as b32
  24. from ._binary import i32le as l32
  25. # --------------------------------------------------------------------
  26. # declarations
  27. PCF_MAGIC = 0x70636601 # "\x01fcp"
  28. PCF_PROPERTIES = 1 << 0
  29. PCF_ACCELERATORS = 1 << 1
  30. PCF_METRICS = 1 << 2
  31. PCF_BITMAPS = 1 << 3
  32. PCF_INK_METRICS = 1 << 4
  33. PCF_BDF_ENCODINGS = 1 << 5
  34. PCF_SWIDTHS = 1 << 6
  35. PCF_GLYPH_NAMES = 1 << 7
  36. PCF_BDF_ACCELERATORS = 1 << 8
  37. BYTES_PER_ROW = [
  38. lambda bits: ((bits + 7) >> 3),
  39. lambda bits: ((bits + 15) >> 3) & ~1,
  40. lambda bits: ((bits + 31) >> 3) & ~3,
  41. lambda bits: ((bits + 63) >> 3) & ~7,
  42. ]
  43. def sz(s, o):
  44. return s[o : s.index(b"\0", o)]
  45. class PcfFontFile(FontFile.FontFile):
  46. """Font file plugin for the X11 PCF format."""
  47. name = "name"
  48. def __init__(self, fp, charset_encoding="iso8859-1"):
  49. self.charset_encoding = charset_encoding
  50. magic = l32(fp.read(4))
  51. if magic != PCF_MAGIC:
  52. msg = "not a PCF file"
  53. raise SyntaxError(msg)
  54. super().__init__()
  55. count = l32(fp.read(4))
  56. self.toc = {}
  57. for i in range(count):
  58. type = l32(fp.read(4))
  59. self.toc[type] = l32(fp.read(4)), l32(fp.read(4)), l32(fp.read(4))
  60. self.fp = fp
  61. self.info = self._load_properties()
  62. metrics = self._load_metrics()
  63. bitmaps = self._load_bitmaps(metrics)
  64. encoding = self._load_encoding()
  65. #
  66. # create glyph structure
  67. for ch, ix in enumerate(encoding):
  68. if ix is not None:
  69. (
  70. xsize,
  71. ysize,
  72. left,
  73. right,
  74. width,
  75. ascent,
  76. descent,
  77. attributes,
  78. ) = metrics[ix]
  79. self.glyph[ch] = (
  80. (width, 0),
  81. (left, descent - ysize, xsize + left, descent),
  82. (0, 0, xsize, ysize),
  83. bitmaps[ix],
  84. )
  85. def _getformat(self, tag):
  86. format, size, offset = self.toc[tag]
  87. fp = self.fp
  88. fp.seek(offset)
  89. format = l32(fp.read(4))
  90. if format & 4:
  91. i16, i32 = b16, b32
  92. else:
  93. i16, i32 = l16, l32
  94. return fp, format, i16, i32
  95. def _load_properties(self):
  96. #
  97. # font properties
  98. properties = {}
  99. fp, format, i16, i32 = self._getformat(PCF_PROPERTIES)
  100. nprops = i32(fp.read(4))
  101. # read property description
  102. p = []
  103. for i in range(nprops):
  104. p.append((i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))))
  105. if nprops & 3:
  106. fp.seek(4 - (nprops & 3), io.SEEK_CUR) # pad
  107. data = fp.read(i32(fp.read(4)))
  108. for k, s, v in p:
  109. k = sz(data, k)
  110. if s:
  111. v = sz(data, v)
  112. properties[k] = v
  113. return properties
  114. def _load_metrics(self):
  115. #
  116. # font metrics
  117. metrics = []
  118. fp, format, i16, i32 = self._getformat(PCF_METRICS)
  119. append = metrics.append
  120. if (format & 0xFF00) == 0x100:
  121. # "compressed" metrics
  122. for i in range(i16(fp.read(2))):
  123. left = i8(fp.read(1)) - 128
  124. right = i8(fp.read(1)) - 128
  125. width = i8(fp.read(1)) - 128
  126. ascent = i8(fp.read(1)) - 128
  127. descent = i8(fp.read(1)) - 128
  128. xsize = right - left
  129. ysize = ascent + descent
  130. append((xsize, ysize, left, right, width, ascent, descent, 0))
  131. else:
  132. # "jumbo" metrics
  133. for i in range(i32(fp.read(4))):
  134. left = i16(fp.read(2))
  135. right = i16(fp.read(2))
  136. width = i16(fp.read(2))
  137. ascent = i16(fp.read(2))
  138. descent = i16(fp.read(2))
  139. attributes = i16(fp.read(2))
  140. xsize = right - left
  141. ysize = ascent + descent
  142. append((xsize, ysize, left, right, width, ascent, descent, attributes))
  143. return metrics
  144. def _load_bitmaps(self, metrics):
  145. #
  146. # bitmap data
  147. bitmaps = []
  148. fp, format, i16, i32 = self._getformat(PCF_BITMAPS)
  149. nbitmaps = i32(fp.read(4))
  150. if nbitmaps != len(metrics):
  151. msg = "Wrong number of bitmaps"
  152. raise OSError(msg)
  153. offsets = []
  154. for i in range(nbitmaps):
  155. offsets.append(i32(fp.read(4)))
  156. bitmap_sizes = []
  157. for i in range(4):
  158. bitmap_sizes.append(i32(fp.read(4)))
  159. # byteorder = format & 4 # non-zero => MSB
  160. bitorder = format & 8 # non-zero => MSB
  161. padindex = format & 3
  162. bitmapsize = bitmap_sizes[padindex]
  163. offsets.append(bitmapsize)
  164. data = fp.read(bitmapsize)
  165. pad = BYTES_PER_ROW[padindex]
  166. mode = "1;R"
  167. if bitorder:
  168. mode = "1"
  169. for i in range(nbitmaps):
  170. xsize, ysize = metrics[i][:2]
  171. b, e = offsets[i : i + 2]
  172. bitmaps.append(
  173. Image.frombytes("1", (xsize, ysize), data[b:e], "raw", mode, pad(xsize))
  174. )
  175. return bitmaps
  176. def _load_encoding(self):
  177. fp, format, i16, i32 = self._getformat(PCF_BDF_ENCODINGS)
  178. first_col, last_col = i16(fp.read(2)), i16(fp.read(2))
  179. first_row, last_row = i16(fp.read(2)), i16(fp.read(2))
  180. i16(fp.read(2)) # default
  181. nencoding = (last_col - first_col + 1) * (last_row - first_row + 1)
  182. # map character code to bitmap index
  183. encoding = [None] * min(256, nencoding)
  184. encoding_offsets = [i16(fp.read(2)) for _ in range(nencoding)]
  185. for i in range(first_col, len(encoding)):
  186. try:
  187. encoding_offset = encoding_offsets[
  188. ord(bytearray([i]).decode(self.charset_encoding))
  189. ]
  190. if encoding_offset != 0xFFFF:
  191. encoding[i] = encoding_offset
  192. except UnicodeDecodeError:
  193. # character is not supported in selected encoding
  194. pass
  195. return encoding