summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/pdfmanagement-testphase/luaotfload-l3color-tmp.lua
blob: 1d4eab4de1ad86b16d545001f2ad96ed79293c59 (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
if not luaotfload.set_transparent_colorstack then return end
local l = lpeg
local spaces = l.P' '^0
local digit16 = l.R('09', 'af', 'AF')

local octet = digit16 * digit16 / function(s) return string.format('%.3g ', tonumber(s, 16) / 255) end
local htmlcolor = l.Cs(octet * octet * octet * -1 * l.Cc'rg')
local color_export = {
  token.create'endlocalcontrol',
  token.create'tex_hpack:D',
  token.new(0, 1),
  token.create'color_export:nnN',
  token.new(0, 1),
  '',
  token.new(0, 2),
  token.new(0, 1),
  'backend',
  token.new(0, 2),
  token.create'l_tmpa_tl',
  token.create'exp_after:wN',
  token.create'__color_select:nn',
  token.create'l_tmpa_tl',
  token.new(0, 2),
}
local group_end = token.create'group_end:'
local value = (1 - l.P'}')^0
luatexbase.add_to_callback('luaotfload.parse_color', function (value)
  local html = htmlcolor:match(value)
  if html then return html end

  tex.runtoks(function()
    token.get_next()
    color_export[6] = value
    tex.sprint(-2, color_export)
  end)
  local list = token.scan_list()
  if not list.head or list.head.next or list.head.subtype ~= node.subtype'pdf_colorstack' then
    error'Unexpected backend behavior'
  end
  local cmd = list.head.data
  node.free(list)
  return cmd
end, 'l3color')

-- Let's also integrate l3opacity

luaotfload.set_transparent_colorstack(token.create'c__opacity_backend_stack_int'.index)

local transparent_register = {
  token.create'pdfmanagement_add:nnn',
  token.new(0, 1),
    'Page/Resources/ExtGState',
  token.new(0, 2),
  token.new(0, 1),
    '',
  token.new(0, 2),
  token.new(0, 1),
    '<</ca ',
    '',
    '/CA ',
    '',
    '>>',
  token.new(0, 2),
}
luatexbase.add_to_callback('luaotfload.parse_transparent', function(value)
  value = (octet * -1):match(value)
  if not value then
    tex.error'Invalid transparency value'
    return
  end
  value = value:sub(1, -2)
  local result = 'opacity' .. value
  tex.runtoks(function()
    transparent_register[6], transparent_register[10], transparent_register[12] = result, value, value
    tex.sprint(-2, transparent_register)
  end)
  return '/' .. result .. ' gs'
end, 'l3opacity')