|
15 | 15 | metrics for those fonts. |
16 | 16 | """ |
17 | 17 |
|
18 | | -from collections import namedtuple |
19 | 18 | import functools |
20 | 19 | import logging |
21 | 20 |
|
22 | | -import numpy as np |
23 | | - |
24 | | -import matplotlib as mpl |
25 | 21 | from matplotlib import _api, _mathtext |
26 | | -from matplotlib.ft2font import FT2Image, LOAD_NO_HINTING |
| 22 | +from matplotlib.ft2font import LOAD_NO_HINTING |
27 | 23 | from matplotlib.font_manager import FontProperties |
28 | 24 | from ._mathtext import ( # noqa: reexported API |
29 | 25 | RasterParse, VectorParse, get_unicode_index) |
|
33 | 29 |
|
34 | 30 | get_unicode_index.__module__ = __name__ |
35 | 31 |
|
36 | | - |
37 | | -@_api.deprecated("3.6") |
38 | | -class MathtextBackend: |
39 | | - """ |
40 | | - The base class for the mathtext backend-specific code. `MathtextBackend` |
41 | | - subclasses interface between mathtext and specific Matplotlib graphics |
42 | | - backends. |
43 | | -
|
44 | | - Subclasses need to override the following: |
45 | | -
|
46 | | - - :meth:`render_glyph` |
47 | | - - :meth:`render_rect_filled` |
48 | | - - :meth:`get_results` |
49 | | -
|
50 | | - And optionally, if you need to use a FreeType hinting style: |
51 | | -
|
52 | | - - :meth:`get_hinting_type` |
53 | | - """ |
54 | | - def __init__(self): |
55 | | - self.width = 0 |
56 | | - self.height = 0 |
57 | | - self.depth = 0 |
58 | | - |
59 | | - def set_canvas_size(self, w, h, d): |
60 | | - """Set the dimension of the drawing canvas.""" |
61 | | - self.width = w |
62 | | - self.height = h |
63 | | - self.depth = d |
64 | | - |
65 | | - def render_glyph(self, ox, oy, info): |
66 | | - """ |
67 | | - Draw a glyph described by *info* to the reference point (*ox*, |
68 | | - *oy*). |
69 | | - """ |
70 | | - raise NotImplementedError() |
71 | | - |
72 | | - def render_rect_filled(self, x1, y1, x2, y2): |
73 | | - """ |
74 | | - Draw a filled black rectangle from (*x1*, *y1*) to (*x2*, *y2*). |
75 | | - """ |
76 | | - raise NotImplementedError() |
77 | | - |
78 | | - def get_results(self, box): |
79 | | - """ |
80 | | - Return a backend-specific tuple to return to the backend after |
81 | | - all processing is done. |
82 | | - """ |
83 | | - raise NotImplementedError() |
84 | | - |
85 | | - def get_hinting_type(self): |
86 | | - """ |
87 | | - Get the FreeType hinting type to use with this particular |
88 | | - backend. |
89 | | - """ |
90 | | - return LOAD_NO_HINTING |
91 | | - |
92 | | - |
93 | | -@_api.deprecated("3.6") |
94 | | -class MathtextBackendAgg(MathtextBackend): |
95 | | - """ |
96 | | - Render glyphs and rectangles to an FTImage buffer, which is later |
97 | | - transferred to the Agg image by the Agg backend. |
98 | | - """ |
99 | | - def __init__(self): |
100 | | - self.ox = 0 |
101 | | - self.oy = 0 |
102 | | - self.image = None |
103 | | - self.mode = 'bbox' |
104 | | - self.bbox = [0, 0, 0, 0] |
105 | | - super().__init__() |
106 | | - |
107 | | - def _update_bbox(self, x1, y1, x2, y2): |
108 | | - self.bbox = [min(self.bbox[0], x1), |
109 | | - min(self.bbox[1], y1), |
110 | | - max(self.bbox[2], x2), |
111 | | - max(self.bbox[3], y2)] |
112 | | - |
113 | | - def set_canvas_size(self, w, h, d): |
114 | | - super().set_canvas_size(w, h, d) |
115 | | - if self.mode != 'bbox': |
116 | | - self.image = FT2Image(np.ceil(w), np.ceil(h + max(d, 0))) |
117 | | - |
118 | | - def render_glyph(self, ox, oy, info): |
119 | | - if self.mode == 'bbox': |
120 | | - self._update_bbox(ox + info.metrics.xmin, |
121 | | - oy - info.metrics.ymax, |
122 | | - ox + info.metrics.xmax, |
123 | | - oy - info.metrics.ymin) |
124 | | - else: |
125 | | - info.font.draw_glyph_to_bitmap( |
126 | | - self.image, ox, oy - info.metrics.iceberg, info.glyph, |
127 | | - antialiased=mpl.rcParams['text.antialiased']) |
128 | | - |
129 | | - def render_rect_filled(self, x1, y1, x2, y2): |
130 | | - if self.mode == 'bbox': |
131 | | - self._update_bbox(x1, y1, x2, y2) |
132 | | - else: |
133 | | - height = max(int(y2 - y1) - 1, 0) |
134 | | - if height == 0: |
135 | | - center = (y2 + y1) / 2.0 |
136 | | - y = int(center - (height + 1) / 2.0) |
137 | | - else: |
138 | | - y = int(y1) |
139 | | - self.image.draw_rect_filled(int(x1), y, np.ceil(x2), y + height) |
140 | | - |
141 | | - def get_results(self, box): |
142 | | - self.image = None |
143 | | - self.mode = 'render' |
144 | | - return _mathtext.ship(box).to_raster() |
145 | | - |
146 | | - def get_hinting_type(self): |
147 | | - from matplotlib.backends import backend_agg |
148 | | - return backend_agg.get_hinting_flag() |
149 | | - |
150 | | - |
151 | | -@_api.deprecated("3.6") |
152 | | -class MathtextBackendPath(MathtextBackend): |
153 | | - """ |
154 | | - Store information to write a mathtext rendering to the text path |
155 | | - machinery. |
156 | | - """ |
157 | | - |
158 | | - _Result = namedtuple("_Result", "width height depth glyphs rects") |
159 | | - |
160 | | - def __init__(self): |
161 | | - super().__init__() |
162 | | - self.glyphs = [] |
163 | | - self.rects = [] |
164 | | - |
165 | | - def render_glyph(self, ox, oy, info): |
166 | | - oy = self.height - oy + info.offset |
167 | | - self.glyphs.append((info.font, info.fontsize, info.num, ox, oy)) |
168 | | - |
169 | | - def render_rect_filled(self, x1, y1, x2, y2): |
170 | | - self.rects.append((x1, self.height - y2, x2 - x1, y2 - y1)) |
171 | | - |
172 | | - def get_results(self, box): |
173 | | - return _mathtext.ship(box).to_vector() |
174 | | - |
175 | | - |
176 | | -@_api.deprecated("3.6") |
177 | | -class MathTextWarning(Warning): |
178 | | - pass |
179 | | - |
180 | | - |
181 | 32 | ############################################################################## |
182 | 33 | # MAIN |
183 | 34 |
|
|
0 commit comments