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

Commit520fd60

Browse files
committed
Use libraqm for text in vector outputs
1 parent5ca3116 commit520fd60

File tree

8 files changed

+39
-108
lines changed

8 files changed

+39
-108
lines changed

‎lib/matplotlib/_text_helpers.py‎

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,23 @@
44

55
from __future__importannotations
66

7-
importdataclasses
7+
fromcollections.abcimportIterator
88

99
from .import_api
10-
from .ft2fontimportFT2Font,GlyphIndexType,Kerning,LoadFlags
10+
from .ft2fontimportFT2Font,CharacterCodeType,LayoutItem,LoadFlags
1111

1212

13-
@dataclasses.dataclass(frozen=True)
14-
classLayoutItem:
15-
ft_object:FT2Font
16-
char:str
17-
glyph_index:GlyphIndexType
18-
x:float
19-
prev_kern:float
20-
21-
22-
defwarn_on_missing_glyph(codepoint,fontnames):
13+
defwarn_on_missing_glyph(codepoint:CharacterCodeType,fontnames:str):
2314
_api.warn_external(
2415
f"Glyph{codepoint} "
2516
f"({chr(codepoint).encode('ascii','namereplace').decode('ascii')}) "
2617
f"missing from font(s){fontnames}.")
2718

2819

29-
deflayout(string,font,*,features=None,kern_mode=Kerning.DEFAULT,language=None):
20+
deflayout(string:str,font:FT2Font,*,
21+
features:tuple[str]|None= ...,
22+
language:str|list[tuple[str,int,int]]|None= ...
23+
)->Iterator[LayoutItem]:
3024
"""
3125
Render *string* with *font*.
3226
@@ -41,8 +35,6 @@ def layout(string, font, *, features=None, kern_mode=Kerning.DEFAULT, language=N
4135
The font.
4236
features : tuple of str, optional
4337
The font features to apply to the text.
44-
kern_mode : Kerning
45-
A FreeType kerning mode.
4638
language : str, optional
4739
The language of the text in a format accepted by libraqm, namely `a BCP47
4840
language code <https://www.w3.org/International/articles/language-tags/>`_.
@@ -51,20 +43,8 @@ def layout(string, font, *, features=None, kern_mode=Kerning.DEFAULT, language=N
5143
------
5244
LayoutItem
5345
"""
54-
x=0
55-
prev_glyph_index=None
56-
char_to_font=font._get_fontmap(string)# TODO: Pass in features and language.
57-
base_font=font
58-
forcharinstring:
59-
# This has done the fallback logic
60-
font=char_to_font.get(char,base_font)
61-
glyph_index=font.get_char_index(ord(char))
62-
kern= (
63-
base_font.get_kerning(prev_glyph_index,glyph_index,kern_mode)/64
64-
ifprev_glyph_indexisnotNoneelse0.
65-
)
66-
x+=kern
67-
glyph=font.load_glyph(glyph_index,flags=LoadFlags.NO_HINTING)
68-
yieldLayoutItem(font,char,glyph_index,x,kern)
69-
x+=glyph.linearHoriAdvance/65536
70-
prev_glyph_index=glyph_index
46+
forraqm_iteminfont._layout(string,LoadFlags.NO_HINTING,
47+
features=features,language=language):
48+
raqm_item.ft_object.load_glyph(raqm_item.glyph_index,
49+
flags=LoadFlags.NO_HINTING)
50+
yieldraqm_item

‎lib/matplotlib/backends/_backend_pdf_ps.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@ def track(self, font: FT2Font, s: str) -> list[tuple[int, CharacterCodeType]]:
158158
and the character codes will be returned from the string unchanged.
159159
"""
160160
font_glyphs= []
161-
char_to_font=font._get_fontmap(s)
162-
for_c,_finchar_to_font.items():
163-
charcode=ord(_c)
164-
glyph_index=_f.get_char_index(charcode)
161+
forraqm_iteminfont._layout(s,ft2font.LoadFlags.NO_HINTING):
162+
font_path=raqm_item.ft_object.fname
163+
charcode=ord(raqm_item.char)
164+
glyph_index=raqm_item.glyph_index
165165
ifself.subset_size!=0:
166166
subset=charcode//self.subset_size
167167
subset_charcode=charcode%self.subset_size
168168
else:
169169
subset=0
170170
subset_charcode=charcode
171-
self.used.setdefault((_f.fname,subset), {})[subset_charcode]=glyph_index
171+
self.used.setdefault((font_path,subset), {})[subset_charcode]=glyph_index
172172
font_glyphs.append((subset,subset_charcode))
173173
returnfont_glyphs
174174

‎lib/matplotlib/backends/backend_pdf.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
frommatplotlib.figureimportFigure
3535
frommatplotlib.font_managerimportget_font,fontManageras_fontManager
3636
frommatplotlib._afmimportAFM
37-
frommatplotlib.ft2fontimportFT2Font,FaceFlags,Kerning,LoadFlags,StyleFlags
37+
frommatplotlib.ft2fontimportFT2Font,FaceFlags,LoadFlags,StyleFlags
3838
frommatplotlib.transformsimportAffine2D,BboxBase
3939
frommatplotlib.pathimportPath
4040
frommatplotlib.datesimportUTC
@@ -2286,6 +2286,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
22862286
# If fonttype is neither 3 nor 42, emit the whole string at once
22872287
# without manual kerning.
22882288
iffonttypenotin [3,42]:
2289+
ifnotmpl.rcParams['pdf.use14corefonts']:
2290+
self.file._character_tracker.track(font,s)
22892291
self.file.output(Op.begin_text,
22902292
self.file.fontName(prop),fontsize,Op.selectfont)
22912293
self._setup_textpos(x,y,angle)
@@ -2327,7 +2329,6 @@ def output_singlebyte_chunk(kerns_or_chars):
23272329
# Emit all the characters in a BT/ET group.
23282330
self.file.output(Op.begin_text)
23292331
foritemin_text_helpers.layout(s,font,features=features,
2330-
kern_mode=Kerning.UNFITTED,
23312332
language=language):
23322333
subset,charcode=self.file._character_tracker.track_glyph(
23332334
item.ft_object,ord(item.char),item.glyph_index)

‎lib/matplotlib/backends/backend_ps.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
769769
ifismath:
770770
returnself.draw_mathtext(gc,x,y,s,prop,angle)
771771

772-
stream= []# list of (ps_name, x, char_name)
772+
stream= []# list of (ps_name, x,y,char_name)
773773

774774
ifmpl.rcParams['ps.useafm']:
775775
font=self._get_font_afm(prop)
@@ -787,7 +787,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
787787
kern=font.get_kern_dist_from_name(last_name,name)
788788
last_name=name
789789
thisx+=kern*scale
790-
stream.append((ps_name,thisx,name))
790+
stream.append((ps_name,thisx,0,name))
791791
thisx+=width*scale
792792

793793
else:
@@ -797,20 +797,20 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
797797
else:
798798
features=language=None
799799
font=self._get_font_ttf(prop)
800-
self._character_tracker.track(font,s)
801800
foritemin_text_helpers.layout(s,font,features=features,
802801
language=language):
802+
self._character_tracker.track_glyph(item.ft_object,ord(item.char),
803+
item.glyph_index)
803804
ps_name= (item.ft_object.postscript_name
804805
.encode("ascii","replace").decode("ascii"))
805806
glyph_name=item.ft_object.get_glyph_name(item.glyph_index)
806-
stream.append((ps_name,item.x,glyph_name))
807+
stream.append((ps_name,item.x,item.y,glyph_name))
807808
self.set_color(*gc.get_rgb())
808809

809-
forps_name,groupinitertools. \
810-
groupby(stream,lambdaentry:entry[0]):
810+
forps_name,groupinitertools.groupby(stream,lambdaentry:entry[0]):
811811
self.set_font(ps_name,prop.get_size_in_points(),False)
812-
thetext="\n".join(f"{x:g}0 m /{name:s} glyphshow"
813-
for_,x,nameingroup)
812+
thetext="\n".join(f"{x:g}{y:g} m /{name:s} glyphshow"
813+
for_,x,y,nameingroup)
814814
self._pswriter.write(f"""\
815815
gsave
816816
{self._get_clip_cmd(gc)}

‎lib/matplotlib/ft2font.pyi‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ class FT2Font(Buffer):
203203
)->None: ...
204204
ifsys.version_info[:2]>= (3,12):
205205
def__buffer__(self,flags:int)->memoryview: ...
206-
def_get_fontmap(self,string:str)->dict[str,FT2Font]: ...
207206
defclear(self)->None: ...
208207
defdraw_glyph_to_bitmap(
209208
self,image:NDArray[np.uint8],x:int,y:int,glyph:Glyph,antialiased:bool= ...

‎lib/matplotlib/tests/test_ft2font.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ def test_fallback_last_resort(recwarn):
972972
"Glyph 128579 (\\N{UPSIDE-DOWN FACE}) missing from font(s)")
973973

974974

975-
deftest__get_fontmap():
975+
deftest__layout():
976976
fonts,test_str=_gen_multi_font_text()
977977
# Add some glyphs that don't exist in either font to check the Last Resort fallback.
978978
missing_glyphs='\n几个汉字'
@@ -981,11 +981,11 @@ def test__get_fontmap():
981981
ft=fm.get_font(
982982
fm.fontManager._find_fonts_by_props(fm.FontProperties(family=fonts))
983983
)
984-
fontmap=ft._get_fontmap(test_str)
985-
forchar,fontinfontmap.items():
986-
ifcharinmissing_glyphs:
987-
assertPath(font.fname).name=='LastResortHE-Regular.ttf'
988-
eliford(char)>127:
989-
assertPath(font.fname).name=='DejaVuSans.ttf'
990-
else:
991-
assertPath(font.fname).name=='cmr10.ttf'
984+
forsubstrintest_str.split('\n'):
985+
foriteminft._layout(substr,ft2font.LoadFlags.DEFAULT):
986+
ifitem.charinmissing_glyphs:
987+
assertPath(item.ft_object.fname).name=='LastResortHE-Regular.ttf'
988+
eliford(item.char)>127:
989+
assertPath(item.ft_object.fname).name=='DejaVuSans.ttf'
990+
else:
991+
assertPath(item.ft_object.fname).name=='cmr10.ttf'

‎lib/matplotlib/textpath.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,16 @@ def get_glyphs_with_font(self, font, s, glyph_map=None,
147147
glyph_map_new=glyph_map
148148

149149
xpositions= []
150+
ypositions= []
150151
glyph_reprs= []
151152
foritemin_text_helpers.layout(s,font,features=features,language=language):
152153
glyph_repr=self._get_glyph_repr(item.ft_object,item.glyph_index)
153154
glyph_reprs.append(glyph_repr)
154155
xpositions.append(item.x)
156+
ypositions.append(item.y)
155157
ifglyph_reprnotinglyph_map:
156158
glyph_map_new[glyph_repr]=item.ft_object.get_path()
157159

158-
ypositions= [0]*len(xpositions)
159160
sizes= [1.]*len(xpositions)
160161

161162
rects= []

‎src/ft2font_wrapper.cpp‎

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -623,54 +623,6 @@ PyFT2Font_get_kerning(PyFT2Font *self, FT_UInt left, FT_UInt right,
623623
return self->get_kerning(left, right, mode);
624624
}
625625

626-
constchar *PyFT2Font_get_fontmap__doc__ =R"""(
627-
Get a mapping between characters and the font that includes them.
628-
629-
.. warning::
630-
This API uses the fallback list and is both private and provisional: do not use
631-
it directly.
632-
633-
Parameters
634-
----------
635-
text : str
636-
The characters for which to find fonts.
637-
638-
Returns
639-
-------
640-
dict[str, FT2Font]
641-
A dictionary mapping unicode characters to `.FT2Font` objects.
642-
)""";
643-
644-
static py::dict
645-
PyFT2Font_get_fontmap(PyFT2Font *self, std::u32string text)
646-
{
647-
std::set<FT_ULong> codepoints;
648-
649-
py::dict char_to_font;
650-
for (auto code : text) {
651-
if (!codepoints.insert(code).second) {
652-
continue;
653-
}
654-
655-
py::object target_font;
656-
int index;
657-
if (self->get_char_fallback_index(code, index)) {
658-
if (index >=0) {
659-
target_font = self->fallbacks[index];
660-
}else {
661-
target_font =py::cast(self);
662-
}
663-
}else {
664-
// TODO Handle recursion!
665-
target_font =py::cast(self);
666-
}
667-
668-
auto key =py::cast(std::u32string(1, code));
669-
char_to_font[key] = target_font;
670-
}
671-
return char_to_font;
672-
}
673-
674626
constchar *PyFT2Font_set_text__doc__ =R"""(
675627
Set the text *string* and *angle*.
676628
@@ -1701,8 +1653,6 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
17011653
"string"_a,"angle"_a=0.0,"flags"_a=LoadFlags::FORCE_AUTOHINT,py::kw_only(),
17021654
"features"_a=nullptr,"language"_a=nullptr,
17031655
PyFT2Font_set_text__doc__)
1704-
.def("_get_fontmap", &PyFT2Font_get_fontmap,"string"_a,
1705-
PyFT2Font_get_fontmap__doc__)
17061656
.def("get_num_glyphs", &PyFT2Font::get_num_glyphs,
17071657
PyFT2Font_get_num_glyphs__doc__)
17081658
.def("load_char", &PyFT2Font_load_char,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp