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

-- maybe: backends.codeinjections.insertswf

local sub, format, match, byte = string.sub, string.format, string.match, string.byte
local concat = table.concat
local floor = math.floor
local tonumber = tonumber

local readstring     = io.readstring
local readnumber     = io.readnumber
local tobitstring    = number.tobitstring
local todimen        = number.todimen
local nodeinjections = backends.nodeinjections
local figures        = figures
local context        = context

local function getheader(name)
    local f = io.open(name,"rb")
    if not f then
        return
    end
    local signature  = readstring(f,3) -- F=uncompressed, C=compressed (zlib)
    local version    = readnumber(f,1)
    local filelength = readnumber(f,-4)
    local compressed = sub(signature,1,1) == "C"
    local buffer
    if compressed then
        buffer = zlib.decompress(f:read('*a'))
    else
        buffer = f:read(20) -- ('*a')
    end
    f:close()
    buffer = { match(buffer,"(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)") }
    for i=1,9 do
        buffer[i] = tobitstring(byte(buffer[i]))
    end
    local framebits = concat(buffer,"",1,9)
    local n = tonumber(sub(framebits,1,5),2)
    local frame = { } -- xmin xmax ymin ymax
    local xmin = tonumber(sub(framebits,6,      5 +   n),2)
    local xmax = tonumber(sub(framebits,6 + 1*n,5 + 2*n),2)
    local ymin = tonumber(sub(framebits,6 + 2*n,5 + 3*n),2)
    local ymax = tonumber(sub(framebits,6 + 3*n,5 + 4*n),2)
    return {
        filename   = name,
        version    = version,
        filelength = filelength,
        framerate  = tonumber(byte(buffer[10]) * 256 + byte(buffer[11])),
        framecount = tonumber(byte(buffer[12]) * 256 + byte(buffer[13])),
     -- framebits  = framebits,
        compressed = compressed,
        width      = floor((xmax - xmin) / 20),
        height     = floor((ymax - ymin) / 20),
        rectangle  = {
            xmin = xmin,
            xmax = xmax,
            ymin = ymin,
            ymax = ymax,
        }
    }
end

function figures.checkers.swf(data)
    local dr, du, ds = data.request, data.used, data.status
    local foundname = du.fullname
    local header = getheader(foundname)
    local width, height = figures.applyratio(dr.width,dr.height,header.width,header.height)
    dr.width, dr.height = width, height
    du.width, du.height, du.foundname = width, height, foundname
    context.startfoundexternalfigure(todimen(width),todimen(height))
        nodeinjections.insertswf {
            foundname = foundname,
            width     = width,
            height    = height,
        --  factor    = number.dimenfactors.bp,
            display   = dr.display,
            controls  = dr.controls,
        --  label     = dr.label,
            resources = dr.resources,
        }
    context.stopfoundexternalfigure()
    return data
end

figures.includers.swf = figures.includers.nongeneric

figures.registersuffix("swf","swf")