grph-pat.lua /size: 2344 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['grph-pat'] = {
2    version   = 1.001,
3    comment   = "companion to grph-pat.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-- This is just a proof of concept. Viewers behave different (offsets) and Acrobat doesn't
10-- show xform based patterns.
11--
12-- This module will be cleaned up and use codeinjections and such. It should be in the lpdf
13-- namespace.
14
15local texsetbox   = tex.setbox
16local texgetbox   = tex.getbox
17
18local nodepool    = nodes.pool
19local new_literal = nodepool.originliteral -- really ?
20local new_hlist   = nodepool.hlist
21
22local names       = { }
23
24interfaces.implement {
25    name      = "registerpattern",
26    arguments = { {
27        { "name" },
28        { "number", "integer" },
29        { "width", "dimension" },
30        { "height", "dimension" },
31        { "hoffset", "dimension" },
32        { "voffset", "dimension" },
33    } },
34    actions   = function(specification)
35        local number = specification.number
36        local name   = specification.name
37        local box    = texgetbox(number)
38        if not name or name == "" then
39            return
40        end
41        nodes.handlers.finalizebox(number)
42        names[name] = backends.codeinjections.registerpattern {
43            number  = number,
44            width   = specification.width  or  box.width,
45            height  = specification.height or (box.height + box.depth) ,
46            hoffset = specification.hoffset,
47            voffset = specification.voffset,
48        }
49    end
50}
51
52interfaces.implement {
53    name      = "applypattern",
54    arguments = { {
55        { "name" },
56        { "number", "integer" },
57        { "width", "dimension" },
58        { "height", "dimension" },
59    } },
60    actions   = function(specification)
61        local number = specification.number
62        local name   = specification.name
63        local width  = specification.width
64        local height = specification.height
65        if not name or name == "" then
66            return
67        end
68        local p = names[name]
69        if p then
70            local l = new_literal(lpdf.patternstream(p,width,height))
71            local h = new_hlist(l,width,height)
72            texsetbox(number,h)
73        end
74    end
75}
76