PcfFontFile.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. raise SyntaxError("not a PCF file")
  53. super().__init__()
  54. count = l32(fp.read(4))
  55. self.toc = {}
  56. for i in range(count):
  57. type = l32(fp.read(4))
  58. self.toc[type] = l32(fp.read(4)), l32(fp.read(4)), l32(fp.read(4))
  59. self.fp = fp
  60. self.info = self._load_properties()
  61. metrics = self._load_metrics()
  62. bitmaps = self._load_bitmaps(metrics)
  63. encoding = self._load_encoding()
  64. #
  65. # create glyph structure
  66. for ch, ix in enumerate(encoding):
  67. if ix is not None:
  68. x, y, l, r, w, a, d, f = metrics[ix]
  69. glyph = (w, 0), (l, d - y, x + l, d), (0, 0, x, y), bitmaps[ix]
  70. self.glyph[ch] = glyph
  71. def _getformat(self, tag):
  72. format, size, offset = self.toc[tag]
  73. fp = self.fp
  74. fp.seek(offset)
  75. format = l32(fp.read(4))
  76. if format & 4:
  77. i16, i32 = b16, b32
  78. else:
  79. i16, i32 = l16, l32
  80. return fp, format, i16, i32
  81. def _load_properties(self):
  82. #
  83. # font properties
  84. properties = {}
  85. fp, format, i16, i32 = self._getformat(PCF_PROPERTIES)
  86. nprops = i32(fp.read(4))
  87. # read property description
  88. p = []
  89. for i in range(nprops):
  90. p.append((i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))))
  91. if nprops & 3:
  92. fp.seek(4 - (nprops & 3), io.SEEK_CUR) # pad
  93. data = fp.read(i32(fp.read(4)))
  94. for k, s, v in p:
  95. k = sz(data, k)
  96. if s:
  97. v = sz(data, v)
  98. properties[k] = v
  99. return properties
  100. def _load_metrics(self):
  101. #
  102. # font metrics
  103. metrics = []
  104. fp, format, i16, i32 = self._getformat(PCF_METRICS)
  105. append = metrics.append
  106. if (format & 0xFF00) == 0x100:
  107. # "compressed" metrics
  108. for i in range(i16(fp.read(2))):
  109. left = i8(fp.read(1)) - 128
  110. right = i8(fp.read(1)) - 128
  111. width = i8(fp.read(1)) - 128
  112. ascent = i8(fp.read(1)) - 128
  113. descent = i8(fp.read(1)) - 128
  114. xsize = right - left
  115. ysize = ascent + descent
  116. append((xsize, ysize, left, right, width, ascent, descent, 0))
  117. else:
  118. # "jumbo" metrics
  119. for i in range(i32(fp.read(4))):
  120. left = i16(fp.read(2))
  121. right = i16(fp.read(2))
  122. width = i16(fp.read(2))
  123. ascent = i16(fp.read(2))
  124. descent = i16(fp.read(2))
  125. attributes = i16(fp.read(2))
  126. xsize = right - left
  127. ysize = ascent + descent
  128. append((xsize, ysize, left, right, width, ascent, descent, attributes))
  129. return metrics
  130. def _load_bitmaps(self, metrics):
  131. #
  132. # bitmap data
  133. bitmaps = []
  134. fp, format, i16, i32 = self._getformat(PCF_BITMAPS)
  135. nbitmaps = i32(fp.read(4))
  136. if nbitmaps != len(metrics):
  137. raise OSError("Wrong number of bitmaps")
  138. offsets = []
  139. for i in range(nbitmaps):
  140. offsets.append(i32(fp.read(4)))
  141. bitmap_sizes = []
  142. for i in range(4):
  143. bitmap_sizes.append(i32(fp.read(4)))
  144. # byteorder = format & 4 # non-zero => MSB
  145. bitorder = format & 8 # non-zero => MSB
  146. padindex = format & 3
  147. bitmapsize = bitmap_sizes[padindex]
  148. offsets.append(bitmapsize)
  149. data = fp.read(bitmapsize)
  150. pad = BYTES_PER_ROW[padindex]
  151. mode = "1;R"
  152. if bitorder:
  153. mode = "1"
  154. for i in range(nbitmaps):
  155. x, y, l, r, w, a, d, f = metrics[i]
  156. b, e = offsets[i], offsets[i + 1]
  157. bitmaps.append(Image.frombytes("1", (x, y), data[b:e], "raw", mode, pad(x)))
  158. return bitmaps
  159. def _load_encoding(self):
  160. fp, format, i16, i32 = self._getformat(PCF_BDF_ENCODINGS)
  161. first_col, last_col = i16(fp.read(2)), i16(fp.read(2))
  162. first_row, last_row = i16(fp.read(2)), i16(fp.read(2))
  163. i16(fp.read(2)) # default
  164. nencoding = (last_col - first_col + 1) * (last_row - first_row + 1)
  165. # map character code to bitmap index
  166. encoding = [None] * min(256, nencoding)
  167. encoding_offsets = [i16(fp.read(2)) for _ in range(nencoding)]
  168. for i in range(first_col, len(encoding)):
  169. try:
  170. encoding_offset = encoding_offsets[
  171. ord(bytearray([i]).decode(self.charset_encoding))
  172. ]
  173. if encoding_offset != 0xFFFF:
  174. encoding[i] = encoding_offset
  175. except UnicodeDecodeError:
  176. # character is not supported in selected encoding
  177. pass
  178. return encoding