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

local attributes       = attributes
local nodes            = nodes
local context          = context

local nuts             = nodes.nuts
local nodepool         = nuts.pool
local userrule         = nuts.rules.userrule
local outlinerule      = nuts.pool.outlinerule
local ruleactions      = nuts.rules.ruleactions

local setattrlist      = nuts.setattrlist
local setattr          = nuts.setattr
local tonode           = nuts.tonode

local getattribute     = tex.getattribute

local direct_value     <const> = tokens.values.direct

local a_color          <const> = attributes.private('color')
local a_transparency   <const> = attributes.private('transparency')
local a_colormodel     <const> = attributes.private('colormodel')

local floor            = math.floor
local getrandom        = utilities.randomizer.get

updaters.register("backends.injections.latebindings",function()
    local codeinjections = backends.codeinjections
    ruleactions.mp       = codeinjections.ruleactionmp
    ruleactions.fill     = codeinjections.ruleactionfill
    ruleactions.draw     = codeinjections.ruleactiondraw
    ruleactions.stroke   = codeinjections.ruleactionstroke
    ruleactions.box      = codeinjections.ruleactionbox
end)

interfaces.implement {
    name      = "frule",
    public    = true,
    protected = true,
    arguments = { {
        { "width",  "dimension" },
        { "height", "dimension" },
        { "depth",  "dimension" },
        { "radius", "dimension" },
        { "line",   "dimension" },
        { "type",   "string" },
        { "data",   "string" },
        { "name",   "string" },
        { "radius", "dimension" },
        { "corner", "string" },
    } } ,
    actions = function(t)
        local rule = userrule(t)
        if t.type == "mp" then
            t.ma = getattribute(a_colormodel) or 1
            t.ca = getattribute(a_color)
            t.ta = getattribute(a_transparency)
        else
            setattrlist(rule,true)
        end
        context(tonode(rule))
    end
}

interfaces.implement {
    name      = "roundedoutline",
    protected = true,
    arguments = { "dimension", "dimension", "dimension", "dimension", "dimension", "string" },
    actions   = function(w,h,d,l,r,c)
        local rule = userrule {
            width  = w,
            height = h,
            depth  = d,
            line   = l,
            radius = r,
            corner = c,
        }
        setattrlist(rule,true)
        context(tonode(rule))
    end
}

interfaces.implement {
    name      = "roundedfill",
    protected = true,
    arguments = { "dimension", "dimension", "dimension", "dimension", "dimension", "string" },
    actions   = function(w,h,d,l,r,c)
        local rule = userrule {
            type   = "fill",
            width  = w,
            height = h,
            depth  = d,
            line   = l,
            radius = r,
            corner = c,
        }
        setattrlist(rule,true)
        context(tonode(rule))
    end
}

interfaces.implement {
    name      = "framedoutline",
 -- public    = true,
 -- protected = true,
    arguments = { "dimension", "dimension", "dimension", "dimension" },
    actions   = function(w,h,d,l)
        local rule = outlinerule(w,h,d,l)
        setattrlist(rule,true)
        context(tonode(rule))
    end
}

interfaces.implement {
    name      = "fakeword",
    arguments = { {
        { "factor", "dimension" },
        { "name",   "string" }, -- can be type
        { "min",    "dimension" },
        { "max",    "dimension" },
        { "n",      "integer" },
    } } ,
    actions = function(t)
        local factor = t.factor or 0
        local amount = getrandom("fakeword",t.min,t.max)
        local rule   = userrule {
            height = 1.25*factor,
            depth  = 0.25*factor,
            width  = floor(amount/10000) * 10000,
            line   = 0.10*factor,
            ma     = getattribute(a_colormodel) or 1,
            ca     = getattribute(a_color),
            ta     = getattribute(a_transparency),
            type   = "mp",
            name   = t.name,
        }
        setattrlist(rule,true)
        context(tonode(rule))
    end
}
