node-typ.lua /size: 4259 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['node-typ'] = {
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
9-- code has been moved to blob-ini.lua
10
11local typesetters       = nodes.typesetters or { }
12nodes.typesetters       = typesetters
13
14local nuts              = nodes.nuts
15local tonode            = nuts.tonode
16local tonut             = nuts.tonut
17
18local setlink           = nuts.setlink
19local setchar           = nuts.setchar
20local setattrlist       = nuts.setattrlist
21
22local getfont           = nuts.getfont
23
24local hpack_node_list   = nuts.hpack
25local vpack_node_list   = nuts.vpack
26local full_hpack_list   = nuts.fullhpack
27
28local nodepool          = nuts.pool
29local new_glyph         = nodepool.glyph
30local new_glue          = nodepool.glue
31
32local utfvalues         = utf.values
33
34local currentfont       = font.current
35local currentattributes = nodes.currentattributes
36local fontparameters    = fonts.hashes.parameters
37
38-- when attrid == true then take from glyph or current else use the given value
39
40local function tonodes(str,fontid,spacing,templateglyph,attrid) -- quick and dirty
41    local head, prev = nil, nil
42    if not fontid then
43        fontid = templateglyph and getfont(templateglyph) or currentfont()
44    end
45    if attrid == true then
46        if templateglyph then
47            attrid = false -- we copy with the glyph
48        else
49            attrid = currentattributes()
50        end
51    end
52    local fp = fontparameters[fontid]
53    local s, p, m
54    if spacing then
55        s, p, m = spacing, 0, 0
56    else
57        s, p, m = fp.space, fp.space_stretch, fp.space_shrink
58    end
59    local spacedone = false
60    for c in utfvalues(str) do
61        local next
62        if c == 32 then
63            if not spacedone then
64                next = new_glue(s,p,m)
65                spacedone = true
66            end
67        elseif templateglyph then
68            next = copy_glyph(templateglyph)
69            setchar(next,c)
70            spacedone = false
71        else
72            next = new_glyph(fontid or 1,c)
73            spacedone = false
74        end
75        if not next then
76            -- nothing
77        elseif not head then
78            if attrid then
79                setattrlist(next,attrid)
80            end
81            head = next
82        else
83            if attrid then
84                setattrlist(next,attrid)
85            end
86            setlink(prev,next)
87        end
88        prev = next
89    end
90    return head
91end
92
93local function tohpack(str,fontid,spacing)
94    return hpack_node_list(tonodes(str,fontid,spacing),"exactly")
95end
96
97local function tohbox(str,fontid,spacing)
98    return full_hpack_list(tonodes(str,fontid,spacing),"exactly")
99end
100
101local function tovpack(str,fontid,spacing)
102    -- vpack is just a hack, and a proper implementation is on the agenda
103    -- as it needs more info etc than currently available
104    return vpack_node_list(tonodes(str,fontid,spacing))
105end
106
107local tovbox = tovpack -- for now no vpack filter
108
109local tnuts       = { }
110nuts.typesetters  = tnuts
111
112tnuts.tonodes     = tonodes
113tnuts.tohpack     = tohpack
114tnuts.tohbox      = tohbox
115tnuts.tovpack     = tovpack
116tnuts.tovbox      = tovbox
117
118typesetters.tonodes  = function(...) local h, b = tonodes(...) return tonode(h), b end
119typesetters.tohpack  = function(...) local h, b = tohpack(...) return tonode(h), b end
120typesetters.tohbox   = function(...) local h, b = tohbox (...) return tonode(h), b end
121typesetters.tovpack  = function(...) local h, b = tovpack(...) return tonode(h), b end
122typesetters.tovbox   = function(...) local h, b = tovbox (...) return tonode(h), b end
123
124typesetters.hpack    = typesetters.tohpack  -- obsolete
125typesetters.hbox     = typesetters.tohbox   -- obsolete
126typesetters.vpack    = typesetters.tovpack  -- obsolete
127
128-- context(nodes.typesetters.tohpack("Hello World!"))
129-- context(nodes.typesetters.tohbox ("Hello World!"))
130-- context(nodes.typesetters.tohpack("Hello World!",1,100*1024*10))
131-- context(nodes.typesetters.tohbox ("Hello World!",1,100*1024*10))
132
133string.tonodes = function(...) return tonode(tonodes(...)) end  -- quite convenient
134