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

-- This is actually very experimental code but it has been so since we began with
-- \LUATEX\ so \unknown. We keep it around because it's not that much code and
-- because we have some demo's (in the test suite) that use it.

local next, setmetatable, getmetatable = next, setmetatable, getmetatable

local allocate          = utilities.storage.allocate
local setmetatableindex = table.setmetatableindex
local fastcopy          = table.fastcopy

local fonts             = fonts
local constructors      = fonts.constructors
local vf                = constructors.handlers.vf
vf.version              = 1.000 -- same as tfm

local definers          = fonts.definers
local methods           = definers.methods

local variants          = allocate()
local combinations      = { }
local combiner          = { }
local whatever          = allocate()
local helpers           = allocate()
local predefined        = fonts.helpers.commands

methods.variants        = variants -- todo .. wrong namespace
vf.combinations         = combinations
vf.combiner             = combiner
vf.whatever             = whatever
vf.helpers              = helpers
vf.predefined           = predefined

setmetatableindex(whatever, function(t,k) local v = { } t[k] = v return v end)

local function checkparameters(g,f)
    if f and g and not g.parameters and #g.fonts > 0 then
        local p = { }
        for k,v in next, f.parameters do
            p[k] = v
        end
        g.parameters = p
        setmetatable(p, getmetatable(f.parameters))
    end
end

function methods.install(tag, rules)
    vf.combinations[tag] = rules
    variants[tag] = function(specification)
        return vf.combine(specification,tag)
    end
end

local function combine_load(g,name)
    return constructors.readanddefine(name or g.specification.name,g.specification.size)
end

local function combine_assign(g, name, from, to, start, force)
    local f, id = combine_load(g,name)
    if f and id then
        -- optimize for whole range, then just g = f
        if not from  then from, to = 0, 0xFF00 end
        if not to    then to       = from      end
        if not start then start    = from      end
        local fc = f.characters
        local gc = g.characters
        local fd = f.descriptions
        local gd = g.descriptions
        local hn = #g.fonts+1
        g.fonts[hn] = { id = id } -- no need to be sparse
        for i=from,to do
            if fc[i] and (force or not gc[i]) then
                gc[i] = fastcopy(fc[i],true) -- can be optimized
                gc[i].commands = { { "slot", hn, start } }
                gd[i] = fd[i]
            end
            start = start + 1
        end
        checkparameters(g,f)
    end
end

local function combine_process(g,list)
    if list then
        for _,v in next, list do
            (combiner.commands[v[1]] or nop)(g,v)
        end
    end
end

local function combine_names(g,name,force)
    local f, id = constructors.readanddefine(name,g.specification.size)
    if f and id then
        local fc = f.characters
        local gc = g.characters
        local fd = f.descriptions
        local gd = g.descriptions
        g.fonts[#g.fonts+1] = { id = id } -- no need to be sparse
        local hn = #g.fonts
        for k, v in next, fc do
            if force or not gc[k] then
                gc[k] = fastcopy(v,true)
                gc[k].commands = { { "slot", hn, k } }
                gd[i] = fd[i]
            end
        end
        checkparameters(g,f)
    end
end

local combine_feature = function(g,v)
    local key   = v[2]
    local value = v[3]
    if key then
        if value == nil then
            value = true
        end
        local specification = g.specification
        if specification then
            local normalfeatures = specification.features.normal
            if normalfeatures then
                normalfeatures[key] = value -- otf?
            end
        end
    end
end

combiner.commands = allocate {
    ["initialize"]      = function(g,v) combine_assign    (g,g.properties.name) end,
    ["include-method"]  = function(g,v) combine_process   (g,combinations[v[2]]) end, -- name
 -- ["copy-parameters"] = function(g,v) combine_parameters(g,v[2]) end, -- name
    ["copy-range"]      = function(g,v) combine_assign    (g,v[2],v[3],v[4],v[5],true) end, -- name, from-start, from-end, to-start
    ["copy-char"]       = function(g,v) combine_assign    (g,v[2],v[3],v[3],v[4],true) end, -- name, from, to
    ["fallback-range"]  = function(g,v) combine_assign    (g,v[2],v[3],v[4],v[5],false) end, -- name, from-start, from-end, to-start
    ["fallback-char"]   = function(g,v) combine_assign    (g,v[2],v[3],v[3],v[4],false) end, -- name, from, to
    ["copy-names"]      = function(g,v) combine_names     (g,v[2],true) end,
    ["fallback-names"]  = function(g,v) combine_names     (g,v[2],false) end,
    ["feature"]         =               combine_feature,
}

function vf.combine(specification,tag)
    local g = {
        name          = specification.name,
        properties    = { },
        fonts         = { },
        characters    = { },
        descriptions  = { },
        specification = fastcopy(specification),
    }
    combine_process(g,combinations[tag])
    return g
end
