PcfFontFile.py 6.2 KB

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