node-typ.lua /size: 4284 b    last modification: 2023-12-21 09:44
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
40-- todo: glyphscale etc
41
42local function tonodes(str,fontid,spacing,templateglyph,attrid) -- quick and dirty
43    local head, prev = nil, nil
44    if not fontid then
45        fontid = templateglyph and getfont(templateglyph) or currentfont()
46    end
47    if attrid == true then
48        if templateglyph then
49            attrid = false -- we copy with the glyph
50        else
51            attrid = currentattributes()
52        end
53    end
54    local fp = fontparameters[fontid]
55    local s, p, m
56    if spacing then
57        s, p, m = spacing, 0, 0
58    else
59        s, p, m = fp.space, fp.space_stretch, fp.space_shrink
60    end
61    local spacedone = false
62    for c in utfvalues(str) do
63        local next
64        if c == 32 then
65            if not spacedone then
66                next = new_glue(s,p,m)
67                spacedone = true
68            end
69        elseif templateglyph then
70            next = copy_glyph(templateglyph)
71            setchar(next,c)
72            spacedone = false
73        else
74            next = new_glyph(fontid or 1,c)
75            spacedone = false
76        end
77        if not next then
78            -- nothing
79        elseif not head then
80            if attrid then
81                setattrlist(next,attrid)
82            end
83            head = next
84        else
85            if attrid then
86                setattrlist(next,attrid)
87            end
88            setlink(prev,next)
89        end
90        prev = next
91    end
92    return head
93end
94
95local function tohpack(str,fontid,spacing)
96    return hpack_node_list(tonodes(str,fontid,spacing),"exactly")
97end
98
99local function tohbox(str,fontid,spacing)
100    return full_hpack_list(tonodes(str,fontid,spacing),"exactly")
101end
102
103local function tovpack(str,fontid,spacing)
104    -- vpack is just a hack, and a proper implementation is on the agenda
105    -- as it needs more info etc than currently available
106    return vpack_node_list(tonodes(str,fontid,spacing))
107end
108
109local tovbox = tovpack -- for now no vpack filter
110
111local tnuts       = { }
112nuts.typesetters  = tnuts
113
114tnuts.tonodes     = tonodes
115tnuts.tohpack     = tohpack
116tnuts.tohbox      = tohbox
117tnuts.tovpack     = tovpack
118tnuts.tovbox      = tovbox
119
120typesetters.tonodes  = function(...) local h, b = tonodes(...) return tonode(h), b end
121typesetters.tohpack  = function(...) local h, b = tohpack(...) return tonode(h), b end
122typesetters.tohbox   = function(...) local h, b = tohbox (...) return tonode(h), b end
123typesetters.tovpack  = function(...) local h, b = tovpack(...) return tonode(h), b end
124typesetters.tovbox   = function(...) local h, b = tovbox (...) return tonode(h), b end
125
126typesetters.hpack    = typesetters.tohpack  -- obsolete
127typesetters.hbox     = typesetters.tohbox   -- obsolete
128typesetters.vpack    = typesetters.tovpack  -- obsolete
129
130-- context(nodes.typesetters.tohpack("Hello World!"))
131-- context(nodes.typesetters.tohbox ("Hello World!"))
132-- context(nodes.typesetters.tohpack("Hello World!",1,100*1024*10))
133-- context(nodes.typesetters.tohbox ("Hello World!",1,100*1024*10))
134
135string.tonodes = function(...) return tonode(tonodes(...)) end  -- quite convenient
136