font_identifier := "music_special_symbols";

% Here are two types of pianobrackets available, they are absolute compatible,
% so choose this one you prefer (search for '%!!!')

% free pos: 196-255

mode_setup;

font_size 20pt#;

nhh#:=5pt#;
nhw#:=6pt#;

qqs#=.4pt#; %width of lines
qqw#=10pt#; %length of horizontal stroke.

med#:=1/33designsize;
thick#:=1/16designsize;

thinwidth:=0.125pt;

define_pixels(qqw,nhh,nhw);
define_blacker_pixels(med,thick);

qqs:=ceiling(qqs#*hppp); % the way rule widths are calculated

pair sw;
sw = down+left;

path p;
picture save_pic;

pen line_pen;
line_pen:= pencircle scaled qqs;

%%%%%%%
message ("<glissandi-elements>");
%%%%%%%

def glissando (expr theta)=
  x1=-1; y1=0; z2=z1+(.5nhw,0); z3=z1+(nhw+1,0);
  p:= z1{1.25up+right}..{1.25down+right}z2{1.25down+right}..{1.25up+right}z3;
  pickup penrazor scaled (.075nhw+thick) rotated (theta+65);
  draw p rotated theta;
  labels (1,2,3);
enddef;

for slope=1 upto 8:

  beginchar(slope-1, (cosd(10slope))*nhw#,(sind(10slope))*nhw#, 0);
  " glissando element slope "&decimal (10slope)&" degree";
    glissando (10slope);
    save_pic:= currentpicture;
  endchar;

  beginchar(8+slope-1, (cosd(10slope))*nhw#, (sind(10slope))*nhw#, 0);
  " glissando element slope "&decimal (-10slope)&" degree";
    currentpicture:= save_pic reflectedabout (origin, right);
  endchar;

endfor;

%%%%%%%
message ("<guitar chords>");
%%%%%%%

def tabulatur (expr nd)=
  hwidth:=1/5w;
  x1=x2=y2=y3=0; x3=w; y1=-nd;
  fill unitsquare xscaled (w+2thinwidth) yscaled (med+.2pt)
       shifted(-thinwidth,0);
  pickup pencircle scaled max(1,.2pt+blacker);
  for v=1 upto 5 :
    draw (z2--z3) shifted (0,v*-hwidth);
  endfor;

  for v=0 upto 5 :
    draw (z1--z2) shifted (v*hwidth,0);
  endfor;
  labels (1,2,3);
enddef;

shift:=.15nhh;
def Circle (expr nh)=
  x1l=.1w; x2=x4=.5w; x3l=.9w;
  y1=y3=.5nh+shift; y2=.1nh+shift; y4=.9nh+shift;
  penpos1(med, 0);
  penpos2(thinwidth,90);
  penpos3(med,180);
  penpos4(thinwidth, 270);
  penstroke z1e..z2e..z3e..z4e..cycle;
  pickup pencircle; draw z1..z2..z3..z4..cycle;
enddef;

def Cross (expr nh)=
  x1=x3=.2w; x2=x4=.8w;
  y1=y4=.2nh+shift; y2=y3=nh-.2nh+shift;
  pickup pencircle scaled med;
  draw z1--z2; draw z3--z4;
  labels (1,2,3,4);
enddef;

beginchar(16, 3nhw#, 0, 0);" small grid";
  tabulatur (108/25nhh);
endchar;

beginchar(17, 3/2nhw#, 0, 0); " small dot";
  fill fullcircle scaled .54nhh shifted (w,-.36nhh);
endchar;

beginchar(18, 3nhw#, 0, 0); " small bar";
  fill unitsquare xscaled w yscaled .24nhh shifted (0,-.48nhh);
endchar;

beginchar(19, 3/5nhw#, 0, 0); " small circle";
  Circle (.72nhh);
endchar;

beginchar(20, 3/5nhw#, 0, 0); " small cross";
  Cross (.72nhh);
endchar;

% the next seems to be senseless, but it isn't

beginchar(28, 3/5nhw#, 0, 0); " small blank";
endchar;

%%%%%%%
message ("<several lines>");
%%%%%%%

hooklength:=4.5pt;

beginchar(21, 0, 0, 0);" bracket hook up";
  pickup line_pen;
  draw origin--(0, hooklength);
endchar;

beginchar(22, 0, 0, 0);" bracket hook down";
  pickup line_pen;
  draw origin--(0, -hooklength);
endchar;

beginchar(23, nhw#, 0, 0);" upper octaveline";
  pickup line_pen;
  draw (.33w, 1.1nhw)--(.66w, 1.1nhw);
endchar;

beginchar(24, 0, 0, 0);" final hook for upper octaveline";
  pickup line_pen;
  draw (1.1nhw,0)--(1.1nhw, 1.1nhw);
  draw (.33nhw, 1.1nhw)--(1.1nhw, 1.1nhw);
endchar;

beginchar(25, nhw#, 0, 0);" lower octaveline";
  pickup line_pen;
  draw (.33w, 0)--(.66w, 0);
endchar;

beginchar(26, 0, 0, 0);" final hook for lower octaveline";
  pickup line_pen;
  draw (1.1nhw,0)--(1.1nhw, 1.1nhw);
  draw (.33nhw,0)--(1.1nhw, 0);
endchar;

beginchar(27, .75nhw#, 0, 0);" used for \bracket";
  pickup line_pen;
  draw origin--(.5nhw, 0);
endchar;

beginchar(29, qqw#, 0, 0);" line ydim 0pt";
  pickup line_pen;
  draw origin--(w,0);
endchar;

% lines length xdim 10pt, ydim (-).25 to (-)5pt

for j=1 upto 20:
  beginchar(29+j, qqw#, 0, 0); " line ydim "&decimal .25j&"pt";
    pickup line_pen;
    draw origin--(w, .25pt*j);
    save_pic:= currentpicture;
  endchar;

  beginchar(49+j, qqw#, 0, 0); " line ydim -"&decimal .25j&"pt";
    currentpicture:= save_pic reflectedabout (origin, right);
  endchar;
endfor;

%%%%%%%
message ("<circles>");
%%%%%%%

for n:=1 upto 14:
  beginchar(70+n-1, 0, 0, 0);
  " fullcircle diameter "&decimal ((n+2)*2)&" pt";
    diameter:=(n+2)*2pt;
    pickup line_pen;
    draw fullcircle scaled diameter;
  endchar;
endfor;

%%%%%%%
message ("<(de-)crescendi>");
%%%%%%%

widthstep:=nhw;
crescendowidth:=.5nhh; % \Internote

for n:=1 upto 32:
  beginchar(84+n-1, 0, 3pt#, 3pt#);
  " crescendo length "&decimal n&" noteheadwidths";
    pickup line_pen;
    draw (n*widthstep, crescendowidth+.0075n*crescendowidth)--(0,0);
    addto currentpicture also currentpicture reflectedabout (origin,right);
    save_pic:= currentpicture;
  endchar;

  beginchar(116+n-1, 0, 3pt#, 3pt#);
  " decrescendo length "&decimal n&" noteheadwidths";
    currentpicture:= save_pic reflectedabout(origin, up) shifted (n*widthstep,0);
  endchar;
endfor;

%%%%%%%
message ("<piano brackets>");
%%%%%%%

% lastbrace is adjustable (originally 99)

firstbrace:=20;  % smallest brace is 2*20 pt (\simeq 14mm)
lastbrace:=67;   % largest brace is 2*67 pt (\simeq 47mm)

%%% from Stanislav Kneifl

def drawpianobracket (expr height) =
  u# := 1nhh#/5pt# * 1pt#;
  v# := (height/40) * 1pt#;
  define_pixels (u, v);
%  beginchar (height-firstbrace, 1.25nhh#, 0, 0);
  beginchar (height-firstbrace+148, 0, 0, 0);
    z1 = origin;
    z2 = (4.7u, 7v);
    z3 = (3.6u, 16.5v);
    z4 = (1.9u, 25v);
    z5 = (2.28u, 32v);
    z6 = (w, 40v);
    penpos1 (max(1,0.1u), 125);
    penpos2 (1.2u, 180);
    penpos3 (3.2u, 195);
    penpos4 (2.5u, 180);
    penpos5 (1.14u, 180);
    penpos6 (max(2,0.3u), 150);
    penstroke z1e{dir 35}..z2e..z3e..z4e..z5e..{dir 60}z6e;
    currenttransform := identity reflectedabout ((0,0),(1,0));
    penstroke z1e{dir 35}..z2e..z3e..z4e..z5e..{dir 60}z6e;
    currenttransform := identity;
% inserted
    currentpicture:=currentpicture shifted (round(-1.75nhh), 0);
%
  endchar;
enddef;

def bigbrace(expr v) =
  beginchar (v-firstbrace+148, 0, 0, 0);
  " brace vsize "&decimal round(2v*pt#)&"pt";
    vwidth:=v*pt;
    hwidth:=max(.1vwidth,3.25pt);
    hwidth:=min(hwidth,6.5pt);
    actwidth:=.035vwidth;
    x1=.25nhw;  y1=vwidth;
    x2l=-.8hwidth; y2=2/3vwidth;
    x3r=.2hwidth; y3=1/3vwidth;
    x4=-hwidth; y4=0;
    penpos1(max(1,.25pt),-35);
    penpos2(1.5med+actwidth,40);
    penpos3(1.75med+actwidth,45);
    penpos4(max(1,.2pt),-90);
    penstroke z1e..z2e..z3e..{left+sw}z4e;
    currentpen:= pencircle; draw z1..z2..z3..{left+sw}z4;
    addto currentpicture also currentpicture reflectedabout (origin, right);
    penlabels(1,2,3,4);
    currentpicture:= currentpicture shifted (round(-.75nhh), 0);
  endchar;
enddef;

%!!! comment the line 'bigbrace' and uncomment the line 'drawpianobracket'

for v=firstbrace upto lastbrace :
  bigbrace (v);
%  drawpianobracket (v);
endfor;

end
