PcfFontFile.py 7.1 KB

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