Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a3cec5

Browse files
committed
Improve font rendering considerably using a vertical hinting hack.
Hinting is disabled for all of the "vector" backends, to correct spacing problems. svn path=/trunk/matplotlib/; revision=3680
1 parent 76ff2d2 commit 6a3cec5

7 files changed

Lines changed: 126 additions & 74 deletions

File tree

lib/matplotlib/backends/backend_agg.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
from matplotlib.cbook import enumerate, is_string_like, exception_to_str
8383
from matplotlib.figure import Figure
8484
from matplotlib.font_manager import fontManager
85-
from matplotlib.ft2font import FT2Font
85+
from matplotlib.ft2font import FT2Font, LOAD_DEFAULT
8686
from matplotlib.mathtext import math_parse_s_ft2font
8787
from matplotlib.transforms import lbwh_to_bbox
8888

@@ -203,11 +203,10 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
203203

204204
font = self._get_agg_font(prop)
205205
if font is None: return None
206-
if len(s)==1 and ord(s)>127:
207-
208-
font.load_char(ord(s))
206+
if len(s) == 1 and ord(s) > 127:
207+
font.load_char(ord(s), flags=LOAD_DEFAULT)
209208
else:
210-
font.set_text(s, angle)
209+
font.set_text(s, angle, flags=LOAD_DEFAULT)
211210
font.draw_glyphs_to_bitmap()
212211

213212
#print x, y, int(x), int(y)
@@ -237,7 +236,7 @@ def get_text_width_height(self, s, prop, ismath, rgb=(0,0,0)):
237236
s, self.dpi.get(), prop)
238237
return width, height
239238
font = self._get_agg_font(prop)
240-
font.set_text(s, 0.0) # the width and height of unrotated string
239+
font.set_text(s, 0.0, flags=LOAD_DEFAULT) # the width and height of unrotated string
241240
w, h = font.get_width_height()
242241
w /= 64.0 # convert from subpixels
243242
h /= 64.0

lib/matplotlib/backends/backend_pdf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from matplotlib.font_manager import fontManager
2929
from matplotlib.afm import AFM
3030
from matplotlib.dviread import Dvi
31-
from matplotlib.ft2font import FT2Font, FIXED_WIDTH, ITALIC, LOAD_NO_SCALE
31+
from matplotlib.ft2font import FT2Font, FIXED_WIDTH, ITALIC, LOAD_NO_SCALE, LOAD_NO_HINTING
3232
from matplotlib.mathtext import math_parse_s_pdf
3333
from matplotlib.transforms import Bbox
3434
from matplotlib import ttconv
@@ -517,7 +517,7 @@ def decode_char(charcode):
517517

518518
def get_char_width(charcode):
519519
unicode = decode_char(charcode)
520-
width = font.load_char(unicode, flags=LOAD_NO_SCALE).horiAdvance
520+
width = font.load_char(unicode, flags=LOAD_NO_SCALE|LOAD_NO_HINTING).horiAdvance
521521
return cvt(width)
522522

523523
firstchar, lastchar = 0, 255
@@ -1195,7 +1195,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
11951195
else:
11961196
font = self._get_font_ttf(prop)
11971197
self.track_characters(font, s)
1198-
font.set_text(s, 0.0)
1198+
font.set_text(s, 0.0, flags=LOAD_NO_HINTING)
11991199
y += font.get_descent() / 64.0
12001200

12011201
self.file.output(Op.begin_text,
@@ -1222,7 +1222,7 @@ def get_text_width_height(self, s, prop, ismath):
12221222

12231223
else:
12241224
font = self._get_font_ttf(prop)
1225-
font.set_text(s, 0.0)
1225+
font.set_text(s, 0.0, flags=LOAD_NO_HINTING)
12261226
w, h = font.get_width_height()
12271227
w /= 64.0
12281228
h /= 64.0

lib/matplotlib/backends/backend_ps.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
1919
from matplotlib.figure import Figure
2020

2121
from matplotlib.font_manager import fontManager
22-
from matplotlib.ft2font import FT2Font, KERNING_UNFITTED, KERNING_DEFAULT, KERNING_UNSCALED
22+
from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING
2323
from matplotlib.ttconv import convert_ttf_to_ps
2424
from matplotlib.mathtext import math_parse_s_ps
2525
from matplotlib.text import Text
@@ -292,7 +292,7 @@ def get_text_width_height(self, s, prop, ismath):
292292
return w, h
293293

294294
font = self._get_font_ttf(prop)
295-
font.set_text(s, 0.0)
295+
font.set_text(s, 0.0, flags=LOAD_NO_HINTING)
296296
w, h = font.get_width_height()
297297
w /= 64.0 # convert from subpixels
298298
h /= 64.0
@@ -738,7 +738,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
738738
return self.draw_unicode(gc, x, y, s, prop, angle)
739739
else:
740740
font = self._get_font_ttf(prop)
741-
font.set_text(s,0)
741+
font.set_text(s, 0, flags=LOAD_NO_HINTING)
742742
self.track_characters(font, s)
743743

744744
self.set_color(*gc.get_rgb())
@@ -782,10 +782,10 @@ def draw_unicode(self, gc, x, y, s, prop, angle):
782782
gind = 0
783783
else:
784784
name = font.get_glyph_name(gind)
785-
glyph = font.load_char(ccode)
785+
glyph = font.load_char(ccode, flags=LOAD_NO_HINTING)
786786

787787
if lastgind is not None:
788-
kern = font.get_kerning(lastgind, gind, KERNING_UNFITTED)
788+
kern = font.get_kerning(lastgind, gind, KERNING_DEFAULT)
789789
else:
790790
kern = 0
791791
lastgind = gind

lib/matplotlib/backends/backend_svg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from matplotlib.colors import rgb2hex
99
from matplotlib.figure import Figure
1010
from matplotlib.font_manager import fontManager, FontProperties
11-
from matplotlib.ft2font import FT2Font, KERNING_UNFITTED, KERNING_DEFAULT, KERNING_UNSCALED
11+
from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING
1212
from matplotlib.mathtext import math_parse_s_ft2font_svg
1313

1414
from xml.sax.saxutils import escape as escape_xml_text
@@ -266,10 +266,10 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
266266
if gind is None:
267267
ccode = ord('?')
268268
gind = 0
269-
glyph = font.load_char(ccode)
269+
glyph = font.load_char(ccode, flags=LOAD_NO_HINTING)
270270

271271
if lastgind is not None:
272-
kern = font.get_kerning(lastgind, gind, KERNING_UNFITTED)
272+
kern = font.get_kerning(lastgind, gind, KERNING_DEFAULT)
273273
else:
274274
kern = 0
275275
lastgind = gind
@@ -305,7 +305,7 @@ def _add_char_def(self, prop, char):
305305
return char_id
306306

307307
path_data = []
308-
glyph = font.load_char(ord(char))
308+
glyph = font.load_char(ord(char), flags=LOAD_NO_HINTING)
309309
currx, curry = 0.0, 0.0
310310
for step in glyph.path:
311311
if step[0] == 0: # MOVE_TO
@@ -431,7 +431,7 @@ def get_text_width_height(self, s, prop, ismath):
431431
math_parse_s_ft2font_svg(s, 72, prop)
432432
return width, height
433433
font = self._get_font(prop)
434-
font.set_text(s, 0.0)
434+
font.set_text(s, 0.0, flags=LOAD_NO_HINTING)
435435
w, h = font.get_width_height()
436436
w /= 64.0 # convert from subpixels
437437
h /= 64.0

lib/matplotlib/mathtext.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
from matplotlib.afm import AFM
149149
from matplotlib.cbook import enumerate, iterable, Bunch, get_realpath_and_stat, \
150150
is_string_like
151-
from matplotlib.ft2font import FT2Font, KERNING_UNFITTED
151+
from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_DEFAULT, LOAD_NO_HINTING
152152
from matplotlib.font_manager import fontManager, FontProperties
153153
from matplotlib._mathtext_data import latex_to_bakoma, \
154154
latex_to_standard, tex2uni, type12uni, tex2type1, uni2type1
@@ -237,6 +237,9 @@ def get_results(self):
237237
"""Return a backend specific tuple of things to return to the
238238
backend after all processing is done."""
239239
raise NotImplementedError()
240+
241+
def get_hinting_type(self):
242+
return LOAD_NO_HINTING
240243

241244
class MathtextBackendAgg(MathtextBackend):
242245
def set_canvas_size(self, w, h):
@@ -261,7 +264,10 @@ def get_results(self):
261264
self.height,
262265
self.fonts_object.get_fonts(),
263266
self.fonts_object.get_used_characters())
264-
267+
268+
def get_hinting_type(self):
269+
return LOAD_DEFAULT
270+
265271
class MathtextBackendPs(MathtextBackend):
266272
def __init__(self):
267273
self.pswriter = StringIO()
@@ -487,7 +493,9 @@ def _get_info (self, fontname, sym, fontsize, dpi, mark_as_used=True):
487493

488494
font = cached_font.font
489495
font.set_size(fontsize, dpi)
490-
glyph = font.load_char(num)
496+
glyph = font.load_char(
497+
num,
498+
flags=self.mathtext_backend.get_hinting_type())
491499

492500
xmin, ymin, xmax, ymax = [val/64.0 for val in glyph.bbox]
493501
offset = self._get_offset(cached_font, glyph, fontsize, dpi)
@@ -538,7 +546,7 @@ def get_kern(self, font1, sym1, fontsize1,
538546
info1 = self._get_info(font1, sym1, fontsize1, dpi)
539547
info2 = self._get_info(font2, sym2, fontsize2, dpi)
540548
font = info1.font
541-
return font.get_kerning(info1.num, info2.num, KERNING_UNFITTED) / 64.0
549+
return font.get_kerning(info1.num, info2.num, KERNING_DEFAULT) / 64.0
542550
return 0.0
543551

544552
class BakomaFonts(TruetypeFonts):

0 commit comments

Comments
 (0)