8000 Add font feature API to FontProperties and Text by QuLogic · Pull Request #29695 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add font feature API to FontProperties and 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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

QuLogic
Copy link
Member
@QuLogic QuLogic commented Mar 1, 2025

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 an elaborate 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 note will not show the effect of this API, and there are only smoke tests.

PR checklist

@QuLogic
Copy link
Member Author
QuLogic commented Mar 1, 2025

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

@anntzer
Copy link
Contributor
anntzer commented Mar 1, 2025

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 (see https://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 like aalt[3:5] likely(?) gets interpreted as "characters 3-to-5 of 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
anntzer commented Mar 2, 2025

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
Member Author
QuLogic commented Mar 22, 2025

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.

8000
)""";

static void
PyFT2Font_set_features(PyFT2Font *self, py::object features_obj)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can just take features_obj as a std::vector<std::string> unless you really want to restrict to taking tuples?

@anntzer
Copy link
Contributor
anntzer commented Mar 22, 2025

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

@@ -52,6 +52,7 @@ class FontProperties:
size: float | str | None = ...,
fname: str | os.PathLike | Path | None = ...,
math_fontfamily: str | None = ...,
features: list[str] | None = ...,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
features: list[str] | None = ...,
features: tuple[str] | list[str] | None = ...,

features.push_back(feature.cast<std::string>());
}
}
self->x->set_features(std::move(features));
Copy link
Member

Choose a reason for hiding this comment

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

Just to check my understanding, this is where we actually tell freetype to do anything ?

@tacaswell
Copy link
Member

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

@anntzer
Copy link
Contributor
anntzer commented May 7, 2025

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").

@QuLogic
Copy link
Member Author
QuLogic commented May 12, 2025

Since we moved the information from FontProperties to Text, I was actually going to implement the splitting. However, I ran into an issue in that the Text object is only supplied to Renderer.draw_text if the string is not multiline:

mtext = self if len(info) == 1 else None

I tried a small change:

diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py
index 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 uses mtext and under certain conditions will place the text using the original position and alignment instead of the x/y passed to draw_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:

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

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 an elaborate re-use of
the font file path [1]. 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.

[1] https://github.com/matplotlib/mplcairo/blob/v0.6.1/README.rst#font-formats-and-features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0