% feta-klef.mf --  implement Clefs -*-Fundamental-*-
% 
% part of LilyPond's pretty-but-neat music font
%
% source file of the Feta (not the Font-En-Tja) music font
% 
% (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>,
% Jan Nieuwenhuizen <janneke@gnu.org>,
% Juergen Reuter <reuterj@ira.uka.de>


fet_begingroup("clefs");

%
% [Ross] says that clefs take 1 staff_space space on the left and right
%
def set_horizontal_spacing =
	save left_space ,right_space;
	left_space# = 0;
	right_space# = 0;
enddef;


% [Wanske] says the bulbs should be positioned about 1/4 right of the
% "arrow"
def draw_c_clef (expr reduction) = 
	save hair, norm, reduced_il, right_edge;
	reduced_il#=staff_space#*reduction;
	norm#:=2/3reduced_il#;
	hair#:=1/6norm#;

	set_horizontal_spacing;
	right_edge# = 15/4norm#+2hair#;

	set_char_box (left_space#, right_edge# + right_space#, 
		2 reduced_il#, 2 reduced_il#);
	define_pixels (hair,norm,reduced_il, right_edge);

	draw_block ((0,-d), (3/4norm+1/2hair,h));
	draw_block ((3/4norm+2hair,-d), 
		(3/4norm+7/2hair,h));
	
	save xoff;
	xoff=3/4norm+7/2hair;
	penpos1(hair,-90);
	z1l=(xoff+norm+hair,h);
	penpos2(norm-3/2hair,180);
	z2l=(right_edge,h/2);
	penpos3(hair,90);
	z3=(((right_edge -xoff)/2)+xoff,2hair);

	penpos4(hair,0);
	z4=(xoff+1/2norm+1/2hair,reduced_il-hair);
	
	penpos5(4hair,0);
	z5=(xoff+5/4hair,0);

	penpos6(norm-hair,90);
	z6=(xoff+3/4norm,0);

	penpos7(hair,-90);
	z7r=(x1,-d);

	save t; t=0.833;
	save p; path p;
	p = z5l..z4l{up}..z4r{down}..z3r{right}..tension t..z2r{up}
		..tension t..z1r{left}..z1l{right}..tension t..z2l{down}
		..z3l{left}..z6r..z5r{down};
	pickup pencircle scaled 1pt#;
	filldraw p..(reverse p yscaled -1)..cycle;
	penlabels (1,2,3,4,5,6);

	% ugh, should be bulb, not flare?
	draw_flare(z1l,180,90,hair,norm-1/2hair);
	draw_flare(z7r,180,-90,hair,norm-1/2hair);
	enddef;

fet_beginchar ("C clef", "C", "altoclef")
	if test = 1:
		draw_staff (-2,2, 0.0);
	fi;
	draw_c_clef (1.0);
fet_endchar;

fet_beginchar ("C clef", "C_change", "caltoclef")
	if test = 1:
		draw_staff (-2,2, 0.0);
	fi;
	draw_c_clef (.8);
fet_endchar;

%
% Inspired by Baerenreiter and Breitkopf
% 
% FIXME: dims
% FIXME: right vertical tangent seems to be lower than the F-line
% FIXME: bulb curve smoothly into "long curve" on the  inside
%
%
% [Wanske] says that the extreme x point should be exactly between 
% the dots, but her picture shows that the extreme is ~ 0.2 ss lower

def draw_bass_clef(expr exact_center, reduction) = 
	save reduced_il, left_tilt, left_thick, ball_to_right;
	reduced_il# = staff_space# * reduction;
	
	set_horizontal_spacing;
	ball_to_right# = 2.1 reduced_il#;
	set_char_box(left_space# +
		- xpart exact_center,
		right_space# +
		xpart exact_center + ball_to_right# + 7/12 reduced_il#, 
		- ypart exact_center + 2.5 reduced_il#, 
		ypart exact_center +reduced_il#);

	define_pixels(reduced_il, ball_to_right);
	left_tilt = 5;
	left_thick = .25  reduced_il;

	x1r - x1l = left_thick;
	z1l = (hround_pixels(xpart exact_center), 
		vround_pixels(ypart exact_center));

	y2 = reduced_il;

	x3l - x1l =  ball_to_right;
	x2 = .5 [x1,x3];
	x3l - x3r = .48 reduced_il;
	y3l = -0.05 staff_space;
	x4 = x1l - stafflinethickness;
	y4 = -2.2  reduced_il;
	z5 = (x3l +  1/3 reduced_il, .5 reduced_il);

	penpos1(whatever, left_tilt);
	penpos2(1.2 stafflinethickness, -90);
	penpos3(whatever,  185);
	penpos4(stafflinethickness, 135);

	draw_bulb(1, z1r,  z1l, .45 reduced_il, 1.0);


	fill z1r{up} .. z2r{right} .. tension 1.0 .. z3r{down}  .. {curl 0} 
		simple_serif(z4r, z4l, 90) {curl 0}
		.. z3l{up} .. tension 0.9 .. z2l{left} 
		.. z1l{dir (-90 + left_tilt)} -- cycle;
	labels(2,4);
	penlabels(1,2,3,4);

	save dot_diam;
	2 dot_diam = reduction* (staff_space - stafflinethickness);
	pickup pencircle scaled dot_diam;
	draw z5;
	draw z5 yscaled -1;
enddef;



fet_beginchar("F clef ", "F", "bassclef")
	if test = 1:
		draw_staff(-3,1, 0.0);
	fi;
	draw_bass_clef((.5 staff_space#, 0), 1.0);
fet_endchar;
fet_beginchar("F clef (reduced)", "F_change", "cbassclef")
	draw_bass_clef((.4 staff_space#, 0),0.8);
fet_endchar;



%
% Inspired by Baerenreiter
% 
% FIXME bulb should curve (see bass clef)
% FIXME start (inside) should be little thinner
% FIXME parametrise.
%

% Beste lezers, kijk,
%
% Een bolletje  hebben we bij toeval allemaal wel eens getekend, maar begint u
% toch eenvoudig.   Eerst een eenvoudig kruis of herstellingsteken
% en via de dubbelslag naar een voorzichtig vlaggetje, en heb geduld!
% Ikzelf heb bijvoorbeeld over mijn eerste gave G-sleutel
% 35 kilobyte metafont, 12 patchlevels, 0 vriendinnen en 45 dagen gedaan
%
%  -- vrij naar Van Kooten & De Bie
%

def draw_gclef (expr exact_center, reduction)=
	save reduced_il, downstroke_dir, downstroke_angle, hair, center;
	save breapth_factor, inner_thick_end, thinness, thickness, thinnib;
	save inner_start_angle, thinness, thinpen;
	reduced_il# = staff_space# * reduction;
	define_pixels(reduced_il);
	pair downstroke_dir, center;

	center := (hround_pixels(xpart exact_center), 
		vround_pixels(ypart exact_center));

	hair =  .3 stafflinethickness;
	thinness = 1.3 stafflinethickness;
	downstroke_dir = (14, -75);
	breapth_factor = 11/7;
	inner_thick_end = 45;
	inner_start_angle = downstroke_angle - 43;
	thickness = .4 reduced_il - hair;

	thinnib = thinness - hair;
	thinpen = thinness;
	set_horizontal_spacing;
	
	set_char_box(
		left_space# +
		-xpart exact_center + 1.0 * breapth_factor* reduced_il#, 
		right_space# +
		xpart exact_center + .66 breapth_factor* reduced_il#,
		-ypart exact_center + 3 * reduced_il#,
		ypart exact_center + 5 * reduced_il#);
	
	pickup pencircle scaled hair;
	downstroke_angle = angle downstroke_dir;

	z1 = center + whatever * dir (inner_start_angle);
	x1 = xpart center -.28 reduced_il;
	
	top z2r = center + (0,reduced_il + stafflinethickness/2);
	
	x4 = xpart center - .1 reduced_il;
	bot y4r = -(reduced_il + .5 stafflinethickness);

	z3 = (z4 - center) rotated inner_thick_end + center;
	
	z5r = (- breapth_factor, .37)* reduced_il + center;
	penpos5(thickness, 135);

	z6 = center + whatever * downstroke_dir;
	y6 = ypart center + 2 reduced_il;

	z7l - z6 = whatever *(z5- z6) ;
	y7l = 3.5 reduced_il;
	z8r = .4 [z9r, z7r] + 1.5 stafflinethickness * dir 52;

	x9 = .7 [x10, x7r];
	top y9l = 5 reduced_il;

	y10 = 3. reduced_il;
	y11 = -11/7 reduced_il;

	y12 = ypart center - 18.5/7 reduced_il;
	x12 = x11 - 5 /7 reduced_il;	

	z13 = z12 + .6 reduced_il*(-1,1);

	(z10r - z10l) dotprod (unitvector downstroke_dir rotated 90) = 
		thinnib;

	center - z10= whatever * downstroke_dir;
	center - z11 =  whatever * downstroke_dir;
	center - z14 = .8 (center - z11);
	penpos1(thinnib, inner_start_angle);
	penpos2(thickness, 90);
	penpos3(thinnib, -90 + inner_thick_end);
	penpos4(thinnib, -90);


	penpos7(thickness, 135);
	penpos8(1.5 thinnib, - 70 + angle downstroke_dir);
	penpos9(1.4 thickness, -80);  % ugh
	penpos10(whatever, downstroke_angle + 10);
	penpos11(thinnib, downstroke_angle + 90);
	penpos14(thinnib, downstroke_angle + 90);
	penpos12(thinnib, -90);
	penpos13(3 thinnib, 180);

	filldraw z2l{right}   .. z3l.. z4l{left} .. z5l{up}  .. z7l{up} 
		%.. z8l	
		.. tension 1.2 
		.. z9l & z9l ..
		{downstroke_dir}z10l --- z11l -- z11r --- z10r{- downstroke_dir} 
		.. tension .8 
		.. z9r & z9r{dir (downstroke_angle+ 40)} % ugh
		%.. z8r
		.. z7r{down} .. z5r{down} .. z4r{right}
		.. z3r .. z2r{left} .. 
		tension .95 .. 
		z1r -- z1l 
		.. tension 0.85 ..cycle;

	filldraw simple_serif(z1r, z1l, 90)  -- cycle;

	filldraw z12r{left} .. z13r{up} -- z13l{down} .. z12l{right} .. cycle;

	draw_bulb(-1,  z13l, lft z13r, 6/14 reduced_il, 1.0);

	pickup pencircle scaled (thinpen);
	draw z10 --- z14 .. z11  .. tension 0.85 ..  z12{left};

	penlabels(range 1 thru 15);
enddef;


fet_beginchar("G clef", "G", "trebleclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_gclef((1.7 staff_space#,0), 1.0);
fet_endchar;
fet_beginchar("G clef", "G_change", "ctrebleclef")
	draw_gclef((1.3 staff_space#,0), .8);
fet_endchar;

%%%%%%%%
%
%
%
% Editio Vaticana
%
%
%
def draw_vaticana_do_clef(expr exact_center, reduction) = 
	save reduced_il;

	reduced_il# = staff_space# * reduction;
	
	set_char_box(0.4reduced_il#, 0.4reduced_il#, 0, reduced_il#);

	define_pixels(reduced_il);

	pickup pencircle xscaled stafflinethickness yscaled 0.6reduced_il;

	save za, zb, zc, zd, ze, zf;
	pair za, zb, zc, zd, ze, zf;

	za = exact_center + (-0.0reduced_il, -.45reduced_il);
	zb = exact_center + (-0.2reduced_il, -.50reduced_il);
	zc = exact_center + (-0.4reduced_il, -.25reduced_il);
	zd = exact_center + (-0.4reduced_il, +.25reduced_il);
	ze = exact_center + (-0.2reduced_il, +.50reduced_il);
	zf = exact_center + (-0.0reduced_il, +.45reduced_il);
	draw za .. zb .. zc --	% lower punctum
	     zd .. ze .. zf;	% upper punctum
enddef;


fet_beginchar("Ed. Vat. do clef", "vaticana_do", "vatdoclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_vaticana_do_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("Ed. Vat. do clef", "vaticana_do_change", "vatcdoclef")
	draw_vaticana_do_clef((0,0), 1.0); % no reduction
fet_endchar;


def draw_vaticana_fa_clef(expr exact_center, reduction) = 

	save reduced_il, z;
	reduced_il# = staff_space# * reduction;
	define_pixels(reduced_il);

	save za, zb, zc, zd, ze;
	pair za, zb, zc, zd, ze;

	%stem
	pickup pencircle scaled stafflinethickness;
	xpart za = xpart zb =
		xpart exact_center + 0.4reduced_il;
	ypart za = ypart exact_center = ypart zb + 1.5reduced_il;
	draw za .. zb;

	%left-handed punctum
	pickup pencircle xscaled stafflinethickness yscaled 0.5reduced_il;
	zc = exact_center +
		(+0.4reduced_il, -0.05reduced_il);
	zd = exact_center +
		(+0.2reduced_il, +0.05reduced_il);
	ze = exact_center +
		(+0.0reduced_il, +0.00reduced_il);
	draw zc .. zd .. ze;

	%right-handed puncta as in do clef
	draw_vaticana_do_clef(
		exact_center +
		(+0.8reduced_il + 1.25stafflinethickness, 0),
		reduction);

	set_char_box(0.5reduced_il#, 1.5reduced_il#, 0, reduced_il#);
enddef;


fet_beginchar("Ed. Vat. fa clef", "vaticana_fa", "vatfaclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_vaticana_fa_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("Ed. Vat. fa clef", "vaticana_fa_change", "vatcfaclef")
	draw_vaticana_fa_clef((0,0), 1.0); % no reduction
fet_endchar;

%%%%%%%%
%
%
%
% Editio Medicaea
%
%
%
def draw_medicaea_do_clef(expr exact_center, reduction) = 
	save reduced_il, reduced_slt;
	reduced_il# = staff_space# * reduction;
	reduced_slt# = stafflinethickness# * reduction;
	define_pixels(reduced_il);
	define_pixels(reduced_slt);

	save flag_height;
	flag_height# = 0.5 reduced_il#;
	define_pixels(flag_height);

	%lower flag
	save za, zb;
	pair za, zb;
	pickup pencircle xscaled reduced_slt yscaled flag_height;
	xpart exact_center
		= xpart za + 0.5 reduced_il
		= xpart zb - 0.5 reduced_il;
	ypart za = ypart exact_center - 0.5 flag_height
			- 0.5 (staff_space - reduced_il);
	ypart zb = ypart za - reduced_il + flag_height;
	draw za .. zb;

	%upper flag
	save za, zb;
	pair za, zb;
	pickup pencircle xscaled reduced_slt yscaled flag_height;
	xpart exact_center
		= xpart za + 0.5 reduced_il
		= xpart zb - 0.5 reduced_il;
	ypart za = ypart exact_center - 0.5 flag_height
			- 0.5 (staff_space - reduced_il)
			+ staff_space;
	ypart zb = ypart za - reduced_il + flag_height;
	draw za .. zb;

	%stem
	save za, zb;
	pair za, zb;
	pickup pencircle scaled reduced_slt;
	xpart za = xpart zb = xpart exact_center - 0.5 reduced_il;
	ypart exact_center
		= ypart za + 1.5 reduced_il
		= ypart zb - 1.5 reduced_il;
	draw za .. zb;

	set_char_box(0.5reduced_il#, 1.5reduced_il#,
		     1.5reduced_il#, 1.5reduced_il#);
enddef;


fet_beginchar("Ed. Med. do clef", "medicaea_do", "meddoclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_medicaea_do_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("Ed. Med. do clef", "medicaea_do_change", "cmeddoclef")
	draw_medicaea_do_clef((0,0), .8);
fet_endchar;


def draw_medicaea_fa_clef(expr exact_center, reduction) = 
	% inspired by Regensburger Edition of Medicaea (1885/86), in:
	% MGG, volume 2, col. 1327 ("Choralreform"), fig. 2.

	save reduced_il, reduced_slt;
	reduced_il# = staff_space# * reduction;
	reduced_slt# = stafflinethickness# * reduction;
	define_pixels(reduced_il);
	define_pixels(reduced_slt);

	save za, zb, zc, zd, ze;
	pair za, zb, zc, zd, ze;

	%stem
	pickup pencircle scaled stafflinethickness;
	xpart za = xpart zb =
		xpart exact_center + 0.4reduced_il;
	ypart za = ypart exact_center = ypart zb + 1.5reduced_il;
	draw za -- zb;

	%left-handed punctum
	pickup pencircle xscaled reduced_slt yscaled reduced_il;
	zc = exact_center +
		(+0.4reduced_il, 0);
	zd = exact_center +
		(+0.0reduced_il, 0);
	draw zc -- zd;

	%right-handed puncta as in do clef
	draw_medicaea_do_clef(
		exact_center +
		(+1.1reduced_il + 1.25stafflinethickness, 0),
		reduction);

	set_char_box(reduced_il#/2, 2.6reduced_il#, 0, reduced_il#);
enddef;


fet_beginchar("Ed. Med. fa clef", "medicaea_fa", "medfaclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_medicaea_fa_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("Ed. Med. fa clef", "medicaea_fa_change", "cmedfaclef")
	draw_medicaea_fa_clef((0,0), .8);
fet_endchar;


%%%%%%%%
%
%
%
% Mensural Notation
%
%
%
brevis_wid# := 2 staff_space#;

def draw_brevis(expr exact_center, reduction, small_width, small_height) =
	%
	% inspired by function draw_brevis of
	% feta-bolletjes.mf
	%
	save reduced_slt, reduced_nht;
	save stem_width, head_width;
	save serif_size, serif_protrude;
	save holeheight, beamheight;

	reduced_slt# = stafflinethickness# * reduction;
	reduced_nht# = noteheight# * reduction;
	stem_width# = 1.4 reduced_slt#;
	if small_width:
		head_width# = 0.5brevis_wid# * reduction;
	else:
		head_width# = brevis_wid# * reduction;
	fi;
	if small_height:
		serif_size# = 0.64 reduced_slt#;
		serif_protrude# = 0.96 serif_size#;
		holeheight# = 3 reduced_slt#;
		beamheight# = 0.32(reduced_nht# - holeheight#);
	else:
		serif_size# = 1.0 reduced_slt#;
		serif_protrude# = 1.5 serif_size#;
		holeheight# = 3 reduced_slt#;
		beamheight# = 0.5(reduced_nht# - holeheight#);
	fi;
	define_pixels(reduced_slt);
	define_pixels(stem_width);
	define_pixels(head_width);
	define_pixels(serif_size);
	define_pixels(serif_protrude);
	define_pixels(beamheight);

	penpos1(stem_width, 0);
	penpos2(stem_width, 0);
	penpos3(beamheight, 90);
	penpos4(beamheight, 90);
	penpos5(stem_width, 180);

	z1l = exact_center;
	z2l = z1l + (0, -reduced_slt/2);
	z3r = z2r + serif_size*(1,-1);
	y4r = y3r;
	x4l = x1l + head_width/2;
	z5l = z3l + (-serif_size, -serif_protrude);

	penlabels(1,2,3,4,5);
	fill z1r -- z1l -- z5r{down} .. z5l{up} .. z3l{right}
		-- z4l -- z4r -- z3r{left} .. z2r{up} -- cycle;
enddef;


def draw_mensural_i_c_clef(expr exact_center, reduction) = 
	draw_brevis(exact_center, reduction, false, false);

	save reduced_il, reduced_slt;
	save stem_width;

	reduced_il# = staff_space# * reduction;
	reduced_slt# = stafflinethickness# * reduction;
	stem_width# = 1.4 reduced_slt#;

	define_pixels(reduced_il);
	define_pixels(reduced_slt);
	define_pixels(stem_width);

	penpos6(stem_width, 0);
	penpos7(stem_width, 0);
	z6l = exact_center;
	z7l = z6l + (0, -2reduced_il);
	fill z6l -- z7l -- z7r -- z6r -- cycle;
	penpos8(stem_width, 0);
	penpos9(stem_width, 0);
	z8l = z6l + (-3reduced_slt, 0);
	z9l = z8l + (0, -2reduced_il);
	fill z8l -- z9l -- z9r -- z8r -- cycle;

	addto currentpicture also currentpicture
		yscaled -1 shifted (0, 2*(ypart exact_center));
	addto currentpicture also currentpicture 
		xscaled -1 shifted (2x4l,0);

	set_char_box(0, 2head_width#,
		     6reduced_slt#*reduction, 6reduced_slt#*reduction);
enddef;


fet_beginchar("mensural c clef", "mensural1_c", "mens1cclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_mensural_i_c_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("mensural c clef", "mensural1_c_change", "cmens1cclef")
	draw_mensural_i_c_clef((1.3 staff_space#,0), .8);
fet_endchar;


def draw_mensural_ii_c_clef(expr exact_center, reduction) = 
	draw_brevis(exact_center, reduction, true, true);

	save reduced_il, reduced_slt;
	save stem_width, interline;

	reduced_il# = staff_space# * reduction;
	reduced_slt# = stafflinethickness# * reduction;
	stem_width# = 1.4 reduced_slt#;
	interline# = staff_space#;

	define_pixels(reduced_il);
	define_pixels(reduced_slt);
	define_pixels(stem_width);
	define_pixels(interline);

	penpos6(stem_width, 0);
	penpos7(stem_width, 0);
	z6l = exact_center + (0, -interline/2);
	z7l = z6l + (0, -1.5reduced_il);
	fill z6l -- z7l -- z7r -- z6r -- cycle;

	addto currentpicture also currentpicture
		yscaled -1 shifted (0, 2*(ypart exact_center) - interline);
	addto currentpicture also currentpicture
		yscaled -1 shifted (0, 4*(ypart exact_center));
	addto currentpicture also currentpicture 
		xscaled -1 shifted (2x4l,0);

	set_char_box(0, 2head_width#,
		     noteheight#*4*reduction, noteheight#*4*reduction);
enddef;


fet_beginchar("mensural c clef", "mensural2_c", "mens2cclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_mensural_ii_c_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("mensural c clef", "mensural2_c_change", "cmens2cclef")
	draw_mensural_ii_c_clef((1.3 staff_space#,0), .8);
fet_endchar;


def draw_mensural_iii_c_clef(expr exact_center, reduction) =
	% inspired by Ockeghem, "Missa Prolationum", in: MGG, volume
	% 9, table 94.
	draw_mensural_ii_c_clef(exact_center, reduction);

	addto currentpicture also currentpicture
		shifted (0, -interline);

	set_char_box(0, 2head_width#,
		     noteheight#*6*reduction, noteheight#*6*reduction);
enddef;


fet_beginchar("mensural c clef", "mensural3_c", "mens3cclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_mensural_iii_c_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("mensural c clef", "mensural3_c_change", "cmens3cclef")
	draw_mensural_iii_c_clef((0,0), .8);
fet_endchar;

def draw_diamond(expr exact_center, reduction) =
	save stem_width, reduced_il, reduced_nht, holeheight, beamheight;
	save rh_height, rh_width;

	stem_width# = 1.4 reduced_slt#;
	reduced_il# = staff_space# * reduction;
	reduced_nht# = noteheight# * reduction;
	holeheight# = 3 reduced_slt#;
	beamheight# = 0.4(reduced_nht# - holeheight#);

	rh_height# = 1.2reduced_il#;
	rh_width# / rh_height# = 0.58; % tan(30)

	define_pixels(beamheight);
	define_pixels(stem_width);
	define_pixels(rh_height);
	define_pixels(rh_width);

	pickup pencircle
		xscaled beamheight
		yscaled stem_width
		rotated 45;

	draw
		exact_center + (-rh_width/2, 0) --
		exact_center + (0, rh_height/2) --
		exact_center + (+rh_width/2, 0) --
		exact_center + (0, -rh_height/2) --
		cycle;
enddef;

def draw_mensural_i_f_clef(expr exact_center, reduction) =
	%
	% inspired by Gaspar van Weerbeke, "Virgo Maria" (1502), in:
	% MGG, volume 9, col. 653 ("Motette"), fig. 3.; also by
	% Andr'e Campra, "Entr'ee des s'er'enades" (1710), in: MGG,
	% volume 2, col. 1649 ("Contredanse"), fig. 2.
	%
	draw_brevis(exact_center, reduction, true, false);

	save reduced_il, reduced_slt;
	save stem_width, interline;

	reduced_il# = staff_space# * reduction;
	reduced_slt# = stafflinethickness# * reduction;
	stem_width# = 1.4 reduced_slt#;
	interline# = staff_space#;

	define_pixels(reduced_il);
	define_pixels(reduced_slt);
	define_pixels(stem_width);
	define_pixels(interline);

	addto currentpicture also currentpicture
		yscaled -1 shifted (0, 2*(ypart exact_center));
	addto currentpicture also currentpicture 
		xscaled -1 shifted (2x4l,0);

	penpos6(stem_width, 0);
	penpos7(stem_width, 0);
	z6r = exact_center + (2x4l, 0);
	z7r = z6r + (0, -4reduced_il);
	fill z6l -- z7l -- z7r -- z6r -- cycle;

	draw_diamond(exact_center +
		     (1.6interline*reduction, interline/2), reduction);
	%% some editions put a stem on top of the upper note head:
	% penpos8(stem_width, 0);
	% penpos9(stem_width, 0);
	% z8l = exact_center + (1.6interline*reduction, interline*reduction);
	% z9l = z8l + (0, 1.5interline*reduction);
	% fill z8l -- z9l -- z9r -- z8r -- cycle;

	draw_diamond(exact_center +
		     (1.6interline*reduction, -interline/2), reduction);
	penpos10(stem_width, 0);
	penpos11(stem_width, 0);
	z10r = exact_center + (1.6interline*reduction, -interline*reduction);
	z11r = z10r + (0, -3.5interline*reduction);
	fill z10l -- z11l -- z11r -- z10r -- cycle;

	set_char_box(0, 3head_width#,
		     3.5noteheight#*reduction, 1.5noteheight#*reduction);
enddef;


fet_beginchar("mensural f clef", "mensural1_f", "mens1fclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_mensural_i_f_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("mensural f clef", "mensural1_f_change", "cmens1fclef")
	draw_mensural_i_f_clef((0,0), .8);
fet_endchar;


def draw_mensural_ii_f_clef(expr exact_center, reduction) =
	%
	% inspired by Philippe le Duc, "Dite Signori" (1590), in: MGG,
	% volume 3, col. 848 ("Duc"); also by John Dowland, "The First
	% Booke of Songes" (1597), in: MGG, volume 3, col. 721
	% ("Dowland"), fig. 3.

	save reduced_slt, stem_width, reduced_nht, holeheight, beamheight;

	reduced_slt# = stafflinethickness# * reduction;
	stem_width# = 1.4 reduced_slt#;
	reduced_nht# = noteheight# * reduction;
	holeheight# = 3 reduced_slt#;
	beamheight# = 0.4(reduced_nht# - holeheight#);

	define_pixels(beamheight, stem_width, staff_space);

	pickup pencircle
		xscaled beamheight
		yscaled stem_width
		rotated 45;

	draw
		exact_center + (1.0 staff_space, 0.6 staff_space) --
		exact_center + (1.25 staff_space, 0.4 staff_space);

	draw
		exact_center + (1.0 staff_space, -0.4 staff_space) --
		exact_center + (1.25 staff_space, -0.6 staff_space);

	draw	halfcircle scaled 1.2 staff_space rotated -90
		shifted (-exact_center + (+0.1 staff_space, 0));

	set_char_box(0, 3staff_space#, 3staff_space#, 0);
enddef;

fet_beginchar("mensural f clef", "mensural2_f", "mens2fclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_mensural_ii_f_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("mensural f clef", "mensural2_f_change", "cmens2fclef")
	draw_mensural_ii_f_clef((0,0), .8);
fet_endchar;


def draw_mensural_g_clef(expr exact_center, reduction) =
	%
	% inspired by Francisco Guerrero, "Lib. 1.  Missarum" (1566),
	% in: MGG, volume 3, col. 858 ("Ducis"); also by Stefano
	% Fabri, "Quam speciosa veteranis" (1611), in: MGG, volume 3,
	% col. 1698 ("Fabri"); also by Philippus Dulichius,
	% "Fasciculus novus ..." (1598), in: MGG, volume 3, col. 919
	% ("Dulichius"), fig. 1; also by Noe Faignient, "Ic sal de
	% Heer myn God gebenedye" (1568), in: MGG, volume 3, col. 1735
	% ("Faignient").
	%
	% Metafont code partially inspired by Schwabacher 'G' of yswab
	% font.

	save reduced_il, stem_width, height, width, apex_o, hair;

	reduced_il#=staff_space#*reduction;

	set_char_box(0.5reduced_il#, 1.5reduced_il#,
		     1.5reduced_il#, 1.5reduced_il#);

	stem_width# = 0.17 reduced_il#;
	height# = 1.5 reduced_il#;
	width# = 1.13 reduced_il#;
	apex_o# = 0.02 reduced_il#;
	hair# =  3 stafflinethickness#;
	define_pixels(reduced_il, stem_width, height, width, apex_o, hair);

	penpos1(2 stem_width, -142);
	z1l = (0.715 width, 0.742 height);
	penpos1'(hair, -90);
	z1'l = z1r;
	penpos2(1.179 stem_width, -142);
	z2l = (width, 0.466 height);
	penpos3(hair, 77);
	z3 = (0.764 width, 0.067 height); 
	z4 = (0.59 width, -apex_o);
	penpos5(1.179 stem_width, 32);
	z5l=(0, 0.457 height);
	penpos6(hair, -56.5);
	z7 = (x4 - 0.843 stem_width, height + apex_o);
	z7 - z6l = whatever * dir33.5;
	penpos8(1.286 stem_width, -130);
	z8r = (0.715 width, 0.742 height) + (-apex_o, apex_o);
	z6r - z8r = whatever * (z7 - z8l);
	filldraw
		z1'r{dir45} .. z2r{down} .. z3r{dir207} .. z5r{up} ..
		z6r{z7-z6l} & z6r -- z8r -- z8l -- z7 --- z6l ...
		z5l{down} .. z4{right} .. z3l{dir27} .. z2l{up} ..
		z1l{1/3[z6l,z7]-z1l} & z1l -- z1r -- z1'r & cycle
		shifted (-exact_center + (0, -0.75reduced_il));

	penpos9(stem_width, 0);
	x9r = x4; y9 = 0.3 height;

	pickup pencircle
		scaled stem_width
		rotated 45;
	draw	z9 -- (z9 + (0, -0.4reduced_il))
		shifted (-exact_center + (0, -0.75reduced_il));

	pickup pencircle
		xscaled stem_width
		yscaled hair
		rotated 30;

	draw halfcircle
		scaled 0.5 reduced_il
		rotated -90
		shifted z8
		shifted (0, 0.25reduced_il)
		shifted (-exact_center + (0, -0.75reduced_il));

	draw halfcircle
		scaled 0.4 reduced_il
		rotated 90
		shifted (z8 + (0, 0.45 reduced_il))
		shifted (0, 0.25reduced_il)
		shifted (-exact_center + (0, -0.75reduced_il));
enddef;


fet_beginchar("mensural g clef", "mensural_g", "mensgclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_mensural_g_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("mensural g clef", "mensural_g_change", "cmensgclef")
	draw_mensural_g_clef((0,0), .8);
fet_endchar;



%%%%%%%%
%
%
%
% Hufnagel
%
%
%
def draw_hufnagel_do_clef(expr exact_center, reduction) =
	%
	% inspired by Graduale of Friedrich Zollner (1442), in: MGG,
	% volume 9, col. 1413 ("Neustift"), fig. 1.
	%
	save reduced_il;

	reduced_il# = staff_space# * reduction;

	define_pixels(reduced_il);

	pickup pencircle
		xscaled (0.60reduced_il)
		yscaled (0.10reduced_il)
		rotated 40;
	save za, zb, zc, zd, ze, zf;
	pair za, zb, zc, zd, ze, zf;

	za = exact_center + (+0.30reduced_il, +.45reduced_il);
	zb = exact_center + (+0.20reduced_il, +.45reduced_il);
	zc = exact_center + (-0.10reduced_il, +.60reduced_il);
	zd = exact_center + (-0.40reduced_il, +.45reduced_il);
	ze = exact_center + (-0.40reduced_il, -.45reduced_il);
	zf = exact_center + (-0.20reduced_il, -.55reduced_il);
	draw za .. zb .. zc -- zd -- ze -- zf;

	set_char_box(0.5reduced_il#, 0.5reduced_il#, 0, reduced_il#);
enddef;


fet_beginchar("Hufnagel do clef", "hufnagel_do", "hufnageldoclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_hufnagel_do_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("Hufnagel do clef", "hufnagel_do_change", "chufnageldoclef")
	draw_hufnagel_do_clef((1.3 staff_space#,0), .8);
fet_endchar;


def draw_hufnagel_fa_clef(expr exact_center, reduction) =
	%
	% inspired by Bamberger Manuscript (15th century), in:
	% MGG, volume 2, table 59.
	%
	save reduced_il;

	reduced_il# = staff_space# * reduction;

	define_pixels(reduced_il);

	pickup pencircle
		xscaled (0.60reduced_il)
		yscaled (0.10reduced_il)
		rotated 40;
	save za, zb, zc, zd, ze, zf;
	pair za, zb, zc, zd, ze, zf;

	za = exact_center + (+0.30reduced_il, +0.70reduced_il);
	zb = exact_center + (+0.20reduced_il, +0.70reduced_il);
	zc = exact_center + (-0.10reduced_il, +0.85reduced_il);
	zd = exact_center + (-0.40reduced_il, +0.70reduced_il);
	ze = exact_center + (-0.40reduced_il, -1.10reduced_il);
	draw za .. zb .. zc -- zd -- ze;

	save zg, zh, zi, zj;
	pair zg, zh, zi, zj;

	zg = exact_center + (+0.30reduced_il, -0.05reduced_il);
	zh = exact_center + (+0.20reduced_il, -0.05reduced_il);
	zi = exact_center + (-0.10reduced_il, +0.10reduced_il);
	zj = exact_center + (-0.40reduced_il, -0.05reduced_il);
	draw zg .. zh .. zi -- zj;

	set_char_box(0.5reduced_il#, 0.5reduced_il#, 0, reduced_il#);
enddef;


fet_beginchar("Hufnagel fa clef", "hufnagel_fa", "hufnagelfaclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_hufnagel_fa_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("Hufnagel fa clef", "hufnagel_fa_change", "chufnagelfaclef")
	draw_hufnagel_fa_clef((0,0), .8);
fet_endchar;


def draw_hufnagel_do_fa_clef(expr exact_center, reduction) =
	draw_hufnagel_do_clef(exact_center, reduction);
	draw_hufnagel_fa_clef(exact_center + (0, -2staff_space), reduction);
enddef;


fet_beginchar("Hufnagel do/fa clef", "hufnagel_do_fa", "hufnageldofaclef")
	if test = 1:
		draw_staff(-1,3, 0.0);
	fi;
	draw_hufnagel_do_fa_clef((0,0), 1.0);
fet_endchar;
fet_beginchar("Hufnagel do/fa clef", "hufnagel_do_fa_change",
	      "chufnageldofaclef")
	draw_hufnagel_do_fa_clef((0,0), .8);
fet_endchar;

def draw_percussion_clef(expr reduction) =
	save reduced_il;
	reduced_il# = staff_space# * reduction;
	define_pixels(reduced_il);
	set_char_box(-.67reduced_il#,2.0reduced_il#,reduced_il#,reduced_il#);
	razt := 0.45reduced_il;
        pickup penrazor scaled razt;
	draw (-b+razt/2,h) -- (-b+razt/2,-d);
	draw (w-razt/2,h) -- (w-razt/2,-d);
enddef;

fet_beginchar("percussion clef", "percussion", "percussionclef")
	draw_percussion_clef(1.0);
fet_endchar;

fet_beginchar("percussion clef", "percussion_change", "cpercussionclef")
	draw_percussion_clef(.8);
fet_endchar;

def draw_tab_clef(expr reduction) =
	save reduced_il;
	reduced_il# = staff_space# * reduction;
	define_pixels(reduced_il);
	set_char_box(-.5reduced_il#,1.8reduced_il#,2.25reduced_il#,2.25reduced_il#);
	bs := -d+0.65reduced_il;
	cx := 0.5(-b+w);
	xp := 0.32 reduced_il;
	yp := 0.25reduced_il;
        pickup pensquare xscaled xp yscaled yp;

	draw (-b,h) -- (w,h);
	draw (cx,h) -- (cx,h-1.2reduced_il);

	pickup penrazor scaled yp rotated 90;
	draw (-0.85b+0.15w,-0.25reduced_il) -- (-0.15b+0.85w,-0.25reduced_il);
	pickup penrazor scaled xp;
	draw (-b,-0.6reduced_il-yp/2) -- (cx,0.6reduced_il+yp/2) -- (w,-0.6reduced_il-yp/2);


        pickup pensquare xscaled xp yscaled yp;
	draw (-b,-d+1.2reduced_il) -- (-b,-d);
        pickup pencircle xscaled xp yscaled yp;
	draw (-b,-d) --- (cx,-d) .. (w,0.5(bs-d)) .. (cx,bs) --- (-b,bs) --- (cx,bs)
	     .. (0.3cx+0.7w,0.5(bs-d+1.2reduced_il)) .. (cx,-d+1.2reduced_il) --- (-b,-d+1.2reduced_il)
enddef;

fet_beginchar("tab clef", "tab", "tabclef")
	draw_tab_clef(1.0);
fet_endchar;

fet_beginchar("tab clef", "tab_change", "ctabclef")
	draw_tab_clef(.8);
fet_endchar;

fet_endgroup("clefs");
