Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6d4ae43

Browse files
committed
New FreeType wrapper.
TODO: ttc support violates the assumption that the filename suffices toidentify the face (when caching). We need to generate a uniquer id orjust key off the face object.[ci skip]
1 parentaa0f868 commit6d4ae43

20 files changed

+1312
-478
lines changed

‎examples/misc/font_indexing.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
"""
1010
from __future__importprint_function
1111
importmatplotlib
12-
frommatplotlib.ft2fontimportFT2Font,KERNING_DEFAULT,KERNING_UNFITTED,KERNING_UNSCALED
12+
frommatplotlibimport_ft2
1313

1414

15-
#fname = '/usr/share/fonts/sfd/FreeSans.ttf'
1615
fname=matplotlib.get_data_path()+'/fonts/ttf/DejaVuSans.ttf'
17-
font=FT2Font(fname)
18-
font.set_charmap(0)
16+
font=_ft2.Face(fname)
17+
font.set_charmap(0)# FIXME
1918

2019
codes=font.get_charmap().items()
2120
#dsu = [(ccode, glyphind) for ccode, glyphind in codes]
@@ -34,11 +33,8 @@
3433
coded[name]=ccode
3534
glyphd[name]=glyphind
3635

37-
code=coded['A']
38-
glyph=font.load_char(code)
39-
#print(glyph.bbox)
4036
print(glyphd['A'],glyphd['V'],coded['A'],coded['V'])
41-
print('AV',font.get_kerning(glyphd['A'],glyphd['V'],KERNING_DEFAULT))
42-
print('AV',font.get_kerning(glyphd['A'],glyphd['V'],KERNING_UNFITTED))
43-
print('AV',font.get_kerning(glyphd['A'],glyphd['V'],KERNING_UNSCALED))
44-
print('AV',font.get_kerning(glyphd['A'],glyphd['T'],KERNING_UNSCALED))
37+
print('AV',font.get_kerning(glyphd['A'],glyphd['V'],_ft2.Kerning.DEFAULT))
38+
print('AV',font.get_kerning(glyphd['A'],glyphd['V'],_ft2.Kerning.UNFITTED))
39+
print('AV',font.get_kerning(glyphd['A'],glyphd['V'],_ft2.Kerning.UNSCALED))
40+
print('AV',font.get_kerning(glyphd['A'],glyphd['T'],_ft2.Kerning.UNSCALED))

‎examples/misc/ftface_props.py

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,54 @@
11
"""
2-
============
3-
Ftface Props
4-
============
5-
6-
This is a demo script to show you how to use all the properties ofan
7-
FT2Fontobject. These describe global font properties. For
8-
individual charactermetrics, use the Glyph object, asreturned by
9-
load_char
2+
===============
3+
Face properties
4+
===============
5+
6+
This is a demo script to show you how to use all the properties ofa Face
7+
object. These describe global font properties. For individual character
8+
metrics, use the Glyph object, asloaded from the glyph attribute after calling
9+
load_char.
1010
"""
1111
from __future__importprint_function
1212
importmatplotlib
13-
importmatplotlib.ft2fontasft
14-
15-
16-
#fname = '/usr/local/share/matplotlib/VeraIt.ttf'
17-
fname=matplotlib.get_data_path()+'/fonts/ttf/DejaVuSans-Oblique.ttf'
18-
#fname = '/usr/local/share/matplotlib/cmr10.ttf'
19-
20-
font=ft.FT2Font(fname)
21-
22-
print('Num faces :',font.num_faces)# number of faces in file
23-
print('Num glyphs :',font.num_glyphs)# number of glyphs in the face
24-
print('Family name :',font.family_name)# face family name
25-
print('Style name :',font.style_name)# face style name
26-
print('PS name :',font.postscript_name)# the postscript name
27-
print('Num fixed :',font.num_fixed_sizes)# number of embedded bitmap in face
28-
29-
# the following are only available if face.scalable
30-
iffont.scalable:
31-
# the face global bounding box (xmin, ymin, xmax, ymax)
32-
print('Bbox :',font.bbox)
33-
# number of font units covered by the EM
34-
print('EM :',font.units_per_EM)
35-
# the ascender in 26.6 units
36-
print('Ascender :',font.ascender)
37-
# the descender in 26.6 units
38-
print('Descender :',font.descender)
39-
# the height in 26.6 units
40-
print('Height :',font.height)
41-
# maximum horizontal cursor advance
42-
print('Max adv width :',font.max_advance_width)
43-
# same for vertical layout
44-
print('Max adv height :',font.max_advance_height)
45-
# vertical position of the underline bar
46-
print('Underline pos :',font.underline_position)
47-
# vertical thickness of the underline
48-
print('Underline thickness :',font.underline_thickness)
49-
50-
forstylein ('Italic',
51-
'Bold',
52-
'Scalable',
53-
'Fixed sizes',
54-
'Fixed width',
55-
'SFNT',
56-
'Horizontal',
57-
'Vertical',
58-
'Kerning',
59-
'Fast glyphs',
60-
'Multiple masters',
61-
'Glyph names',
62-
'External stream'):
63-
bitpos=getattr(ft,style.replace(' ','_').upper())-1
64-
print('%-17s:'%style,bool(font.style_flags& (1<<bitpos)))
65-
66-
print(dir(font))
67-
68-
print(font.get_kerning)
13+
frommatplotlibimportfont_manager,_ft2
14+
15+
16+
fname=matplotlib.get_data_path()+"/fonts/ttf/DejaVuSans-Oblique.ttf"
17+
font=font_manager.get_font(fname)
18+
19+
print("Faces in file :",font.num_faces)
20+
print("Glyphs in face :",font.num_glyphs)
21+
print("Family name :",font.family_name)
22+
print("Style name :",font.style_name)
23+
print("Postscript name :",font.get_postscript_name())
24+
print("Embedded bitmap strikes:",font.num_fixed_sizes)
25+
26+
iffont.face_flags&_ft2.FACE_FLAG_SCALABLE:
27+
print('Global bbox (xmin, ymin, xmax, ymax):',font.bbox)
28+
print('Font units per EM :',font.units_per_EM)
29+
print('Ascender (pixels) :',font.ascender)
30+
print('Descender (pixels) :',font.descender)
31+
print('Height (pixels) :',font.height)
32+
print('Max horizontal advance :',font.max_advance_width)
33+
print('Max vertical advance :',font.max_advance_height)
34+
print('Underline position :',font.underline_position)
35+
print('Underline thickness :',font.underline_thickness)
36+
37+
forstylein ['Style flag italic',
38+
'Style flag bold']:
39+
flag=getattr(_ft2,style.replace(' ','_').upper())-1
40+
print('%-26s:'%style,bool(font.style_flags&flag))
41+
42+
forstylein ['Face flag scalable',
43+
'Face flag fixed sizes',
44+
'Face flag fixed width',
45+
'Face flag SFNT',
46+
'Face flag horizontal',
47+
'Face flag vertical',
48+
'Face flag kerning',
49+
'Face flag fast glyphs',
50+
'Face flag multiple masters',
51+
'Face flag glyph names',
52+
'Face flag external stream']:
53+
flag=getattr(_ft2,style.replace(' ','_').upper())
54+
print('%-26s:'%style,bool(font.face_flags&flag))

‎examples/text_labels_and_annotations/font_table_ttf_sgskip.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
importos
1616

1717
importmatplotlib
18-
frommatplotlib.ft2fontimportFT2Font
18+
frommatplotlibimport_ft2
1919
frommatplotlib.font_managerimportFontProperties
2020
importmatplotlib.pyplotasplt
2121

@@ -35,8 +35,8 @@
3535
fontname=os.path.join(matplotlib.get_data_path(),
3636
'fonts','ttf','DejaVuSans.ttf')
3737

38-
font=FT2Font(fontname)
39-
codes=sorted(font.get_charmap().items())
38+
font=_ft2.Face(fontname)
39+
codes=sorted(font.get_charmap().items())# FIXME
4040

4141
# a 16,16 array of character strings
4242
chars= [[''forcinrange(16)]forrinrange(16)]

‎lib/matplotlib/backends/backend_agg.py

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* alpha blending
1313
* DPI scaling properly - everything scales properly (dashes, linewidths, etc)
1414
* draw polygon
15-
* freetype2 w/ ft2font
15+
* freetype2
1616
1717
TODO:
1818
@@ -28,20 +28,16 @@
2828
importnumpyasnp
2929
fromcollectionsimportOrderedDict
3030
frommathimportradians,cos,sin
31-
frommatplotlibimportcbook,rcParams,__version__
31+
32+
frommatplotlibimport (
33+
_ft2,_png,cbook,colorsasmcolors,font_manager,rcParams,__version__)
3234
frommatplotlib.backend_basesimport (
3335
_Backend,FigureCanvasBase,FigureManagerBase,RendererBase,cursors)
36+
frommatplotlib.backends._backend_aggimportRendererAggas_RendererAgg
3437
frommatplotlib.figureimportFigure
35-
frommatplotlib.font_managerimportfindfont,get_font
36-
frommatplotlib.ft2fontimport (LOAD_FORCE_AUTOHINT,LOAD_NO_HINTING,
37-
LOAD_DEFAULT,LOAD_NO_AUTOHINT)
3838
frommatplotlib.mathtextimportMathTextParser
3939
frommatplotlib.pathimportPath
4040
frommatplotlib.transformsimportBbox,BboxBase
41-
frommatplotlibimportcolorsasmcolors
42-
43-
frommatplotlib.backends._backend_aggimportRendererAggas_RendererAgg
44-
frommatplotlibimport_png
4541

4642
try:
4743
fromPILimportImage
@@ -53,12 +49,12 @@
5349

5450
defget_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
returnmapping[rcParams['text.hinting']]
6460

@@ -111,9 +107,9 @@ def __setstate__(self, state):
111107

112108
def_get_hinting_flag(self):
113109
ifrcParams['text.hinting']:
114-
returnLOAD_FORCE_AUTOHINT
110+
return_ft2.LOAD_FORCE_AUTOHINT
115111
else:
116-
returnLOAD_NO_HINTING
112+
return_ft2.LOAD_NO_HINTING
117113

118114
# for filtering to work with rasterization, methods needs to be wrapped.
119115
# maybe there is better way to do it.
@@ -184,7 +180,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
184180
yd=descent*cos(radians(angle))
185181
x=np.round(x+ox+xd)
186182
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)
188184

189185
defdraw_text(self,gc,x,y,s,prop,angle,ismath=False,mtext=None):
190186
"""
@@ -198,24 +194,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
198194

199195
iffontisNone:
200196
returnNone
201-
iflen(s)==1andord(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)
209199
# The descent needs to be adjusted for the angle
210-
xo,yo=font.get_bitmap_offset()
211-
xo/=64.0
212-
yo/=64.0
213200
xd=-d*sin(radians(angle))
214201
yd=d*cos(radians(angle))
215202

216-
#print x, y, int(x), int(y), s
217203
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)
219206

220207
defget_text_width_height_descent(self,s,prop,ismath):
221208
"""
@@ -239,13 +226,10 @@ def get_text_width_height_descent(self, s, prop, ismath):
239226

240227
flags=get_hinting_flag()
241228
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-
returnw,h,d
229+
layout=_ft2.Layout.simple(s,font,flags)
230+
return (layout.xMax-layout.xMin,
231+
layout.yMax-layout.yMin,
232+
-layout.yMin)
249233

250234
defdraw_tex(self,gc,x,y,s,prop,angle,ismath='TeX!',mtext=None):
251235
# todo, handle props, angle, origins
@@ -272,13 +256,10 @@ def _get_agg_font(self, prop):
272256
"""
273257
Get the font for text instance t, cacheing for efficiency
274258
"""
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)
279261
size=prop.get_size_in_points()
280-
font.set_size(size,self.dpi)
281-
262+
font.set_char_size(size,self.dpi)
282263
returnfont
283264

284265
defpoints_to_pixels(self,points):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp