Skip to content

Instantly share code, notes, and snippets.

@hmenke
Last active February 2, 2023 12:20
Show Gist options
  • Save hmenke/bfca289b498a8375ffa74434120977f8 to your computer and use it in GitHub Desktop.
Save hmenke/bfca289b498a8375ffa74434120977f8 to your computer and use it in GitHub Desktop.
pdfadjustinterwordglue in LuaTeX
\ifdefined\directlua
\csname newcount\endcsname\pdfadjustinterwordglue
\begingroup
\catcode`\^^M=12
\directlua{
local subtypes = node.subtypes("glue")
local knbscode = {}
local fontid = token.create("fontid")
function set_knbscode(field)
token.put_next(fontid)
local fid = token.scan_int()
local chr = token.scan_int()
local str = token.scan_string()
knbscode[fid] = knbscode[fid] or {}
knbscode[fid][chr] = knbscode[fid][chr] or {}
local int = string.gsub(str,"=","")
knbscode[fid][chr][field] = tonumber(int)
end
function get_knbscode(field)
token.put_next(fontid)
local fid = token.scan_int()
local chr = token.scan_int()
knbscode[fid] = knbscode[fid] or {}
knbscode[fid][chr] = knbscode[fid][chr] or {}
tex.sprint([[\numexpr]] .. (knbscode[fid][chr][field] or 0) .. [[\relax]])
end
local microtype_spacing = function(head, tail)
head, tail, success = node.kerning(head, tail)
if tex.count.pdfadjustinterwordglue > 0 then
for space in node.traverse_id(node.id("glue"), head) do
if subtypes[space.subtype] == "spaceskip" then
local prev = node.prev(space)
if prev.id == node.id("glyph") then
local knbs = knbscode[prev.font]
if knbs and knbs[prev.char] then
local f = font.getfont(prev.font)
local em = f.parameters.quad
local width = knbs[prev.char].width or 0
local stretch = knbs[prev.char].stretch or 0
local shrink = knbs[prev.char].shrink or 0
local glue = node.new(node.id("glue"))
glue.width = width*em/1000
glue.stretch = stretch*em/1000
glue.shrink = shrink*em/1000
head = node.insert_before(head, space, glue)
end
end
end
end
end
end
callback.register("kerning", microtype_spacing, "microtype_spacing")
}%
\endgroup
\protected\def\knbscode{\directlua{set_knbscode("width")}}
\protected\def\stbscode{\directlua{set_knbscode("stretch")}}
\protected\def\shbscode{\directlua{set_knbscode("shrink")}}
\def\theknbscode{\the\directlua{get_knbscode("width")}}
\def\thestbscode{\the\directlua{get_knbscode("stretch")}}
\def\theshbscode{\the\directlua{get_knbscode("shrink")}}
\else
\def\theknbscode{\the\knbscode}
\def\thestbscode{\the\stbscode}
\def\theshbscode{\the\shbscode}
\fi
\pdfadjustinterwordglue=1
\knbscode\font`r=1100
\stbscode\font`r=10
\shbscode\font`r=10
\knbscode\font`r 1100
\stbscode\font`r 10
\shbscode\font`r 10
\theknbscode\font`r
\thestbscode\font`r
\theshbscode\font`r
\knbscode\font`m=\theknbscode\font`r\relax
\the\numexpr 1+2\relax
\input lorem
\bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment