Skip to content

ps/pdf: Override font height metrics to support AFM files#31371

Open
QuLogic wants to merge 1 commit intomatplotlib:text-overhaulfrom
QuLogic:afm-height-metrics
Open

ps/pdf: Override font height metrics to support AFM files#31371
QuLogic wants to merge 1 commit intomatplotlib:text-overhaulfrom
QuLogic:afm-height-metrics

Conversation

@QuLogic
Copy link
Member

@QuLogic QuLogic commented Mar 25, 2026

PR summary

When outputting files using the "core 14 fonts" (e.g., rcParams['pdf.use14corefonts'] = True), text will be measured using our internal AFM files instead of any external files.

While we don't have a full API decided for how to pass full Text objects through backends, add in a small internal helper to allow the PS/PDF backends to override font height metrics with the AFM files.

Note, because there is still some small difference in how we calculated line spacing, there is some small difference in the result from before. But it no longer changes results depending on whether Helvetica is installed globally.

AI Disclosure

None

PR checklist

@QuLogic
Copy link
Member Author

QuLogic commented Mar 25, 2026

Test failures should just be from #31343.

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

I didn't check the failing tests, but the change makes sense.

@QuLogic QuLogic force-pushed the afm-height-metrics branch from beec09b to 853aef9 Compare March 25, 2026 20:12
@QuLogic
Copy link
Member Author

QuLogic commented Mar 25, 2026

Side note, as this is the only time this test image has changed, feel free to merge after enough approvals without waiting for me.

When outputting files using the "core 14 fonts" (e.g.,
`rcParams['pdf.use14corefonts'] = True`), text will be measured using
our internal AFM files instead of any external files.

While we don't have a full API decided for how to pass full `Text`
objects through backends, add in a small internal helper to allow the
PS/PDF backends to override font height metrics with the AFM files.
@QuLogic QuLogic force-pushed the afm-height-metrics branch from 853aef9 to e0913d4 Compare March 26, 2026 02:42
@tacaswell
Copy link
Member

    def get_active_overrides() -> GlobalOverrides:
42503
        """Get the currently active GlobalOverrides instance.
42504
    
42505
        If no context is active, creates one from the current environment
42506
        using SETUPTOOLS_SCM prefix for legacy compatibility.
42507
    
42508
        Note: The auto-created instance reads from os.environ at call time,
42509
        so it will pick up environment changes (e.g., from pytest monkeypatch).
42510
    
42511
        Returns:
42512
            GlobalOverrides instance
42513
        """
42514
        global _auto_create_warning_issued
42515
    
42516
        overrides = _active_overrides.get()
42517
        if overrides is None:
42518
            # Auto-create context from environment for backwards compatibility
42519
            # Note: We create a fresh instance each time to pick up env changes
42520
            if not _auto_create_warning_issued:
42521
>               warnings.warn(
42522
                    "No GlobalOverrides context is active. "
42523
                    "Auto-creating one with SETUPTOOLS_SCM prefix for backwards compatibility. "
42524
                    "Consider using 'with GlobalOverrides.from_env(\"YOUR_TOOL\"):' explicitly.",
42525
                    UserWarning,
42526
                    stacklevel=2,
42527
                )
42528
E               UserWarning: No GlobalOverrides context is active. Auto-creating one with SETUPTOOLS_SCM prefix for backwards compatibility. Consider using 'with GlobalOverrides.from_env("YOUR_TOOL"):' explicitly.

This seems unrelated and new 😞 .

@QuLogic
Copy link
Member Author

QuLogic commented Mar 26, 2026

Yea, I reported that to pypa/setuptools-scm#1314

@tacaswell
Copy link
Member

I have

diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py
index b4f3cc7d21..137a6bd760 100644
--- a/lib/matplotlib/__init__.py
+++ b/lib/matplotlib/__init__.py
@@ -227,7 +227,7 @@ def _get_version():
             return setuptools_scm.get_version(
                 root=root,
                 dist_name="matplotlib",
-                version_scheme="release-branch-semver",
+                version_scheme="semver-pep440-release-branch",
                 local_scheme="node-and-date",
                 fallback_version=_version.version,
             )
diff --git a/pyproject.toml b/pyproject.toml
index 112528dc9d..74a3af9fe2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -82,7 +82,7 @@ requires = [
 install = ['--tags=data,python-runtime,runtime']
 
 [tool.setuptools_scm]
-version_scheme = "release-branch-semver"
+version_scheme = "semver-pep440-release-branch"
 local_scheme = "node-and-date"
 parentdir_prefix_version = "matplotlib-"
 fallback_version = "0.0+UNKNOWN"

sitting uncommited on my machine (working on a PR), but that produced a different warning->error.

@tacaswell
Copy link
Member

#31387 is PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

3 participants