if not modules then modules = { } end modules ['font-vfc'] = {
    version   = 1.001,
    comment   = "companion to font-ini.mkiv and hand-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local select, type = select, type
local insert = table.insert

local fonts             = fonts
local helpers           = fonts.helpers

local setmetatableindex = table.setmetatableindex

-- Helpers dealing with virtual fonts: beware, these are final values so
-- don't change the content of tables fetched from here!

local pushcommand  = { "push" }
local popcommand   = { "pop" }
local dummycommand = { "comment" }

local slotcommand = setmetatableindex(function(t,k)
    local v = setmetatableindex(function(tt,kk)
        local vv = { "slot", k, kk }
        tt[kk] = vv
        return vv
    end)
    t[k] = v
    return v
end)

function helpers.prependcommands(commands,...)
    insert(commands,1,pushcommand)
    for i=select("#",...),1,-1 do
        local s = (select(i,...))
        if s then
            insert(commands,1,s)
        end
    end
    insert(commands,popcommand)
    return commands
end

function helpers.appendcommands(commands,...)
    insert(commands,1,pushcommand)
    insert(commands,popcommand)
    for i=1,select("#",...) do
        local s = (select(i,...))
        if s then
            insert(commands,s)
        end
    end
    return commands
end

function helpers.prependcommandtable(commands,t)
    insert(commands,1,pushcommand)
    for i=#t,1,-1 do
        local s = t[i]
        if s then
            insert(commands,1,s)
        end
    end
    insert(commands,popcommand)
    return commands
end

function helpers.appendcommandtable(commands,t)
    insert(commands,1,pushcommand)
    insert(commands,popcommand)
    for i=1,#t do
        local s = t[i]
        if s then
            insert(commands,s)
        end
    end
    return commands
end

helpers.commands = utilities.storage.allocate {
    char  = setmetatableindex(function(t,k) local v = { "char",  k } t[k] = v return v end),
    stay  = setmetatableindex(function(t,k) local v = { "stay",  k } t[k] = v return v end),
    right = setmetatableindex(function(t,k) local v = { "right", k } t[k] = v return v end),
    left  = setmetatableindex(function(t,k) local v = { "left",  k } t[k] = v return v end),
    down  = setmetatableindex(function(t,k) local v = { "down",  k } t[k] = v return v end),
    up    = setmetatableindex(function(t,k) local v = { "up",    k } t[k] = v return v end),
    push  = pushcommand,
    pop   = popcommand,
    dummy = dummycommand,
    slot  = slotcommand,
}

local codeinjections = backends.codeinjections
local vfstartcolor   = codeinjections.vfstartcolor
local vfstopcolor    = codeinjections.vfstopcolor
----- vfliteral      = codeinjections.vfliteral

helpers.vfinjectors = {
 -- startcolor = function(h,v,packet) codeinjections.vfstartcolor(h,v,packet) end,
 -- stopcolor  = function(h,v,packet) codeinjections.vfstartcolor(h,v,packet) end,
 -- literal    = function(h,v,packet) codeinjections.vfliteral   (h,v,packer) end,
    startcolor = function(h,v,packet) vfstartcolor(h,v,packet) end,
    stopcolor  = function(h,v,packet) vfstopcolor (h,v,packet) end,
 -- literal    = function(h,v,packet) vfliteral   (h,v,packer) end,
}

updaters.register("backends.injections.latebindings",function()
    vfstartcolor = backends.codeinjections.vfstartcolor
    vfstopcolor  = backends.codeinjections.vfstopcolor
 -- vfliteral    = backends.codeinjections.vfliteral
end)

-- maybe round()

local defaultline = 16384

helpers.vfspecials = {

    backgrounds = setmetatableindex(function(t,h)
        local v = setmetatableindex(function(t,d)
            local v = setmetatableindex(function(t,w)
                local v = { "frame", w, h, d, defaultline, false, false }
                t[w] = v
                return v
            end)
            t[d] = v
            return v
        end)
        t[h] = v
        return v
    end),

    outlines = setmetatableindex(function(t,h)
        local v = setmetatableindex(function(t,d)
            local v = setmetatableindex(function(t,w)
                local v = { "frame", w, h, d, defaultline, true, false }
                t[w] = v
                return v
            end)
            t[d] = v
            return v
        end)
        t[h] = v
        return v
    end),

}

-- In the past we only copied when we had something that got scaled but the problem
-- is that we then run into issues when we extend a commands in the parent. This
-- needs checking. When we go to glyph scaling, this can go away.
--
-- The alternative is to work with unscaled values and always do scaling at the
-- TeX end. So, then we need accessors instead of tables and we also need to check
-- all the context code: where do we access values?
--
-- On the other hand, in glyph scale mode we hardly do any scaling so we seldom
-- call the next one.
--
-- What if we use named fields: then we can scale by field name.

local scaled = {
    right  = true,
    down   = true,
    left   = true,
    right  = true,
    offset = true,
    rule   = true,
    char   = false,
    font   = false,
    slot   = false,
    use    = false,
    push   = false,
    pop    = false,
    lua    = false,
    -- obsolete
    node   = false,
    -- additional ones are never scaled (color etc)
}

-- normally we don't have these, and if so, only in old school fonts
-- in which case we don't have extensions so ...

function helpers.scalecommands(list,hdelta,vdelta)
    local n = #list
    for i=1,n do
        local cmd = list[i]
        if scaled[cmd[1]] then
            local result = { }
            for i=1,n do
                local cmd = list[i]
                local key = cmd[1]
                if key == "right" or key == "left" then
                    result[i] = { key, cmd[2]*hdelta }
                elseif key == "down" or key == "up" then
                    result[i] = { key, cmd[2]*vdelta }
                elseif key == "offset" then
                    result[i] = { key, cmd[2]*hdelta, cmd[3]*vdelta, cmd[4], cmd[5], cmd[6] }
                elseif key == "rule" then
                    result[i] = { key, cmd[2]*hdelta, cmd[3]*vdelta }
                elseif key == "line" then
                    result[i] = { key, cmd[2]*hdelta, cmd[3]*vdelta, cmd[4]*vdelta, cmd[5] }
             -- elseif key == "frame" then
             --     result[i] = cmd -- already scaled, for now
                else
                    result[i] = cmd -- shared since in cache and untouched
                end
            end
            return result
        end
    end
    return list
end
