%D \module
%D   [       file=meta-imp-outlines,
%D        version=2015.06.02,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Outlines,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\startluacode

local concat      = table.concat
local formatters  = string.formatters
local validstring = string.valid

local f_setbounds = formatters["setbounds currentpicture to (%s) enlarged %.4G;"]
local f_index     = formatters['draw anchored.bot(textext("\\tttf\\setstrut\\strut index %i") ysized 10bp ,.5[llcorner currentpicture,lrcorner currentpicture] shifted (0,%.4G));']
local f_unicode   = formatters['draw anchored.bot(textext("\\tttf\\setstrut\\strut unicode %05X") ysized 10bp ,.5[llcorner currentpicture,lrcorner currentpicture] shifted (0,%.4G));']

local f_in_red    = formatters["draw %s withpen pencircle scaled .15 withcolor .5red;"]
local f_in_green  = formatters["draw %s withpen pencircle scaled .15 withcolor .5green;"]
local f_in_blue   = formatters["draw %s withpen pencircle scaled .15 withcolor .5blue;"]
local f_in_gray   = formatters["draw image(%s) withcolor .75yellow;"]

local f_glyph = formatters [ [[
pickup pencircle scaled .15;
pointlabelfont := "Mono sa .125";
pointlabelscale := 1bp ;
drawoptionsfactor := .2bp ;
originlength := 2bp ;
%s;
]] ]

local metapost  = fonts.metapost

local variables = interfaces.variables

local v_all     = variables.all
local v_page    = variables.page
local v_text    = variables.text
local v_command = variables.command
local v_box     = variables.box
local v_width   = variables.width
local v_min     = variables.min
local v_max     = variables.max
local v_comment = variables.comment
local v_simple  = variables.simple

function metapost.showglyph(specification)
    local fontid      = font.current()
    local shapedata   = fonts.hashes.shapes[fontid] -- by index
    local chardata    = fonts.hashes.characters[fontid] -- by unicode
    local shapeglyphs = shapedata.glyphs or { }
    local character   = validstring(specification.character)
    local index       = validstring(specification.index)
    local alternative = validstring(specification.alternative)
    local command     = validstring(specification.command)
    local options     = utilities.parsers.settings_to_set(specification.option)
    local all         = not next(options) and not options[v_simple] or options[v_all]
    local function shape(index,what,f_comment)
        if not index then
            return
        end
        local glyph = shapeglyphs[index]
        if glyph and (glyph.segments or glyph.sequence) then
            local units  = shapedata.units or 1000
            local factor = 100/units
            local paths  = metapost.paths(glyph,factor)
            if #paths > 0 and glyph.boundingbox and glyph.width then
                local graphic = f_glyph(concat{
                                                    f_in_gray  (metapost.fill(paths)),
                                                                metapost.draw(paths,true), -- true triggers trace
                    (all or options[v_box])     and f_in_red   (metapost.boundingbox(glyph,factor)) or "",
                    (all or options[v_width])   and f_in_green (metapost.widthline(glyph,factor)) or "",
                    (all or options[v_min])     and f_in_blue  (metapost.zeroline(glyph,factor)) or "",
                    (all or options[v_max])     and f_setbounds(metapost.maxbounds(shapedata,index,factor),offset or 1) or "",
                    (all or options[v_comment]) and f_comment  (what,1) or "",
                })
                if alternative == v_page then
                    context.startMPpage()
                        context(graphic)
                    context.stopMPpage()
                elseif alternative == v_command then
                    context[command](graphic)
                else -- v_text
                    context.startMPcode()
                        context(graphic)
                    context.stopMPcode()
                end
            end
        end
    end

    if character == v_all then
        for u, c in table.sortedhash(chardata) do
            shape(c.index,u,f_unicode)
        end
        return
    end
    if type(character) == "string" then
        character = utf.byte(character)
    end
    if type(character) == "number" then
        local c = chardata[character]
        if c then
            shape(c.index,c.index,f_index)
        end
        return
    end
    if type(index) == "number" then
        shape(index,index,f_index)
        return
    end
    local first, last
    if type(index) == "string" then
        first, last = string.split(index,":")
        if first and last then
            first = tonumber(first)
            last  = tonumber(last)
        else
            first = tonumber(index)
            last  = first
        end
        if not first then
            first = 1
            last  = #shapeglyphs
        elseif not last then
            last = first
        end
    else
        first = 1
        last  = #shapeglyphs
    end
    for index=first,last do
        shape(index,index,f_index)
    end
end

\stopluacode

\unprotect

% option: box|width|min|max|comment

\unexpanded\def\showshape
  {\dosingleargument\meta_shapes_show}

\def\meta_shapes_show[#1]%
  {\begingroup
   \letdummyparameter\c!index\empty
   \letdummyparameter\c!character\empty
   \letdummyparameter\c!alternative\v!text
   \letdummyparameter\c!command\empty
   \letdummyparameter\c!option\v!all
   \getdummyparameters[#1]%
   \ctxlua{fonts.metapost.showglyph{
        character   = "\dummyparameter\c!character",
        index       = "\dummyparameter\c!index",
        alternative = "\dummyparameter\c!alternative",
        command     = "\dummyparameter\c!command",
        option      = "\dummyparameter\c!option",
   }}%
   \endgroup}

\protect

\continueifinputfile{meta-imp-outlines.mkiv}

\starttext

% \setupbodyfont[pagella]

% \definedfont[latinmodern-math]\showshape[index=3078,alternative=page]

% \setupbodyfont[pagella]
% \showshape[character=all,alternative=page]

\usemodule[art-01]

% \definedfont[lt55476.afm]

\startcombination[3*1]
    {\ruledhbox{\startMPcode draw textext("\showshape[character=a]") ; \stopMPcode}} {}
    {\ruledhbox{\startMPcode draw textext("\showshape[character=x]") ; \stopMPcode}} {}
    {\ruledhbox{\showshape[character=P,alternative=text]}} {}
\stopcombination

\startcombination[3*1]
    {\ruledhbox{\startMPcode draw textext("\showshape[character=a,option={simple}]") ; \stopMPcode}} {}
    {\ruledhbox{\startMPcode draw textext("\showshape[character=x,option={simple}]") ; \stopMPcode}} {}
    {\ruledhbox{\showshape[character=P,alternative=text,option=simple]}} {}
\stopcombination

% \definedfont[almfixed]
% \showshape[character=all,alternative=page]

% \definedfont[file:sourcehansans-bold.otf]
% \showshape[index=40000:41000,alternative=page]

\stoptext
