1212 * alpha blending
1313 * DPI scaling properly - everything scales properly (dashes, linewidths, etc)
1414 * draw polygon
15- * freetype2 w/ ft2font
15+ * freetype2
1616
1717TODO:
1818
2828import numpy as np
2929from collections import OrderedDict
3030from math import radians , cos , sin
31- from matplotlib import rcParams , __version__
31+
32+ from matplotlib import (
33+ _ft2 , _png , colors as mcolors , font_manager , rcParams , __version__ )
3234from matplotlib .backend_bases import (
3335 _Backend , FigureCanvasBase , FigureManagerBase , RendererBase , cursors )
36+ from matplotlib .backends ._backend_agg import RendererAgg as _RendererAgg
3437from 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 )
3838from matplotlib .mathtext import MathTextParser
3939from matplotlib .path import Path
4040from 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
4541
4642try :
4743 from PIL import Image
5349
5450def get_hinting_flag ():
5551 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
6258 }
6359 return mapping [rcParams ['text.hinting' ]]
6460
@@ -106,9 +102,9 @@ def __setstate__(self, state):
106102
107103 def _get_hinting_flag (self ):
108104 if rcParams ['text.hinting' ]:
109- return LOAD_FORCE_AUTOHINT
105+ return _ft2 . LOAD_FORCE_AUTOHINT
110106 else :
111- return LOAD_NO_HINTING
107+ return _ft2 . LOAD_NO_HINTING
112108
113109 # for filtering to work with rasterization, methods needs to be wrapped.
114110 # maybe there is better way to do it.
@@ -177,7 +173,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
177173 yd = descent * cos (radians (angle ))
178174 x = np .round (x + ox + xd )
179175 y = np .round (y - oy + yd )
180- self ._renderer .draw_text_image (font_image , x , y + 1 , angle , gc )
176+ self ._renderer .draw_text_image (font_image , x , y , angle , gc )
181177
182178 def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
183179 """
@@ -191,24 +187,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
191187
192188 if font is None :
193189 return None
194- if len (s ) == 1 and ord (s ) > 127 :
195- font .load_char (ord (s ), flags = flags )
196- else :
197- # We pass '0' for angle here, since it will be rotated (in raster
198- # space) in the following call to draw_text_image).
199- font .set_text (s , 0 , flags = flags )
200- font .draw_glyphs_to_bitmap (antialiased = rcParams ['text.antialiased' ])
201- d = font .get_descent () / 64.0
190+ layout = _ft2 .Layout .simple (s , font , flags )
191+ d = - np .floor (layout .yMin )
202192 # The descent needs to be adjusted for the angle
203- xo , yo = font .get_bitmap_offset ()
204- xo /= 64.0
205- yo /= 64.0
206193 xd = - d * sin (radians (angle ))
207194 yd = d * cos (radians (angle ))
208195
209- #print x, y, int(x), int(y), s
210196 self ._renderer .draw_text_image (
211- font , np .round (x - xd + xo ), np .round (y + yd + yo ) + 1 , angle , gc )
197+ layout .render (), # FIXME Antialiasing.
198+ np .round (x - xd ), np .round (y + yd ), angle , gc )
212199
213200 def get_text_width_height_descent (self , s , prop , ismath ):
214201 """
@@ -232,13 +219,10 @@ def get_text_width_height_descent(self, s, prop, ismath):
232219
233220 flags = get_hinting_flag ()
234221 font = self ._get_agg_font (prop )
235- font .set_text (s , 0.0 , flags = flags ) # the width and height of unrotated string
236- w , h = font .get_width_height ()
237- d = font .get_descent ()
238- w /= 64.0 # convert from subpixels
239- h /= 64.0
240- d /= 64.0
241- return w , h , d
222+ layout = _ft2 .Layout .simple (s , font , flags )
223+ return (layout .xMax - layout .xMin ,
224+ layout .yMax - layout .yMin ,
225+ - layout .yMin )
242226
243227 def draw_tex (self , gc , x , y , s , prop , angle , ismath = 'TeX!' , mtext = None ):
244228 # todo, handle props, angle, origins
@@ -265,13 +249,10 @@ def _get_agg_font(self, prop):
265249 """
266250 Get the font for text instance t, cacheing for efficiency
267251 """
268- fname = findfont (prop )
269- font = get_font (fname )
270-
271- font .clear ()
252+ fname = font_manager .findfont (prop )
253+ font = font_manager .get_font (fname )
272254 size = prop .get_size_in_points ()
273- font .set_size (size , self .dpi )
274-
255+ font .set_char_size (size , self .dpi )
275256 return font
276257
277258 def points_to_pixels (self , points ):
0 commit comments