summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/font-ocl.lua
blob: af57b786bbf8598a005b4e3a88a0c75da4d7b566 (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
if not modules then modules = { } end modules ['font-ocl'] = {
    version   = 1.001,
    comment   = "companion to font-otf.lua (context)",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- todo : user list of colors

local formatters = string.formatters

local otf = fonts.handlers.otf

local f_color_start = formatters["pdf:direct: %f %f %f rg"]
local s_color_stop  = "pdf:direct:"

local function actualtexthandlers()
    local startactualtext = nil
    local stopactualtext  = nil
    if context then
        local codeinjections = backends.codeinjections
        if codeinjections then
            startactualtext = codeinjections.startunicodetoactualtext
            stopactualtext  = codeinjections.stopunicodetoactualtext
        end
    end
    if not startactualtext then
        -- let's be nice for generic
        local tounicode = fonts.mappings.tounicode16
        startactualtext = function(n)
            return "/Span << /ActualText <feff" .. tounicode(n) .. "> >> BDC"
        end
        stopactualtext = function(n)
            return "EMC"
        end
    end
    return startactualtext, stopactualtext
end

local function initializecolr(tfmdata,kind,value) -- hm, always value
    if value then
        local palettes = tfmdata.resources.colorpalettes
        if palettes then
            --
            local palette = palettes[tonumber(value) or 1] or palettes[1] or { }
            local classes = #palette
            if classes == 0 then
                return
            end
            --
            local characters   = tfmdata.characters
            local descriptions = tfmdata.descriptions
            local properties   = tfmdata.properties
            local colorvalues  = { }
            --
            properties.virtualized = true
            tfmdata.fonts = {
                { id = 0 }
            }
            --
            local startactualtext, stopactualtext = actualtexthandlers()
            --
            for i=1,classes do
                local p = palette[i]
                colorvalues[i] = { "special", f_color_start(p[1]/255,p[2]/255,p[3]/255) }
            end
            --
            local stop = { "special", "pdf:direct:" .. stopactualtext() }
            --
            for unicode, character in next, characters do
                local description = descriptions[unicode]
                if description then
                    local colorlist = description.colors
                    if colorlist then
                        local w = character.width or 0
                        local s = #colorlist
                        local n = 1
                        local t = {
                            { "special", "pdf:direct:" .. startactualtext(unicode) }
                        }
                        for i=1,s do
                            local entry = colorlist[i]
                            n = n + 1 t[n] = colorvalues[entry.class]
                            n = n + 1 t[n] = { "char", entry.slot }
                            if s > 1 and i < s and w ~= 0 then
                                n = n + 1 t[n] = { "right", -w }
                            end
                        end
                        n = n + 1 t[n] = stop
                        character.commands = t
                    end
                end
            end
        end
    end
end

fonts.handlers.otf.features.register {
    name         = "colr",
    description  = "color glyphs",
    manipulators = {
        base = initializecolr,
        node = initializecolr,
    }
}

otf.svgenabled = true -- for now, this might change

local report_svg    = logs.reporter("fonts","svg conversion")

local nofpdfstreams = 0
local f_name        = formatters["svg-glyph-%05i"]
local f_stream      = formatters["memstream:///svg-glyph-%05i"]

-- todo: make a plugin

local function svgtopdf(svgshapes)
    local svgfile = "temp-otf-svg-shape.svg"
    local pdffile = "temp-otf-svg-shape.pdf"
    local command = "inkscape " .. svgfile .. " --export-pdf=" .. pdffile
 -- local command = [[python "c:\Users\Hans Hagen\AppData\Roaming\Python\Scripts\cairosvg" -f pdf ]] .. svgfile .. " -o " .. pdffile
    local testrun = false

    local pdfshapes = { }
    local nofshapes = #svgshapes
    report_svg("processing %i svg containers",nofshapes)
    for i=1,nofshapes do
        local entry = svgshapes[i]
        for j=entry.first,entry.last do
            local svg  = xml.convert(entry.data)
            local data = xml.first(svg,"/svg[@id='glyph"..j.."']")
            io.savedata(svgfile,tostring(data))
            report_svg("processing svg shape of glyph %i in container %i",j,i)
            os.execute(command)
            pdfshapes[j] = io.loaddata(pdffile)
        end
        if testrun and i > testrun then
            report_svg("quiting test run")
            break
        end
    end
    os.remove(svgfile)
    return pdfshapes
end

local function savepdfhandler()
    if context then
        local setmemstream = resolvers.setmemstream
        if setmemstream then
            return function(pdf)
                nofpdfstreams = nofpdfstreams + 1
                setmemstream(f_name(nofpdfstreams),pdf)
                return f_stream(nofpdfstreams)
            end
        end
    end
    return function(pdf)
        nofpdfstreams = nofpdfstreams + 1
        local name = f_name(nofpdfstreams)
        io.savedata(name,pdf)
        return name
    end
end

local function initializesvg(tfmdata,kind,value) -- hm, always value
    if value and otf.svgenabled then
        local characters   = tfmdata.characters
        local descriptions = tfmdata.descriptions
        local properties   = tfmdata.properties
        --
        local svg       = properties.svg
        local hash      = svg and svg.hash
        local timestamp = svg and svg.timestamp
        if not hash then
            return
        end
        --
        local pdffile   = containers.read(otf.pdfcache,hash)
        local pdfshapes = pdffile and pdffile.pdfshapes
        if not pdfshapes or pdffile.timestamp ~= timestamp then
            local svgfile   = containers.read(otf.svgcache,hash)
            local svgshapes = svgfile and svgfile.svgshapes
            pdfshapes = svgshapes and svgtopdf(svgshapes) or { }
            containers.write(otf.pdfcache, hash, {
                pdfshapes = pdfshapes,
                timestamp = timestamp,
            })
        end
        if not pdfshapes or not next(pdfshapes) then
            return
        end
        --
        properties.virtualized = true
        tfmdata.fonts = {
            { id = 0 }
        }
        --
        local startactualtext, stopactualtext = actualtexthandlers()
        local savepdf = savepdfhandler()
        --
        local stop = { "special", "pdf:direct:" .. stopactualtext() }
        --
        for unicode, character in next, characters do
            local index = character.index
            if index then
                local pdf = pdfshapes[index]
                if pdf then
                    local filename = savepdf(pdf)
                    character.commands = {
                        { "special", "pdf:direct:" .. startactualtext(unicode) },
                        { "down", character.depth or 0 },
                        { "image", img.new {
                            filename = filename,
                            width    = character.width,
                            height   = character.height or 0,
                            depth    = character.depth or 0,
                        } },
                        stop
                    }
                    character.svg = true
                end
            end
        end
    end
end

fonts.handlers.otf.features.register {
    name         = "svg",
    description  = "svg glyphs",
    manipulators = {
        base = initializesvg,
        node = initializesvg,
    }
}