summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/meta-imp-dum.mkiv
blob: b84d4ac7ac6f28810343bb257f30188582ad5f12 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
%D \module
%D   [       file=meta-dum,
%D        version=2003.03.21,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Dummy (External) Graphics,
%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.

\unprotect

%D This library overloads the normal external figure
%D placeholder by a nicer one.
%D
%D \startbuffer
%D \useMPlibrary[dum]
%D
%D \startlinecorrection
%D \externalfigure[unknown-a][width=3cm,height=1cm]
%D \stoplinecorrection
%D
%D \startlinecorrection
%D \externalfigure[unknown-b][width=4cm,height=5cm]
%D \stoplinecorrection
%D \stopbuffer
%D
%D \typebuffer \getbuffer

% currently preparempvariables is unable to resolve number
% fractions like reduction

% June 22, 2003, this definition was patched to adapt itself
% to transparent colors, but ... in 2011 we no longer have
% is_transparent so we revert.
%
% \startuseMPgraphic{placeholder}{width,height,reduction,color}
%     numeric w, h, d, r ; color c, b, cc ; path p ; boolean t ;
%     t := is_transparent(\MPvar{color}) ;
%     c := not_transparent(\MPvar{color}) ;
%     b := not_transparent(white) ;
%     w := \MPvar{width} ;
%     h := \MPvar{height} ;
%     r := \MPvar{reduction} ;
%     d := max(w,h) ;
%     p := unitsquare xyscaled (w,h) ;
%     cc := r[.5c,b] ;
%     fill p withcolor if t : transparent(1,.5,cc) else : cc fi ;
%     for i := 1 upto 60 :
%         cc := r[c randomized(.3,.9),b] ;
%         fill fullcircle
%             scaled (d/5 randomized (d/5))
%             shifted (center p randomized (d))
%             withcolor if t : transparent(1,.5,cc) else : cc fi ;
%     endfor ;
%     clip currentpicture to p ;
% \stopuseMPgraphic

\startuseMPgraphic{minifun::figure:placeholder}{width,height,reduction,color}
    begingroup ;
    save w, h, d, r, p, c, b ;
    numeric w, h, d, r ; path p ;
    if cmykcolor \MPvar{color} :
        cmykcolor c, b ; b := (0,0,0,0)
    else :
        color c, b ; b := (1,1,1)
    fi ;
    c := \MPvar{color} ;
    w := \MPvar{width} ;
    h := \MPvar{height} ;
    r := \MPvar{reduction} ;
    d := max(w,h) ;
    p := unitsquare xyscaled (w,h) ;
    fill p withcolor r[.5c,b] ;
    for i := 1 upto 60 :
        fill fullcircle
            scaled (d/5 randomized (d/5))
            shifted (center p randomized (d))
            withcolor r[c randomized(.3,.9),b] ;
    endfor ;
    clip currentpicture to p ;
    endgroup ;
\stopuseMPgraphic

\defineoverlay
  [figure:placeholder:graphic]
  [\useMPgraphic
     {minifun::figure:placeholder}%
     {width=\figurewidth,%
      height=\figureheight,%
      reduction=\externalfigureparameter\c!reduction,%
      color=placeholder:\the\c_grph_replacement_n}] % weird, why do we need to prefix the palette

\definepalet
  [placeholder]
  [1=red,2=green,3=blue,4=cyan,5=magenta,6=yellow]

\newcount\c_grph_replacement_n

\setupexternalfigures
  [\c!reduction=0,
   \c!text=\v!yes]

\let\grph_include_replacement_saved\grph_include_replacement

\unexpanded\def\grph_include_replacement#1#2#3%
  {\begingroup
%    \setuppalet[placeholder]%
   \getpaletsize[placeholder]%
   \ifnum\c_grph_replacement_n=\zerocount
     \getrandomnumber \m_grph_replacement_n\plusone\paletsize
     \global\c_grph_replacement_n \m_grph_replacement_n\relax
   \else
     \global\advance\c_grph_replacement_n\plusone
   \fi
   \ifnum\c_grph_replacement_n>\paletsize
     \global\c_grph_replacement_n\plusone
   \fi
   \setupcurrentexternalfigure
     [\c!width=\figurewidth,
      \c!height=\figureheight,
      \c!frame=\v!off,
      \c!strut=\v!no,
      \c!align={\v!middle,\v!lohi},
      \c!background=figure:placeholder:graphic,
      \c!foregroundcolor=\s!white]%
   \doifelse{\externalfigureparameter\c!text}\v!yes
     {\edef\m_graphics_text_a{#1}\edef\m_graphics_text_a{\ifx\m_graphics_text_a\s!dummy\else\detokenize\expandafter{\m_graphics_text_a}\fi}%
      \edef\m_graphics_text_b{#2}\edef\m_graphics_text_b{\ifx\m_graphics_text_b\s!dummy\else\detokenize\expandafter{\m_graphics_text_b}\fi}%
      \edef\m_graphics_text_c{#3}\edef\m_graphics_text_c{\ifx\m_graphics_text_c\s!dummy\else\detokenize\expandafter{\m_graphics_text_c}\fi}%
      \infofont\setupinterlinespace\dohyphens
      \inheritedexternalfigureframed{\directsetup{figure:placeholder:text}}}%
     {\inheritedexternalfigureframed{}}%
   \endgroup}

\unexpanded\def\dummyfigure
  {\externalfigure[placeholder]}

\startsetups figure:placeholder:text
    \ifx\m_graphics_text_a\empty\else
        \strut name: \m_graphics_text_a\par
    \fi
    \ifx\m_graphics_text_b\empty\else
        \strut
        \ifx\m_graphics_text_a\empty
            \ifx\m_graphics_text_c\empty
            \else
                file:\space
            \fi
        \else
            file:\space
        \fi
        \m_graphics_text_b\par
    \fi
    \ifx\m_graphics_text_c\empty\else
        \strut state: \m_graphics_text_c\par
    \fi
\stopsetups

%D \starttyping
%D \externalfigure[mediashow.swf][comment={Alas, we have no nice preview},background=figure:comment]
%D \externalfigure[mediashow.swf][comment={Alas, we have no nice preview},background=figure:dummy]
%D \externalfigure[mediashow.swf][background=figure:dummy]
%D \stoptyping

\defineframed
  [figurecomment]
  [\c!background=\v!color,
   \c!backgroundcolor=\s!gray,
   \c!frame=\v!off,
   \c!foregroundstyle=\ttbf,
   \c!align={\v!middle,\v!lohi},
   \c!height=\figureheight,
   \c!width=\figurewidth]

\defineframed
  [figuredummy]
  [\c!background=figure:dummy:only,
   \c!foregroundcolor=\s!white,
   \c!backgroundcolor=\v!gray,
   \c!frame=\v!off,
   \c!foregroundstyle=\ttbf,
   \c!align={\v!middle,\v!lohi},
   \c!height=\figureheight,
   \c!width=\figurewidth]

\defineoverlay
  [figure:dummy:only]
  [\grph_include_replacement\empty\empty\empty]

\defineoverlay
  [figure:comment]
  [\figurecomment{\externalfigureparameter\c!comment}]

\defineoverlay
  [figure:dummy]
  [\figuredummy{\externalfigureparameter\c!comment}]

\setupexternalfigures
  [\c!comment=]

\protect

\continueifinputfile{meta-imp-dum.mkiv}

\starttext

    \externalfigure[whatever-missing]

    \blank

    \externalfigure[whatever-missing][width=2cm]

\stoptext