4

I want to make use of the pairwise kerning of this answer: https://tex.stackexchange.com/a/370469/75284 using fonts.handlers.otf.addfeature to adjust the kerning of character pairs with the hyphen -. With the hyphen entered directly this works fine, but I'd also like to use the babel shorthands "= and "~ and have them adjust their kerning as well. How could this be done?

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}

\directlua{fonts.handlers.otf.addfeature
{
    name = "ktest",
    type = "kern",
    data = {["-"] = { ["V"] =  -300 }}}
}

\setmainfont[RawFeature=+ktest]{Times New Roman}

\begin{document}\obeylines
Test-Versuch
Test"=Versuch
Test"~Versuch
\end{document}

screenshot of MWE

Any other solution for kerning pairs with lualatex and the babel shorthands would also be welcome.

4
  • 1
    That is not possible. The shorthand "= will expand to roughly \penalty10000-\hskip0pt. The \hskip is an explicit glue and any kerning accross it is discarded. Commented Apr 19, 2018 at 11:49
  • 1
    The "~ shorthand is approximately \textormath{\leavevmode\hbox{-}}{-}, i.e. \hbox{-} in text mode. There is no kerning between boxes and glyphs for the simple reason that TeX cannot look inside boxes (unless you unhbox it but that would negate the effect "~ is trying to achieve). Commented Apr 19, 2018 at 11:52
  • 1
    @Henri Menke: Thanks. Could "= and "~ be possibly redefined to allow kerning?
    – lblb
    Commented Apr 19, 2018 at 12:19
  • 1
    I can't think of a way to do this without breaking the functionality that "= and "~ are trying to achieve. I'll have a look how ConTeXt does it. Commented Apr 19, 2018 at 21:09

1 Answer 1

3

I have managed to propagate the regular kerning between the hyphen and V across the discretionary. Unfortunately, I don't know how to access the user-defined kerns :( That's why this is not really an answer.

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{fontspec}

\directlua{
local function is_hyphen(item)
   return item.id == node.id("glyph") and item.char == 45
end
%
local function insert_breakpoint(head)
   for item in node.traverse(head) do
      if is_hyphen(item) then
         % Get current char and font
         local char = item.char
         local font = item.font
         % Get next char and font
         local next = node.next(item)
         local nextchar = next.char
         local nextfont = next.font
         % Determine required skip size but only if fonts are equal
         local hskip = node.new("glue",0) % subtype 0 = "userskip"
         if font == nextfont then
            local data = fonts.hashes.identifiers[font]
            local kern = fonts.handlers.otf.getkern(data,char,nextchar)
            hskip.width = kern
         end
         node.insert_after(head, item, node.copy(hskip))
      end
   end
end
%
luatexbase.add_to_callback("hyphenate",
                           function(head)
                              insert_breakpoint(head)
                              lang.hyphenate(head)
                           end,
                           "insert_breakpoints")
}

\setmainfont{TeX Gyre Termes}

\setlength\parindent{0pt} % Just for demonstration!!!

\begin{document}
Test"=Versuch

% Propagate kerning across the hyphen
Test-Versuch

% Inserted negative hskip facilitates line-breaking of next word
{\hsize=10pt Test-Versuch\par}
\end{document}

The kern is barely visible because it is only -52429sp (-0.8pt) wide.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .