meta-grd.mkiv /size: 7510 b    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=meta-grd,
3%D        version=2012.06.28,
4%D          title=\METAPOST\ Graphics,
5%D       subtitle=grids,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{MetaPost Graphics / Grids}
15
16\unprotect
17
18%D This used to be a \TEX\ method, and a rather old one too. We keep it around but
19%D in a more modern way.
20%D
21%D \startbuffer
22%D \basegrid
23%D   [nx=8,ny=5,
24%D    dx=.5,dy=.25,
25%D    unit=cm,scale=2,factor=1,
26%D    offset=1ex,xstep=2,ystep=1,
27%D    align=middle,style=\tt\tx]
28%D \stopbuffer
29%D
30%D \typebuffer
31%D
32%D \placefigure
33%D   {An example of a grid.}
34%D   {\getbuffer}
35
36% \startuseMPgraphic{grid}{nx,ny,dx,dy,factor,scale,xstep,ystep,xoffset,yoffset,align,s:textcolor,s:rulecolor,rulethickness}
37%     begingroup ;
38%         save dx, dy, wd, ht, ox, oy, ax, ay ;
39%
40%         (dx,dy) = (\MPvar{dx},\MPvar{dy})*\MPvar{factor}*\MPvar{scale} ;
41%         (wd,ht) = (\MPvar{nx}*dx,\MPvar{ny}*dy) ;
42%         (ox,oy) = (\MPvar{xoffset},\MPvar{yoffset}) ;
43%         (ax,ay) = if \MPvar{align} = 1 : (dx,dy)/2 else : origin fi ;
44%
45%         draw image (
46%             for i=0 step dx until wd : draw (i,0) -- (i,ht) ; endfor ;
47%             for i=0 step dy until ht : draw (0,i) -- (wd,i) ; endfor ;
48%         )
49%             withpen pencircle scaled \MPvar{rulethickness}
50%             withcolor "\MPvar{rulecolor}" ;
51%         draw image (
52%             if \MPvar{xstep} > 0 :
53%                 for i=1 step \MPvar{xstep} until \MPvar{nx} :
54%                     draw thetextext.bot(decimal i,(i*dx-ax,-ox));
55%                 endfor ;
56%             fi ;
57%             if \MPvar{ystep} > 0 :
58%                 for i=1 step \MPvar{ystep} until \MPvar{ny} :
59%                     draw thetextext.lft(decimal i,(-oy,i*dy-ay)) ;
60%                 endfor ;
61%             fi ;
62%         )
63%             withcolor "\MPvar{color}" ;
64%     endgroup ;
65% \stopuseMPgraphic
66%
67% less tokens (not used often):
68%
69% \startuseMPgraphic{grid}{\c!nx,\c!ny,\c!dx,\c!dy,\c!factor,\c!scale,\c!xstep,\c!ystep,\c!xoffset,\c!yoffset,\c!align,s:\c!textcolor,s:\c!rulecolor,\c!rulethickness}
70%     begingroup ;
71%         save dx, dy, wd, ht, ox, oy, ax, ay ;
72%
73%         (dx,dy) = (\MPvar\c!dx,\MPvar\c!dy)*\MPvar\c!factor*\MPvar\c!scale ;
74%         (wd,ht) = (\MPvar\c!nx*dx,\MPvar\c!ny*dy) ;
75%         (ox,oy) = (\MPvar\c!xoffset,\MPvar\c!yoffset) ;
76%         (ax,ay) = if \MPvar\c!align = 1 : (\c!dx,\c!dy)/2 else : origin fi ;
77%
78%         draw image (
79%             for i=0 step dx until wd : draw (i,0) -- (i,ht) ; endfor ;
80%             for i=0 step dy until ht : draw (0,i) -- (wd,i) ; endfor ;
81%         )
82%             withpen pencircle scaled \MPvar\c!rulethickness\space
83%             withcolor "\MPvar\c!rulecolor" ;
84%         draw image (
85%             if \MPvar\c!xstep > 0 :
86%                 for i=1 step \MPvar\c!xstep until \MPvar\c!nx :
87%                     draw thetextext.bot(decimal i,(i*dx-ax,-ox));
88%                 endfor ;
89%             fi ;
90%             if \MPvar\c!ystep > 0 :
91%                 for i=1 step \MPvar\c!ystep until \MPvar\c!ny :
92%                     draw thetextext.lft(decimal i,(-oy,i*dy-ay)) ;
93%                 endfor ;
94%             fi ;
95%         )
96%             withcolor "\MPvar\c!color" ;
97%     endgroup ;
98% \stopuseMPgraphic
99%
100% \def\typo_grid_base[#1]%
101%   {\dontleavehmode
102%    \hbox\bgroup
103%    \getdummyparameters
104%      [\c!nx=10,\c!ny=10,\c!dx=.5,\c!dy=.5,\c!xstep=0,\c!ystep=0,%
105%       \c!unit=\s!cm,\c!scale=1,\c!factor=1,%
106%       \c!offset=.25ex,%
107%       \c!xoffset=\directdummyparameter\c!offset,%
108%       \c!yoffset=\directdummyparameter\c!offset,%
109%       \c!align=,%
110%       \c!color=\currentcolorname,%
111%       \c!rulecolor=\currentcolorname,%
112%       \c!rulethickness=\linewidth,%
113%       #1]%
114%    \usedummystyleandcolor\c!style\c!color
115%    \edef\p_align{\directdummyparameter\c!align}%
116%    \edef\p_unit {\directdummyparameter\c!unit}%
117%    \useMPgraphic
118%      {grid}%
119%      {nx=\directdummyparameter\c!nx,%
120%       ny=\directdummyparameter\c!ny,%
121%       dx=\directdummyparameter\c!dx\p_unit,%
122%       dy=\directdummyparameter\c!dy\p_unit,%
123%       factor=\directdummyparameter\c!factor,%
124%       scale=\directdummyparameter\c!scale,%
125%       xstep=\directdummyparameter\c!xstep,%
126%       ystep=\directdummyparameter\c!ystep,%
127%       xoffset=\directdummyparameter\c!xoffset,%
128%       yoffset=\directdummyparameter\c!yoffset,%
129%       color=\directdummyparameter\c!color,%
130%       rulecolor=\directdummyparameter\c!rulecolor,%
131%       rulethickness=\directdummyparameter\c!rulethickness,%
132%       align=\ifx\p_align\v!middle\!!plusone\else\!!zerocount\fi}%
133%    \egroup}
134%
135% even less tokens wasted (no need to clog the core with hardly
136% used code):
137
138\startuseMPgraphic{meta:grd:basegrid}
139    begingroup ;
140
141        save sc, dx, dy, wd, ht, ox, oy, ax, ay ;
142
143        sc := \directdummyparameter\c!factor*\directdummyparameter\c!scale ;
144        dx := \MPdpar\directdummyparameter\c!dx*sc ;
145        dy := \MPdpar\directdummyparameter\c!dy*sc ;
146        wd := \directdummyparameter\c!nx*dx ;
147        ht := \directdummyparameter\c!ny*dy ;
148        ox := \MPdpar\directdummyparameter\c!xoffset\relax ;
149        oy := \MPdpar\directdummyparameter\c!yoffset\relax ;
150
151        if "\directdummyparameter\c!align" = "\v!middle" :
152            ax := \MPdpar\directdummyparameter\c!dx/2 ;
153            ay := \MPdpar\directdummyparameter\c!dy/2 ;
154        else :
155            ax := 0 ;
156            ay := 0 ;
157        fi ;
158
159        draw image (
160            for i=0 step dx until wd : draw (i,0) -- (i,ht) ; endfor ;
161            for i=0 step dy until ht : draw (0,i) -- (wd,i) ; endfor ;
162        )
163            withpen pencircle scaled \MPdpar\directdummyparameter\c!rulethickness\space
164            withcolor "\directdummyparameter\c!rulecolor" ;
165
166        draw image (
167            if \directdummyparameter\c!xstep > 0 :
168                for i=1 step \directdummyparameter\c!xstep until \directdummyparameter\c!nx :
169                    draw thetextext.bot(decimal i,(i*dx-ax,oy));
170                endfor ;
171            fi ;
172            if \directdummyparameter\c!ystep > 0 :
173                for i=1 step \directdummyparameter\c!ystep until \directdummyparameter\c!ny :
174                    draw thetextext.lft(decimal i,(ox,i*dy-ay)) ;
175                endfor ;
176            fi ;
177        )
178            withcolor "\directdummyparameter\c!color" ;
179
180    endgroup ;
181\stopuseMPgraphic
182
183\def\typo_grid_base[#1]%
184  {\dontleavehmode
185   \hbox\bgroup
186   \getdummyparameters
187     [\c!nx=10,\c!ny=10,%
188      \c!dx=.5,\c!dy=.5,%
189      \c!xstep=0,\c!ystep=0,%
190      \c!unit=\s!cm,\c!scale=1,\c!factor=1,%
191      \c!offset=.25\exheight,%
192      \c!xoffset=\directdummyparameter\c!offset,%
193      \c!yoffset=\directdummyparameter\c!offset,%
194      \c!align=,%
195      \c!color=\currentcolorname,%
196      \c!rulecolor=\currentcolorname,%
197      \c!rulethickness=\linewidth,%
198      #1]%
199   \usedummystyleandcolor\c!style\c!color
200   \setexpandeddummyparameter\c!dx{\directdummyparameter\c!dx\directdummyparameter\c!unit}%
201   \setexpandeddummyparameter\c!dy{\directdummyparameter\c!dy\directdummyparameter\c!unit}%
202   \useMPgraphic{meta:grd:basegrid}%
203   \egroup}
204
205\unexpanded\def\basegrid
206  {\dosingleempty\typo_grid_base}
207
208\let\grid\basegrid
209
210\protect \endinput
211