12
12
* alpha blending
13
13
* DPI scaling properly - everything scales properly (dashes, linewidths, etc)
14
14
* draw polygon
15
- * freetype2 w/ ft2font
15
+ * freetype2
16
16
17
17
TODO:
18
18
28
28
import numpy as np
29
29
from collections import OrderedDict
30
30
from math import radians , cos , sin
31
- from matplotlib import cbook , rcParams , __version__
31
+
32
+ from matplotlib import (
33
+ _ft2 , _png , cbook , colors as mcolors , font_manager , rcParams , __version__ )
32
34
from matplotlib .backend_bases import (
33
35
_Backend , FigureCanvasBase , FigureManagerBase , RendererBase , cursors )
34
- from matplotlib .font_manager import findfont , get_font
35
- from matplotlib .ft2font import (LOAD_FORCE_AUTOHINT , LOAD_NO_HINTING ,
36
- LOAD_DEFAULT , LOAD_NO_AUTOHINT )
36
+ from matplotlib .backends ._backend_agg import RendererAgg as _RendererAgg
37
37
from matplotlib .mathtext import MathTextParser
38
38
from matplotlib .path import Path
39
39
from matplotlib .transforms import Bbox , BboxBase
40
- from matplotlib import colors as mcolors
41
-
42
- from matplotlib .backends ._backend_agg import RendererAgg as _RendererAgg
43
- from matplotlib import _png
44
40
45
41
try :
46
42
from PIL import Image
52
48
53
49
def get_hinting_flag ():
54
50
mapping = {
55
- True : LOAD_FORCE_AUTOHINT ,
56
- False : LOAD_NO_HINTING ,
57
- 'either' : LOAD_DEFAULT ,
58
- 'native' : LOAD_NO_AUTOHINT ,
59
- 'auto' : LOAD_FORCE_AUTOHINT ,
60
- 'none' : LOAD_NO_HINTING
51
+ True : _ft2 . LOAD_FORCE_AUTOHINT ,
52
+ False : _ft2 . LOAD_NO_HINTING ,
53
+ 'either' : _ft2 . LOAD_DEFAULT ,
54
+ 'native' : _ft2 . LOAD_NO_AUTOHINT ,
55
+ 'auto' : _ft2 . LOAD_FORCE_AUTOHINT ,
56
+ 'none' : _ft2 . LOAD_NO_HINTING
61
57
}
62
58
return mapping [rcParams ['text.hinting' ]]
63
59
@@ -110,9 +106,9 @@ def __setstate__(self, state):
110
106
111
107
def _get_hinting_flag (self ):
112
108
if rcParams ['text.hinting' ]:
113
- return LOAD_FORCE_AUTOHINT
109
+ return _ft2 . LOAD_FORCE_AUTOHINT
114
110
else :
115
- return LOAD_NO_HINTING
111
+ return _ft2 . LOAD_NO_HINTING
116
112
117
113
# for filtering to work with rasterization, methods needs to be wrapped.
118
114
# maybe there is better way to do it.
@@ -183,7 +179,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
183
179
yd = descent * cos (radians (angle ))
184
180
x = np .round (x + ox + xd )
185
181
y = np .round (y - oy + yd )
186
- self ._renderer .draw_text_image (font_image , x , y + 1 , angle , gc )
182
+ self ._renderer .draw_text_image (font_image , x , y , angle , gc )
187
183
188
184
def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
189
185
"""
@@ -197,24 +193,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
197
193
198
194
if font is None :
199
195
return None
200
- if len (s ) == 1 and ord (s ) > 127 :
201
- font .load_char (ord (s ), flags = flags )
202
- else :
203
- # We pass '0' for angle here, since it will be rotated (in raster
204
- # space) in the following call to draw_text_image).
205
- font .set_text (s , 0 , flags = flags )
206
- font .draw_glyphs_to_bitmap (antialiased = rcParams ['text.antialiased' ])
207
- d = font .get_descent () / 64.0
196
+ layout = _ft2 .Layout .simple (s , font , flags )
197
+ d = - np .floor (layout .yMin )
208
198
# The descent needs to be adjusted for the angle
209
- xo , yo = font .get_bitmap_offset ()
210
- xo /= 64.0
211
- yo /= 64.0
212
199
xd = - d * sin (radians (angle ))
213
200
yd = d * cos (radians (angle ))
214
201
215
- #print x, y, int(x), int(y), s
216
202
self ._renderer .draw_text_image (
217
- font , np .round (x - xd + xo ), np .round (y + yd + yo ) + 1 , angle , gc )
203
+ layout .render (), # FIXME Antialiasing.
204
+ np .round (x - xd ), np .round (y + yd ), angle , gc )
218
205
219
206
def get_text_width_height_descent (self , s , prop , ismath ):
220
207
"""
@@ -238,13 +225,10 @@ def get_text_width_height_descent(self, s, prop, ismath):
238
225
239
226
flags = get_hinting_flag ()
240
227
font = self ._get_agg_font (prop )
241
- font .set_text (s , 0.0 , flags = flags )
242
- w , h = font .get_width_height () # width and height of unrotated string
243
- d = font .get_descent ()
244
- w /= 64.0 # convert from subpixels
245
- h /= 64.0
246
- d /= 64.0
247
- return w , h , d
228
+ layout = _ft2 .Layout .simple (s , font , flags )
229
+ return (layout .xMax - layout .xMin ,
230
+ layout .yMax - layout .yMin ,
231
+ - layout .yMin )
248
232
249
233
def draw_tex (self , gc , x , y , s , prop , angle , ismath = 'TeX!' , mtext = None ):
250
234
# todo, handle props, angle, origins
@@ -271,13 +255,10 @@ def _get_agg_font(self, prop):
271
255
"""
272
256
Get the font for text instance t, cacheing for efficiency
273
257
"""
274
- fname = findfont (prop )
275
- font = get_font (fname )
276
-
277
- font .clear ()
258
+ fname = font_manager .findfont (prop )
259
+ font = font_manager .get_font (fname )
278
260
size = prop .get_size_in_points ()
279
- font .set_size (size , self .dpi )
280
-
261
+ font .set_char_size (size , self .dpi )
281
262
return font
282
263
283
264
def points_to_pixels (self , points ):
0 commit comments