1% feta-klef.mf --  implement Clefs -*-Fundamental-*-
2%
3% part of LilyPond's pretty-but-neat music font
4%
5% source file of the Feta (not the Font-En-Tja) music font
6%
7% (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>,
8% Jan Nieuwenhuizen <janneke@gnu.org>,
9% Juergen Reuter <reuter@ipd.uka.de>
10
11
12fet_begingroup ("clefs");
13
14%
15% [Wanske] says the bulbs should be positioned about 1/4 right of the
16% `arrow'.
17%
18% TODO: The hair-curve at z6r looks a little awkward.
19%
20
21def draw_c_clef (expr reduction) =
22	save hair, norm, reduced_ss, right_edge;
23	save xoff;
24
25	reduced_ss# = staff_space# * reduction;
26	norm# := 2/3 reduced_ss#;
27	hair# := 0.06 reduced_ss# + 0.5 linethickness#;
28	right_edge# = 15/4 norm# + 2 hair#;
29	define_pixels (norm, reduced_ss, right_edge);
30	define_whole_vertical_blacker_pixels (hair);
31
32	set_char_box (0, right_edge#, 2 reduced_ss#, 2 reduced_ss#);
33
34	% make unreduced glyph fit exactly into five staff lines
35	if reduction = 1:
36		h := d := 2 staff_space_rounded;
37	fi;
38
39	% assure that the gap between the left and right stem
40	% has the same number of pixels as the thickness of the right
41	% stem
42	draw_block ((0, -d + feta_shift),
43		    (3/4 norm + 1/2 hair, h));
44 	draw_block ((3/4 norm + 1/2 hair + hround (3/2 hair), -d + feta_shift),
45		    (3/4 norm + 1/2 hair + 2 hround (3/2 hair), h));
46
47	% assure symmetry
48	h := h - feta_shift;
49
50	pickup feta_fillpen;
51
52	xoff = 3/4 norm + 1/2 hair + 2 hround (3/2 hair);
53	z5l = (xoff - 3/4 hair, 0);
54	z5r = (x4, 0);
55
56	penpos1 (hair - pen_top - pen_bot, -90);
57	top z1l = (xoff + norm + hair, h);
58
59	penpos2 (hround (norm - 3/2 hair) - pen_lft - pen_rt, 180);
60	rt z2l = (w, h / 2);
61
62	penpos3 (hair - pen_top - pen_bot, 90);
63	bot z3l = ((right_edge - xoff) / 2 + xoff,
64		   vround (.5 norm - 1.5 hair));
65
66	penpos4 (hair - pen_lft - pen_rt, 0);
67	top z4 = (xoff + 1/2 norm + 1/2 hair,
68		  vfloor (reduced_ss - linethickness - .2 hair));
69
70	bot z6 = (xoff + 3/4 norm, vround (.5 norm - .5 hair));
71
72	save t;
73	t = 0.833;
74
75	save pat;
76	path pat;
77
78	pat = z5l{curl 1}
79	      .. z4l{up}
80	      .. z4r{down}
81	      .. z3r{right}
82	      ..tension t.. z2r{up}
83	      ..tension t.. flare_path (top z1l, 180, 90,
84					hair, hfloor (norm - 1/2 hair), -1)
85	      ..tension t.. z2l{down}
86	      .. z3l{left}
87	      .. z6
88	      .. z5r{down};
89
90	filldraw pat shifted (0, feta_shift)
91		 -- reverse pat yscaled -1 shifted (0, -feta_eps)
92		 -- cycle;
93
94	penlabels (1, 2, 3, 4, 5, 6);
95
96	% ugh, should be bulb, not flare?
97
98	draw_staff (-2, 2, 0);
99enddef;
100
101
102fet_beginchar ("C clef", "C");
103	draw_c_clef (1.0);
104fet_endchar;
105
106
107fet_beginchar ("C clef", "C_change");
108	draw_c_clef (.8);
109fet_endchar;
110
111
112%
113% New bulb routine:
114%
115% Insert a brushed piece of the path, and draw the rest of the bulb
116% separately.
117%
118% The bulb has circular form.  Neat merging of the bulb and brushed path
119% is done by playing with tension.
120%
121
122def new_bulb (expr start_point, start_angle,
123	      outer_tangent_point,
124	      end_point, end_angle,
125	      big_radius, bulb_radius, flare,
126	      direction, turning_dir) =
127begingroup;
128	save pat, before, after;
129	save center;
130	save u, v;
131	path pat, before, after;
132	pair center;
133
134	clearxy;
135
136	center = outer_tangent_point
137		 + big_radius * dir (0);
138%		 + (big_radius - bulb_radius) * dir (-turning_dir * 90)
139
140	z1' = center + bulb_radius * dir (turning_dir * 180);
141	z2' = outer_tangent_point + flare * dir (0);
142	z3' = center + bulb_radius * dir (0);
143	z4' = center + bulb_radius * dir (turning_dir * 90);
144	z5' = center - 0.5 [big_radius, bulb_radius] * dir (turning_dir * 90);
145
146	labels (1', 2', 3', 4', 5');
147
148	before := z3'{dir (turning_dir * 90)}
149		  .. z4'{-dir (0)}
150		  ..tension 1.1.. z1'{-dir (turning_dir* 90)};
151	after := z2'{dir (turning_dir * 90)}
152		 .. end_point{dir (end_angle)};
153	(u, v) = before intersectiontimes after;
154
155	pat := start_point{dir (start_angle)}
156	       .. outer_tangent_point{dir (-turning_dir * 90)}
157	       ..tension 1.02.. z5'{dir (0)}
158	       .. subpath (0, u) of before
159	       .. subpath (v, infinity) of after;
160
161	if direction = 0:
162		pat := reverse pat;
163	fi
164
165pat
166endgroup
167enddef;
168
169
170%
171% There is some variation in the shape of bass clefs.
172%
173% * In some clefs the size of the swoosh tip almost reaches the
174%   bottom staff line; in some it crosses the 2nd line from the bottom
175%   with a small overshoot.
176%
177%   The most popular design is where the X part of the tip is aligned
178%   with the left bulb boundary, and the Y part ends on the 2nd
179%   staffline exactly.  This is what we do.
180%
181% * The diameter of the bulb is the width of the open space.
182%
183% * The vertical center of the bulb can be on or slightly above the
184%   staff line.
185%
186% * The vertical position of the dots can be symmetrical around the
187%   staffline, centered in the staff space.  The Baerenreiter SCS has
188%   the bottom dot raised by approximately 0.1 staff space.
189%
190% * Uncarefully set music may have overshoots at the top.  We have none.
191%
192% * It is not exactly clear where the vertical tangent at the right
193%   of the swoosh should be.
194%
195
196def draw_bass_clef (expr exact_center, reduction) =
197	save reduced_ss, swoosh_width;
198	save right_thickness, right_offset, tip_protrude;
199	save dot_diam, bulb_y_offset, bulb_flare;
200	pair tip_protrude;
201
202	reduced_ss# = staff_space# * reduction;
203	2.2 dot_diam# = reduction * (staff_space# - stafflinethickness#);
204	right_thickness# = 0.37 staff_space# + 1.2 linethickness#;
205	swoosh_width# = 2.1 reduced_ss#;
206	define_pixels (swoosh_width);
207	define_whole_pixels (reduced_ss);
208	define_whole_blacker_pixels (dot_diam, right_thickness);
209
210	right_offset = 0.05 staff_space;
211	bulb_y_offset := 0.075 staff_space;
212	bulb_flare := 2.5 linethickness;
213%	tip_protrude := (-linethickness, -.2 staff_space);
214	tip_protrude := (0, 0);
215
216	set_char_box (-xpart exact_center,
217		      xpart exact_center + swoosh_width# + 7/12 reduced_ss#,
218		      -ypart exact_center + 2.5 reduced_ss#,
219		      ypart exact_center + reduced_ss#);
220
221	y1 = bulb_y_offset;
222	x1 = 0;
223
224	x2 = .5 [x1, x3];
225	x2l = x2r = x2;
226
227	y2l := vround_pixels (reduced_ss# + 0.5 linethickness#);
228	y2l - y2r = linethickness;
229
230	x3l - x1 = swoosh_width;
231	x3l - x3r = right_thickness;
232
233	% optical correction: the top dot seems farther away if y3l = 0.
234	y3l = right_offset;
235
236	z4 = -(0, 2.0 reduced_ss) + tip_protrude;
237
238	penpos3 (whatever, 185);
239	penpos4 (linethickness, 135);
240
241	fill new_bulb (z2l, 180, z1, z2r, 0,
242		       0.45 reduced_ss, 0.4 reduced_ss,
243		       bulb_flare, 1, 1)
244	     .. z3r{down}
245	     .. {curl 0}simple_serif (z4r, z4l, 90){curl 0}
246	     .. z3l{up}
247	     ..tension 0.9.. cycle;
248
249	pickup pencircle scaled dot_diam;
250
251	lft x5 = hround (x3l + 1/3 reduced_ss - dot_diam / 2);
252	bot y5 = vfloor (.5 reduced_ss - dot_diam / 2);
253	z6 = z5 yscaled -1;
254
255	% for symmetry
256	y5 := y5 + feta_shift;
257
258	drawdot z5;
259	drawdot z6;
260
261	penlabels (1, 2, 3, 4, 5, 6);
262
263	draw_staff (-3, 1, 0);
264enddef;
265
266
267fet_beginchar ("F clef ", "F");
268	draw_bass_clef ((0, 0), 1.0);
269fet_endchar;
270
271
272fet_beginchar ("F clef (reduced)", "F_change");
273	draw_bass_clef ((0, 0), 0.8);
274fet_endchar;
275
276
277
278%
279% Inspired by Baerenreiter
280%
281%
282% Beste lezers, kijk,
283%
284% Een bolletje hebben we bij toeval allemaal wel eens getekend, maar begint u
285% toch eenvoudig.  Eerst een eenvoudig kruis of herstellingsteken
286% en via de dubbelslag naar een voorzichtig vlaggetje, en heb geduld!
287% Ikzelf heb bijvoorbeeld over mijn eerste gave G-sleutel
288% 35 kilobyte metafont, 12 patchlevels, 0 vriendinnen en 45 dagen gedaan
289%
290%  -- vrij naar Van Kooten & De Bie
291%
292
293def debugfill = fill enddef;
294
295def draw_gclef (expr reduction) =
296	save reduced_ss, downstroke_dir, downstroke_angle, center;
297	save breapth_factor, inner_thick_end, thinness, thickness, thinnib;
298	save start_angle, inner_start_angle, thinness;
299	save upward_swoosh_angle, bot_angle;
300	save pat;
301	path pat;
302	pair downstroke_dir, center;
303
304	reduced_ss# = staff_space# * reduction;
305	define_pixels (reduced_ss);
306
307	thinness = 0.095 staff_space + 0.75 linethickness;
308	downstroke_dir = unitvector (14, -75);
309	downstroke_angle = angle downstroke_dir;
310	bot_angle = -180;		% downstroke_angle - 87
311
312	upward_swoosh_angle = 132;
313	start_angle = -97;
314
315	breapth_factor = 21/14;
316	inner_thick_end = 45;
317	inner_start_angle = downstroke_angle - 43;
318	thickness = .32 reduced_ss + 1.1 linethickness;
319
320	thinnib = thinness;
321
322	set_char_box (0, 1.71 * breapth_factor * reduced_ss#,
323		      2.6 * reduced_ss#, 5 * reduced_ss#);
324
325	center := (breapth_factor * reduced_ss, 0);
326
327	z1 = center + whatever * dir (inner_start_angle);
328	x1 = xpart center - .28 reduced_ss;
329	penpos1 (thinnib, inner_start_angle);
330
331	x2r = xpart center;
332	y2r = vround_pixels (reduced_ss# + .5 stafflinethickness#);
333	penpos2 (thickness, 90);
334
335	z3 = (z4 - center) rotated inner_thick_end + center;
336	penpos3 (thinnib, -90 + inner_thick_end);
337
338	x4 = xpart center - .1 reduced_ss;
339	y4r = -y2r + feta_shift;
340	penpos4 (thinnib, -90);
341
342	x5r = -breapth_factor * reduced_ss + xpart center;
343	y5r = .37 reduced_ss + ypart center;
344	penpos5 (thickness, upward_swoosh_angle);
345
346	z6 = center + whatever * downstroke_dir;
347	y6 = ypart center + 2 reduced_ss;
348	% penpos6 is computed later
349
350	z7l - z6 = whatever * (z5 - z6) ;
351	y7l = 3.5 reduced_ss;
352	penpos7 (thickness, upward_swoosh_angle);
353
354	x9 = .7 [x10, x7r];
355	top y9l = 5 reduced_ss;
356	penpos9 (1.45 thickness, -70);
357
358	x11 - x13r = 1.5 reduced_ss + 0.5 thinnib;
359	y11 = ypart center - 47/28 reduced_ss;
360	y12 = ypart center - 71/28 reduced_ss;
361	y13 = .48 [y12, y4r];
362	x12r = xpart (.45 [z13r, z11] + .75 reduced_ss * downstroke_dir);
363
364%	z10 = center + whatever * dir (downstroke_angle - 1.5);
365	x10 = x6 - 2 thinnib;
366	y10 = ypart center + 3.5 reduced_ss;
367	y10l - y10r = 1.0 thickness;
368	z10r - z10l = .7 thinnib * dir (downstroke_angle + 90)
369		      + whatever * downstroke_dir;
370	z10 = .5 [z10l, z10r];
371	z11 = center + whatever * downstroke_dir + (-0.05 reduced_ss, 0);
372
373	penpos11 (thinnib, start_angle + 90);
374 	penpos12 (thinnib, bot_angle + 90);
375	penpos13 (thinnib + 0.14 staff_space, 180);
376
377	% this auxiliary point ensures good contour overlapping
378	z8 = .5 [z9l, z9r] + .25 ((z9r - z9l) rotated -90);
379
380	z20 = z9l - (0, .25 blot_diameter);
381	penpos20 (blot_diameter, 0);
382
383	pat := z10{down}
384	       .. z6
385	       ..tension 1.1.. z11{dir (start_angle)};
386
387	penpos6 (thinnib, angle (direction 1 of pat) + 90);
388
389	% two auxiliary points to simulate `draw' with `penstroke'
390	z10' = point 0.3 of pat;
391	penpos10' (1.3 thinnib, angle (direction 0.3 of pat) + 50);
392
393	z11' = point 1.5 of pat;
394	penpos11' (thinnib, angle (direction 1.5 of pat) + 90);
395
396	z21l = z20l;
397	z21r = z9r;
398
399	pat := z21l
400	       .. z10l{down}
401	       .. z10'l;
402
403	fill z2l{right}
404	     .. z3l
405	     .. z4l{left}
406	     ..tension 1.07.. z5l{up}  % inside curve
407	     .. z7l{up}
408	     ..tension 1.2.. z20r{curl 1}
409	     .. {direction 0 of pat}z20l
410	     -- z8
411	     -- z9r			% {dir (downstroke_angle + 0)}
412	     ..tension 0.8.. z7r{down}
413	     .. z5r{down}
414	     .. z4r{right}
415	     .. z3r
416	     .. z2r{left}
417	     ..tension .95.. z1r
418	     -- simple_serif (z1r, z1l, 80)
419	     -- z1l
420	     ..tension 0.85.. cycle;
421
422	penstroke z21e
423		  .. z10e{down}
424		  .. z10'e
425		  .. z6e
426		  .. z11'e
427		  .. z11e{dir (-95)}
428		  .. z12e{dir (bot_angle)};
429
430	fill new_bulb (z12r, bot_angle, z13r, z12l, bot_angle + 180,
431		       0.45 reduced_ss, 0.38 reduced_ss,
432		       thinnib + .05 staff_space, 1, -1)
433	     -- cycle;
434
435	penlabels (range 1 thru 21);
436	penlabels (10', 11');
437
438	draw_staff (-1, 3, 0);
439enddef;
440
441
442fet_beginchar ("G clef", "G");
443	draw_gclef (1.0);
444fet_endchar;
445
446
447fet_beginchar ("G clef", "G_change");
448	draw_gclef (0.8);
449fet_endchar;
450
451
452%%%%
453%
454% PERCUSSION
455%
456
457%
458% The percussion clef extent is not coincident with its bbox, since
459% the percussion clef needs more space in front than a normal clef.
460%
461
462def draw_percussion_clef (expr reduction) =
463	save reduced_ss, razt;
464
465	reduced_ss# = staff_space# * reduction;
466	define_pixels (reduced_ss);
467
468	set_char_box (-.67 reduced_ss#, 2.0 reduced_ss#,
469		      reduced_ss#, reduced_ss#);
470
471	razt := hround (0.45 reduced_ss);
472
473	d := d - feta_shift;
474
475	draw_block ((-b, -d), (-b + razt, h));
476	draw_block ((w - razt, -d), (w, h));
477
478	draw_staff (-3, 1, 1);
479enddef;
480
481
482fet_beginchar ("percussion clef", "percussion");
483	draw_percussion_clef (1.0);
484fet_endchar;
485
486
487fet_beginchar ("percussion clef (reduced)", "percussion_change");
488	draw_percussion_clef (.8);
489fet_endchar;
490
491
492def draw_tab_T (expr pos, siz, slant) =
493begingroup;
494	save vx, vy;
495	pair vx, vy;
496
497	clearxy;
498
499	vx = (xpart siz) * dir 0;
500	vy = (ypart siz) * dir 90;
501
502	penpos1 (.75 penh, 100);
503	z1 = z2 + (1/6 * vx - .15 * vy);
504	penpos2 (hround (.9 penw), 0);
505	x2l = hround xpart (pos + .75 vy);
506	y2l = ypart (pos + .75 vy);
507	penpos3 (penh, -100);
508	z3l = pos + .4 vx + vy;
509	penpos4 (penh, -90);
510	z4 = -.1 vy + .5 [z3, z5];
511	penpos5 (.8 penh, -30);
512	x5r = xpart (pos + siz);
513	y5l = ypart (pos + siz);
514
515	penpos10 (penw, 170);
516	z10 = pos + .55 vx + .9 vy;
517	penpos11 (.75 [penh, penw], 170);
518	z11 = z10 - .5 vy + .025 vx;
519	penpos12 (penh, 100);
520	z12l = (xpart .5 [z13, z11], ypart (pos - .025 * siz));
521	penpos13 (.75 penh, 60);
522	z13 = pos + .2 vx + .15 vy;
523
524	% penlabels (range 1 thru 13);
525
526	soft_penstroke (z1e
527			..tension 1.1.. z2e
528			.. z3e{right}
529			..tension 1.5.. z4e
530			..z5e)
531	  slanted slant shifted (slant * -ypart pos, 0);
532
533	soft_end_penstroke (z10e
534			    ..tension 1.5.. z11e
535			    .. z12e
536			    ..tension 1.1.. z13e{(z13r - z13l) rotated 90})
537	  slanted slant shifted (slant * -ypart pos, 0);
538endgroup;
539enddef;
540
541
542def draw_tab_A (expr pos, siz, slant) =
543begingroup;
544	save vx, vy, pat;
545	pair vx, vy;
546	path pat;
547
548	clearxy;
549
550	vx = (xpart siz) * dir 0;
551	vy = (ypart siz) * dir 90;
552
553	penpos1 (.75 penh, -110);
554	z1r = pos + .07 vy;
555	penpos2 (penh, -75);
556	z2r = (.5 [x1, x3], ypart pos);
557	penpos3 (.25 [penh, penw], -30);
558	z3 = (.45 [x2, x4], .15 [y2, y4]);
559	penpos4 (1 [penh, penw], 0);
560	z4 = pos + .5 vx + .975 vy;
561
562	penpos5 (1 [penh, penw], -180);
563	z5 = z4;
564	penpos6 (.2 [penh, penw], -150);
565	z6l = (.8 [x5l, x7l], .9 [y5l, y7l]);
566	penpos7 (penh,-90);
567	z7r = (.5 [x6, x8], ypart pos);
568	penpos8 (.75 penh, -70);
569	z8r = (xpart (pos + siz), y7r + .075 ypart (siz));
570
571	pat := z2
572	       .. z3
573	       .. z4;
574
575	penpos10 (penh, angle (direction 1.2 of pat) - 180);
576	z10 = point 1.2 of pat;
577	penpos11 (.9 penh, -90);
578	z11 = .4 [z10, z6] - 0.05 vy;
579	penpos12 (.75 penh, -75);
580	z12 = .3 [z11, z6] + 0.02 vy;
581
582	% penlabels (range 1 thru 12);
583
584	soft_penstroke (z1e{(z1r - z1l) rotated 90}
585			.. z2e
586			.. z3e
587			.. z4e)
588	  slanted slant shifted (slant * -ypart pos, 0);
589
590	soft_end_penstroke (z5e
591			    .. z6e
592			    .. z7e
593			    .. z8e{(z8r - z8l) rotated 90})
594	  slanted slant shifted (slant * -ypart pos, 0);
595
596	soft_end_penstroke (z10e
597			    .. z11e
598			    .. z12e)
599	  slanted slant shifted (slant * -ypart pos, 0);
600endgroup;
601enddef;
602
603
604def draw_tab_B (expr pos, siz, slant) =
605begingroup;
606	save vx, vy;
607	pair vx, vy;
608
609	clearxy;
610
611	vx = (xpart siz) * dir 0;
612	vy = (ypart siz) * dir 90;
613
614	penpos1 (.75 penh, 100);
615	z1 = z2 + (.15 * vx - .1 * vy);
616	penpos2 (hround (.9 penw), 0);
617	x2l = hround xpart (pos + .75 vy);
618	y2l = ypart (pos + .75 vy);
619	penpos3 (penh, -100);
620	z3l = pos + .4 vx + 1.05 vy;
621	penpos4 (.8 [penh, penw], -180);
622	z4 = (xpart (pos + .75 siz), .5 [y3, y5]);
623	penpos5 (.8 penh, 90);
624	z5 = (.5 [x10, x4], ypart (pos + .55 siz));
625
626	penpos6 (.8 penh, 270);
627	z6 = z5;
628	penpos7 (penw, 180);
629	z7l = (xpart (pos + siz), .5 [y6, y8]);
630	penpos8 (.8 penh, 45);
631	z8 = .5 [z12l, z11l] + .15 vx - .05 vy;
632
633	penpos10 (.75 [penh, penw], 170);
634	z10 = pos + .375 vx + vy;
635	penpos11 (.8 [penh, penw], 150);
636	z11 = z10 - .5 vy + .04 vx;
637	penpos12 (penh, 100);
638	z12l = (xpart .5 [z13, z11], ypart pos);
639	penpos13 (.75 penh, 60);
640	z13 = pos + .1 vx + .15 vy;
641
642	% penlabels (range 1 thru 13);
643
644	soft_penstroke (z1e
645			..tension 1.1.. z2e
646			.. z3e
647			.. z4e
648			..z5e {left})
649	  slanted slant shifted (slant * -ypart pos, 0);
650
651	soft_end_penstroke (z6e{right}
652			    .. z7e
653			    .. z8e{(z8r - z8l) rotated 90})
654	  slanted slant shifted (slant * -ypart pos, 0);
655
656	soft_end_penstroke (z10e
657			    ..tension 1.5.. z11e
658			    .. z12e
659			    ..tension 1.1.. z13e{(z13r - z13l) rotated 90})
660	  slanted slant shifted (slant * -ypart pos, 0);
661endgroup;
662enddef;
663
664
665def draw_tab_clef (expr reduction) =
666	save reduced_ss, letterheight, penw, penh;
667
668	reduced_ss# = staff_space# * reduction;
669	letterheight# = 1.8 reduced_ss#;
670	define_pixels (reduced_ss, letterheight);
671
672	set_char_box (-.2 reduced_ss#, 2.8 reduced_ss#,
673		      1.6 letterheight#, 1.6 letterheight#);
674
675	penw = .45 reduced_ss;
676	penh = .2 reduced_ss;
677
678	draw_tab_T ((-b + .15 reduced_ss, h - letterheight),
679		    (2.1 reduced_ss, letterheight), 0.2);
680	draw_tab_A ((-b - .05 reduced_ss, -.5 letterheight +.15 reduced_ss),
681		    (2.2 reduced_ss, letterheight), 0.4);
682	draw_tab_B ((-b + .025 reduced_ss, -d),
683		    (2.1 reduced_ss, letterheight), 0.25);
684
685	draw_staff (-3, 2, 0.5);
686enddef;
687
688
689fet_beginchar ("tab clef", "tab");
690	draw_tab_clef (1.0);
691fet_endchar;
692
693
694fet_beginchar ("tab clef (reduced)", "tab_change");
695	draw_tab_clef (.8);
696fet_endchar;
697
698fet_endgroup ("clefs");
699