summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/meta-imp-outlines.mkiv
blob: 7629c0c928ca88a941ae53230aeb0923c89a38ba (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
%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(data,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