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

Add font feature API to Text#29695

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
QuLogic merged 1 commit intomatplotlib:text-overhaulfromQuLogic:font-features
Oct 3, 2025

Conversation

QuLogic
Copy link
Member

PR summary

Font features allow font designers to provide alternate glyphs or shaping within a single font. These features may be accessed via special tags corresponding to internal tables of glyphs.

The mplcairo backend supports font features via anelaborate re-use of the font file path. This commit adds the API to make this officially supported in the main user API.

At this time, nothing in Matplotlib itself uses these settings, but they will have an effect with libraqm. I am opening this PR early for review of the API while I work through some issues with the latter. Consequently, the What's New notewill not show the effect of this API, and there are only smoke tests.

PR checklist

@QuLogic
Copy link
MemberAuthor

The What's New entrywith libraqm will look something more like this (minus a bug with the kerning, hopefully):
image

@anntzer
Copy link
Contributor

A difficulty that I've not really handled with mplcairo but warrants at least discussion is what you want to do with subranges. Harfbuzz supports toggling a feature only for some of the characters (seehttps://harfbuzz.github.io/harfbuzz-hb-common.html#hb-feature-from-string, e.g.aalt[3:5]=2), which mplcairo just forwards directly to harfbuzz, but this can(?) become problematic for multiline inputs, which get fed (by matplotlib) one-line-at-a-time to the rendering machinery, so something likeaalt[3:5] likely(?) gets interpreted as "characters 3-to-5of each line" rather than "characters 3-to-5 of the entire string".

The two main alternatives I can think of are either to do nothing, like mplcairo (subranges are interpreted as "repeated over each line"), or to reparse ranges and reinterpret them as "indices over the full string" (after line splitting, matplotlib tweaks the actual ranges than get fed to harfbuzz when shaping each line).

@anntzer
Copy link
Contributor

Also, looking at this again, I wonder how well this will interact with font fallback: it seems not unreasonable that two entries in the font fallback list would want to use different font features (e.g., a latin script and a chinese script likely want very different font features). Perhaps the real question here is whether font fallback should really have been implemented by stashing (references to) multiple fonts in a single FontProperties (though I'm not sure I can immediately design something much better), but the PR here makes the question more salient, I'd say.

@QuLogic
Copy link
MemberAuthor

Since we have automatic wrapping (Text(..., wrap=True)), I think the only possible implementation is to reparse the ranges ourselves.

For settings across font fallback, I guess that is possible to do, but it might require quite a bit of bookkeeping work.

@anntzer
Copy link
Contributor

The comments at#29794 (comment) also apply, but because this PR doesn't actually touch the rendering API, I guess it's fine.

@tacaswell
Copy link
Member

Discussed on a developer call we decided to not implement the sub-range application yet. Promoting a tuple of strings toDict[tuple[int, int], Tuple[str, ...]] is something we can do unambiguously later and given that we expect most strings to be "short", hopefully the demand for mixed language within oneText object will be low.

@anntzer
Copy link
Contributor

Even if we don't explicitly support sub-ranges, we still need to decide what happens if someone writes e.g.fontfeatures=["+kern[3:5]"] (where the whole syntax gets interpreted by harfbuzz). It's probably fine to just document the limitation for now ("the interaction of subranges and multiline text is currently unspecified and behavior may change in the future").

tacaswell and timhoffm reacted with thumbs up emoji

@QuLogic
Copy link
MemberAuthor

Since we moved the information fromFontProperties toText, I was actually going to implement the splitting.However, I ran into an issue in that theText object is only supplied toRenderer.draw_text if the string is not multiline:

mtext=selfiflen(info)==1elseNone

I tried a small change:

diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.pyindex 3b0de58814..b255a93c52 100644--- a/lib/matplotlib/text.py+++ b/lib/matplotlib/text.py@@ -800,9 +800,7 @@ class Text(Artist):              angle = self.get_rotation()              for line, wh, x, y in info:--                mtext = self if len(info) == 1 else None                 x = x + posx                 y = y + posy                 if renderer.flipy():@@ -816,14 +814,19 @@ class Text(Artist):                 else:                     textrenderer = renderer-                if self.get_usetex():-                    textrenderer.draw_tex(gc, x, y, clean_line,-                                          self._fontproperties, angle,-                                          mtext=mtext)-                else:-                    textrenderer.draw_text(gc, x, y, clean_line,-                                           self._fontproperties, angle,-                                           ismath=ismath, mtext=mtext)+                xt, yt = self.get_transform().inverted().transform((x, y))+                with cbook._setattr_cm(self, _x=xt, _y=yt, _text=clean_line,+                                       convert_xunits=lambda x: x,+                                       convert_yunits=lambda y: y,+                                       _horizontalalignment='left', _verticalalignment='bottom'):+                    if self.get_usetex():+                        textrenderer.draw_tex(gc, x, y, clean_line,+                                              self._fontproperties, angle,+                                              mtext=self)+                    else:+                        textrenderer.draw_text(gc, x, y, clean_line,+                                               self._fontproperties, angle,+                                               ismath=ismath, mtext=self)          gc.restore()         renderer.close_group('text')

AFAICT, only the PGF backend usesmtext and under certain conditions will place the text using the original position and alignment instead of thex/y passed todraw_text. Unfortunately, these don't seem to match (I assume the x/y passed in accounts for the descenders and other flourishes), so this breaks the PGF tests. I wonder if there's a better condition to be put in here:

ifmtextand (
(angle==0or
mtext.get_rotation_mode()=="anchor")and
mtext.get_verticalalignment()!="center_baseline"):

@QuLogicQuLogic changed the base branch frommain totext-overhaulJune 5, 2025 01:38
@QuLogicQuLogic moved this toWaiting for other PR inFont and text overhaulJun 5, 2025
@QuLogicQuLogic moved this fromWaiting for other PR toTodo inFont and text overhaulJun 5, 2025
@QuLogicQuLogic linked an issueJul 1, 2025 that may beclosed by this pull request
@QuLogicQuLogic moved this fromWaiting for other PR toReady for Review inFont and text overhaulSep 20, 2025
@image_comparison(baseline_images=['features.png'],remove_text=False,style='mpl20')
deftest_text_features():
fig=plt.figure(figsize=(5,1.5))
t=fig.text(0,0.7,'Default: fi ffi fl st',fontsize=32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can you pad out the label strings to the same size so that the first letters of the example text line up? Does not matter for the machine to check, but makes it a bit easier for the humans to check the differences.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Do you want it aligned in the input text, or in the resulting image? If I align it in the input, then it's not quite right in the result because it's not monospace:
features
but I could change to right alignment, which would be close, I think?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Right alignment looks good, I think:
features

tacaswell reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I spend so much time looking at monospace fonts I forget not all fonts are monospace 🤦🏻 .

Right-align is exactly what I wanted.

@QuLogicQuLogicforce-pushed thefont-features branch 2 times, most recently fromb474c3e to062b130CompareSeptember 26, 2025 02:40
Copy link
Member

@ksundenksunden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Approving but not merging because some image change squashing/interactions with other PRs can be handled by@QuLogic

Font features allow font designers to provide alternate glyphs orshaping within a single font. These features may be accessed via specialtags corresponding to internal tables of glyphs.The mplcairo backend supports font features via an elaborate re-use ofthe font file path [1]. This commit adds the API to make this officiallysupported in the main user API.[1]https://github.com/matplotlib/mplcairo/blob/v0.6.1/README.rst#font-formats-and-features
@QuLogic
Copy link
MemberAuthor

Rebased without the image change (moved totext-overhaul-figures branch) so this can be merged. Going to ignore the linting error, as that's known#30626.

@QuLogicQuLogic merged commita6ac58f intomatplotlib:text-overhaulOct 3, 2025
36 of 37 checks passed
@github-project-automationgithub-project-automationbot moved this fromReady for Review toDone inFont and text overhaulOct 3, 2025
@QuLogicQuLogic deleted the font-features branchOctober 3, 2025 03:05
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@tacaswelltacaswelltacaswell approved these changes

@anntzeranntzeranntzer left review comments

@ksundenksundenksunden approved these changes

Assignees
No one assigned
Projects
Status: Done
Milestone
v3.11.0
Development

Successfully merging this pull request may close these issues.

OTF feature support (alternate figure styles, etc.)
7 participants
@QuLogic@anntzer@tacaswell@timhoffm@story645@khaledhosny@ksunden

[8]ページ先頭

©2009-2025 Movatter.jp