Skip to content

Commit cf5e358

Browse files
committed
Expose more hinting options to the user.
1 parent 1d78d3d commit cf5e358

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

lib/matplotlib/backends/backend_agg.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
from matplotlib.cbook import is_string_like, maxdict
3131
from matplotlib.figure import Figure
3232
from matplotlib.font_manager import findfont
33-
from matplotlib.ft2font import FT2Font, LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING
33+
from matplotlib.ft2font import FT2Font, LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING, \
34+
LOAD_DEFAULT, LOAD_NO_AUTOHINT
3435
from matplotlib.mathtext import MathTextParser
3536
from matplotlib.path import Path
3637
from matplotlib.transforms import Bbox, BboxBase
@@ -40,6 +41,18 @@
4041

4142
backend_version = 'v2.2'
4243

44+
def get_hinting_flag():
45+
mapping = {
46+
True: LOAD_FORCE_AUTOHINT,
47+
False: LOAD_NO_HINTING,
48+
'either': LOAD_DEFAULT,
49+
'native': LOAD_NO_AUTOHINT,
50+
'auto': LOAD_FORCE_AUTOHINT,
51+
'none': LOAD_NO_HINTING
52+
}
53+
return mapping[rcParams['text.hinting']]
54+
55+
4356
class RendererAgg(RendererBase):
4457
"""
4558
The renderer handles all the drawing primitives using a graphics
@@ -69,12 +82,6 @@ def __init__(self, width, height, dpi):
6982
if __debug__: verbose.report('RendererAgg.__init__ done',
7083
'debug-annoying')
7184

72-
def _get_hinting_flag(self):
73-
if rcParams['text.hinting']:
74-
return LOAD_FORCE_AUTOHINT
75-
else:
76-
return LOAD_NO_HINTING
77-
7885
# for filtering to work with rasterization, methods needs to be wrapped.
7986
# maybe there is better way to do it.
8087
def draw_markers(self, *kl, **kw):
@@ -141,7 +148,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
141148
if ismath:
142149
return self.draw_mathtext(gc, x, y, s, prop, angle)
143150

144-
flags = self._get_hinting_flag()
151+
flags = get_hinting_flag()
145152
font = self._get_agg_font(prop)
146153
if font is None: return None
147154
if len(s) == 1 and ord(s) > 127:
@@ -178,7 +185,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
178185
self.mathtext_parser.parse(s, self.dpi, prop)
179186
return width, height, descent
180187

181-
flags = self._get_hinting_flag()
188+
flags = get_hinting_flag()
182189
font = self._get_agg_font(prop)
183190
font.set_text(s, 0.0, flags=flags) # the width and height of unrotated string
184191
w, h = font.get_width_height()

lib/matplotlib/mathtext.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,8 @@ def get_results(self, box, used_characters):
224224
return result
225225

226226
def get_hinting_type(self):
227-
if rcParams['text.hinting']:
228-
return LOAD_FORCE_AUTOHINT
229-
else:
230-
return LOAD_NO_HINTING
227+
from matplotlib.backends import backend_agg
228+
return backend_agg.get_hinting_flag()
231229

232230
class MathtextBackendBitmap(MathtextBackendAgg):
233231
def get_results(self, box, used_characters):

lib/matplotlib/rcsetup.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ def deprecate_svg_embed_char_paths(value):
313313

314314
validate_svg_fonttype = ValidateInStrings('fonttype', ['none', 'path', 'svgfont'])
315315

316+
def validate_hinting(s):
317+
if s in (True, False):
318+
return s
319+
if s.lower() in ('auto', 'native', 'either', 'none'):
320+
return s.lower()
321+
raise ValueError("hinting should be 'auto', 'native', 'either' or 'none'")
322+
316323
class ValidateInterval:
317324
"""
318325
Value must be in interval
@@ -407,7 +414,7 @@ def __call__(self, s):
407414
'text.latex.preamble' : [[''], validate_stringlist],
408415
'text.latex.preview' : [False, validate_bool],
409416
'text.dvipnghack' : [None, validate_bool_maybe_none],
410-
'text.hinting' : [True, validate_bool],
417+
'text.hinting' : [True, validate_hinting],
411418
'text.hinting_factor' : [8, validate_int],
412419

413420
# The following are deprecated and replaced by, e.g., 'font.style'

matplotlibrc.template

+11-3
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,17 @@ backend : %(backend)s
170170
# correction off. None will try and
171171
# guess based on your dvipng version
172172

173-
#text.hinting : True # If True, text will be hinted, otherwise not. This only
174-
# affects the Agg backend.
175-
#text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
173+
#text.hinting : 'auto' # May be one of the following:
174+
# 'none': Perform no hinting
175+
# 'auto': Use freetype's autohinter
176+
# 'native': Use the hinting information in the
177+
# font file, if available, and if your
178+
# freetype library supports it
179+
# 'either': Use the native hinting information,
180+
# or the autohinter if none is available.
181+
# For backward compatibility, this value may also be
182+
# True === 'auto' or False === 'none'.
183+
text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
176184
# horizontal direction. A value of 1 will hint to full
177185
# pixels. A value of 2 will hint to half pixels etc.
178186

0 commit comments

Comments
 (0)