Skip to content
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

Font changes on its own within log-scale tick label #1296

Closed
moble opened this issue Sep 7, 2021 · 9 comments
Closed

Font changes on its own within log-scale tick label #1296

moble opened this issue Sep 7, 2021 · 9 comments

Comments

@moble
Copy link

moble commented Sep 7, 2021

When using a font different from the default, and using a log scale, it is obvious that the font changes within the superscript of the tick labels. Here's an MWE simplified from the BeautifulMakie example:

using CairoMakie

fig = Figure(font = "CMU Serif")
ax = Axis(fig, yscale=log10)
lines!(0:1:50, x-> exp(-x))
fig[1,1] = ax
save("logscale.pdf", fig)  # Same result when using `display` in jupyter
save("logscale.png", fig)  # Same result when using `display` in jupyter

As you can see, the last digit in the exponents of the tick labels on the y scale are in the wrong font, and seemingly incorrectly aligned vertically:

logscale.pdf

logscale

(Note that the "last" digit is the only digit in the 10^0 and 10^-5 superscripts, but their heights are clearly inconsistent with the other superscripts, and at least the 5 is obviously sans serif.)

It seems that a workaround is to add the argument

ytickformat=ys->map(y->L"10^{%$(round(Int, log10(y)))}", ys)

to the Axis call, though that makes the superscripts a little lighter and harder to read.

Here's my version info:

(makie_logscale) pkg> status
      Status `/tmp/makie_logscale/Project.toml`
  [ee78f7c6] Makie v0.15.2

julia> versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, haswell)
@moble moble changed the title Changing font within log scale exponent tick label Font changes on its own within log-scale tick label Sep 7, 2021
@jonas-schulze
Copy link
Contributor

Using e.g. LaTeX you can observe that some fonts distinguish between digits occuring in text and in math mode. Some digits decent below the baseline when in text mode but don't when in math mode. I suspect that tick labels are rendered like classical strings (cf #1560 (comment)), i.e. in text mode only, and the exponents are "text mode superscripts". As a hotfix, maybe there is a different variant of your font that is identical up to this particular handling of digits.

@moble
Copy link
Author

moble commented Mar 9, 2022

There is evidently a font-selection problem, but it's not just a baseline issue, or that the the base and exponent are in different fonts, it's that the font changes within the superscript!

For example, if you zoom in on the $10^{-15}$ on the y axis, this is what you see:

logscale-2

The 1 and the 5 in the exponent are clearly different fonts — not just at different positions relative to the baseline. In fact, if I crop that little section out of the PDF, pdffonts says that the $10^{-1}$ is in PTSerif-Regular for some reason, and the 5 is in DejaVuSans. They're literally different fonts.

For reference, here is what 15 looks like in these three different fonts:

test_mathfont_CMU

test_mathfont_PTSerif

test_mathfont_DejaVuSans

Note that the bottoms of 1 and 5 are aligned in all three fonts, and the results are consistent with what pdffonts is telling me about the output from CairoMakie.

@moble
Copy link
Author

moble commented Mar 9, 2022

I've just tried this code again with a fresh julia (1.7.2) project, in which the only dependency was CairoMakie, and got the same result, except that it now correctly uses CMU Serif for the $10^{-1}$. Still DejaVu Sans for the 5, though.

And again, I should clarify that this pattern is the same for the exponents in all the y ticklabels: everything uses CMU Serif except for the last digit in the exponent, which is in DejaVu Sans — whether it's the final 0 in $10^0$, $10^{-10}$, or $10^{-20}$, or the 5 in $10^{-5}$ or $10^{-15}$.

@SimonDanisch
Copy link
Member

At least for GLMakie, a fallback font gets used if a certain glyph can't be found in the font.
Since those are unicode superscripts which aren't supported equally well by all fonts, maybe it can't find the 5 in CMU Serif

@jonas-schulze
Copy link
Contributor

jonas-schulze commented Mar 10, 2022

@SimonDanisch, how did you find which glyphs are included in a particular font?

I was trying to find out which digit glyphs are included for Utopia, but could only find the "normal" digits. Could the issue be related to superscript 1, 2, 3 being in the Latin-1 Supplement group while 4-9, 0 are in the Superscripts and Subscripts range? Maybe, for the Latin-1 Supplement, some fonts know how to transform the default character set, while for the other group they just pass.

cf https://github.com/SimonDanisch/UnicodeFun.jl/blob/master/src/sub_super_scripts.jl#L76-L90

@SimonDanisch
Copy link
Member

Yeah, haven't looked it up, but sounds about right that they're in different groups.

using FreeTypeAbstraction
font = FreeTypeAbstraction.findfont("Dejavu Sans")
FreeTypeAbstraction.FT_Get_Char_Index(font, '') !== 0

@jonas-schulze
Copy link
Contributor

Thanks! FYI, Utopia has glyphs for Latin-1 Supplement only (i.e. superscript 1,2,3; I fixed my comment above). I don't have CMU Serif installed, so I can't check.

@moble
Copy link
Author

moble commented Mar 11, 2022

Ah, I didn't realize it was trying to use unicode to do superscripts. Indeed, on my system at least, lualatex can only find superscript glyphs in CMU Serif for 1, 2, and 3. But it can actually create superscripts in the usual tex way. So I guess the problem is to get ticklabels created using LaTeXStrings...

@moble
Copy link
Author

moble commented Aug 8, 2023

This seems to have been resolved (possibly by #2321), at least as of Makie v0.19.7 and CairoMakie v0.10.7.

The relevant BeautifulMakie page is now here, and my MWE above would have to change as

-fig = Figure(font = "CMU Serif")
+fig = Figure(fonts = (;regular="CMU Serif"))

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

No branches or pull requests

4 participants