1if not modules then modules = { } end modules [ ' back-ini ' ] = {
2 version = 1 . 001 ,
3 comment = " companion to back-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 next , type = next , type
10local format = string . format
11
12backends = backends or { }
13local backends = backends
14
15local context = context
16
17local trace = false trackers . register ( " backend " , function ( v ) trace = v end )
18local report = logs . reporter ( " backend " )
19
20local allocate = utilities . storage . allocate
21local setmetatableindex = table . setmetatableindex
22local setaction = nodes . tasks . setaction
23
24local implement = interfaces . implement
25local variables = interfaces . variables
26
27local texset = tex . set
28
29local nodeinjections = { }
30local codeinjections = { }
31local registrations = { }
32local tables = allocate ( )
33
34local function nothing ( )
35 return nil
36end
37
38backends . nothing = nothing
39
40local function donothing ( t , k )
41 t [ k ] = nothing
42 return nothing
43end
44
45setmetatableindex ( nodeinjections , donothing )
46setmetatableindex ( codeinjections , donothing )
47setmetatableindex ( registrations , donothing )
48
49local defaults = {
50 nodeinjections = nodeinjections ,
51 codeinjections = codeinjections ,
52 registrations = registrations ,
53 tables = tables ,
54}
55
56backends . defaults = defaults
57
58backends . nodeinjections = { } setmetatableindex ( backends . nodeinjections , nodeinjections )
59backends . codeinjections = { } setmetatableindex ( backends . codeinjections , codeinjections )
60backends . registrations = { } setmetatableindex ( backends . registrations , registrations )
61backends . tables = { } setmetatableindex ( backends . tables , tables )
62
63backends . current = " unknown "
64
65local lmtx_mode = nil
66
67local function lmtxmode ( )
68 if lmtx_mode = = nil then
69 lmtx_mode = CONTEXTLMTXMODE > 0 and drivers and drivers . lmtxversion
70 end
71 return lmtx_mode
72end
73
74codeinjections . lmtxmode = lmtxmode
75
76function backends . install ( what )
77 if type ( what ) = = " string " then
78 local backend = backends [ what ]
79 if backend then
80 if trace then
81 if backend . comment then
82 report ( " initializing backend %a, %a " , what , backend . comment )
83 else
84 report ( " initializing backend %a " , what )
85 end
86 end
87 backends . current = what
88 for category , default in next , defaults do
89 local target = backends [ category ]
90 local plugin = backend [ category ]
91 setmetatableindex ( plugin , default )
92 setmetatableindex ( target , plugin )
93 end
94 elseif trace then
95 report ( " no backend named %a " , what )
96 end
97 end
98end
99
100statistics . register ( " used backend " , function ( )
101 local bc = backends . current
102 if bc ~ = " unknown " then
103 local lmtx = lmtxmode ( )
104 local cmnt = backends [ bc ] . comment or " no comment "
105 if lmtx then
106 return format ( " lmtx version %0.2f, %s (%s) " , lmtx , bc , cmnt )
107 else
108 return format ( " %s (%s) " , bc , cmnt )
109 end
110 else
111 return nil
112 end
113end )
114
115local comment = { " comment " , " " }
116
117tables . vfspecials = allocate {
118 red = comment ,
119 green = comment ,
120 blue = comment ,
121 black = comment ,
122 startslant = comment ,
123 stopslant = comment ,
124}
125
126
127
128interfaces . implement {
129 name = " setrealspaces " ,
130 arguments = " string " ,
131 actions = function ( v )
132 setaction ( " shipouts " , " nodes.handlers.accessibility " , v = = variables . yes )
133 end
134}
135
136
137
138local included = table . setmetatableindex ( {
139 context = true ,
140 id = true ,
141 metadata = true ,
142 date = true ,
143 id = true ,
144 pdf = true ,
145} , function ( t , k )
146 return true
147end )
148
149backends . included = included
150
151function backends . timestamp ( )
152 return os . date ( " %Y-%m-%dT%X " ) . . os . timezone ( true )
153end
154
155
156
157local paper_width = 0
158local paper_height = 0
159
160function codeinjections . setpagedimensions ( paperwidth , paperheight )
161 if paperwidth then
162 paper_width = paperwidth
163 end
164 if paperheight then
165 paper_height = paperheight
166 end
167 if not lmtxmode ( ) then
168 texset ( " global " , " pageheight " , paper_height )
169 texset ( " global " , " pagewidth " , paper_width )
170 end
171 return paper_width , paper_height
172end
173
174function codeinjections . getpagedimensions ( )
175 return paper_width , paper_height
176end
177
178implement {
179 name = " shipoutoffset " ,
180 actions = function ( )
181 context ( lmtxmode ( ) and " 0pt " or " -1in " )
182 end
183}
184
185
186local page_x_origin = 0
187local page_y_origin = 0
188
189function codeinjections . setpageorigin ( x , y )
190 page_x_origin = x
191 page_y_origin = y
192end
193
194function codeinjections . getpageorigin ( )
195 local x = page_x_origin
196 local y = page_y_origin
197 page_x_origin = 0
198 page_y_origin = 0
199 return x , y , ( x ~ = 0 or y ~ = 0 )
200end
201
202implement {
203 name = " setpageorigin " ,
204 arguments = { " dimension " , " dimension " } ,
205 actions = codeinjections . setpageorigin ,
206}
207
208
209
210function backends . noflatelua ( )
211 return status . late_callbacks or 0
212end
213 |