anch-bar.mkiv /size: 6583 b    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=anch-bar,
3%D        version=2003.03.16,
4%D          title=\CONTEXT\ Anchoring Macros,
5%D       subtitle=Margin Bars and alike,
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}{ConTeXt Anchoring Macros / Margin Bars}
15
16\unprotect
17
18%D This can be done better now ...
19
20%D We will implement a sidebar mechanism using the functionality from
21%D \type {core-pos}.
22%D
23%D \starttyping
24%D \definesidebar[whow][rulecolor=green,distance=0pt]
25%D
26%D \input tufte \par
27%D \startsidebar
28%D   \input tufte \par
29%D   \input tufte \par
30%D   \startsidebar[whow]
31%D     \input tufte \par
32%D     \input tufte \par
33%D     \input tufte
34%D   \stopsidebar \par
35%D   \input tufte \par
36%D   \input tufte
37%D \stopsidebar \par
38%D \input tufte \par
39%D \input tufte \par
40%D \startsidebar
41%D   \input tufte \par
42%D   \input tufte \par
43%D   \input tufte \par
44%D   \input tufte \par
45%D   \input tufte
46%D \stopsidebar \par
47%D \input tufte \par
48%D \input tufte \par
49%D \startsidebar
50%D   \input tufte
51%D   \input tufte
52%D   \input tufte
53%D   \input tufte
54%D   \input tufte
55%D \stopsidebar
56%D \stoptyping
57
58\installcorenamespace{sidebar}
59
60\installcommandhandler \??sidebar {sidebar} \??sidebar
61
62\newcount\c_anch_sidebars_n
63\newcount\c_anch_sidebars_current % local
64\newdimen\d_anch_sidebars_distance
65\newcount\c_anch_sidebars_level
66
67% \setupMPvariables
68%   [mpos:sidebar]
69%   [linecolor=red,
70%    linewidth=2pt,
71%    distance=5pt]
72
73\setupsidebar
74  [\c!rulethickness=\dimexpr\bodyfontsize/6\relax, % 2pt default
75   \c!rulecolor=\s!black,
76   \c!alternative=0,
77   \c!topoffset=\zeropoint,
78   \c!bottomoffset=\zeropoint,
79   \c!distance=.5\bodyfontsize,
80   \c!level=,
81   \c!leftmargindistance=\zeropoint]
82
83\let\setupsidebars\setupsidebar
84
85\unexpanded\def\startsidebar
86  {\dodoubleempty\anch_sidebars_start}
87
88\def\anch_sidebars_start[#1][#2]%
89  {\bgroup
90   \dontleavehmode
91   \advance\c_anch_sidebars_level\plusone
92   \global\advance\c_anch_sidebars_n\plusone
93   \c_anch_sidebars_current\c_anch_sidebars_n\relax % relax needed
94   \doifelseassignment{#1}
95     {\edef\currentsidebar{\the\c_anch_sidebars_level}%
96      \checksidebarparent
97      \setupcurrentsidebar[#1]}
98     {\def\currentsidebar{#1}%
99      \setupcurrentsidebar[#2]}%
100   \scratchdistance\sidebarparameter\c!distance\relax
101   \scratchdimen\sidebarparameter\c!leftmargindistance\relax
102   \edef\m_level{\sidebarparameter\c!level}%
103   \ifx\m_level\empty
104     \ifnum\c_anch_sidebars_level=\plusone
105       \ifzeropt\scratchdimen
106         \advance\d_anch_sidebars_distance\scratchdistance\relax
107       \else
108         \d_anch_sidebars_distance\scratchdimen
109       \fi
110     \else
111       \advance\d_anch_sidebars_distance\scratchdistance\relax
112     \fi
113   \else
114     \ifnum\m_level=\plusone
115       \ifzeropt\scratchdimen
116         \advance\d_anch_sidebars_distance\scratchdistance\relax
117       \else
118         \d_anch_sidebars_distance\scratchdimen
119       \fi
120     \else
121       \d_anch_sidebars_distance\dimexpr\scratchdimen+\numexpr\m_level-\plusone\relax\dimexpr\scratchdistance\relax\relax
122     \fi
123   \fi
124   \startpositionoverlay{\v!text-1}%
125     \normalexpanded{\setMPpositiongraphicrange % maybe expand in definition
126       {b:sidebar:\the\c_anch_sidebars_n}%
127       {e:sidebar:\the\c_anch_sidebars_n}%
128       {mpos:sidebar}%
129       {self=sidebar:\the\c_anch_sidebars_n,
130        linewidth=\sidebarparameter\c!rulethickness,
131        linecolor=\sidebarparameter\c!rulecolor,
132        alternative=\sidebarparameter\c!alternative,
133        topoffset=\the\dimexpr\sidebarparameter\c!topoffset,
134        bottomoffset=\the\dimexpr\sidebarparameter\c!bottomoffset,
135        distance=\the\d_anch_sidebars_distance}%
136     }%
137   \stoppositionoverlay
138   \bpos{sidebar:\the\c_anch_sidebars_current}%
139   \ignorespaces}
140
141\unexpanded\def\stopsidebar
142  {\removelastspace
143   \epos{sidebar:\the\c_anch_sidebars_current}
144   \carryoverpar\egroup}
145
146%D Let's keep this nice and simple (okay, we could pass the 6 variables in
147%D one lua call).
148
149\startMPpositionmethod{mpos:sidebar}
150    \startMPpositiongraphic{mpos:sidebar}{linecolor,linewidth,distance,alternative}%
151        anch_sidebars_draw (
152            \MPp\MPbself,\MPp\MPeself,\MPy\MPbself,\MPy\MPeself,\MPh\MPbself,\MPd\MPeself,
153            \MPx{\textanchor},\MPy{\textanchor},\MPw{\textanchor},\MPh{\textanchor},
154            \MPvar{alternative},\MPvar{distance},\MPvar{linewidth},\MPvar{linecolor},
155            \MPvar{topoffset}, \MPvar{bottomoffset}
156        ) ;
157    \stopMPpositiongraphic
158    \MPpositiongraphic{mpos:sidebar}{}%
159\stopMPpositionmethod
160
161%D We now reimplement the \MKII\ margin rules handler in a more
162%D modern way.
163%D
164%D \setupmarginrules
165%D   [rulecolor=darkred,
166%D    rulethickness=2pt]
167%D
168%D \setupmarginrules % sidebar
169%D   [2]
170%D   [rulecolor=darkblue]
171%D
172%D \startmarginrule[1]
173%D     \input ward
174%D     \startmarginrule[2]
175%D         \input ward
176%D         \startmarginrule[3]
177%D             \input ward
178%D             \startmarginrule[level=6,rulecolor=darkgreen]
179%D                 \input ward
180%D             \stopmarginrule
181%D             \input ward
182%D         \stopmarginrule
183%D         \input ward
184%D     \stopmarginrule
185%D     \input ward
186%D \stopmarginrule
187%D
188%D Compared to the old mechanism we now can pass settings too.
189
190\definesidebar
191  [\v!margin]
192  [\c!leftmargindistance=\dimexpr\leftmargindistance+\sidebarparameter\c!rulethickness/2\relax]
193
194\dorecurse{5}{\definesidebar[\v!margin:#1][\v!margin]} % let's be nice and predefine 5 levels
195
196\unexpanded\def\setupmarginrule
197  {\dodoubleargument\anch_marginrules_setup}
198
199\def\anch_marginrules_setup[#1][#2]%
200  {\ifsecondargument
201     \setupsidebar[\v!margin:#1][#2]%
202   \else
203     \setupsidebar[\v!margin][#1]%
204   \fi}
205
206\let\setupmarginrules\setupmarginrule
207
208\unexpanded\def\startmarginrule
209  {\dosingleempty\anch_marginrules_start}
210
211\def\anch_marginrules_start[#1]% pretty inefficient checking
212  {\edef\m_anch_marginrules_kind{#1}%
213   \ifx\m_anch_marginrules_kind\empty
214     \anch_sidebars_start[\v!margin][]%
215   \else
216     \doifelseassignment\m_anch_marginrules_kind
217       {\anch_sidebars_start[\v!margin][#1]}%
218       {\anch_marginrules_check{#1}%
219        \anch_sidebars_start[\v!margin:#1][\c!level=#1]}%
220   \fi}
221
222\def\anch_marginrules_check#1%
223  {\doifnotcommandhandler\??sidebar{\v!margin:#1}{\definesidebar[\v!margin:#1][\v!margin]}}
224
225\let\stopmarginrule\stopsidebar
226
227\protect \endinput
228