PageRenderTime 131ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/matplotlib/mathtext.py

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