| 1 |
if not modules then modules = { } end modules ['luatex-fonts-nod'] = { |
| 2 |
version = 1.001, |
| 3 |
comment = "companion to luatex-fonts.lua", |
| 4 |
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5 |
copyright = "PRAGMA ADE / ConTeXt Development Team", |
| 6 |
license = "see context related readme files" |
| 7 |
} |
| 8 |
|
| 9 |
if context then |
| 10 |
os.exit() |
| 11 |
end |
| 12 |
|
| 13 |
-- Don't depend on code here as it is only needed to complement the font handler |
| 14 |
-- code. I will move some to another namespace as I don't see other macro packages |
| 15 |
-- use the context logic. It's a subset anyway. More will be stripped. |
| 16 |
|
| 17 |
-- Attributes: |
| 18 |
|
| 19 |
if tex.attribute[0] ~= 0 then |
| 20 |
|
| 21 |
texio.write_nl("log","!") |
| 22 |
texio.write_nl("log","! Attribute 0 is reserved for ConTeXt's font feature management and has to be") |
| 23 |
texio.write_nl("log","! set to zero. Also, some attributes in the range 1-255 are used for special") |
| 24 |
texio.write_nl("log","! purposes so setting them at the TeX end might break the font handler.") |
| 25 |
texio.write_nl("log","!") |
| 26 |
|
| 27 |
tex.attribute[0] = 0 -- else no features |
| 28 |
|
| 29 |
end |
| 30 |
|
| 31 |
attributes = attributes or { } |
| 32 |
attributes.unsetvalue = -0x7FFFFFFF |
| 33 |
|
| 34 |
local numbers, last = { }, 127 |
| 35 |
|
| 36 |
attributes.private = attributes.private or function(name) |
| 37 |
local number = numbers[name] |
| 38 |
if not number then |
| 39 |
if last < 255 then |
| 40 |
last = last + 1 |
| 41 |
end |
| 42 |
number = last |
| 43 |
numbers[name] = number |
| 44 |
end |
| 45 |
return number |
| 46 |
end |
| 47 |
|
| 48 |
-- Nodes (a subset of context so that we don't get too much unused code): |
| 49 |
|
| 50 |
nodes = { } |
| 51 |
nodes.handlers = { } |
| 52 |
|
| 53 |
local nodecodes = { } |
| 54 |
local glyphcodes = node.subtypes("glyph") |
| 55 |
local disccodes = node.subtypes("disc") |
| 56 |
|
| 57 |
for k, v in next, node.types() do |
| 58 |
v = string.gsub(v,"_","") |
| 59 |
nodecodes[k] = v |
| 60 |
nodecodes[v] = k |
| 61 |
end |
| 62 |
for k, v in next, glyphcodes do |
| 63 |
glyphcodes[v] = k |
| 64 |
end |
| 65 |
for k, v in next, disccodes do |
| 66 |
disccodes[v] = k |
| 67 |
end |
| 68 |
|
| 69 |
nodes.nodecodes = nodecodes |
| 70 |
nodes.glyphcodes = glyphcodes |
| 71 |
nodes.disccodes = disccodes |
| 72 |
nodes.dirvalues = { lefttoright = 0, righttoleft = 1 } |
| 73 |
|
| 74 |
nodes.handlers.protectglyphs = node.protectglyphs or node.protect_glyphs -- beware: nodes! |
| 75 |
nodes.handlers.unprotectglyphs = node.unprotectglyphs or node.unprotect_glyphs -- beware: nodes! |
| 76 |
|
| 77 |
-- in generic code, at least for some time, we stay nodes, while in context |
| 78 |
-- we can go nuts (e.g. experimental); this split permits us us keep code |
| 79 |
-- used elsewhere stable but at the same time play around in context |
| 80 |
|
| 81 |
-- much of this will go away .. it's part of the context interface and not |
| 82 |
-- officially in luatex-*.lua |
| 83 |
|
| 84 |
local direct = node.direct |
| 85 |
local nuts = { } |
| 86 |
nodes.nuts = nuts |
| 87 |
|
| 88 |
local tonode = direct.tonode |
| 89 |
local tonut = direct.todirect |
| 90 |
|
| 91 |
nodes.tonode = tonode |
| 92 |
nodes.tonut = tonut |
| 93 |
|
| 94 |
nuts.tonode = tonode |
| 95 |
nuts.tonut = tonut |
| 96 |
|
| 97 |
nuts.getattr = direct.get_attribute |
| 98 |
nuts.getboth = direct.getboth |
| 99 |
nuts.getchar = direct.getchar |
| 100 |
nuts.getdirection = direct.getdirection |
| 101 |
nuts.getdisc = direct.getdisc |
| 102 |
nuts.getreplace = direct.getreplace |
| 103 |
nuts.getfield = direct.getfield |
| 104 |
nuts.getfont = direct.getfont |
| 105 |
nuts.getid = direct.getid |
| 106 |
nuts.getkern = direct.getkern |
| 107 |
nuts.getlist = direct.getlist |
| 108 |
nuts.getnext = direct.getnext |
| 109 |
nuts.getoffsets = direct.getoffsets |
| 110 |
nuts.getoptions = direct.getoptions or function() return 0 end |
| 111 |
nuts.getprev = direct.getprev |
| 112 |
nuts.getsubtype = direct.getsubtype |
| 113 |
nuts.getwidth = direct.getwidth |
| 114 |
nuts.setattr = direct.setfield |
| 115 |
nuts.setboth = direct.setboth |
| 116 |
nuts.setchar = direct.setchar |
| 117 |
nuts.setcomponents = direct.setcomponents |
| 118 |
nuts.setdirection = direct.setdirection |
| 119 |
nuts.setdisc = direct.setdisc |
| 120 |
nuts.setreplace = direct.setreplace |
| 121 |
nuts.setfield = direct.setfield |
| 122 |
nuts.setkern = direct.setkern |
| 123 |
nuts.setlink = direct.setlink |
| 124 |
nuts.setlist = direct.setlist |
| 125 |
nuts.setnext = direct.setnext |
| 126 |
nuts.setoffsets = direct.setoffsets |
| 127 |
nuts.setprev = direct.setprev |
| 128 |
nuts.setsplit = direct.setsplit |
| 129 |
nuts.setsubtype = direct.setsubtype |
| 130 |
nuts.setwidth = direct.setwidth |
| 131 |
|
| 132 |
nuts.getglyphdata = nuts.getattribute or nuts.getattr |
| 133 |
nuts.setglyphdata = nuts.setattribute or nuts.setattr |
| 134 |
|
| 135 |
nuts.ischar = direct.ischar or direct.is_char |
| 136 |
nuts.isglyph = direct.isglyph or direct.is_glyph |
| 137 |
nuts.copy = direct.copy |
| 138 |
nuts.copynode = direct.copy |
| 139 |
nuts.copylist = direct.copylist or direct.copy_list |
| 140 |
nuts.endofmath = direct.endofmath or direct.end_of_math |
| 141 |
nuts.flush = direct.flush |
| 142 |
nuts.flushlist = direct.flushlist or direct.flush_list |
| 143 |
nuts.flushnode = direct.flushnode or direct.flush_node |
| 144 |
nuts.free = direct.free |
| 145 |
nuts.insertafter = direct.insertafter or direct.insert_after |
| 146 |
nuts.insertbefore = direct.insertbefore or direct.insert_before |
| 147 |
nuts.isnode = direct.isnode or direct.is_node |
| 148 |
nuts.isdirect = direct.isdirect or direct.is_direct |
| 149 |
nuts.isnut = direct.isdirect or direct.is_direct |
| 150 |
nuts.kerning = direct.kerning |
| 151 |
nuts.ligaturing = direct.ligaturing |
| 152 |
nuts.new = direct.new |
| 153 |
nuts.remove = direct.remove |
| 154 |
nuts.tail = direct.tail |
| 155 |
nuts.traverse = direct.traverse |
| 156 |
nuts.traversechar = direct.traversechar or direct.traverse_char |
| 157 |
nuts.traverseglyph = direct.traverseglyph or direct.traverse_glyph |
| 158 |
nuts.traverseid = direct.traverseid or direct.traverse_id |
| 159 |
|
| 160 |
-- properties as used in the (new) injector: |
| 161 |
|
| 162 |
local propertydata = (direct.getpropertiestable or direct.get_properties_table)() |
| 163 |
nodes.properties = { data = propertydata } |
| 164 |
|
| 165 |
if direct.set_properties_mode then |
| 166 |
direct.set_properties_mode(true,true) |
| 167 |
function direct.set_properties_mode() end |
| 168 |
end |
| 169 |
|
| 170 |
nuts.getprop = function(n,k) |
| 171 |
local p = propertydata[n] |
| 172 |
if p then |
| 173 |
return p[k] |
| 174 |
end |
| 175 |
end |
| 176 |
|
| 177 |
nuts.setprop = function(n,k,v) |
| 178 |
if v then |
| 179 |
local p = propertydata[n] |
| 180 |
if p then |
| 181 |
p[k] = v |
| 182 |
else |
| 183 |
propertydata[n] = { [k] = v } |
| 184 |
end |
| 185 |
end |
| 186 |
end |
| 187 |
|
| 188 |
nodes.setprop = nodes.setproperty |
| 189 |
nodes.getprop = nodes.getproperty |
| 190 |
|
| 191 |
-- a few helpers (we need to keep 'm in sync with context) .. some day components |
| 192 |
-- might go so here we isolate them |
| 193 |
|
| 194 |
local setprev = nuts.setprev |
| 195 |
local setnext = nuts.setnext |
| 196 |
local getnext = nuts.getnext |
| 197 |
local setlink = nuts.setlink |
| 198 |
local getfield = nuts.getfield |
| 199 |
local setfield = nuts.setfield |
| 200 |
local getsubtype = nuts.getsubtype |
| 201 |
local isglyph = nuts.isglyph |
| 202 |
local find_tail = nuts.tail |
| 203 |
local flushlist = nuts.flushlist |
| 204 |
local flushnode = nuts.flushnode |
| 205 |
local traverseid = nuts.traverseid |
| 206 |
local copynode = nuts.copynode |
| 207 |
|
| 208 |
local glyph_code = nodes.nodecodes.glyph |
| 209 |
local ligature_code = nodes.glyphcodes.ligature |
| 210 |
|
| 211 |
do -- this is consistent with the rest of context, not that we need it |
| 212 |
|
| 213 |
local p = nodecodes.localpar or nodecodes.local_par |
| 214 |
|
| 215 |
if p then |
| 216 |
nodecodes.par = p |
| 217 |
nodecodes[p] = "par" |
| 218 |
nodecodes.localpar = p -- for old times sake |
| 219 |
nodecodes.local_par = p -- for old times sake |
| 220 |
end |
| 221 |
|
| 222 |
end |
| 223 |
|
| 224 |
do |
| 225 |
|
| 226 |
local getcomponents = node.direct.getcomponents |
| 227 |
local setcomponents = node.direct.setcomponents |
| 228 |
|
| 229 |
local function copynocomponents(g,copyinjection) |
| 230 |
local components = getcomponents(g) |
| 231 |
if components then |
| 232 |
setcomponents(g) |
| 233 |
local n = copynode(g) |
| 234 |
if copyinjection then |
| 235 |
copyinjection(n,g) |
| 236 |
end |
| 237 |
setcomponents(g,components) |
| 238 |
-- maybe also upgrade the subtype but we don't use it anyway |
| 239 |
return n |
| 240 |
else |
| 241 |
local n = copynode(g) |
| 242 |
if copyinjection then |
| 243 |
copyinjection(n,g) |
| 244 |
end |
| 245 |
return n |
| 246 |
end |
| 247 |
end |
| 248 |
|
| 249 |
local function copyonlyglyphs(current) |
| 250 |
local head = nil |
| 251 |
local previous = nil |
| 252 |
for n in traverseid(glyph_code,current) do |
| 253 |
n = copynode(n) |
| 254 |
if head then |
| 255 |
setlink(previous,n) |
| 256 |
else |
| 257 |
head = n |
| 258 |
end |
| 259 |
previous = n |
| 260 |
end |
| 261 |
return head |
| 262 |
end |
| 263 |
|
| 264 |
local function countcomponents(start,marks) |
| 265 |
local char = isglyph(start) |
| 266 |
if char then |
| 267 |
if getsubtype(start) == ligature_code then |
| 268 |
local n = 0 |
| 269 |
local components = getcomponents(start) |
| 270 |
while components do |
| 271 |
n = n + countcomponents(components,marks) |
| 272 |
components = getnext(components) |
| 273 |
end |
| 274 |
return n |
| 275 |
elseif not marks[char] then |
| 276 |
return 1 |
| 277 |
end |
| 278 |
end |
| 279 |
return 0 |
| 280 |
end |
| 281 |
|
| 282 |
local function flushcomponents() |
| 283 |
-- this is a no-op in mkiv / generic |
| 284 |
end |
| 285 |
|
| 286 |
nuts.components = { |
| 287 |
set = setcomponents, |
| 288 |
get = getcomponents, |
| 289 |
copyonlyglyphs = copyonlyglyphs, |
| 290 |
copynocomponents = copynocomponents, |
| 291 |
count = countcomponents, |
| 292 |
flush = flushcomponents, |
| 293 |
} |
| 294 |
|
| 295 |
end |
| 296 |
|
| 297 |
nuts.usesfont = direct.usesfont or direct.uses_font |
| 298 |
|
| 299 |
do |
| 300 |
|
| 301 |
-- another poor mans substitute ... i will move these to a more protected |
| 302 |
-- namespace .. experimental hack |
| 303 |
|
| 304 |
local dummy = tonut(node.new("glyph")) |
| 305 |
|
| 306 |
nuts.traversers = { |
| 307 |
glyph = nuts.traverseid(nodecodes.glyph,dummy), |
| 308 |
glue = nuts.traverseid(nodecodes.glue,dummy), |
| 309 |
disc = nuts.traverseid(nodecodes.disc,dummy), |
| 310 |
boundary = nuts.traverseid(nodecodes.boundary,dummy), |
| 311 |
|
| 312 |
char = nuts.traversechar(dummy), |
| 313 |
|
| 314 |
node = nuts.traverse(dummy), |
| 315 |
} |
| 316 |
|
| 317 |
end |
| 318 |
|
| 319 |
if not nuts.setreplace then |
| 320 |
|
| 321 |
local getdisc = nuts.getdisc |
| 322 |
local setfield = nuts.setfield |
| 323 |
|
| 324 |
function nuts.getreplace(n) |
| 325 |
local _, _, h, _, _, t = getdisc(n,true) |
| 326 |
return h, t |
| 327 |
end |
| 328 |
|
| 329 |
function nuts.setreplace(n,h) |
| 330 |
setfield(n,"replace",h) |
| 331 |
end |
| 332 |
|
| 333 |
end |
| 334 |
|
| 335 |
do |
| 336 |
|
| 337 |
local getsubtype = nuts.getsubtype |
| 338 |
|
| 339 |
function nuts.startofpar(n) |
| 340 |
local s = getsubtype(n) |
| 341 |
return s == 0 or s == 2 -- sorry, hardcoded, won't change anyway |
| 342 |
end |
| 343 |
|
| 344 |
end |