| 1 |
-- |
| 2 |
-- This is file `microtype.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- microtype.dtx (with options: `luafile') |
| 8 |
-- |
| 9 |
-- ------------------------------------------------------------------------ |
| 10 |
-- |
| 11 |
-- The `microtype' package |
| 12 |
-- Subliminal refinements towards typographical perfection |
| 13 |
-- Copyright (c) 2004--2026 R Schlicht <[email protected]> |
| 14 |
-- |
| 15 |
-- This work may be distributed and/or modified under the conditions of the |
| 16 |
-- LaTeX Project Public License, either version 1.3c of this license or (at |
| 17 |
-- your option) any later version. The latest version of this license is in: |
| 18 |
-- https://www.latex-project.org/lppl.txt, and version 1.3c or later is part |
| 19 |
-- of all distributions of LaTeX version 2005/12/01 or later. |
| 20 |
-- |
| 21 |
-- This work has the LPPL maintenance status `maintained'. |
| 22 |
-- |
| 23 |
-- This work consists of the files microtype.dtx, microtype-utf.dtx and |
| 24 |
-- microtype.ins and the derived files microtype.sty, microtype-pdftex.def, |
| 25 |
-- microtype-luatex.def, microtype-xetex.def, microtype.lua, letterspace.sty |
| 26 |
-- and microtype-show.sty. |
| 27 |
-- |
| 28 |
-- ------------------------------------------------------------------------ |
| 29 |
-- This file contains auxiliary lua functions. |
| 30 |
-- It was originally contributed by Elie Roux <elie.roux{at}telecom-bretagne.eu>. |
| 31 |
-- (Bugs are mine.) |
| 32 |
-- ------------------------------------------------------------------------ |
| 33 |
-- |
| 34 |
microtype = microtype or {} |
| 35 |
local microtype = microtype |
| 36 |
microtype.module = { |
| 37 |
name = "microtype", |
| 38 |
version = "3.2d", |
| 39 |
date = "2026/03/01", |
| 40 |
description = "microtype module.", |
| 41 |
author = "E. Roux, R. Schlicht and P. Gesang", |
| 42 |
copyright = "E. Roux, R. Schlicht and P. Gesang", |
| 43 |
license = "LPPL", |
| 44 |
} |
| 45 |
luatexbase.provides_module(microtype.module) |
| 46 |
|
| 47 |
function microtype.info(...) |
| 48 |
luatexbase.module_info("microtype",...) |
| 49 |
end |
| 50 |
|
| 51 |
local find = string.find |
| 52 |
local match = string.match |
| 53 |
local gsub = string.gsub |
| 54 |
local tex_write = tex.write |
| 55 |
|
| 56 |
local catpackage |
| 57 |
if luatexbase.registernumber then |
| 58 |
catpackage = luatexbase.registernumber("catcodetable@atletter") -- LaTeX |
| 59 |
else |
| 60 |
catpackage = luatexbase.catcodetables.CatcodeTableLaTeXAtLetter -- luatexbase |
| 61 |
end |
| 62 |
function microtype.sprint (...) |
| 63 |
tex.sprint(catpackage, ...) |
| 64 |
end |
| 65 |
|
| 66 |
if not math.tointeger or not pcall(math.tointeger,0) then |
| 67 |
math.mininteger=-0x4FFFFFFFFFFF |
| 68 |
math.maxinteger=0x4FFFFFFFFFFF |
| 69 |
local floor=math.floor |
| 70 |
function math.tointeger(n) |
| 71 |
local f=floor(n) |
| 72 |
return f==n and f or nil |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
local function if_int(s) |
| 77 |
if find(s,"^-*[0-9]+ *$") then |
| 78 |
tex_write("@firstoftwo") |
| 79 |
else |
| 80 |
tex_write("@secondoftwo") |
| 81 |
end |
| 82 |
end |
| 83 |
microtype.if_int = if_int |
| 84 |
|
| 85 |
local function if_dimen(s) |
| 86 |
if (find(s, "^-*[0-9]+(%a*) *$") or |
| 87 |
find(s, "^-*[0-9]*[.,][0-9]+(%a*) *$")) then |
| 88 |
tex_write("@firstoftwo") |
| 89 |
else |
| 90 |
tex_write("@secondoftwo") |
| 91 |
end |
| 92 |
end |
| 93 |
microtype.if_dimen = if_dimen |
| 94 |
|
| 95 |
local function if_str_eq(s1, s2) |
| 96 |
if s1 == s2 then |
| 97 |
tex_write("@firstoftwo") |
| 98 |
else |
| 99 |
tex_write("@secondoftwo") |
| 100 |
end |
| 101 |
end |
| 102 |
microtype.if_str_eq = if_str_eq |
| 103 |
|
| 104 |
local function if_opentype_font() |
| 105 |
local thefont = font.getfont(font.current()) |
| 106 |
if thefont and ( thefont.format == "opentype" or thefont.format == "truetype" ) |
| 107 |
then tex.write("@firstoftwo") |
| 108 |
else tex.write("@secondoftwo") |
| 109 |
end |
| 110 |
end |
| 111 |
microtype.if_opentype_font = if_opentype_font |
| 112 |
|
| 113 |
local function do_font() |
| 114 |
local thefont = font.getfont(font.current()) |
| 115 |
if thefont then |
| 116 |
for i,v in next,thefont.characters do |
| 117 |
if v.index == nil or ( v.index > 0 and i < 1114112 ) then |
| 118 |
microtype.sprint([[\@tempcnta=]]..i..[[\relax\MT@dofont@function]]) |
| 119 |
end |
| 120 |
end |
| 121 |
end |
| 122 |
end |
| 123 |
microtype.do_font = do_font |
| 124 |
|
| 125 |
local function add_ls(k,feat) |
| 126 |
local f = tex.fontname(font.current()) |
| 127 |
local spec,size = match(f,'^(.+)( at .+)$') |
| 128 |
if not spec then spec = f end |
| 129 |
spec = gsub(spec,"kernfactor=[-]?%d+%.%d+;","") |
| 130 |
local q = match(spec,'^"') or "" |
| 131 |
local a,b,c = match(spec,'^'..q..'([^:]+):?([^:]*):?(.*)'..q..'$') |
| 132 |
local ls = "kernfactor=" .. k/1000 .. ';' |
| 133 |
microtype.sprint(q..a..':') |
| 134 |
if (a == "name" or a == "file") then |
| 135 |
microtype.sprint(b..':'..ls..c..feat..q) |
| 136 |
else |
| 137 |
microtype.sprint(ls..b..feat..q) |
| 138 |
end |
| 139 |
if size then |
| 140 |
microtype.sprint(size) |
| 141 |
end |
| 142 |
end |
| 143 |
microtype.add_ls = add_ls |
| 144 |
|
| 145 |
microtype.ligs = microtype.ligs or { } |
| 146 |
|
| 147 |
local function noligatures(fontcs,liga) |
| 148 |
local fontcs = match(fontcs,"([^ ]+)") |
| 149 |
microtype.ligs[fontcs] = microtype.ligs[fontcs] or { } |
| 150 |
table.insert(microtype.ligs[fontcs],liga) |
| 151 |
end |
| 152 |
microtype.noligatures = noligatures |
| 153 |
|
| 154 |
local function keepligature(c) |
| 155 |
local nodedirect = node.direct |
| 156 |
local getfield = nodedirect.getfield |
| 157 |
local getfont = nodedirect.getfont |
| 158 |
local f,ch |
| 159 |
if type(c) == "userdata" then -- in older luaotfload versions, c was a node |
| 160 |
f = c.font |
| 161 |
ch = c.components.char |
| 162 |
else -- since 2.6, c is a (direct node) number |
| 163 |
f = getfont(c) |
| 164 |
ch = getfield(getfield(c,"components"),"char") |
| 165 |
end |
| 166 |
-- if ch then -- should always be true |
| 167 |
local ligs = microtype.ligs[match(tex.fontidentifier(f),"\\([^ ]+)")] |
| 168 |
if ligs then |
| 169 |
for _,lig in pairs(ligs) do |
| 170 |
if lig == "_all_" or tonumber(lig) == ch then |
| 171 |
return false |
| 172 |
end |
| 173 |
end |
| 174 |
end |
| 175 |
return true |
| 176 |
-- end |
| 177 |
end |
| 178 |
|
| 179 |
if luaotfload and luaotfload.letterspace then |
| 180 |
if luaotfload.letterspace.keepligature then |
| 181 |
microtype.info("overwriting function `keepligature'") |
| 182 |
end |
| 183 |
luaotfload.letterspace.keepligature = keepligature |
| 184 |
end |
| 185 |
|
| 186 |
if luaotfload and luaotfload.aux and luaotfload.aux.slot_of_name then |
| 187 |
local slot_of_name = luaotfload.aux.slot_of_name |
| 188 |
microtype.name_to_slot = function(name, unsafe) |
| 189 |
local n = slot_of_name(font.current(), name, unsafe) |
| 190 |
if not n then return -1 end |
| 191 |
if n > 1114111 then return -1 end |
| 192 |
return math.tointeger(n) |
| 193 |
end |
| 194 |
else |
| 195 |
-- we dig into internal structure (should be avoided) |
| 196 |
local function name_to_slot(name, unsafe) |
| 197 |
if fonts then |
| 198 |
local unicodes |
| 199 |
if fonts.ids then -- legacy luaotfload |
| 200 |
local tfmdata = fonts.ids[font.current()] |
| 201 |
if not tfmdata then return end |
| 202 |
unicodes = tfmdata.shared.otfdata.luatex.unicodes |
| 203 |
else -- new location |
| 204 |
local tfmdata = fonts.hashes.identifiers[font.current()] |
| 205 |
if not tfmdata then return end |
| 206 |
unicodes = tfmdata.resources.unicodes |
| 207 |
end |
| 208 |
local unicode = unicodes[name] |
| 209 |
if unicode then -- does the 'or' branch actually exist? |
| 210 |
return type(unicode) == "number" and unicode or unicode[1] |
| 211 |
end |
| 212 |
end |
| 213 |
end |
| 214 |
microtype.name_to_slot = name_to_slot |
| 215 |
end |
| 216 |
|
| 217 |
-- |
| 218 |
-- |
| 219 |
-- End of file `microtype.lua'. |