8000 Bad font hinting / quality with Agg renderer by mdboom · Pull Request #719 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Bad font hinting / quality with Agg renderer #719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 29, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from matplotlib.cbook import is_string_like, maxdict
from matplotlib.figure import Figure
from matplotlib.font_manager import findfont
from matplotlib.ft2font import FT2Font, LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING
from matplotlib.ft2font import FT2Font, LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING, \
LOAD_DEFAULT, LOAD_NO_AUTOHINT
from matplotlib.mathtext import MathTextParser
from matplotlib.path import Path
from matplotlib.transforms import Bbox, BboxBase
Expand All @@ -40,6 +41,18 @@

backend_version = 'v2.2'

def get_hinting_flag():
mapping = {
True: LOAD_FORCE_AUTOHINT,
False: LOAD_NO_HINTING,
'either': LOAD_DEFAULT,
'native': LOAD_NO_AUTOHINT,
'auto': LOAD_FORCE_AUTOHINT,
'none': LOAD_NO_HINTING
}
return mapping[rcParams['text.hinting']]


class RendererAgg(RendererBase):
"""
The renderer handles all the drawing primitives using a graphics
Expand Down Expand Up @@ -69,12 +82,6 @@ def __init__(self, width, height, dpi):
if __debug__: verbose.report('RendererAgg.__init__ done',
'debug-annoying')

def _get_hinting_flag(self):
if rcParams['text.hinting']:
return LOAD_FORCE_AUTOHINT
else:
return LOAD_NO_HINTING

# for filtering to work with rasterization, methods needs to be wrapped.
# maybe there is better way to do it.
def draw_markers(self, *kl, **kw):
Expand Down Expand Up @@ -141,7 +148,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
if ismath:
return self.draw_mathtext(gc, x, y, s, prop, angle)

flags = self._get_hinting_flag()
flags = get_hinting_flag()
font = self._get_agg_font(prop)
if font is None: return None
if len(s) == 1 and ord(s) > 127:
Expand Down Expand Up @@ -178,7 +185,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
self.mathtext_parser.parse(s, self.dpi, prop)
return width, height, descent

flags = self._get_hinting_flag()
flags = get_hinting_flag()
font = self._get_agg_font(prop)
font.set_text(s, 0.0, flags=flags) # the width and height of unrotated string
w, h = font.get_width_height()
Expand Down Expand Up @@ -220,7 +227,9 @@ def _get_agg_font(self, prop):
fname = findfont(prop)
font = self._fontd.get(fname)
if font is None:
font = FT2Font(str(fname))
font = FT2Font(
str(fname),
hinting_factor=rcParams['text.hinting_factor'])
self._fontd[fname] = font
self._fontd[key] = font

Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,8 @@ def get_results(self, box, used_characters):
return result

def get_hinting_type(self):
if rcParams['text.hinting']:
return LOAD_FORCE_AUTOHINT
else:
return LOAD_NO_HINTING
from matplotlib.backends import backend_agg
return backend_agg.get_hinting_flag()

class MathtextBackendBitmap(MathtextBackendAgg):
def get_results(self, box, used_characters):
Expand Down
10 changes: 9 additions & 1 deletion lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ def deprecate_svg_embed_char_paths(value):

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

def validate_hinting(s):
if s in (True, False):
return s
if s.lower() in ('auto', 'native', 'either', 'none'):
return s.lower()
raise ValueError("hinting should be 'auto', 'native', 'either' or 'none'")

class ValidateInterval:
"""
Value must be in interval
Expand Down Expand Up @@ -407,7 +414,8 @@ def __call__(self, s):
'text.latex.preamble' : [[''], validate_stringlist],
'text.latex.preview' : [False, validate_bool],
'text.dvipnghack' : [None, validate_bool_maybe_none],
'text.hinting' : [True, validate_bool],
'text.hinting' : [True, validate_hinting],
'text.hinting_factor' : [8, validate_int],

# The following are deprecated and replaced by, e.g., 'font.style'
#'text.fontstyle' : ['normal', str],
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/tests/__init__.py