fontspec icon indicating copy to clipboard operation
fontspec copied to clipboard

strange size selection for italic with \setmainfont{Latin Modern Roman} after using \slshape

Open cabohah opened this issue 2 years ago • 10 comments
trafficstars

When a LuaLaTeX document with base font size 12pt and \setmainfont{Latin Modern Roman} switches to \slshape and than switches to \itshape the italic text is printed smaller.

\documentclass[12pt]{report}

\usepackage{fontspec}
\setmainfont{Latin Modern Roman}

\begin{document}
Some text.

\itshape Some italic text with correct size.

\normalfont\slshape Some slanted text.

\normalfont\itshape Some italic text, which is smaller without using any fontsize command.

\end{document}

Results in:

smaller second italic text

As you can see, the fourth line is printed smaller than the second, which should not happen.

See also https://tex.stackexchange.com/q/689449/277964 where the issue has been reported first.

cabohah avatar Jun 24 '23 13:06 cabohah

you get a font warning in the log:

LaTeX Font Info:    Font shape `TU/LatinModernRoman(0)/m/sl' in size <12> not available
(Font)              Font shape `TU/LatinModernRoman(0)/m/it' tried instead on input line 211.

I don't know if fontspec could catch that, but you can avoid it by setting the slantedfont explicitly

\documentclass[12pt]{report}

\usepackage{fontspec}

\setmainfont{Latin Modern Roman}[SlantedFont=LatinModernRoman/I]

\begin{document}
Some text.

\itshape Some italic text with correct size.

\normalfont\slshape Some slanted text.

\normalfont\itshape Some italic text, which is smaller without using any fontsize command.

\end{document}

u-fischer avatar Jun 24 '23 14:06 u-fischer

I've seen the Font Info, but IHMO an automatic font replacement should not change the size of another font, i.e., not if that other font already has been used with the correct size.

On TeX.Stackexchange (see the link above) I've also already commented, that either setting ItalicFont or SlantedFont explicitly, fixes the problem. But IMHO this is suboptimal. I would maybe understand, if the SlantedFont would have been in the wrong size. But that selecting slanted breaks the italic font is unexpected not only for me, but IMHO for the average user.

cabohah avatar Jun 24 '23 15:06 cabohah

I don't fully understand what is happening here, but I agree it's not good!

wspr avatar Apr 29 '24 06:04 wspr

It looks to me as if the source of the trouble is this NFSS macro

\def\do@subst@correction{%
       \xdef\subst@correction{%
          \font@name
          \global\expandafter\font
            \csname \curr@fontshape/\f@size\endcsname
            \noexpand\fontname\font
           \relax}%
       \aftergroup\subst@correction
}

Its long long time ago but the code attempts to reset the display name in something like \showoutput for the the font that was the source of of the of the substitution, if I remember correctly (the docs are somewhat sketchy).

If an external font was used with two \font declarations then TeX would always reports the last macro as the font selector and the \do@subst@correction was correcting that, ie avoiding that with

\documentclass{article}

\begin{document}

foo

\font\xxx=cmr10

bar

\showoutput
\end{document}

you still see \OT1/cmr/m/n/10 and not suddenly \xxx (the font shape that was subsituted to also mean cmr10) all over the place.

Obviously, this seems to fails with fontspec when (as @u-fischer pointed out to me) fonts with optical sizes are involved. @zauguin any thoughts from your end?

FrankMittelbach avatar Apr 29 '24 08:04 FrankMittelbach

By the way, the problem is luatex specific, with XeTeX the \do@subst@correction code behaves correctly.

FrankMittelbach avatar Apr 29 '24 10:04 FrankMittelbach

The problem is that with fonts with optical sizes \fontname\font doesn't give back the size:

\input{luaotfload.sty}
\font\test={name:LatinModernRoman:mode=node;script=latn;language=dflt;+tlig;} at 12pt
\test aaa

\immediate\write17{\fontname\font}

\font\testB = \fontname\font \testB 
aaa

\font\test={name:arial:mode=node;script=latn;language=dflt;+tlig;} at 12pt
\test aaa

\immediate\write17{\fontname\font}

\font\testB = \fontname\font \testB 
aaa
\bye

types out

name:LatinModernRoman:mode=node;script=latn;language=dflt;+tlig;
name:arial:mode=node;script=latn;language=dflt;+tlig; at 12.0pt

and the "reused" font are smaller for latin modern:

image

Imho that is something that should be handled by luaotfload.

u-fischer avatar Apr 29 '24 12:04 u-fischer

Does anyone see an issue with just always adding the size in \fontname and other places by claiming a designsize of 0?

zauguin avatar Apr 29 '24 19:04 zauguin

Well, what is important is basically a way to be able feed in the result of \fontname\font into a a font declaration and this way produce that same font selector again, sort of

...currently in some font ...
\edef\foo{\fontname\font}
...
\font\xxx=\foo\relax

Now \xxx should produce the same fontselector (with the same size) as we started with.

I would be possible to do this on the macro level (given that we know the \f@size), via

\edef\foo{\fontname\font\space at \f@size pt}

but that would badly fail if the font does already has some at...pt so the code would need to analyse the result and append only if necessary (but that is fixing a problem that shouldn't be there in the first place).

So it would be much better if \fontname really contains what it should, which is data that allows for the roundtrip. pdfTeX and XeTeX do while luatex doesn't. Not sure if this is a bug in the primitive or if there is no primitive in luaTeX and the data is produced by luaotfload ... in either case I think it should be correct at the engine/luaotfload side.

If pretending a design size of 0 does the trick I don't know.

FrankMittelbach avatar Apr 29 '24 20:04 FrankMittelbach

I now implemented the "design size 0" approach in luaotfload's dev branch, so far it looks like it's working.

zauguin avatar May 01 '24 16:05 zauguin

sounds good @zauguin! much better if this works out of the box rather than having to analyze the return value of \fontname and try to correct it on macro level.

FrankMittelbach avatar May 01 '24 18:05 FrankMittelbach