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