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