summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/anch-bar.mkiv
blob: 9f9770fb6a3adf5b222a975755fdf3566e2bad51 (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
%D \module
%D   [       file=anch-bar,
%D        version=2003.03.16,
%D          title=\CONTEXT\ Anchoring Macros,
%D       subtitle=Margin Bars and alike,
%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.

\writestatus{loading}{ConTeXt Anchoring Macros / Margin Bars}

\unprotect

%D We will implement a sidebar mechanism using the
%D functionality from \type {core-pos}.
%D
%D \starttyping
%D \definesidebar[whow][rulecolor=green,distance=]
%D
%D \input tufte \par
%D \startsidebar
%D   \input tufte \par
%D   \input tufte \par
%D   \startsidebar[whow]
%D     \input tufte \par
%D     \input tufte \par
%D     \input tufte
%D   \stopsidebar \par
%D   \input tufte \par
%D   \input tufte
%D \stopsidebar \par
%D \input tufte \par
%D \input tufte \par
%D \startsidebar
%D   \input tufte \par
%D   \input tufte \par
%D   \input tufte \par
%D   \input tufte \par
%D   \input tufte
%D \stopsidebar \par
%D \input tufte \par
%D \input tufte \par
%D \startsidebar
%D   \input tufte
%D   \input tufte
%D   \input tufte
%D   \input tufte
%D   \input tufte
%D \stopsidebar
%D \stoptyping

\installcorenamespace{sidebar}

\installcommandhandler \??sidebar {sidebar} \??sidebar

\newcount\c_anch_sidebars_n
\newdimen\c_anch_sidebars_distance

% \setupMPvariables
%   [mpos:sidebar]
%   [linecolor=red,
%    linewidth=2pt,
%    distance=5pt]

\setupsidebar
  [\c!rulethickness=2pt,
   \c!rulecolor=\s!black,
   \c!alternative=0,
   \c!topoffset=0pt,
   \c!bottomoffset=0pt,
   \c!distance=.5\bodyfontsize]

\let\setupsidebars\setupsidebar

\unexpanded\def\startsidebar
  {\dosingleempty\anch_sidebars_start}

\def\anch_sidebars_start[#1]%
  {\bgroup
   \def\currentsidebar{#1}%
   \dontleavehmode
   \global\advance\c_anch_sidebars_n\plusone
   \advance\c_anch_sidebars_distance\sidebarparameter\c!distance
   \startpositionoverlay{text-1}%
     \normalexpanded{\setMPpositiongraphicrange % maybe expand in definition
       {b:sidebar:\the\c_anch_sidebars_n}%
       {e:sidebar:\the\c_anch_sidebars_n}%
       {mpos:sidebar}%
       {self=sidebar:\the\c_anch_sidebars_n,
        linewidth=\sidebarparameter\c!rulethickness,
        linecolor=\sidebarparameter\c!rulecolor,
        alternative=\sidebarparameter\c!alternative,
        topoffset=\sidebarparameter\c!topoffset,
        bottomoffset=\sidebarparameter\c!bottomoffset,
        distance=\the\c_anch_sidebars_distance}}%
   \stoppositionoverlay
   \bpos{sidebar:\the\c_anch_sidebars_n}}

\unexpanded\def\stopsidebar
  {\removelastspace
   \epos{sidebar:\the\c_anch_sidebars_n}
   \carryoverpar\egroup}

%D Let's keep this nice and simple (okay, we could pass the 6 variables in
%D one lua call).

\startMPpositionmethod{mpos:sidebar}
    \startMPpositiongraphic{mpos:sidebar}{linecolor,linewidth,distance,alternative}%
        if unknown context_apos : input mp-apos.mpiv ; fi ;
        anch_sidebars_draw (
            \MPp\MPbself,\MPp\MPeself,\MPy\MPbself,\MPy\MPeself,\MPh\MPbself,\MPd\MPeself,
            \MPx{\textanchor},\MPy{\textanchor},\MPw{\textanchor},\MPh{\textanchor},
            \MPvar{alternative},\MPvar{distance},\MPvar{linewidth},\MPvar{linecolor},
            \MPvar{topoffset}, \MPvar{bottomoffset}
        ) ;
    \stopMPpositiongraphic
    \MPpositiongraphic{mpos:sidebar}{}%
\stopMPpositionmethod

%D We now reimplement the margin rules handler defined in
%D \type {core-rul}:
%D
%D \setupmarginrules[level=5]
%D
%D \startmarginrule[1]
%D First we set the level at~5. Next we typeset this first
%D paragraph as a level~1 one. As expected no rule show up.
%D \stopmarginrule
%D
%D \startmarginrule[5]
%D The second paragraph is a level~5 one. As we can see here,
%D the marginal rule gets a width according to its level.
%D \stopmarginrule
%D
%D \startmarginrule[8]
%D It will of course be no surprise that this third paragraph
%D has a even thicker margin rule. This behavior can be
%D overruled by specifying the width explictly.
%D \stopmarginrule

\definesidebar
  [\v!margin]
  [\c!rulethickness=\@@karulethickness,
   \c!distance=\dimexpr\leftmargindistance-\@@karulethickness/2\relax]

\definecomplexorsimple\startmarginrule

\def\simplestartmarginrule
  {\complexstartmarginrule[1]}

\def\complexstartmarginrule[#1]%
  {\bgroup
   \ifnum#1<\@@kalevel\relax
     \let\stopmarginrule\egroup
   \else
     \def\@@kadefaultwidth{#1}%
     \let\stopmarginrule\dostopmarginrule
     \normalexpanded{\startsidebar[\v!margin]}% why expanded
   \fi}

\def\dostopmarginrule
  {\stopsidebar
   \egroup}

\protect \endinput