fontspec
fontspec copied to clipboard
Support fraction expressions for Scale
Description
Using Scale=<decimal number> is subject to TeX’s binary rounding errors. It is possible to employ \dimexpr from eTeX (enabled under both XeTeX and LuaTeX) to get more accurate results. The latter can be realized by hacking into NFSS, but a self-contained fontspec solution would be even better.
Check/indicate
- [x] Relevant for XeTeX
- [x] Relevant for LuaTeX
- [x] Issue tracker has been searched for similar issues?
- [x] https://tex.stackexchange.com/q/485339
Minimal example demonstrating the issue
\documentclass{article}
\makeatletter
% Look ahead for either a decimal or a fraction
% and extract all stuff after the first slash:
\def\detect@decimal@or@frac#1/#2\detect@decimal@or@frac{%
\edef\@tempa{#2}%
}
% Hack into NFSS:
\def\empty@sfcnt#1{%
\@tempdimb \f@size\p@
\ifx\optional@arg\@empty
\else
% The following line was the original:
% \@tempdimb \optional@arg\@tempdimb
% Replaced with my new implementation:
\expandafter\detect@decimal@or@frac
\optional@arg/0\detect@decimal@or@frac
\def\@tempb{0}%
\ifx\@tempa\@tempb
% A decimal or an integer is passed here
\@tempdimb \optional@arg\@tempdimb
\else
% A fraction is passed here
\@tempdimb \dimexpr\@tempdimb*\optional@arg\relax
\fi
% End of new implementation
#1{Font\space shape\space `\curr@fontshape'\space
will\space be\MessageBreak
scaled\space to\space size\space \the\@tempdimb}%
\fi
\edef\external@font{\mandatory@arg\space at\the\@tempdimb}}
\makeatother
\usepackage{fontspec}
\newcommand\test[1]{%
\begingroup
\texttt{\string\fontspec\{texgyretermes-regular.otf\}[Scale=\{#1\}]}:\\
\fontspec{texgyretermes-regular.otf}[Scale={#1}]%
\the\fontdimen6\font\par
\medskip
\endgroup
}
\setlength\parindent{0pt}
\begin{document}
\test{1}
\test{1.2}
\test{6/5}
\test{1,4}
\test{14/10}
\end{document}
Further details
(Perhaps this is more suitable for the LaTeX3 team?)
