I am using LuaLaTeX (>=2022), OpenType fonts, microtype. Platform is Linux. I have permission to write files in the same directory (or subdirectory) as my main *.tex document. I cannot use --shell-escape, but I can use Lua io.open, write, read, close, as needed.
Although I am using a custom document class, this question should be applicable to standard classes. I have looked through the microtype documentation, and the fontspec documentation, without seeing what I need. I did find the Lua script sidebearings.lua
by Philipp Gesang @phi-gamma which is functional, and possibly useful as a component of what I need.
Instead of reading a microtype.cfg
file (standard, or one of my own design), I would like to (a) load the font using fontspec
, (b) Get the side bearings of a few characters of my choice, (c) Create a custom-microtype.cfg
on-the-fly, either in memory or written, (d) Use that configuration when I load microtype. In this regard, I do not load microtype until \AtBeginDocument
.
Why I wish to do this: The output PDF will be robot-inspected for minimum margins. If a letter, such as lowercase j, intrudes into the margin, then the file will be rejected. It does not help to set protrusion=false
, due to font kerning (which is required). If the document always used one or two fonts, I could manually learn what I need. But the document may use any OpenType fonts, including professional (not free) fonts that I cannot access, because there are other users.
I propose to use sidebearings.lua
to discover the numbers, then nullify negative side bearings, using negative protrusion in a custom microtype configuration.
My questions: (1) Is this already available? I searched, but did not find. (2) If not already available, is it worth the effort to do this, or is there some "gotcha" that I do not understand? If it is worth the effort, I will do it myself.
EDIT: Here is a MWE for visualization only. Normally, there would be no black bars, and I would also compensate for the bearings of O.
\documentclass{article} % Compile with lualatex.
\RequirePackage{fontspec}
\setmainfont{Libre Caslon Text} % Already installed.
\begin{document}
% The rules are for visualization.
\noindent\rule{2pt}{10pt}Old Nick was a\\
jolly elf.\par
\noindent\rule{2pt}{10pt}Old Nick was a\\
\strut\hspace{.143em}jolly elf.\par % The .143 is from fontforge.
\end{document}
And here is the result. Note that the first j protrudes to the left of the reference bars. The second j does not. I am trying to do this without such an hspace
trick.
EDIT2: Apparently, my request is possible, without needing to write a *.cfg file. Using the sidebearings method from
Accessing side-bearings in LuaTeX
I can do it while the document compiles. After loading microtype, the \SetProtrusion
command works in Preamble. It can be used to re-set whatever was in the *cfg file.
microtype
. In my opinion this is a bit more flexible: gist.github.com/hmenke/b8a1e7c47b92b8cf8778efa7d3eef315