Hello,

I made an experiment to add alternatives to sidebars (see attachment).

I discovered the sidebar is drawn by anch_sidebars_draw in metapost/context/base/mpiv/mp-apos.mpiv.

I've redefined it locally to add alternatives.

To make fancy, zig zag bars I defined a macro, draw_pattern_bar, that draws a bar repeating a pattern which is a path connecting (0,0) and (1,0).

The macro has 3 parameters (actually it has more params, but the remainder are anch_sidebars_draw params):

- a pattern, which is a path from (0,0) to (1,0) that should have a vertical size = 1; it's a sort of waveform

- a pattern length, by which the pattern is xsized

- a pattern width, by which the pattern is ysized

Then the pattern is rotated along the bar direction (so -90 degrees) and drawn many times until it covers the bar length.

A proposal and a question:

- patternlength and patternwidth could become \setupsidebar params; alternative (=2,3,4...) could be used to specify the pattern (a sort of waveform); currently alternative=0 is a solid bar and alternative=1 is a dashed one

- in case of long patterns (a patternlength spanning more lines of text) the last replica of the pattern should be clipped not to exceed the last line of the sidebarred text (see the last example): how can that be done?

Massi


\definesidebar[zigzag1][rulecolor=black,distance=4pt,rulethickness=1pt,alternative=2]
\definesidebar[zigzag2][rulecolor=red,distance=4pt,rulethickness=1pt,alternative=3]
\definesidebar[wave1][rulecolor=blue,distance=4pt,rulethickness=1pt,alternative=4]
\definesidebar[wave2][rulecolor=cyan,distance=4pt,rulethickness=1pt,alternative=5]
\definesidebar[obliquedashes][rulecolor=darkgreen,distance=4pt,rulethickness=1pt,alternative=6]
\definesidebar[longobliquedashes][rulecolor=orange,distance=10pt,rulethickness=1pt,alternative=7]

\startMPcode
def draw_pattern_bar(expr a, b, pattern, patternlength, patternheight, linewidth, linecolor) =
    begingroup ;
        save p, z, step ;
        pair p, z, step ;
        step := ( ( b - a ) / arclength( a -- b ) ) * patternlength ;
        path z ; z := pattern xscaled patternlength yscaled patternheight rotated (angle(step)) ;
        p := a ;
        forever :
            draw
                z shifted p
                withpen pencircle scaled linewidth
                withcolor linecolor ;
            p := p + step ;
            exitif arclength( a -- p ) > arclength( a -- b ) ;
        endfor ;
    endgroup ;
enddef ;

% from metapost/context/base/mpiv/mp-apos.mpiv
def anch_sidebars_draw (expr p_b_self, p_e_self, y_b_self, y_e_self, h_b_self, d_e_self,
        x, y, w, h, alternative, distance, linewidth, linecolor, topoffset, bottomoffset) =
    % beware, we anchor at (x,y)
    begingroup ;
    if alternative = 1 :
        interim linecap := rounded ;
    else :
        interim linecap := butt ;
    fi ;
    save a, b ; pair a, b ;
    if p_b_self = p_e_self :
        a := (-distance,y_b_self+h_b_self-y) ;
        b := (-distance,y_e_self-d_e_self-y) ;
    elseif RealPageNumber = p_b_self :
        a := (-distance,y_b_self+h_b_self-y) ;
        b := (-distance,0) ;
    elseif RealPageNumber = p_e_self :
        a := (-distance,h) ;
        b := (-distance,y_e_self-d_e_self-y) ;
    else :
        a := (-distance,h) ;
        b := (-distance,0) ;
    fi ;
    a := (xpart a, min(ypart a + topoffset,   h)) ;
    b := (xpart b, max(ypart b - bottomoffset,0)) ;
    if alternative = 2 :
        draw_pattern_bar( a, b, ((0,0)--(0.25,-0.5)--(0.75,0.5)--(1,0)), 2pt, 2pt, linewidth, linecolor ) ;
    elseif alternative = 3 :
        draw_pattern_bar( a, b, ((0,0)--(0.25,-0.5)--(0.75,0.5)--(1,0)), 4pt, 1.5pt, linewidth, linecolor ) ;
    elseif alternative = 4 :
        draw_pattern_bar( a, b, ( ((0,0) .. controls (0,0.5) and (0.5,0.5) .. (0.5,0)) -- ((0.5,0) .. controls (0.5,-0.5) and (1,-0.5) .. (1,0)) ), 6pt, 4pt, linewidth, linecolor ) ;
    elseif alternative = 5 :
        draw_pattern_bar( a, b, ( (0,0) .. controls (0,1) and (1,1) .. (1,0) ), 4pt, 2pt, linewidth, linecolor ) ;
    elseif alternative = 6 :
        draw_pattern_bar( a, b, ( (0,0.5) .. (1,-0.5) ), 4pt, 2pt, linewidth, linecolor ) ;
    elseif alternative = 7 :
        draw_pattern_bar( a, b, ( (0,-0.5) .. (1,0.5) ), 22pt, 5pt, linewidth, linecolor ) ;
    else :
        draw
            a -- b
            if alternative = 1 :
                dashed (withdots scaled (linewidth/2))
            fi
            withpen pencircle scaled linewidth
            withcolor linecolor ;
    fi ;
    endgroup ;
enddef ;
\stopMPcode

\starttext
\startsidebar[zigzag1]\input knuth\relax\stopsidebar\par
\blank
\startsidebar[zigzag2]\input sapolsky\relax\stopsidebar\par
\blank
\startsidebar[wave1]\input knuth\relax\stopsidebar\par
\blank
\startsidebar[wave2]\input sapolsky\relax\stopsidebar\par
\blank
\startsidebar[obliquedashes]\input knuth\relax\stopsidebar\par
\blank
\startsidebar[longobliquedashes]\input sapolsky\relax\stopsidebar\par
\stoptext

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to