]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
release: 1.3.120
[lilypond.git] / lily / stem.cc
1 /*
2   stem.cc -- implement Stem
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8
9   TODO: This is way too hairy
10 */
11 #include <math.h>               // m_pi
12
13 #include "lookup.hh"
14 #include "directional-element-interface.hh"
15 #include "note-head.hh"
16 #include "stem.hh"
17 #include "debug.hh"
18 #include "paper-def.hh"
19 #include "rhythmic-head.hh"
20 #include "font-interface.hh"
21 #include "molecule.hh"
22 #include "paper-column.hh"
23 #include "misc.hh"
24 #include "beam.hh"
25 #include "rest.hh"
26 #include "group-interface.hh"
27 #include "cross-staff.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "spanner.hh"
30
31
32 void
33 Stem::set_beaming (Grob*me ,int i,  Direction d )
34 {
35   SCM pair = me->get_grob_property ("beaming");
36   
37   if (!gh_pair_p (pair))
38     {
39       pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
40       me->      set_grob_property ("beaming", pair);
41     }
42   index_set_cell (pair, d, gh_int2scm (i));
43 }
44
45 int
46 Stem::beam_count (Grob*me,Direction d)
47 {
48   SCM p=me->get_grob_property ("beaming");
49   if (gh_pair_p (p))
50     return gh_scm2int (index_cell (p,d));
51   else
52     return 0;
53 }
54
55 Interval
56 Stem::head_positions (Grob*me) 
57 {
58   if (!heads_i (me))
59     {
60       Interval iv;
61       return iv;
62     }
63
64   Drul_array<Grob*> e (extremal_heads (me));
65
66   return Interval (Staff_symbol_referencer::position_f (e[DOWN]),
67                    Staff_symbol_referencer::position_f ( e[UP]));
68 }
69
70
71 Real
72 Stem::chord_start_f (Grob*me) 
73 {
74   return head_positions(me)[get_direction (me)]
75     * Staff_symbol_referencer::staff_space (me)/2.0;
76 }
77
78 Real
79 Stem::stem_end_position (Grob*me) 
80 {
81   SCM p =me->get_grob_property ("stem-end-position");
82   Real pos;
83   if (!gh_number_p (p))
84     {
85
86       pos = get_default_stem_end_position (me);
87       me->set_grob_property ("stem-end-position", gh_double2scm (pos));
88     }
89   else
90     pos = gh_scm2double (p);
91
92   return pos;
93 }
94
95 Direction
96 Stem::get_direction (Grob*me)
97 {
98   Direction d = Directional_element_interface::get (me);
99
100   if (!d)
101     {
102        d = get_default_dir (me);
103        // urg, AAARGH!
104        Directional_element_interface::set (me, d);
105     }
106   return d ;
107 }
108
109
110 void
111 Stem::set_stemend (Grob*me, Real se)
112 {
113   // todo: margins
114   Direction d= get_direction (me);
115   
116   if (d && d * head_positions(me)[get_direction (me)] >= se*d)
117     warning (_ ("Weird stem size; check for narrow beams"));
118
119   me->set_grob_property ("stem-end-position", gh_double2scm (se));
120 }
121
122 int
123 Stem::type_i (Grob*me) 
124 {
125   return first_head (me) ?  Rhythmic_head::balltype_i (first_head (me)) : 2;
126 }
127
128 /*
129   Note head that determines hshift for upstems
130  */ 
131 Grob*
132 Stem::support_head (Grob*me)
133 {
134   SCM h = me->get_grob_property ("support-head");
135   Grob * nh = unsmob_grob (h);
136   if (nh)
137     return nh;
138   else if (heads_i (me) == 1)
139     {
140       /*
141         UGH.
142        */
143       
144       return unsmob_grob (gh_car (me->get_grob_property ("heads")));
145     }
146   else
147     return first_head (me);
148 }
149
150
151 int
152 Stem::heads_i (Grob*me)
153 {
154   return  Pointer_group_interface::count (me, "heads");
155 }
156
157 /*
158   The note head which forms one end of the stem.  
159  */
160 Grob*
161 Stem::first_head (Grob*me)
162 {
163   return extremal_heads (me)[-get_direction (me)];
164 }
165
166 /*
167   START is part where stem reaches `last' head. 
168  */
169 Drul_array<Grob*>
170 Stem::extremal_heads (Grob*me) 
171 {
172   const int inf = 1000000;
173   Drul_array<int> extpos;
174   extpos[DOWN] = inf;
175   extpos[UP] = -inf;  
176   
177   Drul_array<Grob *> exthead;
178   exthead[LEFT] = exthead[RIGHT] =0;
179   
180   for (SCM s = me->get_grob_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
181     {
182       Grob * n = unsmob_grob (gh_car (s));
183
184       
185       int p = int(Staff_symbol_referencer::position_f (n));
186
187       Direction d = LEFT;
188       do {
189       if (d* p > d* extpos[d])
190         {
191           exthead[d] = n;
192           extpos[d] = p;
193         }
194       } while (flip (&d) != DOWN);
195     }
196
197   return exthead;
198 }
199
200 void
201 Stem::add_head (Grob*me, Grob *n)
202 {
203   n->set_grob_property ("stem", me->self_scm ());
204   n->add_dependency (me);
205
206   if (Note_head::has_interface (n))
207     {
208       Pointer_group_interface::add_element (me, "heads",n);
209     }
210   else
211     {
212       n->set_grob_property ("rest", n->self_scm ());
213     }
214 }
215
216 bool
217 Stem::invisible_b (Grob*me)
218 {
219   return !(heads_i (me) && Rhythmic_head::balltype_i (support_head (me)) >= 1);
220 }
221
222 int
223 Stem::get_center_distance (Grob*me, Direction d)
224 {
225   int staff_center = 0;
226   int distance = (int) (d*(head_positions(me)[d] - staff_center));
227   return distance >? 0;
228 }
229
230 Direction
231 Stem::get_default_dir (Grob*me) 
232 {
233   int du = get_center_distance (me,UP);
234   int dd = get_center_distance (me,DOWN);
235
236   if (sign (dd - du))
237     return Direction (sign (dd -du));
238
239   return to_dir (me->get_grob_property ("default-neutral-direction"));
240 }
241
242 Real
243 Stem::get_default_stem_end_position (Grob*me) 
244 {
245   bool grace_b = to_boolean (me->get_grob_property ("grace"));
246   SCM s;
247   Array<Real> a;
248
249   Real length_f = 0.;
250   SCM scm_len = me->get_grob_property("length");
251   if (gh_number_p (scm_len))
252     {
253       length_f = gh_scm2double (scm_len);
254     }
255   else
256     {
257       s = me->get_grob_property("lengths");
258       for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
259         a.push (gh_scm2double (gh_car (q)));
260                 
261       // stem uses half-spaces
262       length_f = a[((flag_i (me) - 2) >? 0) <? (a.size () - 1)] * 2;
263     }
264
265
266   a.clear ();
267   s = me->get_grob_property ("stem-shorten");
268   for (SCM q = s; gh_pair_p (q); q = gh_cdr (q))
269     a.push (gh_scm2double (gh_car (q)));
270
271
272   // stem uses half-spaces
273
274   // fixme: use gh_list_ref () iso. array[]
275   Real shorten_f = a[((flag_i (me) - 2) >? 0) <? (a.size () - 1)] * 2;
276
277   /* URGURGURG
278      'set-default-stemlen' sets direction too
279    */
280   Direction dir = get_direction (me);
281   if (!dir)
282     {
283       dir = get_default_dir (me);
284       Directional_element_interface::set (me, dir);
285     }
286   
287   /* 
288     stems in unnatural (forced) direction should be shortened, 
289     according to [Roush & Gourlay]
290    */
291   if (((int)chord_start_f (me))
292       && (get_direction (me) != get_default_dir (me)))
293     length_f -= shorten_f;
294
295
296    Real st = head_positions(me)[dir] + dir * length_f;
297   
298    bool no_extend_b = to_boolean (me->get_grob_property ("no-stem-extend"));
299    if (!grace_b && !no_extend_b && dir * st < 0) // junkme?
300       st = 0.0;
301
302   return st;
303 }
304
305 /*
306   Number of hooks on the flag, ie. the log of the duration.
307  */
308 int
309 Stem::flag_i (Grob*me) 
310 {
311   SCM s = me->get_grob_property ("duration-log");
312   return  (gh_number_p (s)) ? gh_scm2int (s) : 2;
313 }
314
315 void
316 Stem::position_noteheads (Grob*me)
317 {
318   if (!heads_i (me))
319     return;
320   
321   Link_array<Grob> heads =
322     Pointer_group_interface__extract_elements (me, (Grob*)0, "heads");
323
324   heads.sort (compare_position);
325   Direction dir =get_direction (me);
326   
327   if (dir < 0)
328     heads.reverse ();
329
330
331   Grob *hed = support_head (me);
332   Real w = hed->extent (hed, X_AXIS)[dir];
333   for (int i=0; i < heads.size (); i++)
334     {
335       heads[i]->translate_axis (w - heads[i]->extent (heads[i], X_AXIS)[dir], X_AXIS);
336     }
337   
338   bool parity= true;            // todo: make me settable.
339   int lastpos = int (Staff_symbol_referencer::position_f (heads[0]));
340   for (int i=1; i < heads.size (); i ++)
341     {
342       Real p = Staff_symbol_referencer::position_f (heads[i]);
343       int dy =abs (lastpos- (int)p);
344
345       if (dy <= 1)
346         {
347           if (parity)
348             {
349               Real l = heads[i]->extent (heads[i], X_AXIS).length ();
350               heads[i]->translate_axis (l * get_direction (me), X_AXIS);
351             }
352           parity = !parity;
353         }
354       else
355         parity = true;
356       
357       lastpos = int (p);
358     }
359 }
360
361 MAKE_SCHEME_CALLBACK(Stem,before_line_breaking,1);
362 SCM
363 Stem::before_line_breaking (SCM smob)
364 {
365   Grob*me = unsmob_grob (smob);
366   stem_end_position (me);       // ugh. Trigger direction calc.
367   position_noteheads (me);
368
369   if (invisible_b (me))
370     {
371       me->remove_grob_property ("molecule-callback");
372       // suicide();
373     }
374   
375   set_spacing_hints (me);
376   return SCM_UNSPECIFIED;
377 }
378
379
380 /*
381   ugh.
382   When in a beam with tuplet brackets, brew_mol is called early,
383   caching a wrong value.
384  */
385 MAKE_SCHEME_CALLBACK (Stem, height, 2);
386 SCM
387 Stem::height (SCM smob, SCM ax)
388 {
389   Axis a = (Axis)gh_scm2int (ax);
390   Grob * me = unsmob_grob (smob);
391   assert ( a == Y_AXIS);
392
393   SCM mol = me->get_uncached_molecule ();
394   Interval iv;
395   if (mol != SCM_EOL)
396     iv = unsmob_molecule (mol)->extent (a);
397   return ly_interval2scm (iv);
398 }
399
400
401 /**
402    set stem directions for hinting the optical spacing correction.
403
404    Modifies DIR_LIST property of the Stem's Paper_column
405
406    TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
407  */
408 void
409 Stem::set_spacing_hints (Grob*me) 
410 {
411   if (!invisible_b (me))
412     {
413       SCM scmdir  = gh_int2scm (get_direction (me));
414
415       Item* item = dynamic_cast<Item*> (me);
416       Item * col =  item->column_l ();
417       SCM dirlist =col->get_grob_property ("dir-list");
418       if (scm_sloppy_memq (scmdir, dirlist) == SCM_BOOL_F)
419         {
420           dirlist = gh_cons (scmdir, dirlist);
421           col->set_grob_property ("dir-list", dirlist);
422         }
423     }
424 }
425
426 Molecule
427 Stem::flag (Grob*me)
428 {
429   String style;
430   SCM st = me->get_grob_property ("flag-style");
431   if ( gh_string_p (st))
432     {
433       style = ly_scm2string (st);
434     }
435
436   char c = (get_direction (me) == UP) ? 'u' : 'd';
437   Molecule m = Font_interface::get_default_font (me)->find_by_name (String ("flags-") + to_str (c) + 
438                                       to_str (flag_i (me)));
439   if (!style.empty_b ())
440     m.add_molecule(Font_interface::get_default_font (me)->find_by_name (String ("flags-") + to_str (c) + style));
441   return m;
442 }
443
444 MAKE_SCHEME_CALLBACK(Stem,dim_callback,2);
445 SCM
446 Stem::dim_callback (SCM e, SCM ax)
447 {
448   Axis a = (Axis) gh_scm2int (ax);
449   assert (a == X_AXIS);
450   Grob *se = unsmob_grob (e);
451   Interval r (0, 0);
452   if (unsmob_grob (se->get_grob_property ("beam")) || abs (flag_i (se)) <= 2)
453     ;   // TODO!
454   else
455     {
456       r = flag (se).extent (X_AXIS);
457     }
458   return ly_interval2scm ( r);
459 }
460
461
462 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh! Should be settable.
463
464
465 MAKE_SCHEME_CALLBACK(Stem,brew_molecule,1);
466
467 SCM
468 Stem::brew_molecule (SCM smob) 
469 {
470   Grob*me = unsmob_grob (smob);
471   Molecule mol;
472   Direction d = get_direction (me);
473   
474   
475   Real y1 = Staff_symbol_referencer::position_f (first_head (me));
476   Real y2 = stem_end_position (me);
477   
478   Interval stem_y(y1,y2);
479   stem_y.unite (Interval (y2,y1));
480
481   Real dy = Staff_symbol_referencer::staff_space (me)/2.0;
482   Real head_wid = 0;
483   
484   if (Grob *hed = support_head (me))
485     head_wid = hed->extent (hed,X_AXIS).length ();
486   stem_y[Direction(-d)] += d * head_wid * tan(ANGLE)/(2*dy);
487   
488   if (!invisible_b (me))
489     {
490       Real stem_width = gh_scm2double (me->get_grob_property ("thickness")) * me->paper_l ()->get_var ("stafflinethickness");
491       Molecule ss =Lookup::filledbox (Box (Interval (-stem_width/2, stem_width/2),
492                                            Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
493       mol.add_molecule (ss);
494     }
495
496   if (!beam_l (me) && abs (flag_i (me)) > 2)
497     {
498       Molecule fl = flag (me);
499       fl.translate_axis(stem_y[d]*dy, Y_AXIS);
500       mol.add_molecule (fl);
501     }
502
503   return mol.smobbed_copy ();
504 }
505
506 MAKE_SCHEME_CALLBACK(Stem,off_callback,2);
507 SCM
508 Stem::off_callback (SCM element_smob, SCM )
509 {
510   Grob *me = unsmob_grob (element_smob);
511   
512   Real r=0;
513   if (Grob * f = first_head (me))
514     {
515       Interval head_wid(0, f->extent (f,X_AXIS).length ());
516
517       if (to_boolean (me->get_grob_property ("stem-centered")))
518         return gh_double2scm ( head_wid.center ());
519       
520       Real rule_thick = gh_scm2double (me->get_grob_property ("thickness")) * me->paper_l ()->get_var ("stafflinethickness");
521       Direction d = get_direction (me);
522       r = head_wid[d] - d * rule_thick ;
523     }
524   return gh_double2scm (r);
525 }
526
527
528
529 Grob*
530 Stem::beam_l (Grob*me)
531 {
532   SCM b=  me->get_grob_property ("beam");
533   return unsmob_grob (b);
534 }
535
536
537 // ugh still very long.
538 Stem_info
539 Stem::calc_stem_info (Grob*me) 
540 {
541   Grob * beam = beam_l (me);
542
543   Direction beam_dir = Directional_element_interface::get (beam);
544   if (!beam_dir)
545     {
546       programming_error ("Beam dir not set.");
547       beam_dir = UP;
548     }
549     
550
551   Real staff_space = Staff_symbol_referencer::staff_space (me);
552   Real half_space = staff_space / 2;
553   int multiplicity = Beam::get_multiplicity (beam);
554
555
556   SCM space_proc = beam->get_grob_property ("space-function");
557   SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
558   Real interbeam_f = gh_scm2double (space) * staff_space;
559
560   Real thick = gh_scm2double (beam->get_grob_property ("thickness"));
561   Stem_info info; 
562   info.idealy_f_ = chord_start_f (me);
563
564   // for simplicity, we calculate as if dir == UP
565   info.idealy_f_ *= beam_dir;
566   SCM grace_prop = me->get_grob_property ("grace");
567
568   bool grace_b = to_boolean (grace_prop);
569   
570   Array<Real> a;
571   SCM s;
572   
573   s = me->get_grob_property("beamed-minimum-lengths");
574   a.clear ();
575   for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
576     a.push (gh_scm2double (gh_car (q)));
577
578
579   Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
580   s = me->get_grob_property ("beamed-lengths");
581
582   a.clear();
583   for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
584     a.push (gh_scm2double (gh_car (q)));
585
586   Real stem_length =  a[multiplicity <? (a.size () - 1)] * staff_space;
587
588   if (!beam_dir || (beam_dir == Directional_element_interface::get (me)))
589     /* normal beamed stem */
590     {
591       if (multiplicity)
592         {
593           info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
594         }
595       info.miny_f_ = info.idealy_f_;
596       info.maxy_f_ = INT_MAX;
597
598       info.idealy_f_ += stem_length;
599       info.miny_f_ += minimum_length;
600
601       /*
602         lowest beam of (UP) beam must never be lower than second staffline
603
604         Hmm, reference (Wanske?)
605
606         Although this (additional) rule is probably correct,
607         I expect that highest beam (UP) should also never be lower
608         than middle staffline, just as normal stems.
609         
610       */
611       bool no_extend_b = to_boolean (me->get_grob_property ("no-stem-extend"));
612       if (!grace_b && !no_extend_b)
613         {
614           /* highest beam of (UP) beam must never be lower than middle
615              staffline
616              lowest beam of (UP) beam must never be lower than second staffline
617            */
618           info.miny_f_ =
619             info.miny_f_ >? 0
620             >? (- 2 * half_space - thick
621                 + (multiplicity > 0) * thick
622                 + interbeam_f * (multiplicity - 1));
623         }
624     }
625   else
626     /* knee */
627     {
628       info.idealy_f_ -= thick;
629       info.maxy_f_ = info.idealy_f_;
630       info.miny_f_ = -INT_MAX;
631
632       info.idealy_f_ -= stem_length;
633       info.maxy_f_ -= minimum_length;
634     }
635   
636   info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
637
638   s = beam->get_grob_property ("shorten");
639   if (gh_number_p (s))
640     info.idealy_f_ -= gh_scm2double (s);
641
642   Real interstaff_f = -beam_dir* calc_interstaff_dist (dynamic_cast<Item*> (me), dynamic_cast<Spanner*> (beam));
643
644   info.idealy_f_ += interstaff_f;
645   info.miny_f_ += interstaff_f;
646   info.maxy_f_ += interstaff_f ;
647
648   return info;
649 }
650
651 bool
652 Stem::has_interface (Grob*m)
653 {
654   return m && m->has_interface (ly_symbol2scm ("stem-interface"));
655 }
656
657 void
658 Stem::set_interface (Grob*me)
659 {    
660   me->set_interface (ly_symbol2scm ("stem-interface"));
661 }