summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/oberdiek/oberdiek.luacolor-pre065.lua
blob: 06c2db5170c6d21b03c02774b5b0585faf691c90 (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
-- 
--  This is file `oberdiek.luacolor-pre065.lua',
--  generated with the docstrip utility.
-- 
--  The original source files were:
-- 
--  luacolor.dtx  (with options: `lua,pre065')
--  
--  This is a generated file.
--  
--  Project: luacolor
--  Version: 2011/03/29 v1.4
--  
--  Copyright (C) 2007, 2009-2011 by
--     Heiko Oberdiek <heiko.oberdiek at googlemail.com>
--  
--  This work may be distributed and/or modified under the
--  conditions of the LaTeX Project Public License, either
--  version 1.3c of this license or (at your option) any later
--  version. This version of this license is in
--     http://www.latex-project.org/lppl/lppl-1-3c.txt
--  and the latest version of this license is in
--     http://www.latex-project.org/lppl.txt
--  and version 1.3 or later is part of all distributions of
--  LaTeX version 2005/12/01 or later.
--  
--  This work has the LPPL maintenance status "maintained".
--  
--  This Current Maintainer of this work is Heiko Oberdiek.
--  
--  This work consists of the main source file luacolor.dtx
--  and the derived files
--     luacolor.sty, luacolor.pdf, luacolor.ins, luacolor.drv,
--     luacolor-test1.tex, luacolor-test2.tex, luacolor-test3.tex,
--     oberdiek.luacolor.lua, luacolor.lua,
--     oberdiek.luacolor-pre065.lua, luacolor-pre065.lua.
--  
module("oberdiek.luacolor", package.seeall)
local ifpdf
if tonumber(tex.pdfoutput) > 0 then
  ifpdf = true
else
  ifpdf = false
end
local prefix
local prefixes = {
  dvips   = "color ",
  dvipdfm = "pdf:sc ",
  truetex = "textcolor:",
  pctexps = "ps::",
}
local patterns = {
  ["^color "]            = "dvips",
  ["^pdf: *begincolor "] = "dvipdfm",
  ["^pdf: *bcolor "]     = "dvipdfm",
  ["^pdf: *bc "]         = "dvipdfm",
  ["^pdf: *setcolor "]   = "dvipdfm",
  ["^pdf: *scolor "]     = "dvipdfm",
  ["^pdf: *sc "]         = "dvipdfm",
  ["^textcolor:"]        = "truetex",
  ["^ps::"]              = "pctexps",
}
local function info(msg, term)
  local target = "log"
  if term then
    target = "term and log"
  end
  texio.write_nl(target, "Package luacolor info: " .. msg .. ".")
  texio.write_nl(target, "")
end
function dvidetect()
  local v = tex.box[0]
  assert(v.id == node.id("hlist"))
  for v in node.traverse_id(node.id("whatsit"), v.list) do
    if v and v.subtype == 3 then -- special
      local data = v.data
      for pattern, driver in pairs(patterns) do
        if string.find(data, pattern) then
          prefix = prefixes[driver]
          tex.write(driver)
          return
        end
      end
      info("\\special{" .. data .. "}", true)
      return
    end
  end
  info("Missing \\special", true)
end
local map = {
  n = 0,
}
function get(color)
  local n = map[color]
  if not n then
    n = map.n + 1
    map.n = n
    map[n] = color
    map[color] = n
  end
  tex.write("" .. n)
end
local attribute
function setattribute(attr)
  attribute = attr
end
function process(box)
  local color = ""
  local list = tex.getbox(box)
  traverse(list, color)
end
local LIST = 1
local COLOR = 2
local type = {
  [node.id("hlist")] = LIST,
  [node.id("vlist")] = LIST,
  [node.id("rule")]  = COLOR,
  [node.id("glyph")] = COLOR,
  [node.id("disc")]  = COLOR,
}
local subtype = {
  [3] = COLOR, -- special
  [8] = COLOR, -- pdf_literal
}
local mode = 2 -- luatex.pdfliteral.direct
local WHATSIT = node.id("whatsit")
local SPECIAL = 3
local PDFLITERAL = 8
function traverse(list, color)
  if not list then
    return color
  end
  if type[list.id] ~= LIST then
    texio.write_nl("!!! Error: Wrong list type: " .. node.type(list.id))
    return color
  end
  local head = list.list
  for n in node.traverse(head) do
    local type = type[n.id]
    if type == LIST then
      color = traverse(n, color)
    elseif type == COLOR
           or (type == WHATSIT
               and subtype[n.subtype]) then
      local v = node.has_attribute(n, attribute)
      if v then
        local newColor = map[v]
        if newColor ~= color then
          color = newColor
          local newNode
          if ifpdf then
            newNode = node.new(WHATSIT, PDFLITERAL)
            newNode.mode = mode
            newNode.data = color
          else
            newNode = node.new(WHATSIT, SPECIAL)
            newNode.data = prefix .. color
          end
          if head == n then
            newNode.next = head
            local old_prev = head.prev
            head.prev = newNode
            head = newNode
            head.prev = old_prev
          else
            head = node.insert_before(head, n, newNode)
          end
        end
      end
    end
  end
  list.list = head
  return color
end
-- 
--  End of File `oberdiek.luacolor-pre065.lua'.