summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/lualatex/simurgh/simurgh-unibidi.lua
blob: 4b62c06561bc00194fec6b7b919eb6bece64aded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
local err, warn, info = luatexbase.provides_module {
  name        = "simurgh-unibidi",
    version   = 0.01,
    comment   = "unicode bidi",
    author    = "Vafa Khalighi",
    copyright = "Vafa Khalighi",
    license   = "see simurgh package documentation",
    derived   = "derived from import-typo-dir by Philipp Gesang"
}
 
info "unicode bidi"






require "lualibs"  -- we need the extended set

local texsprint     = tex.sprint
local stringformat  = string.format
local tableconcat   = table.concat
local copynode      = node.copy

-----------------------------------------------------------------------
---                             prepare
-----------------------------------------------------------------------

--- 1) wrap Luatexbase attribute handler in a Context style interface

attributes            = attributes or { }
local texsetattribute = tex.setattribute


local hidden = {
  state       = luatexbase.new_attribute ("typo-dir:state",      true),
  directions  = luatexbase.new_attribute ("typo-dir:directions", true),
  mathbidi    = luatexbase.new_attribute ("typo-dir:mathbidi",   true),
}

local a_directions = hidden.directions

attributes.private = attributes.private or function (attr_name)
  local res = hidden [attr_name]
  if not res then
    res = luatexbase.new_attribute (attr_name)
  end
  return res
end

local unsetvalue      = luatexbase.get_unset_value ()
attributes.unsetvalue = unsetvalue

--- 2)  simulate the multilingual interface; safe to purge afterwards
---     since Context uses local copies

interfaces                      = interfaces or { }
interfaces.variables            = interfaces.variables or { }
interfaces.variables.global     = "global"
interfaces.variables["local"]   = "local"
interfaces.variables.default    = "default"
interfaces.variables.on         = "on"
interfaces.variables.yes        = "yes"

--- 3) node tasks; we don’t have real node processors so we will need
---    to set up a makeshift interface

nodes.tasks               = nodes.tasks or { }
nodes.tasks.enableaction  = function () end

--- 4) commands namespace

commands = commands or { } -- already present due to luaotfload-extrablibs

--- 5) typesetters namespace
---

-- require "typo-krn"

typesetters = typesetters or { }

--- 6)  the Context namespace; cannot be cleaned up without breaking
---     things; srsly there needs to be a non-Context equivalent for
---     this

context = context or { }

setmetatable (context, {

  --- this is quite primitive and considerably less functional than
  --- the real deal as defined in cldf-ini.lua, but we already get
  --- quite far with this reduced version

  __index = function (t, k)

    if k == "sprint" then
      return function (...) texsprint (...) end

    elseif type (k) == "string" then
      local command = [[\]] .. k
      return function (...)
        local res = { command }
        for i = 1, select ("#", ...) do
          --- just simple grouped arguments
          res [#res + 1] = "{"
          res [#res + 1] = select (i, ...)
          res [#res + 1] = "}"
        end
        texsprint (tableconcat (res))
      end

    else
      return context
    end
  end,

  __call = function (t, fmt, first, ...)

    if t == nil or fmt == nil then
      return
    end

    local tf = type (fmt)

    if tf == "string" then
      if first then
        texsprint (-1, stringformat (fmt, first, ...))
      else
        texsprint (-1, fmt)
      end
    elseif tf == "function" then
      texsprint (-1, fmt (first, ...))
    elseif first then --- and so on,
      texsprint (-1, tostring (fmt), first, ...)
    else
      texsprint (-1, tostring (fmt))
    end
  end,
})

--- 7)  catcodes namespace

catcodes                      = catcodes or { }
catcodes.numbers              = catcodes.numbers or { }
catcodes.numbers.ctxcatcodes  = -1

--- 8) whatsit prototype; expected to be present in the nodepool

local n_textdir     = node.new (node.id "whatsit", nodes.whatsitcodes.dir)
nodes.pool          = nodes.pool or { }

nodes.pool.textdir  = function (dir)
  local n = copynode (n_textdir)
  n.dir = dir
  return n
end

--- 9) node identifiers

nodes.nodecodes = table.mirrored (nodes.nodecodes) --> convenience

----------------------------------------------------------------------
---                             import
-----------------------------------------------------------------------

if not characters.directions then
  require "simurgh-char-def" --> characters.data (unicode)
  require "simurgh-char-ini" --> characters.*
end
--- the next three used to share “typo-dir.lua” before it was split
require "simurgh-unibidi-ini" --> typesetters.directions
require "simurgh-unibidi-core" --> typesetters.directions
require "simurgh-unibidi-math" --> typesetters.directions

-----------------------------------------------------------------------
---                            wrappers
-----------------------------------------------------------------------

--- we use the *packagedata* namespace which should become canonical
--- anyways; also we keep a copy of typesetters.directions in a
--- subtable

packagedata                     = packagedata or { }
local directions                = typesetters.directions
local typo_dir                  = { directions = directions }
packagedata.typo_dir            = typo_dir
local directionprocessor        = directions.process
local mathdirectionprocessor    = directions.processmath
local processorid               = "typesetters.directions"

--- emulate node tasks capability; we override the original definitions
--- of set() and setmath() with surrogates that work with Luatexbase
--- callback handlers

directions.set                  = nil
directions.setmath              = nil


--- we need to track which callbacks the node processor is hooked
--- into since we lack the combined version Context has

local registered_as = { } --- procname -> callbacks


--- (node_t -> node_t) -> string -> string list -> bool

local add_processor = function (processor, name, ...)

  for i=1, select ("#", ...) do
    local callback = select (i, ...)
    --- *IMPORTANT* the processor must be inserted at the top,
    --- i.e. with a priority higher than any other callback!
    luatexbase.add_to_callback (callback, processor, name, 1)
  end

  registered_as [name] = { ... }
  return true

end


--- string -> bool

local remove_processor = function (name)

  local callbacks = registered_as [name]

  if callbacks then
    for i=1, #callbacks do
      luatexbase.remove_from_callback (callbacks [i], name)
    end
    return true
  end

  return false
end

--- we use the same callbacks as a node processor in Context
--- unit -> bool

local enabledirectionprocessor = function (math)

  local processor

  if math == true then
    processor = function (hd)
      --- different signature from the normal one
      return mathdirectionprocessor (hd)
    end
  else
    processor = function (hd)
      return directionprocessor ("directions", a_directions, hd)
    end
  end

  return add_processor (processor,
                        processorid,
                        "pre_linebreak_filter",
                        "hpack_filter")
end

typo_dir.enable = enabledirectionprocessor


--- unit -> bool
local disabledirectionprocessor = function ( )
  return remove_processor (processorid)
end

typo_dir.disable = disabledirectionprocessor


local active = false --- activation state of direction processor

typo_dir.set = function (n)

  if not n or n == 0 then
    n = unsetvalue
  end

  if not active then
    info ("Installing simurgh direction handler (%d).", n)
    enabledirectionprocessor ()
    active = true
  end

  texsetattribute (a_directions, n)
end


local active = false --- activation state of math direction processor

typo_dir.setmath = function (n)

  if not active and n and n > 0 then
    info ("Installing simurgh math direction handler (%d).", n)
    enabledirectionprocessor (true)
    active = true
  end
end

typo_dir.getbidimode = directions.getbidimode

typo_dir.getbidimode = function (specification)
  context (directions.tomode (specification))
end

-----------------------------------------------------------------------
---                              clean
-----------------------------------------------------------------------

attributes.private    = nil
attributes            = nil
interfaces.variables  = nil
interfaces            = nil
nodes.tasks           = nil

collectgarbage "collect"