You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.0 KiB

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