1. Philipp Gesang
  2. lua[la]tex tests

Commits

Philipp Gesang  committed b9811d3

add example wrapper for keeptogether()/keepligature() in typo-krn.lua

  • Participants
  • Parent commits 2af2a9c
  • Branches default

Comments (0)

Files changed (3)

File ltx-fontspec-feature-2.tex

View file
 
 \newfontfamily{\smBlackSC}[%Small | Black | Smallcap
    Letters={UppercaseSmallCaps, SmallCaps},
-   LetterSpace=9 .0,
+   LetterSpace=0.9,
    Scale=.9,
    Numbers={OldStyle,Proportional}
 ] {Liberation Serif}

File pln-letterspace-5.lua

View file
+--- Simple wrapper around typo-krn.lua
+
+packagedata                   = packagedata or { }
+packagedata.characterkerning  = packagedata.characterkerning or { }
+local characterkerning        = packagedata.characterkerning
+
+local lpeg                    = require "lpeg"
+local C, Ct, P                = lpeg.C, lpeg.Ct, lpeg.P
+local lpegmatch               = lpeg.match
+
+local kerns                   = typesetters.kerns
+local nodecodes               = nodes.nodecodes
+local glyph_t                 = nodecodes.glyph
+
+local defaultwidth            = 0.125
+local keepligature            = false --- defaults in Context
+local keeptogether            = false
+
+local splitlist
+do
+  local separator   = P","
+  local noseparator = 1 - separator
+  local escaped     = P"\\" * P(1)
+  local element     = C((escaped + noseparator)^1)
+  splitlist   = Ct((element + separator)^1)
+end
+
+--- register a list of character pairs that should not be kerned
+
+local suppresskerns = function (list)
+  if list == "" then
+    keeptogether = false
+  end
+
+  list = lpegmatch (splitlist, list)
+  if not list then -- garbage
+    keeptogether = false
+    return
+  end
+
+  --- kludge, should be optimized with an 2d array lookup, using
+  --- integers instead of strings as keys
+  local pairs = table.tohash (list)
+
+  keeptogether = function (node1, node2)
+    if node1.id == glyph_t and node2.id == glyph_t then
+      local pair = utf.char (node1.char) .. utf.char (node2.char)
+      return pairs[pair] == true
+    end
+  end
+end
+
+characterkerning.suppresskerns = suppresskerns
+
+--- register a list of ligatures
+
+local ignoreligatures = function (list)
+  if list == "" then
+    keepligatures = false
+  end
+
+  list = lpegmatch (splitlist, list)
+  if not list then -- garbage
+    keepligatures = false
+    return
+  end
+
+  local keep = table.tohash (list)
+
+  keepligature = function (liganode)
+    --- first check if the glyph of the ligature is registered
+    if keep[utf.char (liganode.char)] == true then
+      return true
+    end
+    --- else, test if the components are registered
+    local ligachars = { }
+    for n in node.traverse_id (glyph_t, liganode.components) do
+      ligachars[#ligachars+1] = utf.char (n.char)
+    end
+
+    return keep[table.concat (ligachars)] == true
+  end
+end
+
+characterkerning.ignoreligatures = ignoreligatures
+
+--- activate letterspacing
+
+local enable = function (factor)
+  kerns.keepligature = keepligature
+  kerns.keeptogether = keeptogether
+  factor = tonumber (factor) or defaultwidth
+  kerns.set (factor)
+end
+
+characterkerning.enable = enable
+

File pln-letterspace-5.tex

View file
+\input luaotfload.sty
+\directlua{dofile "pln-letterspace-5.lua"}
+
+\def\kerncmd#1{\directlua{packagedata.characterkerning.#1}}
+
+%% #1 -> kernfactor
+\def\setcharacterkerning#1{\kerncmd{enable[[#1]]}}
+
+%% #1 -> comma separated list of pairs
+\def\suppresscharacterkerning#1{\kerncmd{suppresskerns[[#1]]}}
+
+%% #1 -> comma separated list of ligatures,
+%%       either the unicode ligature character or its components
+\def\suppressligaturekerning#1{\kerncmd{ignoreligatures[[#1]]}}
+
+\font\iwona="file:Iwona-Regular.otf" at 42pt
+
+%% example: we’d like “cd” and “ij” to remain unaffected
+\suppresscharacterkerning{cd,ij} %% list of pairs to ignore
+
+%% example: we’d like the “fl” and “ff” ligatures to be
+%% unanalyzed
+\suppressligaturekerning{fl,ff}   %% list of ligatures to ignore
+
+{\iwona
+ before\par
+ {\setcharacterkerning{0.5}% very noticable
+  abcdefghijk\par
+  ff fi ffi fl}\par
+ after}\par
+
+\bye
+