PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/matplotlib/mathtext.py

https://github.com/tris-sondon/matplotlib
Python | 1442 lines | 1260 code | 36 blank | 146 comment | 49 complexity | cd879810c884e5c809968da2c02c0d0d MD5 | raw file
  1. r"""
  2. :mod:`~matplotlib.mathtext` is a module for parsing a subset of the
  3. TeX math syntax and drawing them to a matplotlib backend.
  4. For a tutorial of its usage see :ref:`mathtext-tutorial`. This
  5. document is primarily concerned with implementation details.
  6. The module uses pyparsing_ to parse the TeX expression.
  7. .. _pyparsing: http://pyparsing.wikispaces.com/
  8. The Bakoma distribution of the TeX Computer Modern fonts, and STIX
  9. fonts are supported. There is experimental support for using
  10. arbitrary fonts, but results may vary without proper tweaking and
  11. metrics for those fonts.
  12. If you find TeX expressions that don't parse or render properly,
  13. please email mdroe@stsci.edu, but please check KNOWN ISSUES below first.
  14. """
  15. from __future__ import (absolute_import, division, print_function,
  16. unicode_literals)
  17. import six
  18. import os, sys
  19. if six.PY3:
  20. unichr = chr
  21. from math import ceil
  22. try:
  23. set
  24. except NameError:
  25. from sets import Set as set
  26. import unicodedata
  27. from warnings import warn
  28. from numpy import inf, isinf
  29. import numpy as np
  30. import pyparsing
  31. from pyparsing import Combine, Group, Optional, Forward, \
  32. Literal, OneOrMore, ZeroOrMore, ParseException, Empty, \
  33. ParseResults, Suppress, oneOf, StringEnd, ParseFatalException, \
  34. FollowedBy, Regex, ParserElement, QuotedString, ParseBaseException
  35. # Enable packrat parsing
  36. if (six.PY3 and
  37. [int(x) for x in pyparsing.__version__.split('.')] < [2, 0, 0]):
  38. warn("Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing "
  39. "has been disabled. Mathtext rendering will be much slower as a "
  40. "result. Install pyparsing 2.0.0 or later to improve performance.")
  41. else:
  42. ParserElement.enablePackrat()
  43. from matplotlib.afm import AFM
  44. from matplotlib.cbook import Bunch, get_realpath_and_stat, \
  45. is_string_like, maxdict
  46. from matplotlib.ft2font import FT2Font, FT2Image, KERNING_DEFAULT, LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING
  47. from matplotlib.font_manager import findfont, FontProperties
  48. from matplotlib._mathtext_data import latex_to_bakoma, \
  49. latex_to_standard, tex2uni, latex_to_cmex, stix_virtual_fonts
  50. from matplotlib import get_data_path, rcParams
  51. import matplotlib.colors as mcolors
  52. import matplotlib._png as _png
  53. ####################
  54. ##############################################################################
  55. # FONTS
  56. def get_unicode_index(symbol):
  57. """get_unicode_index(symbol) -> integer
  58. Return the integer index (from the Unicode table) of symbol. *symbol*
  59. can be a single unicode character, a TeX command (i.e. r'\pi'), or a
  60. Type1 symbol name (i.e. 'phi').
  61. """
  62. # From UTF #25: U+2212 minus sign is the preferred
  63. # representation of the unary and binary minus sign rather than
  64. # the ASCII-derived U+002D hyphen-minus, because minus sign is
  65. # unambiguous and because it is rendered with a more desirable
  66. # length, usually longer than a hyphen.
  67. if symbol == '-':
  68. return 0x2212
  69. try:# This will succeed if symbol is a single unicode char
  70. return ord(symbol)
  71. except TypeError:
  72. pass
  73. try:# Is symbol a TeX symbol (i.e. \alpha)
  74. return tex2uni[symbol.strip("\\")]
  75. except KeyError:
  76. message = """'%(symbol)s' is not a valid Unicode character or
  77. TeX/Type1 symbol"""%locals()
  78. raise ValueError(message)
  79. def unichr_safe(index):
  80. """Return the Unicode character corresponding to the index,
  81. or the replacement character if this is a narrow build of Python
  82. and the requested character is outside the BMP."""
  83. try:
  84. return unichr(index)
  85. except ValueError:
  86. return unichr(0xFFFD)
  87. class MathtextBackend(object):
  88. """
  89. The base class for the mathtext backend-specific code. The
  90. purpose of :class:`MathtextBackend` subclasses is to interface
  91. between mathtext and a specific matplotlib graphics backend.
  92. Subclasses need to override the following:
  93. - :meth:`render_glyph`
  94. - :meth:`render_filled_rect`
  95. - :meth:`get_results`
  96. And optionally, if you need to use a Freetype hinting style:
  97. - :meth:`get_hinting_type`
  98. """
  99. def __init__(self):
  100. self.width = 0
  101. self.height = 0
  102. self.depth = 0
  103. def set_canvas_size(self, w, h, d):
  104. 'Dimension the drawing canvas'
  105. self.width = w
  106. self.height = h
  107. self.depth = d
  108. def render_glyph(self, ox, oy, info):
  109. """
  110. Draw a glyph described by *info* to the reference point (*ox*,
  111. *oy*).
  112. """
  113. raise NotImplementedError()
  114. def render_filled_rect(self, x1, y1, x2, y2):
  115. """
  116. Draw a filled black rectangle from (*x1*, *y1*) to (*x2*, *y2*).
  117. """
  118. raise NotImplementedError()
  119. def get_results(self, box):
  120. """
  121. Return a backend-specific tuple to return to the backend after
  122. all processing is done.
  123. """
  124. raise NotImplementedError()
  125. def get_hinting_type(self):
  126. """
  127. Get the Freetype hinting type to use with this particular
  128. backend.
  129. """
  130. return LOAD_NO_HINTING
  131. class MathtextBackendAgg(MathtextBackend):
  132. """
  133. Render glyphs and rectangles to an FTImage buffer, which is later
  134. transferred to the Agg image by the Agg backend.
  135. """
  136. def __init__(self):
  137. self.ox = 0
  138. self.oy = 0
  139. self.image = None
  140. self.mode = 'bbox'
  141. self.bbox = [0, 0, 0, 0]
  142. MathtextBackend.__init__(self)
  143. def _update_bbox(self, x1, y1, x2, y2):
  144. self.bbox = [min(self.bbox[0], x1),
  145. min(self.bbox[1], y1),
  146. max(self.bbox[2], x2),
  147. max(self.bbox[3], y2)]
  148. def set_canvas_size(self, w, h, d):
  149. MathtextBackend.set_canvas_size(self, w, h, d)
  150. if self.mode != 'bbox':
  151. self.image = FT2Image(ceil(w), ceil(h + d))
  152. def render_glyph(self, ox, oy, info):
  153. if self.mode == 'bbox':
  154. self._update_bbox(ox + info.metrics.xmin,
  155. oy - info.metrics.ymax,
  156. ox + info.metrics.xmax,
  157. oy - info.metrics.ymin)
  158. else:
  159. info.font.draw_glyph_to_bitmap(
  160. self.image, ox, oy - info.metrics.iceberg, info.glyph,
  161. antialiased=rcParams['text.antialiased'])
  162. def render_rect_filled(self, x1, y1, x2, y2):
  163. if self.mode == 'bbox':
  164. self._update_bbox(x1, y1, x2, y2)
  165. else:
  166. height = max(int(y2 - y1) - 1, 0)
  167. if height == 0:
  168. center = (y2 + y1) / 2.0
  169. y = int(center - (height + 1) / 2.0)
  170. else:
  171. y = int(y1)
  172. self.image.draw_rect_filled(int(x1), y, ceil(x2), y + height)
  173. def get_results(self, box, used_characters):
  174. self.mode = 'bbox'
  175. orig_height = box.height
  176. orig_depth = box.depth
  177. ship(0, 0, box)
  178. bbox = self.bbox
  179. bbox = [bbox[0] - 1, bbox[1] - 1, bbox[2] + 1, bbox[3] + 1]
  180. self.mode = 'render'
  181. self.set_canvas_size(
  182. bbox[2] - bbox[0],
  183. (bbox[3] - bbox[1]) - orig_depth,
  184. (bbox[3] - bbox[1]) - orig_height)
  185. ship(-bbox[0], -bbox[1], box)
  186. result = (self.ox,
  187. self.oy,
  188. self.width,
  189. self.height + self.depth,
  190. self.depth,
  191. self.image,
  192. used_characters)
  193. self.image = None
  194. return result
  195. def get_hinting_type(self):
  196. from matplotlib.backends import backend_agg
  197. return backend_agg.get_hinting_flag()
  198. class MathtextBackendBitmap(MathtextBackendAgg):
  199. def get_results(self, box, used_characters):
  200. ox, oy, width, height, depth, image, characters = \
  201. MathtextBackendAgg.get_results(self, box, used_characters)
  202. return image, depth
  203. class MathtextBackendPs(MathtextBackend):
  204. """
  205. Store information to write a mathtext rendering to the PostScript
  206. backend.
  207. """
  208. def __init__(self):
  209. self.pswriter = six.moves.cStringIO()
  210. self.lastfont = None
  211. def render_glyph(self, ox, oy, info):
  212. oy = self.height - oy + info.offset
  213. postscript_name = info.postscript_name
  214. fontsize = info.fontsize
  215. symbol_name = info.symbol_name
  216. if (postscript_name, fontsize) != self.lastfont:
  217. ps = """/%(postscript_name)s findfont
  218. %(fontsize)s scalefont
  219. setfont
  220. """ % locals()
  221. self.lastfont = postscript_name, fontsize
  222. self.pswriter.write(ps)
  223. ps = """%(ox)f %(oy)f moveto
  224. /%(symbol_name)s glyphshow\n
  225. """ % locals()
  226. self.pswriter.write(ps)
  227. def render_rect_filled(self, x1, y1, x2, y2):
  228. ps = "%f %f %f %f rectfill\n" % (x1, self.height - y2, x2 - x1, y2 - y1)
  229. self.pswriter.write(ps)
  230. def get_results(self, box, used_characters):
  231. ship(0, 0, box)
  232. return (self.width,
  233. self.height + self.depth,
  234. self.depth,
  235. self.pswriter,
  236. used_characters)
  237. class MathtextBackendPdf(MathtextBackend):
  238. """
  239. Store information to write a mathtext rendering to the PDF
  240. backend.
  241. """
  242. def __init__(self):
  243. self.glyphs = []
  244. self.rects = []
  245. def render_glyph(self, ox, oy, info):
  246. filename = info.font.fname
  247. oy = self.height - oy + info.offset
  248. self.glyphs.append(
  249. (ox, oy, filename, info.fontsize,
  250. info.num, info.symbol_name))
  251. def render_rect_filled(self, x1, y1, x2, y2):
  252. self.rects.append((x1, self.height - y2, x2 - x1, y2 - y1))
  253. def get_results(self, box, used_characters):
  254. ship(0, 0, box)
  255. return (self.width,
  256. self.height + self.depth,
  257. self.depth,
  258. self.glyphs,
  259. self.rects,
  260. used_characters)
  261. class MathtextBackendSvg(MathtextBackend):
  262. """
  263. Store information to write a mathtext rendering to the SVG
  264. backend.
  265. """
  266. def __init__(self):
  267. self.svg_glyphs = []
  268. self.svg_rects = []
  269. def render_glyph(self, ox, oy, info):
  270. oy = self.height - oy + info.offset
  271. self.svg_glyphs.append(
  272. (info.font, info.fontsize, info.num, ox, oy, info.metrics))
  273. def render_rect_filled(self, x1, y1, x2, y2):
  274. self.svg_rects.append(
  275. (x1, self.height - y1 + 1, x2 - x1, y2 - y1))
  276. def get_results(self, box, used_characters):
  277. ship(0, 0, box)
  278. svg_elements = Bunch(svg_glyphs = self.svg_glyphs,
  279. svg_rects = self.svg_rects)
  280. return (self.width,
  281. self.height + self.depth,
  282. self.depth,
  283. svg_elements,
  284. used_characters)
  285. class MathtextBackendPath(MathtextBackend):
  286. """
  287. Store information to write a mathtext rendering to the text path
  288. machinery.
  289. """
  290. def __init__(self):
  291. self.glyphs = []
  292. self.rects = []
  293. def render_glyph(self, ox, oy, info):
  294. oy = self.height - oy + info.offset
  295. thetext = info.num
  296. self.glyphs.append(
  297. (info.font, info.fontsize, thetext, ox, oy))
  298. def render_rect_filled(self, x1, y1, x2, y2):
  299. self.rects.append(
  300. (x1, self.height-y2 , x2 - x1, y2 - y1))
  301. def get_results(self, box, used_characters):
  302. ship(0, 0, box)
  303. return (self.width,
  304. self.height + self.depth,
  305. self.depth,
  306. self.glyphs,
  307. self.rects)
  308. class MathtextBackendCairo(MathtextBackend):
  309. """
  310. Store information to write a mathtext rendering to the Cairo
  311. backend.
  312. """
  313. def __init__(self):
  314. self.glyphs = []
  315. self.rects = []
  316. def render_glyph(self, ox, oy, info):
  317. oy = oy - info.offset - self.height
  318. thetext = unichr_safe(info.num)
  319. self.glyphs.append(
  320. (info.font, info.fontsize, thetext, ox, oy))
  321. def render_rect_filled(self, x1, y1, x2, y2):
  322. self.rects.append(
  323. (x1, y1 - self.height, x2 - x1, y2 - y1))
  324. def get_results(self, box, used_characters):
  325. ship(0, 0, box)
  326. return (self.width,
  327. self.height + self.depth,
  328. self.depth,
  329. self.glyphs,
  330. self.rects)
  331. class Fonts(object):
  332. """
  333. An abstract base class for a system of fonts to use for mathtext.
  334. The class must be able to take symbol keys and font file names and
  335. return the character metrics. It also delegates to a backend class
  336. to do the actual drawing.
  337. """
  338. def __init__(self, default_font_prop, mathtext_backend):
  339. """
  340. *default_font_prop*: A
  341. :class:`~matplotlib.font_manager.FontProperties` object to use
  342. for the default non-math font, or the base font for Unicode
  343. (generic) font rendering.
  344. *mathtext_backend*: A subclass of :class:`MathTextBackend`
  345. used to delegate the actual rendering.
  346. """
  347. self.default_font_prop = default_font_prop
  348. self.mathtext_backend = mathtext_backend
  349. self.used_characters = {}
  350. def destroy(self):
  351. """
  352. Fix any cyclical references before the object is about
  353. to be destroyed.
  354. """
  355. self.used_characters = None
  356. def get_kern(self, font1, fontclass1, sym1, fontsize1,
  357. font2, fontclass2, sym2, fontsize2, dpi):
  358. """
  359. Get the kerning distance for font between *sym1* and *sym2*.
  360. *fontX*: one of the TeX font names::
  361. tt, it, rm, cal, sf, bf or default/regular (non-math)
  362. *fontclassX*: TODO
  363. *symX*: a symbol in raw TeX form. e.g., '1', 'x' or '\sigma'
  364. *fontsizeX*: the fontsize in points
  365. *dpi*: the current dots-per-inch
  366. """
  367. return 0.
  368. def get_metrics(self, font, font_class, sym, fontsize, dpi):
  369. """
  370. *font*: one of the TeX font names::
  371. tt, it, rm, cal, sf, bf or default/regular (non-math)
  372. *font_class*: TODO
  373. *sym*: a symbol in raw TeX form. e.g., '1', 'x' or '\sigma'
  374. *fontsize*: font size in points
  375. *dpi*: current dots-per-inch
  376. Returns an object with the following attributes:
  377. - *advance*: The advance distance (in points) of the glyph.
  378. - *height*: The height of the glyph in points.
  379. - *width*: The width of the glyph in points.
  380. - *xmin*, *xmax*, *ymin*, *ymax* - the ink rectangle of the glyph
  381. - *iceberg* - the distance from the baseline to the top of
  382. the glyph. This corresponds to TeX's definition of
  383. "height".
  384. """
  385. info = self._get_info(font, font_class, sym, fontsize, dpi)
  386. return info.metrics
  387. def set_canvas_size(self, w, h, d):
  388. """
  389. Set the size of the buffer used to render the math expression.
  390. Only really necessary for the bitmap backends.
  391. """
  392. self.width, self.height, self.depth = ceil(w), ceil(h), ceil(d)
  393. self.mathtext_backend.set_canvas_size(self.width, self.height, self.depth)
  394. def render_glyph(self, ox, oy, facename, font_class, sym, fontsize, dpi):
  395. """
  396. Draw a glyph at
  397. - *ox*, *oy*: position
  398. - *facename*: One of the TeX face names
  399. - *font_class*:
  400. - *sym*: TeX symbol name or single character
  401. - *fontsize*: fontsize in points
  402. - *dpi*: The dpi to draw at.
  403. """
  404. info = self._get_info(facename, font_class, sym, fontsize, dpi)
  405. realpath, stat_key = get_realpath_and_stat(info.font.fname)
  406. used_characters = self.used_characters.setdefault(
  407. stat_key, (realpath, set()))
  408. used_characters[1].add(info.num)
  409. self.mathtext_backend.render_glyph(ox, oy, info)
  410. def render_rect_filled(self, x1, y1, x2, y2):
  411. """
  412. Draw a filled rectangle from (*x1*, *y1*) to (*x2*, *y2*).
  413. """
  414. self.mathtext_backend.render_rect_filled(x1, y1, x2, y2)
  415. def get_xheight(self, font, fontsize, dpi):
  416. """
  417. Get the xheight for the given *font* and *fontsize*.
  418. """
  419. raise NotImplementedError()
  420. def get_underline_thickness(self, font, fontsize, dpi):
  421. """
  422. Get the line thickness that matches the given font. Used as a
  423. base unit for drawing lines such as in a fraction or radical.
  424. """
  425. raise NotImplementedError()
  426. def get_used_characters(self):
  427. """
  428. Get the set of characters that were used in the math
  429. expression. Used by backends that need to subset fonts so
  430. they know which glyphs to include.
  431. """
  432. return self.used_characters
  433. def get_results(self, box):
  434. """
  435. Get the data needed by the backend to render the math
  436. expression. The return value is backend-specific.
  437. """
  438. result = self.mathtext_backend.get_results(box, self.get_used_characters())
  439. self.destroy()
  440. return result
  441. def get_sized_alternatives_for_symbol(self, fontname, sym):
  442. """
  443. Override if your font provides multiple sizes of the same
  444. symbol. Should return a list of symbols matching *sym* in
  445. various sizes. The expression renderer will select the most
  446. appropriate size for a given situation from this list.
  447. """
  448. return [(fontname, sym)]
  449. class TruetypeFonts(Fonts):
  450. """
  451. A generic base class for all font setups that use Truetype fonts
  452. (through FT2Font).
  453. """
  454. class CachedFont:
  455. def __init__(self, font):
  456. self.font = font
  457. self.charmap = font.get_charmap()
  458. self.glyphmap = dict(
  459. [(glyphind, ccode) for ccode, glyphind in six.iteritems(self.charmap)])
  460. def __repr__(self):
  461. return repr(self.font)
  462. def __init__(self, default_font_prop, mathtext_backend):
  463. Fonts.__init__(self, default_font_prop, mathtext_backend)
  464. self.glyphd = {}
  465. self._fonts = {}
  466. filename = findfont(default_font_prop)
  467. default_font = self.CachedFont(FT2Font(filename))
  468. self._fonts['default'] = default_font
  469. self._fonts['regular'] = default_font
  470. def destroy(self):
  471. self.glyphd = None
  472. Fonts.destroy(self)
  473. def _get_font(self, font):
  474. if font in self.fontmap:
  475. basename = self.fontmap[font]
  476. else:
  477. basename = font
  478. cached_font = self._fonts.get(basename)
  479. if cached_font is None and os.path.exists(basename):
  480. font = FT2Font(basename)
  481. cached_font = self.CachedFont(font)
  482. self._fonts[basename] = cached_font
  483. self._fonts[font.postscript_name] = cached_font
  484. self._fonts[font.postscript_name.lower()] = cached_font
  485. return cached_font
  486. def _get_offset(self, cached_font, glyph, fontsize, dpi):
  487. if cached_font.font.postscript_name == 'Cmex10':
  488. return ((glyph.height/64.0/2.0) + (fontsize/3.0 * dpi/72.0))
  489. return 0.
  490. def _get_info(self, fontname, font_class, sym, fontsize, dpi):
  491. key = fontname, font_class, sym, fontsize, dpi
  492. bunch = self.glyphd.get(key)
  493. if bunch is not None:
  494. return bunch
  495. cached_font, num, symbol_name, fontsize, slanted = \
  496. self._get_glyph(fontname, font_class, sym, fontsize)
  497. font = cached_font.font
  498. font.set_size(fontsize, dpi)
  499. glyph = font.load_char(
  500. num,
  501. flags=self.mathtext_backend.get_hinting_type())
  502. xmin, ymin, xmax, ymax = [val/64.0 for val in glyph.bbox]
  503. offset = self._get_offset(cached_font, glyph, fontsize, dpi)
  504. metrics = Bunch(
  505. advance = glyph.linearHoriAdvance/65536.0,
  506. height = glyph.height/64.0,
  507. width = glyph.width/64.0,
  508. xmin = xmin,
  509. xmax = xmax,
  510. ymin = ymin+offset,
  511. ymax = ymax+offset,
  512. # iceberg is the equivalent of TeX's "height"
  513. iceberg = glyph.horiBearingY/64.0 + offset,
  514. slanted = slanted
  515. )
  516. result = self.glyphd[key] = Bunch(
  517. font = font,
  518. fontsize = fontsize,
  519. postscript_name = font.postscript_name,
  520. metrics = metrics,
  521. symbol_name = symbol_name,
  522. num = num,
  523. glyph = glyph,
  524. offset = offset
  525. )
  526. return result
  527. def get_xheight(self, font, fontsize, dpi):
  528. cached_font = self._get_font(font)
  529. cached_font.font.set_size(fontsize, dpi)
  530. pclt = cached_font.font.get_sfnt_table('pclt')
  531. if pclt is None:
  532. # Some fonts don't store the xHeight, so we do a poor man's xHeight
  533. metrics = self.get_metrics(font, rcParams['mathtext.default'], 'x', fontsize, dpi)
  534. return metrics.iceberg
  535. xHeight = (pclt['xHeight'] / 64.0) * (fontsize / 12.0) * (dpi / 100.0)
  536. return xHeight
  537. def get_underline_thickness(self, font, fontsize, dpi):
  538. # This function used to grab underline thickness from the font
  539. # metrics, but that information is just too un-reliable, so it
  540. # is now hardcoded.
  541. return ((0.75 / 12.0) * fontsize * dpi) / 72.0
  542. def get_kern(self, font1, fontclass1, sym1, fontsize1,
  543. font2, fontclass2, sym2, fontsize2, dpi):
  544. if font1 == font2 and fontsize1 == fontsize2:
  545. info1 = self._get_info(font1, fontclass1, sym1, fontsize1, dpi)
  546. info2 = self._get_info(font2, fontclass2, sym2, fontsize2, dpi)
  547. font = info1.font
  548. return font.get_kerning(info1.num, info2.num, KERNING_DEFAULT) / 64.0
  549. return Fonts.get_kern(self, font1, fontclass1, sym1, fontsize1,
  550. font2, fontclass2, sym2, fontsize2, dpi)
  551. class BakomaFonts(TruetypeFonts):
  552. """
  553. Use the Bakoma TrueType fonts for rendering.
  554. Symbols are strewn about a number of font files, each of which has
  555. its own proprietary 8-bit encoding.
  556. """
  557. _fontmap = { 'cal' : 'cmsy10',
  558. 'rm' : 'cmr10',
  559. 'tt' : 'cmtt10',
  560. 'it' : 'cmmi10',
  561. 'bf' : 'cmb10',
  562. 'sf' : 'cmss10',
  563. 'ex' : 'cmex10'
  564. }
  565. def __init__(self, *args, **kwargs):
  566. self._stix_fallback = StixFonts(*args, **kwargs)
  567. TruetypeFonts.__init__(self, *args, **kwargs)
  568. self.fontmap = {}
  569. for key, val in six.iteritems(self._fontmap):
  570. fullpath = findfont(val)
  571. self.fontmap[key] = fullpath
  572. self.fontmap[val] = fullpath
  573. _slanted_symbols = set(r"\int \oint".split())
  574. def _get_glyph(self, fontname, font_class, sym, fontsize):
  575. symbol_name = None
  576. if fontname in self.fontmap and sym in latex_to_bakoma:
  577. basename, num = latex_to_bakoma[sym]
  578. slanted = (basename == "cmmi10") or sym in self._slanted_symbols
  579. cached_font = self._get_font(basename)
  580. if cached_font is not None:
  581. symbol_name = cached_font.font.get_glyph_name(num)
  582. num = cached_font.glyphmap[num]
  583. elif len(sym) == 1:
  584. slanted = (fontname == "it")
  585. cached_font = self._get_font(fontname)
  586. if cached_font is not None:
  587. num = ord(sym)
  588. gid = cached_font.charmap.get(num)
  589. if gid is not None:
  590. symbol_name = cached_font.font.get_glyph_name(
  591. cached_font.charmap[num])
  592. if symbol_name is None:
  593. return self._stix_fallback._get_glyph(
  594. fontname, font_class, sym, fontsize)
  595. return cached_font, num, symbol_name, fontsize, slanted
  596. # The Bakoma fonts contain many pre-sized alternatives for the
  597. # delimiters. The AutoSizedChar class will use these alternatives
  598. # and select the best (closest sized) glyph.
  599. _size_alternatives = {
  600. '(' : [('rm', '('), ('ex', '\xa1'), ('ex', '\xb3'),
  601. ('ex', '\xb5'), ('ex', '\xc3')],
  602. ')' : [('rm', ')'), ('ex', '\xa2'), ('ex', '\xb4'),
  603. ('ex', '\xb6'), ('ex', '\x21')],
  604. '{' : [('cal', '{'), ('ex', '\xa9'), ('ex', '\x6e'),
  605. ('ex', '\xbd'), ('ex', '\x28')],
  606. '}' : [('cal', '}'), ('ex', '\xaa'), ('ex', '\x6f'),
  607. ('ex', '\xbe'), ('ex', '\x29')],
  608. # The fourth size of '[' is mysteriously missing from the BaKoMa
  609. # font, so I've ommitted it for both '[' and ']'
  610. '[' : [('rm', '['), ('ex', '\xa3'), ('ex', '\x68'),
  611. ('ex', '\x22')],
  612. ']' : [('rm', ']'), ('ex', '\xa4'), ('ex', '\x69'),
  613. ('ex', '\x23')],
  614. r'\lfloor' : [('ex', '\xa5'), ('ex', '\x6a'),
  615. ('ex', '\xb9'), ('ex', '\x24')],
  616. r'\rfloor' : [('ex', '\xa6'), ('ex', '\x6b'),
  617. ('ex', '\xba'), ('ex', '\x25')],
  618. r'\lceil' : [('ex', '\xa7'), ('ex', '\x6c'),
  619. ('ex', '\xbb'), ('ex', '\x26')],
  620. r'\rceil' : [('ex', '\xa8'), ('ex', '\x6d'),
  621. ('ex', '\xbc'), ('ex', '\x27')],
  622. r'\langle' : [('ex', '\xad'), ('ex', '\x44'),
  623. ('ex', '\xbf'), ('ex', '\x2a')],
  624. r'\rangle' : [('ex', '\xae'), ('ex', '\x45'),
  625. ('ex', '\xc0'), ('ex', '\x2b')],
  626. r'\__sqrt__' : [('ex', '\x70'), ('ex', '\x71'),
  627. ('ex', '\x72'), ('ex', '\x73')],
  628. r'\backslash': [('ex', '\xb2'), ('ex', '\x2f'),
  629. ('ex', '\xc2'), ('ex', '\x2d')],
  630. r'/' : [('rm', '/'), ('ex', '\xb1'), ('ex', '\x2e'),
  631. ('ex', '\xcb'), ('ex', '\x2c')],
  632. r'\widehat' : [('rm', '\x5e'), ('ex', '\x62'), ('ex', '\x63'),
  633. ('ex', '\x64')],
  634. r'\widetilde': [('rm', '\x7e'), ('ex', '\x65'), ('ex', '\x66'),
  635. ('ex', '\x67')],
  636. r'<' : [('cal', 'h'), ('ex', 'D')],
  637. r'>' : [('cal', 'i'), ('ex', 'E')]
  638. }
  639. for alias, target in [('\leftparen', '('),
  640. ('\rightparent', ')'),
  641. ('\leftbrace', '{'),
  642. ('\rightbrace', '}'),
  643. ('\leftbracket', '['),
  644. ('\rightbracket', ']'),
  645. (r'\{', '{'),
  646. (r'\}', '}'),
  647. (r'\[', '['),
  648. (r'\]', ']')]:
  649. _size_alternatives[alias] = _size_alternatives[target]
  650. def get_sized_alternatives_for_symbol(self, fontname, sym):
  651. return self._size_alternatives.get(sym, [(fontname, sym)])
  652. class UnicodeFonts(TruetypeFonts):
  653. """
  654. An abstract base class for handling Unicode fonts.
  655. While some reasonably complete Unicode fonts (such as DejaVu) may
  656. work in some situations, the only Unicode font I'm aware of with a
  657. complete set of math symbols is STIX.
  658. This class will "fallback" on the Bakoma fonts when a required
  659. symbol can not be found in the font.
  660. """
  661. use_cmex = True
  662. def __init__(self, *args, **kwargs):
  663. # This must come first so the backend's owner is set correctly
  664. if rcParams['mathtext.fallback_to_cm']:
  665. self.cm_fallback = BakomaFonts(*args, **kwargs)
  666. else:
  667. self.cm_fallback = None
  668. TruetypeFonts.__init__(self, *args, **kwargs)
  669. self.fontmap = {}
  670. for texfont in "cal rm tt it bf sf".split():
  671. prop = rcParams['mathtext.' + texfont]
  672. font = findfont(prop)
  673. self.fontmap[texfont] = font
  674. prop = FontProperties('cmex10')
  675. font = findfont(prop)
  676. self.fontmap['ex'] = font
  677. _slanted_symbols = set(r"\int \oint".split())
  678. def _map_virtual_font(self, fontname, font_class, uniindex):
  679. return fontname, uniindex
  680. def _get_glyph(self, fontname, font_class, sym, fontsize):
  681. found_symbol = False
  682. if self.use_cmex:
  683. uniindex = latex_to_cmex.get(sym)
  684. if uniindex is not None:
  685. fontname = 'ex'
  686. found_symbol = True
  687. if not found_symbol:
  688. try:
  689. uniindex = get_unicode_index(sym)
  690. found_symbol = True
  691. except ValueError:
  692. uniindex = ord('?')
  693. warn("No TeX to unicode mapping for '%s'" %
  694. sym.encode('ascii', 'backslashreplace'),
  695. MathTextWarning)
  696. fontname, uniindex = self._map_virtual_font(
  697. fontname, font_class, uniindex)
  698. new_fontname = fontname
  699. # Only characters in the "Letter" class should be italicized in 'it'
  700. # mode. Greek capital letters should be Roman.
  701. if found_symbol:
  702. if fontname == 'it':
  703. if uniindex < 0x10000:
  704. unistring = unichr(uniindex)
  705. if (not unicodedata.category(unistring)[0] == "L"
  706. or unicodedata.name(unistring).startswith("GREEK CAPITAL")):
  707. new_fontname = 'rm'
  708. slanted = (new_fontname == 'it') or sym in self._slanted_symbols
  709. found_symbol = False
  710. cached_font = self._get_font(new_fontname)
  711. if cached_font is not None:
  712. try:
  713. glyphindex = cached_font.charmap[uniindex]
  714. found_symbol = True
  715. except KeyError:
  716. pass
  717. if not found_symbol:
  718. if self.cm_fallback:
  719. warn("Substituting with a symbol from Computer Modern.",
  720. MathTextWarning)
  721. return self.cm_fallback._get_glyph(
  722. fontname, 'it', sym, fontsize)
  723. else:
  724. if fontname in ('it', 'regular') and isinstance(self, StixFonts):
  725. return self._get_glyph('rm', font_class, sym, fontsize)
  726. warn("Font '%s' does not have a glyph for '%s' [U%x]" %
  727. (new_fontname, sym.encode('ascii', 'backslashreplace'), uniindex),
  728. MathTextWarning)
  729. warn("Substituting with a dummy symbol.", MathTextWarning)
  730. fontname = 'rm'
  731. new_fontname = fontname
  732. cached_font = self._get_font(fontname)
  733. uniindex = 0xA4 # currency character, for lack of anything better
  734. glyphindex = cached_font.charmap[uniindex]
  735. slanted = False
  736. symbol_name = cached_font.font.get_glyph_name(glyphindex)
  737. return cached_font, uniindex, symbol_name, fontsize, slanted
  738. def get_sized_alternatives_for_symbol(self, fontname, sym):
  739. if self.cm_fallback:
  740. return self.cm_fallback.get_sized_alternatives_for_symbol(
  741. fontname, sym)
  742. return [(fontname, sym)]
  743. class StixFonts(UnicodeFonts):
  744. """
  745. A font handling class for the STIX fonts.
  746. In addition to what UnicodeFonts provides, this class:
  747. - supports "virtual fonts" which are complete alpha numeric
  748. character sets with different font styles at special Unicode
  749. code points, such as "Blackboard".
  750. - handles sized alternative characters for the STIXSizeX fonts.
  751. """
  752. _fontmap = { 'rm' : 'STIXGeneral',
  753. 'it' : 'STIXGeneral:italic',
  754. 'bf' : 'STIXGeneral:weight=bold',
  755. 'nonunirm' : 'STIXNonUnicode',
  756. 'nonuniit' : 'STIXNonUnicode:italic',
  757. 'nonunibf' : 'STIXNonUnicode:weight=bold',
  758. 0 : 'STIXGeneral',
  759. 1 : 'STIXSizeOneSym',
  760. 2 : 'STIXSizeTwoSym',
  761. 3 : 'STIXSizeThreeSym',
  762. 4 : 'STIXSizeFourSym',
  763. 5 : 'STIXSizeFiveSym'
  764. }
  765. use_cmex = False
  766. cm_fallback = False
  767. _sans = False
  768. def __init__(self, *args, **kwargs):
  769. TruetypeFonts.__init__(self, *args, **kwargs)
  770. self.fontmap = {}
  771. for key, name in six.iteritems(self._fontmap):
  772. fullpath = findfont(name)
  773. self.fontmap[key] = fullpath
  774. self.fontmap[name] = fullpath
  775. def _map_virtual_font(self, fontname, font_class, uniindex):
  776. # Handle these "fonts" that are actually embedded in
  777. # other fonts.
  778. mapping = stix_virtual_fonts.get(fontname)
  779. if (self._sans and mapping is None and
  780. fontname not in ('regular', 'default')):
  781. mapping = stix_virtual_fonts['sf']
  782. doing_sans_conversion = True
  783. else:
  784. doing_sans_conversion = False
  785. if mapping is not None:
  786. if isinstance(mapping, dict):
  787. mapping = mapping.get(font_class, 'rm')
  788. # Binary search for the source glyph
  789. lo = 0
  790. hi = len(mapping)
  791. while lo < hi:
  792. mid = (lo+hi)//2
  793. range = mapping[mid]
  794. if uniindex < range[0]:
  795. hi = mid
  796. elif uniindex <= range[1]:
  797. break
  798. else:
  799. lo = mid + 1
  800. if uniindex >= range[0] and uniindex <= range[1]:
  801. uniindex = uniindex - range[0] + range[3]
  802. fontname = range[2]
  803. elif not doing_sans_conversion:
  804. # This will generate a dummy character
  805. uniindex = 0x1
  806. fontname = rcParams['mathtext.default']
  807. # Handle private use area glyphs
  808. if (fontname in ('it', 'rm', 'bf') and
  809. uniindex >= 0xe000 and uniindex <= 0xf8ff):
  810. fontname = 'nonuni' + fontname
  811. return fontname, uniindex
  812. _size_alternatives = {}
  813. def get_sized_alternatives_for_symbol(self, fontname, sym):
  814. fixes = {'\{': '{', '\}': '}', '\[': '[', '\]': ']'}
  815. sym = fixes.get(sym, sym)
  816. alternatives = self._size_alternatives.get(sym)
  817. if alternatives:
  818. return alternatives
  819. alternatives = []
  820. try:
  821. uniindex = get_unicode_index(sym)
  822. except ValueError:
  823. return [(fontname, sym)]
  824. fix_ups = {
  825. ord('<'): 0x27e8,
  826. ord('>'): 0x27e9 }
  827. uniindex = fix_ups.get(uniindex, uniindex)
  828. for i in range(6):
  829. cached_font = self._get_font(i)
  830. glyphindex = cached_font.charmap.get(uniindex)
  831. if glyphindex is not None:
  832. alternatives.append((i, unichr_safe(uniindex)))
  833. # The largest size of the radical symbol in STIX has incorrect
  834. # metrics that cause it to be disconnected from the stem.
  835. if sym == r'\__sqrt__':
  836. alternatives = alternatives[:-1]
  837. self._size_alternatives[sym] = alternatives
  838. return alternatives
  839. class StixSansFonts(StixFonts):
  840. """
  841. A font handling class for the STIX fonts (that uses sans-serif
  842. characters by default).
  843. """
  844. _sans = True
  845. class StandardPsFonts(Fonts):
  846. """
  847. Use the standard postscript fonts for rendering to backend_ps
  848. Unlike the other font classes, BakomaFont and UnicodeFont, this
  849. one requires the Ps backend.
  850. """
  851. basepath = os.path.join( get_data_path(), 'fonts', 'afm' )
  852. fontmap = { 'cal' : 'pzcmi8a', # Zapf Chancery
  853. 'rm' : 'pncr8a', # New Century Schoolbook
  854. 'tt' : 'pcrr8a', # Courier
  855. 'it' : 'pncri8a', # New Century Schoolbook Italic
  856. 'sf' : 'phvr8a', # Helvetica
  857. 'bf' : 'pncb8a', # New Century Schoolbook Bold
  858. None : 'psyr' # Symbol
  859. }
  860. def __init__(self, default_font_prop):
  861. Fonts.__init__(self, default_font_prop, MathtextBackendPs())
  862. self.glyphd = {}
  863. self.fonts = {}
  864. filename = findfont(default_font_prop, fontext='afm',
  865. directory=self.basepath)
  866. if filename is None:
  867. filename = findfont('Helvetica', fontext='afm',
  868. directory=self.basepath)
  869. with open(filename, 'r') as fd:
  870. default_font = AFM(fd)
  871. default_font.fname = filename
  872. self.fonts['default'] = default_font
  873. self.fonts['regular'] = default_font
  874. self.pswriter = six.moves.cStringIO()
  875. def _get_font(self, font):
  876. if font in self.fontmap:
  877. basename = self.fontmap[font]
  878. else:
  879. basename = font
  880. cached_font = self.fonts.get(basename)
  881. if cached_font is None:
  882. fname = os.path.join(self.basepath, basename + ".afm")
  883. with open(fname, 'r') as fd:
  884. cached_font = AFM(fd)
  885. cached_font.fname = fname
  886. self.fonts[basename] = cached_font
  887. self.fonts[cached_font.get_fontname()] = cached_font
  888. return cached_font
  889. def _get_info (self, fontname, font_class, sym, fontsize, dpi):
  890. 'load the cmfont, metrics and glyph with caching'
  891. key = fontname, sym, fontsize, dpi
  892. tup = self.glyphd.get(key)
  893. if tup is not None:
  894. return tup
  895. # Only characters in the "Letter" class should really be italicized.
  896. # This class includes greek letters, so we're ok
  897. if (fontname == 'it' and
  898. (len(sym) > 1 or
  899. not unicodedata.category(six.text_type(sym)).startswith("L"))):
  900. fontname = 'rm'
  901. found_symbol = False
  902. if sym in latex_to_standard:
  903. fontname, num = latex_to_standard[sym]
  904. glyph = chr(num)
  905. found_symbol = True
  906. elif len(sym) == 1:
  907. glyph = sym
  908. num = ord(glyph)
  909. found_symbol = True
  910. else:
  911. warn("No TeX to built-in Postscript mapping for '%s'" % sym,
  912. MathTextWarning)
  913. slanted = (fontname == 'it')
  914. font = self._get_font(fontname)
  915. if found_symbol:
  916. try:
  917. symbol_name = font.get_name_char(glyph)
  918. except KeyError:
  919. warn("No glyph in standard Postscript font '%s' for '%s'" %
  920. (font.postscript_name, sym),
  921. MathTextWarning)
  922. found_symbol = False
  923. if not found_symbol:
  924. glyph = sym = '?'
  925. num = ord(glyph)
  926. symbol_name = font.get_name_char(glyph)
  927. offset = 0
  928. scale = 0.001 * fontsize
  929. xmin, ymin, xmax, ymax = [val * scale
  930. for val in font.get_bbox_char(glyph)]
  931. metrics = Bunch(
  932. advance = font.get_width_char(glyph) * scale,
  933. width = font.get_width_char(glyph) * scale,
  934. height = font.get_height_char(glyph) * scale,
  935. xmin = xmin,
  936. xmax = xmax,
  937. ymin = ymin+offset,
  938. ymax = ymax+offset,
  939. # iceberg is the equivalent of TeX's "height"
  940. iceberg = ymax + offset,
  941. slanted = slanted
  942. )
  943. self.glyphd[key] = Bunch(
  944. font = font,
  945. fontsize = fontsize,
  946. postscript_name = font.get_fontname(),
  947. metrics = metrics,
  948. symbol_name = symbol_name,
  949. num = num,
  950. glyph = glyph,
  951. offset = offset
  952. )
  953. return self.glyphd[key]
  954. def get_kern(self, font1, fontclass1, sym1, fontsize1,
  955. font2, fontclass2, sym2, fontsize2, dpi):
  956. if font1 == font2 and fontsize1 == fontsize2:
  957. info1 = self._get_info(font1, fontclass1, sym1, fontsize1, dpi)
  958. info2 = self._get_info(font2, fontclass2, sym2, fontsize2, dpi)
  959. font = info1.font
  960. return (font.get_kern_dist(info1.glyph, info2.glyph)
  961. * 0.001 * fontsize1)
  962. return Fonts.get_kern(self, font1, fontclass1, sym1, fontsize1,
  963. font2, fontclass2, sym2, fontsize2, dpi)
  964. def get_xheight(self, font, fontsize, dpi):
  965. cached_font = self._get_font(font)
  966. return cached_font.get_xheight() * 0.001 * fontsize
  967. def get_underline_thickness(self, font, fontsize, dpi):
  968. cached_font = self._get_font(font)
  969. return cached_font.get_underline_thickness() * 0.001 * fontsize
  970. ##############################################################################
  971. # TeX-LIKE BOX MODEL
  972. # The following is based directly on the document 'woven' from the
  973. # TeX82 source code. This information is also available in printed
  974. # form:
  975. #
  976. # Knuth, Donald E.. 1986. Computers and Typesetting, Volume B:
  977. # TeX: The Program. Addison-Wesley Professional.
  978. #
  979. # The most relevant "chapters" are:
  980. # Data structures for boxes and their friends
  981. # Shipping pages out (Ship class)
  982. # Packaging (hpack and vpack)
  983. # Data structures for math mode
  984. # Subroutines for math mode
  985. # Typesetting math formulas
  986. #
  987. # Many of the docstrings below refer to a numbered "node" in that
  988. # book, e.g., node123
  989. #
  990. # Note that (as TeX) y increases downward, unlike many other parts of
  991. # matplotlib.
  992. # How much text shrinks when going to the next-smallest level. GROW_FACTOR
  993. # must be the inverse of SHRINK_FACTOR.
  994. SHRINK_FACTOR = 0.7
  995. GROW_FACTOR = 1.0 / SHRINK_FACTOR
  996. # The number of different sizes of chars to use, beyond which they will not
  997. # get any smaller
  998. NUM_SIZE_LEVELS = 6
  999. # Percentage of x-height of additional horiz. space after sub/superscripts
  1000. SCRIPT_SPACE = 0.2
  1001. # Percentage of x-height that sub/superscripts drop below the baseline
  1002. SUBDROP = 0.3
  1003. # Percentage of x-height that superscripts drop below the baseline
  1004. SUP1 = 0.5
  1005. # Percentage of x-height that subscripts drop below the baseline
  1006. SUB1 = 0.0
  1007. # Percentage of x-height that superscripts are offset relative to the subscript
  1008. DELTA = 0.18
  1009. class MathTextWarning(Warning):
  1010. pass
  1011. class Node(object):
  1012. """
  1013. A node in the TeX box model
  1014. """
  1015. def __init__(self):
  1016. self.size = 0
  1017. def __repr__(self):
  1018. return self.__internal_repr__()
  1019. def __internal_repr__(self):
  1020. return self.__class__.__name__
  1021. def get_kerning(self, next):
  1022. return 0.0
  1023. def shrink(self):
  1024. """
  1025. Shrinks one level smaller. There are only three levels of
  1026. sizes, after which things will no longer get smaller.
  1027. """
  1028. self.size += 1
  1029. def grow(self):
  1030. """
  1031. Grows one level larger. There is no limit to how big
  1032. something can get.
  1033. """
  1034. self.size -= 1
  1035. def render(self, x, y):
  1036. pass
  1037. class Box(Node):
  1038. """
  1039. Represents any node with a physical location.
  1040. """
  1041. def __init__(self, width, height, depth):
  1042. Node.__init__(self)
  1043. self.width = width
  1044. self.height = height
  1045. self.depth = depth
  1046. def shrink(self):
  1047. Node.shrink(self)
  1048. if self.size < NUM_SIZE_LEVELS:
  1049. self.width *= SHRINK_FACTOR
  1050. self.height *= SHRINK_FACTOR
  1051. self.depth *= SHRINK_FACTOR
  1052. def grow(self):
  1053. Node.grow(self)
  1054. self.width *= GROW_FACTOR
  1055. self.height *= GROW_FACTOR
  1056. self.depth *= GROW_FACTOR
  1057. def render(self, x1, y1, x2, y2):
  1058. pass
  1059. class Vbox(Box):
  1060. """
  1061. A box with only height (zero width).
  1062. """
  1063. def __init__(self, height, depth):
  1064. Box.__init__(self, 0., height, depth)
  1065. class Hbox(Box):
  1066. """
  1067. A box with only width (zero height and depth).
  1068. """
  1069. def __init__(self, width):
  1070. Box.__init__(self, width, 0., 0.)
  1071. class Char(Node):
  1072. """
  1073. Represents a single character. Unlike TeX, the font information
  1074. and metrics are stored with each :class:`Char` to make it easier
  1075. to lookup the font metrics when needed. Note that TeX boxes have
  1076. a width, height, and depth, unlike Type1 and Truetype which use a
  1077. full bounding box and an advance in the x-direction. The metrics
  1078. must be converted to the TeX way, and the advance (if different
  1079. from width) must be converted into a :class:`Kern` node when the
  1080. :class:`Char` is added to its parent :class:`Hlist`.
  1081. """
  1082. def __init__(self, c, state):
  1083. Node.__init__(self)
  1084. self.c = c
  1085. self.font_output = state.font_output
  1086. assert isinstance(state.font, (six.string_types, int))
  1087. self.font = state.font
  1088. self.font_class = state.font_class
  1089. self.fontsize = state.fontsize
  1090. self.dpi = state.dpi
  1091. # The real width, height and depth will be set during the
  1092. # pack phase, after we know the real fontsize
  1093. self._update_metrics()
  1094. def __internal_repr__(self):
  1095. return '`%s`' % self.c
  1096. def _update_metrics(self):
  1097. metrics = self._metrics = self.font_output.get_metrics(
  1098. self.font, self.font_class, self.c, self.fontsize, self.dpi)
  1099. if self.c == ' ':
  1100. self.width = metrics.advance
  1101. else:
  1102. self.width = metrics.width
  1103. self.height = metrics.iceberg
  1104. self.depth = -(metrics.iceberg - metrics.height)
  1105. def is_slanted(self):
  1106. return self._metrics.slanted
  1107. def get_kerning(self, next):
  1108. """
  1109. Return the amount of kerning between this and the given
  1110. character. Called when characters are strung together into
  1111. :class:`Hlist` to create :class:`Kern` nodes.
  1112. """
  1113. advance = self._metrics.advance - self.width
  1114. kern = 0.
  1115. if isinstance(next, Char):
  1116. kern = self.font_output.get_kern(
  1117. self.font, self.font_class, self.c, self.fontsize,
  1118. next.font, next.font_class, next.c, next.fontsize,
  1119. self.dpi)
  1120. return advance + kern
  1121. def render(self, x, y):
  1122. """
  1123. Render the character to the canvas
  1124. """
  1125. self.font_output.render_glyph(
  1126. x, y,
  1127. self.font, self.font_class, self.c, self.fontsize, self.dpi)
  1128. def shrink(self):
  1129. Node.shrink(self)
  1130. if self.size < NUM_SIZE_LEVELS:
  1131. self.fontsize *= SHRINK_FACTOR
  1132. self.width *= SHRINK_FACTOR
  1133. self.height *= SHRINK_FACTOR
  1134. self.depth *= SHRINK_FACTOR
  1135. def grow(self):
  1136. Node.grow(self)
  1137. self.fontsize *= GROW_FACTOR
  1138. self.width *= GROW_FACTOR
  1139. self.height *= GROW_FACTOR
  1140. self.depth *= GROW_FACTOR
  1141. class Accent(Char):
  1142. """
  1143. The font metrics need to be dealt with differently for accents,
  1144. since they are already offset correctly from the baseline in
  1145. TrueType fonts.
  1146. """
  1147. def _update_metrics(self):
  1148. metrics = self._metrics = self.font_output.get_metrics(
  1149. self.font, self.font_class, self.c, self.fontsize, self.dpi)
  1150. self.width = metrics.xmax - metrics.xmin
  1151. self.height = metrics.ymax - metrics.ymin
  1152. self.depth = 0
  1153. def shrink(self):
  1154. Char.shrink(self)
  1155. self._update_metrics()
  1156. def grow(self):
  1157. Char.grow(self)
  1158. self._update_metrics()
  1159. def render(self, x, y):
  1160. """
  1161. Render the character to the canvas.
  1162. """
  1163. self.font_output.render_glyph(
  1164. x - self._metrics.xmin, y + self._metrics.ymin,
  1165. self.font, self.font_class, self.c, self.fontsize, self.dpi)
  1166. class List(Box):
  1167. """
  1168. A list of nodes (either horizontal or vertical).
  1169. """
  1170. def __init__(self, elements):
  1171. Box.__init__(self, 0., 0., 0.)
  1172. self.shift_amount = 0. # An arbitrary offset
  1173. self.children = elements # The child nodes of this list
  1174. # The following parameters are set in the vpack and hpack functions
  1175. self.glue_set = 0. # The glue setting of this list
  1176. self.glue_sign = 0 # 0: normal, -1: shrinking, 1: stretching
  1177. self.glue_order = 0 # The order of infinity (0 - 3) for the glue
  1178. def __repr__(self):
  1179. return '[%s <%.02f %.02f %.02f %.02f> %s]' % (
  1180. self.__internal_repr__(),
  1181. self.width, self.height,
  1182. self.depth, self.shift_amount,
  1183. ' '.join([repr(x) for x in self.children]))
  1184. def _determine_order(self, totals):
  1185. """
  1186. A helper function to determine the highest order of glue
  1187. used by the members of this list. Used by vpack and hpack.
  1188. """
  1189. o = 0
  1190. for i in range(len(totals) - 1, 0, -1):
  1191. if totals[i] != 0.0:
  1192. o = i
  1193. break
  1194. return o
  1195. def _set_glue(self, x, sign, totals, error_type):
  1196. o = self._determine_order(totals)
  1197. self.glue_order = o
  1198. self.glue_sign = sign
  1199. if totals[o] != 0.:
  1200. self.glue_set = x / totals[o]
  1201. else:
  1202. self.glue_sign = 0
  1203. self.glue_ratio = 0.
  1204. if o == 0:
  1205. if len(self.children):
  1206. warn("%s %s: %r" % (error_type, self.__class__.__name__, self),
  1207. MathTextWarning)
  1208. def shrink(self):
  1209. for child in self.children:
  1210. child.shrink()
  1211. Box.shrink(self)
  1212. if self.size < NUM_SIZE_LEVELS:
  1213. self.shift_amount *= SHRINK_FACTOR
  1214. self.glue_set *= SHRINK_FACTOR
  1215. def grow(self):
  1216. for child in self.children:
  1217. child.grow()
  1218. Box.grow(self)
  1219. self.shift_amount *= GROW_FAC