GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Implement TeX's fraction and script alignment by QuLogic · Pull Request #31046 · matplotlib/matplotlib Conversation
|
|
WRT the original #22852 (comment):
Also unsure about this one; it appears that one square root sign is sized a bit different?
Seems to be the case, but apostrophes are handled by the
This is no longer failing; I think this may be one of the rounding issues for the initial character found (and fixed) in #30059. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
TODO: Since the height of fractions is a little bigger, I think I may need to tweak the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I've gone through the sub/super script changes and they seem fine as well, other than a couple known shortcuts. I'm only uncertain about the constants; they may have only been chosen to minimize image changes instead of matching TeX. For CM, it was also calculated with our current hinting settings and not the defaults that have been switched to in the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Modulo re basing and shortening the constants.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
b8b8063 to
b6c10a0
Compare
b6c10a0 to
75c11c0
Compare
There was an error while loading. Please reload this page.
|
While looking at the TeX algorithms, I found out about Comparing the values, we have:
Remember that we multiply everything by x-height, so here I've divided that metric out in the last column. Also, we use So, the numbers we have for For the fraction metrics ( Before we had this (with I haven't found any corresponding TFM files with the same metrics in them for the other fonts we have. I believe modern LaTeX can synthesize these directly from the font, and I do see some embedded MATH tables in there, but I haven't worked out the conversions for those yet. I've pushed the changes to the Computer Modern constants as two commits for ease of review. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I reported this as a bug back then (#23474). Changing the script rendering logic is what exposed it. After merging a fix (#23482), the test passed even with the updated logic.
🎯💯
I remember being puzzled that using the same constants (from either Computer Modern or Latin Modern—I can't remember which one I had checked) didn't yield neatly aligned denominators. The multiplication by the x-height appears to be the key, which didn't strike me then. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| sup1 = 0.79716796875 | ||
| sub1 = 0.354296875 | ||
| sub2 = 0.5314453125 | ||
| supdrop = 0.386108 / 0.430555 |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You can get more accurate (effectively fixed-point representation) values from the tfm file, as tftopl prints values after scaling and drops some decimal points, e.g. for cmsy10 I get
slant=262144, space=0, space_stretch=0, space_shrink=0, x_height=451470, quad=1048579, extra_space=0 num1=709370, num2=412858, num3=465286, denom1=719272, denom2=361592, sup1=432949, sup2=380520, sup3=302922, sub1=157286, sub2=259226, supdrop=404864, subdrop=52429, delim1=2506096, delim2=1059062, axis_height=262144
(values need to be scaled by 2**20).
I extended the Tfm class to read these values at anntzer@a360989 if you want to try your hand at it (we don't need to decide now whether we actually want to integrate this functionality into the Tfm class).
Note that this also allows reading the actual definition of 1em and 1ex ("quad" and "x_height"), which should be better than the current approach of trying to guess the values (see @tfpf's comment
I reported this as a bug back then (#23474). Changing the script rendering logic is what exposed it. After merging a fix (#23482), the test passed even with the updated logic.
the linked threads (#23474 (comment)), and https://tex.stackexchange.com/a/98139).
Perhaps also worth fixing this properly?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's very useful, and might be something to implement along with stuff needed for #31048. For now, I've just put the numbers you have read directly into the file. They're very close and it only affects one image.
For quad / x_height, I can take a look, but we can also make that change independently from this one, I think?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sure (though it'll probably again break all images, so it has to be done in the text-overhaul branch too).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
BTW, I just noticed that fontTools has a TFM parser; I'm not sure how extensive it is.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
From a very quick look it looks complete; you can run python -mfontTools.tfmLib foo.tfm and in particular the params above will be printed (again they are scaled by 2**20 but all the decimal points are given so you can just remultiply back).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
It appears to me that this specific issue has been fixed by #30059, can you confirm? (I haven't looked carefully yet at how this interacts with the fraction rendering.) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
By the way, https://www.tug.org/TUGboat/tb27-1/tb86jackowski.pdf has some nice figures explaining fraction layout, and https://www.tug.org/~vieth/papers/bachotex2008/math-font-paper.pdf some more discussion on inferring font parameters (though neither the x-height nor the axis-height...). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
According to The LaTeX Font Catalogue, I don't think DejaVu Sans provides math support in LaTeX, so I'm a bit uncertain how to compare our math constants with it. Opening up the Instead, I've managed to find values in FontForge's TeX tables in a slightly-hidden settings page, then dividing by the xheight in that same setting page (1120), we get:
Before, we had: Note that the right side of the figure is DejaVu Serif has the same metrics, except the xheight is 1063 instead, so I won't post the table over again. Instead, just before and after images: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
There is a dejavu math font: https://www.gust.org.pl/projects/e-foundry/tex-gyre-dejavu-math / https://ctan.org/pkg/tex-gyre-math-dejavu?lang=en but it's only a serif font :/ not sure how well the constants would compare? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Well, we do have DejaVu Serif as well with almost the same metrics; only the xheight is different at 1063 instead of 1120 (updated above with those images as well.) But it looks like the TeX Gyre fonts are for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
#31064 enables to use the TeX Gyre fonts from within usetex. Currently, it does not dynamically set the "constants" but uses the defaults, but one could in principle read the otf MATH table. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
a77b250 to
df76ad7
Compare
|
Squashed into fewer commits and rebased on the latest |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
As described in *TeX: the Program* by Don Knuth. New font constants are set to the nearest integral multiples of 0.1 for which numerators and denominators containing normal text do not have to be shifted beyond their default shift amounts at font size 30 in display and text styles. To better process superscripts and subscripts, the x-height is now always calculated instead of being retrieved from the font table (which was the case for Computer Modern); the affected font constants have been changed. A duplicate test was also fixed in the process.
At the call site https://github.com/matplotlib/matplotlib/blob/51fbfc4eb0e882ef7e95ceab9777c7047f4db819/lib/matplotlib/_mathtext.py#L1706-L1709 the box should clearly go from `cur_v + off_v` to `cur_v + off_v - rule_height` (this is why `cur_v` is shifted by `+ rule_height` just before; also at that point some print debugging indicates that y's go *downwards*), so `Rule.render` should indeed call `render_rect_filled` from `y - h` to `y`.
Computer Modern values are taken from `cmsy10.tfm`, and divided by the x-height in that output to match the scale used in Matplotlib. DejaVu Sans/Serif and STIX constants are taken from the embedded TeX table extracted with FontForge.
ce85a59 to
1cd8510
Compare
|
OK, going to merge, but the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
|
||
|
|
||
| class STIXSansFontConstants(FontConstantsBase): | ||
| # These values are extracted from the TeX table of STIXGeneral.ttf using FreeType, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This should have said FontForge, not FreeType; I'll fix it in a followup.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…cal-align Implement TeX's fraction and script alignment
This includes images changes for the following pull requests / commits: * [Fix center of rotation with rotation_mode='anchor'](matplotlib#29199) (c44db77) * [Remove ttconv backwards-compatibility code](matplotlib#30145) (8caff88) * [Remove kerning_factor from tests](matplotlib#29816) (7b4d725) * [Set text hinting to defaults](matplotlib#29816) (8255ae2) * [Update FreeType to 2.13.3](matplotlib#29816) (89c054d) * [Implement text shaping with libraqm](matplotlib#30000) (b0ded3a, 9813523) * [Add language parameter to Text objects](matplotlib#29794) (7ce8eae) * [Fix auto-sized glyphs with BaKoMa fonts](matplotlib#29936) (3ba2c13) * [pdf: Improve text with characters outside embedded font limits](matplotlib#30512) (b70fb88, 6cedcf7) * [Prepare `CharacterTracker` for advanced font features](matplotlib#30608) (8274e17, 70dc388, df670cf, ed5e074) * [Add font feature API to Text](matplotlib#29695) (972a688) * [Fix spacing in r"$\max f$"](matplotlib#30715) (4a99a83) * [Implement libraqm for vector outputs](matplotlib#30607) (bd17cd4) * [Drop the FT2Font intermediate buffer](matplotlib#30059) (9d7d7b4) * [Rasterize dvi files without dvipng](matplotlib#30039) (7627118) * [Update bundled FreeType and HarfBuzz libraries](matplotlib#30938) (a161658, 9619bcc) * [Fix positioning of wide mathtext accents](matplotlib#31069) (c2fa7ba) * [Refactor RendererAgg.draw_{mathtext,text,tex} to use same base algorithm](matplotlib#31085) (931bcf3) * [Implement TeX's fraction and script alignment](matplotlib#31046) (94ff452, 4bfa0f9, 1cd8510) * [Fix confusion between text height and ascent in metrics calculations](matplotlib#31107) (60f2310) * [mathtext: Fetch quad width & axis height from font metrics](matplotlib#31110) (692df3f, 383028b) * [mathtext: add mathnormal and distinguish between normal and italic family](matplotlib#31121) (a6913f3) * [ENH: Ignore empty text for tightbbox](matplotlib#31285) (d772043) * [Drop axis_artist tickdir image compat, due to text-overhaul merge](matplotlib#31281) (2057583) * [text: Use font metrics to determine line heights](matplotlib#31291) (3ab6a27, d961462, 97f4943) * [ps/pdf: Override font height metrics to support AFM files](matplotlib#31371) (e0913d4) * [TST: Cleanup back-compat code in tests touched by text overhaul](matplotlib#31295) (7c33379) * [TST: Set tests touched by text overhaul to mpl20 style](matplotlib#31300) (41c4d8d)
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
[Bug]: \genfrac has bad spacing with (high) custom ruler Vertical positioning in mathtext fraction rendering could be improved sub/superscripts should be moved further from the baseline following large delimiters
There was an error while loading. Please reload this page.