-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathmathtext.py
More file actions
605 lines (497 loc) · 19.2 KB
/
mathtext.py
File metadata and controls
605 lines (497 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
r"""
A module for parsing a subset of the TeX math syntax and rendering it to a
Matplotlib backend.
For a tutorial of its usage, see :doc:`/tutorials/text/mathtext`. This
document is primarily concerned with implementation details.
The module uses pyparsing_ to parse the TeX expression.
.. _pyparsing: https://pypi.org/project/pyparsing/
The Bakoma distribution of the TeX Computer Modern fonts, and STIX
fonts are supported. There is experimental support for using
arbitrary fonts, but results may vary without proper tweaking and
metrics for those fonts.
"""
from collections import namedtuple
import functools
from io import StringIO
import logging
import types
import numpy as np
from PIL import Image
from matplotlib import (
_api, colors as mcolors, rcParams, _mathtext, _mathtext_data)
from matplotlib.ft2font import FT2Image, LOAD_NO_HINTING
from matplotlib.font_manager import FontProperties
_log = logging.getLogger(__name__)
@_api.caching_module_getattr
class __getattr__:
locals().update({
name: _api.deprecated("3.4")(
property(lambda self, _mod=mod, _name=name: getattr(_mod, _name)))
for mod, names in [
(_mathtext, ["SHRINK_FACTOR", "GROW_FACTOR", "NUM_SIZE_LEVELS"]),
(_mathtext_data, [
"latex_to_bakoma", "latex_to_cmex", "latex_to_standard",
"stix_virtual_fonts", "tex2uni"])]
for name in names})
get_unicode_index = _mathtext.get_unicode_index
get_unicode_index.__module__ = __name__
class MathtextBackend:
"""
The base class for the mathtext backend-specific code. `MathtextBackend`
subclasses interface between mathtext and specific Matplotlib graphics
backends.
Subclasses need to override the following:
- :meth:`render_glyph`
- :meth:`render_rect_filled`
- :meth:`get_results`
And optionally, if you need to use a FreeType hinting style:
- :meth:`get_hinting_type`
"""
def __init__(self):
self.width = 0
self.height = 0
self.depth = 0
def set_canvas_size(self, w, h, d):
"""Set the dimension of the drawing canvas."""
self.width = w
self.height = h
self.depth = d
def render_glyph(self, ox, oy, info):
"""
Draw a glyph described by *info* to the reference point (*ox*,
*oy*).
"""
raise NotImplementedError()
def render_rect_filled(self, x1, y1, x2, y2):
"""
Draw a filled black rectangle from (*x1*, *y1*) to (*x2*, *y2*).
"""
raise NotImplementedError()
def get_results(self, box):
"""
Return a backend-specific tuple to return to the backend after
all processing is done.
"""
raise NotImplementedError()
def get_hinting_type(self):
"""
Get the FreeType hinting type to use with this particular
backend.
"""
return LOAD_NO_HINTING
class MathtextBackendAgg(MathtextBackend):
"""
Render glyphs and rectangles to an FTImage buffer, which is later
transferred to the Agg image by the Agg backend.
"""
def __init__(self):
self.ox = 0
self.oy = 0
self.image = None
self.mode = 'bbox'
self.bbox = [0, 0, 0, 0]
super().__init__()
def _update_bbox(self, x1, y1, x2, y2):
self.bbox = [min(self.bbox[0], x1),
min(self.bbox[1], y1),
max(self.bbox[2], x2),
max(self.bbox[3], y2)]
def set_canvas_size(self, w, h, d):
super().set_canvas_size(w, h, d)
if self.mode != 'bbox':
self.image = FT2Image(np.ceil(w), np.ceil(h + max(d, 0)))
def render_glyph(self, ox, oy, info):
if self.mode == 'bbox':
self._update_bbox(ox + info.metrics.xmin,
oy - info.metrics.ymax,
ox + info.metrics.xmax,
oy - info.metrics.ymin)
else:
info.font.draw_glyph_to_bitmap(
self.image, ox, oy - info.metrics.iceberg, info.glyph,
antialiased=rcParams['text.antialiased'])
def render_rect_filled(self, x1, y1, x2, y2):
if self.mode == 'bbox':
self._update_bbox(x1, y1, x2, y2)
else:
height = max(int(y2 - y1) - 1, 0)
if height == 0:
center = (y2 + y1) / 2.0
y = int(center - (height + 1) / 2.0)
else:
y = int(y1)
self.image.draw_rect_filled(int(x1), y, np.ceil(x2), y + height)
def get_results(self, box, used_characters):
self.mode = 'bbox'
orig_height = box.height
orig_depth = box.depth
_mathtext.ship(0, 0, box)
bbox = self.bbox
bbox = [bbox[0] - 1, bbox[1] - 1, bbox[2] + 1, bbox[3] + 1]
self.mode = 'render'
self.set_canvas_size(
bbox[2] - bbox[0],
(bbox[3] - bbox[1]) - orig_depth,
(bbox[3] - bbox[1]) - orig_height)
_mathtext.ship(-bbox[0], -bbox[1], box)
result = (self.ox,
self.oy,
self.width,
self.height + self.depth,
self.depth,
self.image,
used_characters)
self.image = None
return result
def get_hinting_type(self):
from matplotlib.backends import backend_agg
return backend_agg.get_hinting_flag()
@_api.deprecated("3.4", alternative="`.mathtext.math_to_image`")
class MathtextBackendBitmap(MathtextBackendAgg):
def get_results(self, box, used_characters):
ox, oy, width, height, depth, image, characters = \
super().get_results(box, used_characters)
return image, depth
@_api.deprecated("3.4", alternative="`.MathtextBackendPath`")
class MathtextBackendPs(MathtextBackend):
"""
Store information to write a mathtext rendering to the PostScript backend.
"""
_PSResult = namedtuple(
"_PSResult", "width height depth pswriter used_characters")
def __init__(self):
super().__init__()
self.pswriter = StringIO()
self.lastfont = None
def render_glyph(self, ox, oy, info):
oy = self.height - oy + info.offset
postscript_name = info.postscript_name
fontsize = info.fontsize
if (postscript_name, fontsize) != self.lastfont:
self.lastfont = postscript_name, fontsize
self.pswriter.write(
f"/{postscript_name} findfont\n"
f"{fontsize} scalefont\n"
f"setfont\n")
self.pswriter.write(
f"{ox:f} {oy:f} moveto\n"
f"/{info.symbol_name} glyphshow\n")
def render_rect_filled(self, x1, y1, x2, y2):
ps = "%f %f %f %f rectfill\n" % (
x1, self.height - y2, x2 - x1, y2 - y1)
self.pswriter.write(ps)
def get_results(self, box, used_characters):
_mathtext.ship(0, 0, box)
return self._PSResult(self.width,
self.height + self.depth,
self.depth,
self.pswriter,
used_characters)
@_api.deprecated("3.4", alternative="`.MathtextBackendPath`")
class MathtextBackendPdf(MathtextBackend):
"""Store information to write a mathtext rendering to the PDF backend."""
_PDFResult = namedtuple(
"_PDFResult", "width height depth glyphs rects used_characters")
def __init__(self):
super().__init__()
self.glyphs = []
self.rects = []
def render_glyph(self, ox, oy, info):
filename = info.font.fname
oy = self.height - oy + info.offset
self.glyphs.append(
(ox, oy, filename, info.fontsize,
info.num, info.symbol_name))
def render_rect_filled(self, x1, y1, x2, y2):
self.rects.append((x1, self.height - y2, x2 - x1, y2 - y1))
def get_results(self, box, used_characters):
_mathtext.ship(0, 0, box)
return self._PDFResult(self.width,
self.height + self.depth,
self.depth,
self.glyphs,
self.rects,
used_characters)
@_api.deprecated("3.4", alternative="`.MathtextBackendPath`")
class MathtextBackendSvg(MathtextBackend):
"""
Store information to write a mathtext rendering to the SVG
backend.
"""
def __init__(self):
super().__init__()
self.svg_glyphs = []
self.svg_rects = []
def render_glyph(self, ox, oy, info):
oy = self.height - oy + info.offset
self.svg_glyphs.append(
(info.font, info.fontsize, info.num, ox, oy, info.metrics))
def render_rect_filled(self, x1, y1, x2, y2):
self.svg_rects.append(
(x1, self.height - y1 + 1, x2 - x1, y2 - y1))
def get_results(self, box, used_characters):
_mathtext.ship(0, 0, box)
svg_elements = types.SimpleNamespace(svg_glyphs=self.svg_glyphs,
svg_rects=self.svg_rects)
return (self.width,
self.height + self.depth,
self.depth,
svg_elements,
used_characters)
class MathtextBackendPath(MathtextBackend):
"""
Store information to write a mathtext rendering to the text path
machinery.
"""
_Result = namedtuple("_Result", "width height depth glyphs rects")
def __init__(self):
super().__init__()
self.glyphs = []
self.rects = []
def render_glyph(self, ox, oy, info):
oy = self.height - oy + info.offset
self.glyphs.append((info.font, info.fontsize, info.num, ox, oy))
def render_rect_filled(self, x1, y1, x2, y2):
self.rects.append((x1, self.height - y2, x2 - x1, y2 - y1))
def get_results(self, box, used_characters):
_mathtext.ship(0, 0, box)
return self._Result(self.width,
self.height + self.depth,
self.depth,
self.glyphs,
self.rects)
@_api.deprecated("3.4", alternative="`.MathtextBackendPath`")
class MathtextBackendCairo(MathtextBackend):
"""
Store information to write a mathtext rendering to the Cairo
backend.
"""
def __init__(self):
super().__init__()
self.glyphs = []
self.rects = []
def render_glyph(self, ox, oy, info):
oy = oy - info.offset - self.height
thetext = chr(info.num)
self.glyphs.append(
(info.font, info.fontsize, thetext, ox, oy))
def render_rect_filled(self, x1, y1, x2, y2):
self.rects.append(
(x1, y1 - self.height, x2 - x1, y2 - y1))
def get_results(self, box, used_characters):
_mathtext.ship(0, 0, box)
return (self.width,
self.height + self.depth,
self.depth,
self.glyphs,
self.rects)
for _cls_name in [
"Fonts",
*[c.__name__ for c in _mathtext.Fonts.__subclasses__()],
"FontConstantsBase",
*[c.__name__ for c in _mathtext.FontConstantsBase.__subclasses__()],
"Node",
*[c.__name__ for c in _mathtext.Node.__subclasses__()],
"Ship", "Parser",
]:
globals()[_cls_name] = _api.deprecated("3.4")(
type(_cls_name, (getattr(_mathtext, _cls_name),), {}))
class MathTextWarning(Warning):
pass
@_api.deprecated("3.4")
def ship(ox, oy, box):
_mathtext.ship(ox, oy, box)
##############################################################################
# MAIN
class MathTextParser:
_parser = None
_backend_mapping = {
'bitmap': MathtextBackendBitmap,
'agg': MathtextBackendAgg,
'ps': MathtextBackendPs,
'pdf': MathtextBackendPdf,
'svg': MathtextBackendSvg,
'path': MathtextBackendPath,
'cairo': MathtextBackendCairo,
'macosx': MathtextBackendAgg,
}
_font_type_mapping = {
'cm': _mathtext.BakomaFonts,
'dejavuserif': _mathtext.DejaVuSerifFonts,
'dejavusans': _mathtext.DejaVuSansFonts,
'stix': _mathtext.StixFonts,
'stixsans': _mathtext.StixSansFonts,
'custom': _mathtext.UnicodeFonts,
}
def __init__(self, output):
"""Create a MathTextParser for the given backend *output*."""
self._output = output.lower()
def parse(self, s, dpi=72, prop=None, *, _force_standard_ps_fonts=False):
"""
Parse the given math expression *s* at the given *dpi*. If *prop* is
provided, it is a `.FontProperties` object specifying the "default"
font to use in the math expression, used for all non-math text.
The results are cached, so multiple calls to `parse`
with the same expression should be fast.
"""
if _force_standard_ps_fonts:
_api.warn_deprecated(
"3.4",
removal="3.5",
message=(
"Mathtext using only standard PostScript fonts has "
"been likely to produce wrong output for a while, "
"has been deprecated in %(since)s and will be removed "
"in %(removal)s, after which ps.useafm will have no "
"effect on mathtext."
)
)
# lru_cache can't decorate parse() directly because the ps.useafm and
# mathtext.fontset rcParams also affect the parse (e.g. by affecting
# the glyph metrics).
return self._parse_cached(s, dpi, prop, _force_standard_ps_fonts)
@functools.lru_cache(50)
def _parse_cached(self, s, dpi, prop, force_standard_ps_fonts):
if prop is None:
prop = FontProperties()
fontset_class = (
_mathtext.StandardPsFonts if force_standard_ps_fonts
else _api.check_getitem(
self._font_type_mapping, fontset=prop.get_math_fontfamily()))
backend = self._backend_mapping[self._output]()
font_output = fontset_class(prop, backend)
fontsize = prop.get_size_in_points()
# This is a class variable so we don't rebuild the parser
# with each request.
if self._parser is None:
self.__class__._parser = _mathtext.Parser()
box = self._parser.parse(s, font_output, fontsize, dpi)
font_output.set_canvas_size(box.width, box.height, box.depth)
return font_output.get_results(box)
@_api.deprecated("3.4", alternative="`.mathtext.math_to_image`")
def to_mask(self, texstr, dpi=120, fontsize=14):
r"""
Convert a mathtext string to a grayscale array and depth.
Parameters
----------
texstr : str
A valid mathtext string, e.g., r'IQ: $\sigma_i=15$'.
dpi : float
The dots-per-inch setting used to render the text.
fontsize : int
The font size in points
Returns
-------
array : 2D uint8 alpha
Mask array of rasterized tex.
depth : int
Offset of the baseline from the bottom of the image, in pixels.
"""
assert self._output == "bitmap"
prop = FontProperties(size=fontsize)
ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)
return np.asarray(ftimage), depth
@_api.deprecated("3.4", alternative="`.mathtext.math_to_image`")
def to_rgba(self, texstr, color='black', dpi=120, fontsize=14):
r"""
Convert a mathtext string to an RGBA array and depth.
Parameters
----------
texstr : str
A valid mathtext string, e.g., r'IQ: $\sigma_i=15$'.
color : color
The text color.
dpi : float
The dots-per-inch setting used to render the text.
fontsize : int
The font size in points.
Returns
-------
array : (M, N, 4) array
RGBA color values of rasterized tex, colorized with *color*.
depth : int
Offset of the baseline from the bottom of the image, in pixels.
"""
x, depth = self.to_mask(texstr, dpi=dpi, fontsize=fontsize)
r, g, b, a = mcolors.to_rgba(color)
RGBA = np.zeros((x.shape[0], x.shape[1], 4), dtype=np.uint8)
RGBA[:, :, 0] = 255 * r
RGBA[:, :, 1] = 255 * g
RGBA[:, :, 2] = 255 * b
RGBA[:, :, 3] = x
return RGBA, depth
@_api.deprecated("3.4", alternative="`.mathtext.math_to_image`")
def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14):
r"""
Render a tex expression to a PNG file.
Parameters
----------
filename
A writable filename or fileobject.
texstr : str
A valid mathtext string, e.g., r'IQ: $\sigma_i=15$'.
color : color
The text color.
dpi : float
The dots-per-inch setting used to render the text.
fontsize : int
The font size in points.
Returns
-------
int
Offset of the baseline from the bottom of the image, in pixels.
"""
rgba, depth = self.to_rgba(
texstr, color=color, dpi=dpi, fontsize=fontsize)
Image.fromarray(rgba).save(filename, format="png")
return depth
@_api.deprecated("3.4", alternative="`.mathtext.math_to_image`")
def get_depth(self, texstr, dpi=120, fontsize=14):
r"""
Get the depth of a mathtext string.
Parameters
----------
texstr : str
A valid mathtext string, e.g., r'IQ: $\sigma_i=15$'.
dpi : float
The dots-per-inch setting used to render the text.
Returns
-------
int
Offset of the baseline from the bottom of the image, in pixels.
"""
assert self._output == "bitmap"
prop = FontProperties(size=fontsize)
ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)
return depth
def math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None):
"""
Given a math expression, renders it in a closely-clipped bounding
box to an image file.
Parameters
----------
s : str
A math expression. The math portion must be enclosed in dollar signs.
filename_or_obj : str or path-like or file-like
Where to write the image data.
prop : `.FontProperties`, optional
The size and style of the text.
dpi : float, optional
The output dpi. If not set, the dpi is determined as for
`.Figure.savefig`.
format : str, optional
The output format, e.g., 'svg', 'pdf', 'ps' or 'png'. If not set, the
format is determined as for `.Figure.savefig`.
"""
from matplotlib import figure
# backend_agg supports all of the core output formats
from matplotlib.backends import backend_agg
if prop is None:
prop = FontProperties()
parser = MathTextParser('path')
width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop)
fig = figure.Figure(figsize=(width / 72.0, height / 72.0))
fig.text(0, depth/height, s, fontproperties=prop)
backend_agg.FigureCanvasAgg(fig)
fig.savefig(filename_or_obj, dpi=dpi, format=format)
return depth