% 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, 1998 Han-Wen Nienhuys <hanwen@stack.nl>,
% Jan Nieuwenhuizen <janneke@gnu.org>


fet_begingroup("clefs");

%
% [Ross] says that clefs take 1 interline 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#=interline#*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", "alto", "altoclef")
	if test = 1:
		draw_staff (-2,2);
	fi;
	draw_c_clef (1.0);
fet_endchar;

fet_beginchar ("C clef", "alto_change", "caltoclef")
	if test = 1:
		draw_staff (-2,2);
	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_klef(expr exact_center, reduction) = 
	save reduced_il, left_tilt, left_thick, ball_to_right;
	reduced_il# = interline# * 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#, 
		- 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 interline;
	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* (interline - stafflinethickness);
	pickup pencircle scaled dot_diam;
	draw z5;
	draw z5 yscaled -1;
enddef;



fet_beginchar("F clef ", "bass", "bassclef")
	if test = 1:
		draw_staff(-3,1);
	fi;
	draw_bass_klef((.5 interline#, 0), 1.0);
fet_endchar;
fet_beginchar("F clef (reduced)", "bass_change", "cbassclef")
	draw_bass_klef((.4 interline#, 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
%
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# = interline# * 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", "treble", "trebleclef")
	if test = 1:
		draw_staff(-1,3);
	fi;
	draw_gclef((1.7 interline#,0), 1.0);
fet_endchar;
fet_beginchar("G clef", "treble_change", "ctrebleclef")
	draw_gclef((1.3 interline#,0), .8);
fet_endchar;

fet_endgroup("clefs");
