Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a86c83d

Browse files
committed
Better positioning of mathtext glyphs.
1 parent 1c20824 commit a86c83d

File tree

1 file changed

+53
-54
lines changed

1 file changed

+53
-54
lines changed

lib/matplotlib/mathtext.py

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -101,70 +101,69 @@ class MathtextBackendAgg(MathtextBackend):
101101
Render glyphs and rectangles to an FTImage buffer, which is later
102102
transferred to the Agg image by the Agg backend.
103103
"""
104-
def __init__(self):
105-
self.ox = 0
106-
self.oy = 0
107-
self.image = None
108-
self.mode = 'bbox'
109-
self.bbox = [0, 0, 0, 0]
110-
super().__init__()
111-
112-
def _update_bbox(self, x1, y1, x2, y2):
113-
self.bbox = [min(self.bbox[0], x1),
114-
min(self.bbox[1], y1),
115-
max(self.bbox[2], x2),
116-
max(self.bbox[3], y2)]
117104

118-
def set_canvas_size(self, w, h, d):
119-
super().set_canvas_size(w, h, d)
120-
if self.mode != 'bbox':
121-
self.image = FT2Image(np.ceil(w), np.ceil(h + max(d, 0)))
105+
def __init__(self):
106+
MathtextBackend.__init__(self)
107+
self._xmin = self._ymin = np.inf
108+
self._xmax = self._ymax = -np.inf
109+
self._glyphs = []
110+
self._rects = []
111+
112+
ox = cbook.deprecated("3.4")(property(lambda self: 0))
113+
oy = cbook.deprecated("3.4")(property(lambda self: 0))
114+
mode = cbook.deprecated("3.4")(property(lambda self: "bbox"))
115+
image = cbook.deprecated("3.4")(property(lambda self: None))
116+
bbox = cbook.deprecated("3.4")(property(
117+
lambda self: [self._xmin, self._ymin, self._xmax, self._ymax]))
122118

123119
def render_glyph(self, ox, oy, info):
124-
if self.mode == 'bbox':
125-
self._update_bbox(ox + info.metrics.xmin,
126-
oy - info.metrics.ymax,
127-
ox + info.metrics.xmax,
128-
oy - info.metrics.ymin)
129-
else:
130-
info.font.draw_glyph_to_bitmap(
131-
self.image, ox, oy - info.metrics.iceberg, info.glyph,
132-
antialiased=rcParams['text.antialiased'])
120+
self._glyphs.append((ox, oy, info))
121+
metrics = info.metrics
122+
self._xmin = min(self._xmin, ox + metrics.xmin)
123+
self._ymin = min(self._ymin, self.height - oy + metrics.ymin)
124+
self._xmax = max(self._xmax, ox + metrics.xmax)
125+
self._ymax = max(self._ymax, self.height - oy + metrics.ymax)
133126

134127
def render_rect_filled(self, x1, y1, x2, y2):
135-
if self.mode == 'bbox':
136-
self._update_bbox(x1, y1, x2, y2)
137-
else:
128+
self._rects.append((x1, y1, x2, y2))
129+
self._xmin = min(self._xmin, x1)
130+
self._xmax = max(self._xmax, x2)
131+
self._ymin = min(self._ymin, y1)
132+
self._ymax = max(self._ymax, y2)
133+
134+
def get_results(self, box, used_characters):
135+
orig_height = box.height
136+
orig_depth = box.depth
137+
_mathtext.ship(0, 0, box)
138+
xmin = np.floor(self._xmin)
139+
xmax = np.ceil(self._xmax)
140+
ymin = np.floor(self._ymin)
141+
ymax = np.ceil(self._ymax)
142+
dxmin = self._xmin - xmin
143+
dymin = self._ymin - ymin
144+
image = FT2Image(np.ceil(xmax - xmin) + 1, np.ceil(ymax - ymin) + 1)
145+
146+
for ox, oy, info in self._glyphs:
147+
info.font.draw_glyph_to_bitmap(
148+
image, ox + dxmin, oy - info.metrics.iceberg + dymin,
149+
info.glyph, antialiased=rcParams['text.antialiased'])
150+
for x1, y1, x2, y2 in self._rects:
151+
x1 += dxmin
152+
x2 += dymin
153+
y1 += dxmin
154+
y2 += dymin
138155
height = max(int(y2 - y1) - 1, 0)
139156
if height == 0:
140-
center = (y2 + y1) / 2.0
141-
y = int(center - (height + 1) / 2.0)
157+
center = (y2 + y1) / 2
158+
y = int(center - (height + 1) / 2)
142159
else:
143160
y = int(y1)
144-
self.image.draw_rect_filled(int(x1), y, np.ceil(x2), y + height)
161+
image.draw_rect_filled(int(x1), y, np.ceil(x2), y + height)
145162

146-
def get_results(self, box, used_characters):
147-
self.mode = 'bbox'
148-
orig_height = box.height
149-
orig_depth = box.depth
150-
_mathtext.ship(0, 0, box)
151-
bbox = self.bbox
152-
bbox = [bbox[0] - 1, bbox[1] - 1, bbox[2] + 1, bbox[3] + 1]
153-
self.mode = 'render'
154-
self.set_canvas_size(
155-
bbox[2] - bbox[0],
156-
(bbox[3] - bbox[1]) - orig_depth,
157-
(bbox[3] - bbox[1]) - orig_height)
158-
_mathtext.ship(-bbox[0], -bbox[1], box)
159-
result = (self.ox,
160-
self.oy,
161-
self.width,
162-
self.height + self.depth,
163-
self.depth,
164-
self.image,
165-
used_characters)
166-
self.image = None
167-
return result
163+
return (0, 0,
164+
np.ceil(xmax - xmin), np.ceil(ymax - ymin), -ymin,
165+
image,
166+
used_characters)
168167

169168
def get_hinting_type(self):
170169
from matplotlib.backends import backend_agg

0 commit comments

Comments
 (0)