summaryrefslogtreecommitdiff
path: root/otfl-font-msc.lua
blob: bb049272d065c08f50a3b9f0a129d2045cf8d70f (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
if not modules then modules = { } end modules ['font-msc'] = {
    version   = 1.001,
    comment   = "companion to font-otf.lua (miscellaneous)",
    author    = "Khaled Hosny",
    copyright = "Khaled Hosny",
    license   = "GPL"
}

--[[
 Support for font slanting and extending.
--]]

fonts.triggers            = fonts.triggers            or { }
fonts.initializers        = fonts.initializers        or { }
fonts.initializers.common = fonts.initializers.common or { }

local initializers, format = fonts.initializers, string.format

table.insert(fonts.triggers,"slant")

function fonts.initializers.common.slant(tfmdata,value)
    value = tonumber(value)
    if not value then
        value =  0
    elseif value >  1 then
        value =  1
    elseif value < -1 then
        value = -1
    end
    tfmdata.slant_factor = value
end

initializers.base.otf.slant = initializers.common.slant
initializers.node.otf.slant = initializers.common.slant

table.insert(fonts.triggers,"extend")

function initializers.common.extend(tfmdata,value)
    value = tonumber(value)
    if not value then
        value =  0
    elseif value >  10 then
        value =  10
    elseif value < -10 then
        value = -10
    end
    tfmdata.extend_factor = value
end

initializers.base.otf.extend = initializers.common.extend
initializers.node.otf.extend = initializers.common.extend

--[[
  Support for font color.
--]]

table.insert(fonts.triggers,"color")

function initializers.common.color(tfmdata,value)
    if value then
        tfmdata.color = value
        luaotfload.add_color_callback()
    end
end

initializers.base.otf.color = initializers.common.color
initializers.node.otf.color = initializers.common.color

local function hex2dec(hex,one)
    if one then
        return format("%.1g", tonumber(hex, 16)/255)
    else
        return format("%.3g", tonumber(hex, 16)/255)
    end
end

local res

local function pageresources(a)
    local res2
    if not res then
       res = "/TransGs1<</ca 1/CA 1>>"
    end
    res2 = format("/TransGs%s<</ca %s/CA %s>>", a, a, a)
    res  = format("%s%s", res, res:find(res2) and "" or res2)
end

local function hex_to_rgba(hex)
    local r, g, b, a, push, pop, res3
    if hex then
        if #hex == 6 then
            _, _, r, g, b    = hex:find('(..)(..)(..)')
        elseif #hex == 8 then
            _, _, r, g, b, a = hex:find('(..)(..)(..)(..)')
            a                = hex2dec(a,true)
            pageresources(a)
        end
    else
        return nil
    end
    r = hex2dec(r)
    g = hex2dec(g)
    b = hex2dec(b)
    if a then
        push = format('/TransGs%g gs %s %s %s rg', a, r, g, b)
        pop  = '0 g /TransGs1 gs'
    else
        push = format('%s %s %s rg', r, g, b)
        pop  = '0 g'
    end
    return push, pop
end

local glyph   = node.id('glyph')
local hlist   = node.id('hlist')
local vlist   = node.id('vlist')
local whatsit = node.id('whatsit')
local pgi     = node.id('page_insert')
local sbml    = node.id('sub_mlist')

function luaotfload.node_colorize(head)
    for n in node.traverse(head) do
       if n.id == hlist or n.id == vlist then
           n.list = luaotfload.node_colorize(n.list)
       end
       if n.id == glyph then
           local tfmdata = fonts.ids[n.font]
           if tfmdata and tfmdata.color then
               local prevg, nextg = n.prev, n.next
               local found = nil
               while prevg and not found do
                   if prevg.id == glyph then
                       found = 1
                   elseif prevg.id == hlist or prevg.id == vlist or prevg.id == whatsit 
                           or prevg.id == pgi or prevg.id == sbml then
                       prevg = nil
                   else
                       prevg = prevg.prev
                   end
               end
               found = nil
               while nextg and not found do
                   if nextg.id == glyph then
                       found = 1
                   elseif nextg.id == hlist or nextg.id == vlist or nextg.id == whatsit
                           or nextg.id == pgi or nextg.id == sbml then
                       nextg = nil
                   else
                       nextg = nextg.next
                   end
               end
               if prevg and fonts.ids[prevg.font].color == tfmdata.color then
               else
                   local pushcolor = hex_to_rgba(tfmdata.color)
                   local push = node.new(whatsit, 8)
                   push.mode  = 1
                   push.data  = pushcolor
                   head       = node.insert_before(head, n, push)
               end
               if nextg and fonts.ids[nextg.font].color == tfmdata.color then
               else
                   local _, popcolor = hex_to_rgba(tfmdata.color)
                   local pop  = node.new(whatsit, 8)
                   pop.mode   = 1
                   pop.data   = popcolor
                   head       = node.insert_after(head, n, pop)
               end
           end
       end
   end
   return head
end

function luaotfload.colorize(head)
   local h = luaotfload.node_colorize(head)
   if res then
      local r = "/ExtGState<<"..res..">>"
      local s = tex.pdfpageresources:find(r) and "" or r
      tex.pdfpageresources = tex.pdfpageresources..s
   end
   return h
end

luaotfload.color_callback_activated = 0

function luaotfload.add_color_callback()
    if luaotfload.color_callback_activated == 0 then
        callback.add("pre_output_filter",    luaotfload.colorize, "loaotfload.colorize")
        luaotfload.color_callback_activated = 1
    end
end

function luaotfload.remove_color_callback()
    if luaotfload.color_callback_activated == 1 then
        callback.remove("pre_output_filter",    "loaotfload.colorize")
        luaotfload.color_callback_activated = 0
    end
end