Title: Fix usetex path optical sizing by krishsinghhura · Pull Request #32392 · matplotlib/matplotlib · GitHub
Open Graph Title: Fix usetex path optical sizing by krishsinghhura · Pull Request #32392 · matplotlib/matplotlib
X Title: Fix usetex path optical sizing by krishsinghhura · Pull Request #32392 · matplotlib/matplotlib
Description: PR summary Closes #32389 Problem In usetex mode, TextToPath.get_glyphs_tex hardcoded LaTeX compilation to a fixed FONT_SCALE = 100 pt. Because TeX fonts have size-specific optical designs, TeX selected the large 17 pt design (CMSS17) and scaled it down, while layout measurement (TexManager.get_text_width_height_descent) typeset at the actual text size (e.g. CMSS8 for 6–8 pt). This caused: Font mismatch: SVG output used CMSS17 while PDF output used CMSS8. Alignment errors: Right-aligned text with path_effects was narrower than its layout box and ended short of the alignment boundary. TextPath(size=...) issue: TextPath accepted size separately without propagating it to prop. Solution Updated TextToPath.get_glyphs_tex to typeset at the requested font size (fontsize = prop.get_size_in_points()) and rescale DVI coordinates and boxes back to FONT_SCALE units via scale = self.FONT_SCALE / fontsize. Normalizes prop via FontProperties._from_any(prop) for defensive input handling. Forwards explicit size in TextPath.__init__ to a copy of prop. Added unit tests in test_usetex.py covering optical font selection and TextPath size propagation. Reproduction & Verification Click to view reproduction code import re import matplotlib as mpl import matplotlib.patheffects as pe import matplotlib.pyplot as plt mpl.rcParams["text.usetex"] = True s = "Hamburgefonstiv" # 1. 6 pt text saved as SVG and as PDF fig = plt.figure(figsize=(2, 1)) fig.text(0.1, 0.5, s, fontsize=6) fig.savefig("usetex.svg") fig.savefig("usetex.pdf") with open("usetex.svg", encoding="utf-8") as f: svg = f.read() with open("usetex.pdf", "rb") as f: pdf = f.read() print("SVG fonts:", sorted(set(re.findall(r'id="(CM[A-Z]+\d+)-', svg)))) print("PDF fonts:", sorted({m.decode() for m in re.findall(rb"/BaseFont\s*/(?:[A-Z]{6}\+)?(\w+)", pdf)})) # 2. Right-aligned 8 pt text on Agg, without and with path effects fig = plt.figure(figsize=(1.6, 0.5), dpi=600) fig.add_artist(plt.Line2D([0.93, 0.93], [0, 1], lw=0.3, color="tab:red")) fig.text(0.93, 0.62, s, fontsize=8, ha="right", va="center") fig.text(0.93, 0.25, s, fontsize=8, ha="right", va="center", path_effects=[pe.Normal()]) fig.savefig("usetex_path_effects.png") Expected Results with this PR: Output: SVG fonts: ['CMSS8'] PDF fonts: ['CMSS8'] (Before: SVG used ['CMSS17']) Image: In usetex_path_effects.png, both lines of text align flush with the red guide line. Tests Run: pytest lib/matplotlib/tests/test_usetex.py -k "optical_sizing or textpath_size" (Passed) Regression suite: test_mathdefault, test_multiline_eqnarray, test_usetex_with_underscore, test_usetex_fallback, test_rotation, test_unicode_sizing (All passed) Code checks: git diff --check passed, no trailing whitespace or lint issues. AI Disclosure I used AI assistance to investigate the issue, trace relevant rendering paths, draft the implementation and tests, and help analyze test output. I reviewed the resulting changes, ran verification steps, and am responsible for the final contribution. PR quality check Use an expressive title (e.g. Fix usetex path font optical sizing and alignment) New and changed code is tested [N/A] Plotting related features are demonstrated in an example [N/A] New features and API changes have release notes Documentation complies with general and docstring guidelines
Open Graph Description: PR summary Closes #32389 Problem In usetex mode, TextToPath.get_glyphs_tex hardcoded LaTeX compilation to a fixed FONT_SCALE = 100 pt. Because TeX fonts have size-specific optical designs, TeX sele...
X Description: PR summary Closes #32389 Problem In usetex mode, TextToPath.get_glyphs_tex hardcoded LaTeX compilation to a fixed FONT_SCALE = 100 pt. Because TeX fonts have size-specific optical designs, TeX sele...
Opengraph URL: https://github.com/matplotlib/matplotlib/pull/32392
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:ef362ef7-0a2a-9efe-dbe8-6a940983764e |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9758:1577AD:66E018:8C9A8B:6B06859B |
| html-safe-nonce | c147b5d20c97f4d2a6e471566ac300e386f7880dbd6fb899a3acb07df304974f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzU4OjE1NzdBRDo2NkUwMTg6OEM5QThCOjZCMDY4NTlCIiwidmlzaXRvcl9pZCI6IjY4NDA3ODM2ODA0NTg2MjIzNjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c542d22671a2f83ce07d95fbde4b37f632572050266c626db1dac12f5b0704a4 |
| hovercard-subject-tag | pull_request:4632137603 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/matplotlib/matplotlib/pull/32392/files |
| twitter:image | https://avatars.githubusercontent.com/u/95937258?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/95937258?s=400&v=4 |
| og:image:alt | PR summary Closes #32389 Problem In usetex mode, TextToPath.get_glyphs_tex hardcoded LaTeX compilation to a fixed FONT_SCALE = 100 pt. Because TeX fonts have size-specific optical designs, TeX sele... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4462ff7ebfd3d551f202659f2423dae617b6849d23b1df6465dc504ef497d14a |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 6d3909414814ff86ce7da4c51a0f4918a13fb41b |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width