node-gcm.lua /size: 2738 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['node-gmc'] = {
2    version   = 1.001,
3    comment   = "companion to node-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local type, tostring = type, tostring
10
11local nodes         = nodes
12local nodecodes     = nodes.nodecodes
13local ligature_code = nodes.glyphcodes.ligature
14local nuts          = nodes.nuts
15
16local getnext       = nuts.getnext
17local getsubtype    = nuts.getsubtype
18local getprev       = nuts.getprev
19local setlink       = nuts.setlink
20local nextglyph     = nuts.traversers.glyph
21local copynode      = nuts.copy
22local isglyph       = nuts.isglyph
23
24local report_error  = logs.reporter("node-aux:error")
25
26local getcomponents = node.direct.getcomponents
27local setcomponents = node.direct.setcomponents
28
29local function copynocomponents(g,copyinjection)
30    local components = getcomponents(g)
31    if components then
32        setcomponents(g)
33        local n = copynode(g)
34        if copyinjection then
35            copyinjection(n,g)
36        end
37        setcomponents(g,components)
38        -- maybe also upgrade the subtype but we don't use it anyway
39        return n
40    else
41        local n = copynode(g)
42        if copyinjection then
43            copyinjection(n,g)
44        end
45        return n
46    end
47end
48
49local function copyonlyglyphs(current)
50    local head     = nil
51    local previous = nil
52    for n in nextglyph, current do
53        n = copynode(n)
54        if head then
55            setlink(previous,n)
56        else
57            head = n
58        end
59        previous = n
60    end
61    return head
62end
63
64-- start is a mark and we need to keep that one
65
66local function countcomponents(start,marks)
67    local char = isglyph(start)
68    if char then
69        if getsubtype(start) == ligature_code then
70            local n = 0
71            local components = getcomponents(start)
72            while components do
73                n = n + countcomponents(components,marks)
74                components = getnext(components)
75            end
76            return n
77        elseif not marks[char] then
78            return 1
79        end
80    end
81    return 0
82end
83
84local function flushcomponents()
85    -- this is a no-op in mkiv / generic
86end
87
88nuts.components = {
89    set              = setcomponents,
90    get              = getcomponents,
91    copyonlyglyphs   = copyonlyglyphs,
92    copynocomponents = copynocomponents,
93    count            = countcomponents,
94    flush            = flushcomponents,
95}
96
97nuts.setcomponents = function() report_error("unsupported: %a","setcomponents") end
98nuts.getcomponents = function() report_error("unsupported: %a","getcomponents") end
99