| 1 |
-- Lua code for the selnolig package. |
| 2 |
-- To be loaded with an instruction such as |
| 3 |
-- \directlua{ require("selnolig.lua") } |
| 4 |
-- from a (Lua)LaTeX .sty file. |
| 5 |
-- |
| 6 |
-- Author: Mico Loretan (loretan dot mico at gmail dot com) |
| 7 |
-- (with crucial contributions from Taco Hoekwater, |
| 8 |
-- Patrick Gundlach, and Steffen Hildebrandt) |
| 9 |
-- |
| 10 |
-- The entire selnolig package is placed under the terms |
| 11 |
-- of the LaTeX Project Public License, version 1.3 or |
| 12 |
-- later. (http://www.latex-project.org/lppl.txt). |
| 13 |
-- It has the status "maintained". |
| 14 |
|
| 15 |
selnolig = { } |
| 16 |
selnolig.module = { |
| 17 |
name = "selnolig", |
| 18 |
version = "0.256", |
| 19 |
date = "2015/10/26", |
| 20 |
description = "Selective suppression of typographic ligatures", |
| 21 |
author = "Mico Loretan", |
| 22 |
copyright = "Mico Loretan", |
| 23 |
license = "LPPL 1.3 or later" |
| 24 |
} |
| 25 |
|
| 26 |
debug=false -- default: don't output detailed information |
| 27 |
|
| 28 |
-- Define variables corresponding to various text nodes; |
| 29 |
-- cf. sections 8.1.2 and 8.1.4 of LuaTeX reference guide |
| 30 |
local rule = node.id('rule') |
| 31 |
local glue = node.id("glue") -- |
| 32 |
local kern = node.id('kern') |
| 33 |
local glyph = node.id('glyph') -- |
| 34 |
local whatsit = node.id("whatsit") -- |
| 35 |
|
| 36 |
local userdefined |
| 37 |
|
| 38 |
for n,v in pairs ( node.whatsits() ) do |
| 39 |
if v == 'user_defined' then userdefined = n end |
| 40 |
end |
| 41 |
|
| 42 |
local identifier = 123456 -- any unique identifier |
| 43 |
local noliga={} |
| 44 |
local keepliga={} -- String -> Boolean |
| 45 |
|
| 46 |
function debug_info(s) |
| 47 |
if debug then |
| 48 |
texio.write_nl(s) |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
local blocknode = node.new(whatsit, userdefined) |
| 53 |
blocknode.type = 100 |
| 54 |
blocknode.user_id = identifier |
| 55 |
|
| 56 |
local suppression_on = true -- if false, process_ligatures won't do anything |
| 57 |
|
| 58 |
local prefix_length = function(word, byte) |
| 59 |
return unicode.utf8.len( string.sub(word,0,byte) ) |
| 60 |
end |
| 61 |
|
| 62 |
-- Problem: string.find and unicode.utf8.find return |
| 63 |
-- the byte-position at which the pattern is found |
| 64 |
-- instead of the character-position. Fix this by |
| 65 |
-- providing a dedicated string search function. |
| 66 |
|
| 67 |
local unicode_find = function(s, pattern, position) |
| 68 |
-- Start by correcting the incoming position |
| 69 |
if position ~= nil then |
| 70 |
-- debug_info("Position: "..position) |
| 71 |
sub = string.sub(s, 1, position) |
| 72 |
position=position+string.len(sub) - unicode.utf8.len(sub) |
| 73 |
-- debug_info("Corrected position: "..position) |
| 74 |
end |
| 75 |
-- Now execute find and fix it accordingly |
| 76 |
byte_pos = unicode.utf8.find(s, pattern, position) |
| 77 |
if byte_pos ~= nil then |
| 78 |
-- "convert" byte_pos to "unicode_pos" |
| 79 |
return unicode.utf8.len( string.sub(s, 1, byte_pos) ) |
| 80 |
else |
| 81 |
return nil |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
function process_ligatures(nodes,tail) |
| 86 |
if not suppression_on then |
| 87 |
return -- suppression disabled |
| 88 |
end |
| 89 |
|
| 90 |
local s={} |
| 91 |
local current_node=nodes |
| 92 |
local build_liga_table = function(strlen,t) |
| 93 |
local p={} |
| 94 |
for i = 1, strlen do |
| 95 |
p[i]=0 |
| 96 |
end |
| 97 |
for k,v in pairs(t) do |
| 98 |
-- debug_info("Match: "..v[3]) |
| 99 |
local c= unicode_find(noliga[v[3]],"|") |
| 100 |
local correction=1 |
| 101 |
while c~=nil do |
| 102 |
--debug_info("Position "..(v[1]+c)) |
| 103 |
p[v[1]+c-correction] = 1 |
| 104 |
c = unicode_find(noliga[v[3]],"|",c+1) |
| 105 |
correction = correction+1 |
| 106 |
end |
| 107 |
end |
| 108 |
--debug_info("Liga table: "..table.concat(p, "")) |
| 109 |
return p |
| 110 |
end |
| 111 |
local apply_ligatures=function(head,ligatures) |
| 112 |
local i=1 |
| 113 |
local hh=head |
| 114 |
local last=node.tail(head) |
| 115 |
for curr in node.traverse_id(glyph,head) do |
| 116 |
if ligatures[i]==1 then |
| 117 |
debug_info("Inserting nolig whatsit before glyph: " ..unicode.utf8.char(curr.char)) |
| 118 |
node.insert_before(hh,curr, node.copy(blocknode)) |
| 119 |
hh=curr |
| 120 |
end |
| 121 |
last=curr |
| 122 |
if i==#ligatures then |
| 123 |
-- debug_info("Leave node list on position: "..i) |
| 124 |
break |
| 125 |
end |
| 126 |
i=i+1 |
| 127 |
end |
| 128 |
if(last~=nil) then |
| 129 |
debug_info("Last char: "..unicode.utf8.char(last.char)) |
| 130 |
end |
| 131 |
end |
| 132 |
for t in node.traverse(nodes) do |
| 133 |
if t.id==glyph then |
| 134 |
s[#s+1]=unicode.utf8.char(t.char) |
| 135 |
end |
| 136 |
if ( t.id==glue or t.next==nil or t.id==kern or t.id==rule ) then |
| 137 |
local f=string.gsub(table.concat(s,""),"[\\?!,\\.]+","") |
| 138 |
local throwliga={} |
| 139 |
for k,v in pairs (noliga) do |
| 140 |
local count=1 |
| 141 |
local match = string.find(f,k) |
| 142 |
while match do |
| 143 |
count = match |
| 144 |
keep = false |
| 145 |
debug_k1 = "" |
| 146 |
for k1,v1 in pairs (keepliga) do |
| 147 |
if v1 and string.find(f,k1) and string.find(k1,k) then |
| 148 |
debug_k1=k1 |
| 149 |
keep=true |
| 150 |
break |
| 151 |
end |
| 152 |
end |
| 153 |
if not keep then |
| 154 |
debug_info("pattern match: "..f .." - "..k) |
| 155 |
local n = match + string.len(k) - 1 |
| 156 |
table.insert(throwliga,{prefix_length(f,match),n,k}) |
| 157 |
else |
| 158 |
debug_info("pattern match nolig and keeplig: "..f .." - "..k.." - "..debug_k1) |
| 159 |
end |
| 160 |
match= string.find(f,k,count+1) |
| 161 |
end |
| 162 |
end |
| 163 |
if #throwliga==0 then |
| 164 |
-- debug_info("No ligature suppression for: "..f) |
| 165 |
else |
| 166 |
debug_info("Do ligature suppression for: "..f) |
| 167 |
local ligabreaks = build_liga_table(f:len(),throwliga) |
| 168 |
apply_ligatures(current_node,ligabreaks) |
| 169 |
end |
| 170 |
s = {} |
| 171 |
current_node = t |
| 172 |
end |
| 173 |
end |
| 174 |
end -- end of function process_ligatures(nodes,tail) |
| 175 |
|
| 176 |
function suppress_liga(s,t) |
| 177 |
noliga[s] = t |
| 178 |
end |
| 179 |
|
| 180 |
function always_keep_liga(s) |
| 181 |
keepliga[s] = true |
| 182 |
end |
| 183 |
|
| 184 |
function enable_suppression(val) |
| 185 |
suppression_on = val |
| 186 |
if val then |
| 187 |
debug_info("Turning ligature suppression back on") |
| 188 |
else |
| 189 |
debug_info("Turning ligature suppression off") |
| 190 |
end |
| 191 |
end |
| 192 |
|
| 193 |
function enableselnolig() |
| 194 |
luatexbase.add_to_callback( "ligaturing", |
| 195 |
process_ligatures, "Suppress ligatures selectively", 1 ) |
| 196 |
end |
| 197 |
|
| 198 |
function disableselnolig() |
| 199 |
luatexbase.remove_from_callback( "ligaturing", |
| 200 |
"Suppress ligatures selectively" ) |
| 201 |
end |