]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
release: 1.3.98
[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 "directional-element-interface.hh"
14 #include "note-head.hh"
15 #include "stem.hh"
16 #include "debug.hh"
17 #include "paper-def.hh"
18 #include "rhythmic-head.hh"
19 #include "lookup.hh"
20 #include "molecule.hh"
21 #include "paper-column.hh"
22 #include "misc.hh"
23 #include "beam.hh"
24 #include "rest.hh"
25 #include "group-interface.hh"
26 #include "cross-staff.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "spanner.hh"
29
30
31 void
32 Stem::set_beaming (Score_element*me ,int i,  Direction d )
33 {
34   SCM pair = me->get_elt_property ("beaming");
35   
36   if (!gh_pair_p (pair))
37     {
38       pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
39       me->      set_elt_property ("beaming", pair);
40     }
41   index_set_cell (pair, d, gh_int2scm (i));
42 }
43
44 int
45 Stem::beam_count (Score_element*me,Direction d)
46 {
47   SCM p=me->get_elt_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 (Score_element*me) 
56 {
57   if (!heads_i (me))
58     {
59       Interval iv;
60       return iv;
61     }
62
63   Drul_array<Score_element*> 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 (Score_element*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 (Score_element*me) 
79 {
80   SCM p =me->get_elt_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_elt_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 (Score_element*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 (Score_element*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_elt_property ("stem-end-position", gh_double2scm (se));
119 }
120
121 int
122 Stem::type_i (Score_element*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 Score_element*
131 Stem::support_head (Score_element*me)
132 {
133   SCM h = me->get_elt_property ("support-head");
134   Score_element * nh = unsmob_element (h);
135   if (nh)
136     return nh;
137   else if (heads_i (me) == 1)
138     {
139       /*
140         UGH.
141        */
142       
143       return unsmob_element (gh_car (me->get_elt_property ("heads")));
144     }
145   else
146     return first_head (me);
147 }
148
149
150 int
151 Stem::heads_i (Score_element*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 Score_element*
160 Stem::first_head (Score_element*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<Score_element*>
169 Stem::extremal_heads (Score_element*me) 
170 {
171   const int inf = 1000000;
172   Drul_array<int> extpos;
173   extpos[DOWN] = inf;
174   extpos[UP] = -inf;  
175   
176   Drul_array<Score_element *> exthead;
177   exthead[LEFT] = exthead[RIGHT] =0;
178   
179   for (SCM s = me->get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
180     {
181       Score_element * n = unsmob_element (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 (Score_element*me, Score_element *n)
201 {
202   n->set_elt_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_elt_property ("rest", n->self_scm ());
212     }
213 }
214
215 bool
216 Stem::invisible_b (Score_element*me)
217 {
218   return !(heads_i (me) && Rhythmic_head::balltype_i (support_head (me)) >= 1);
219 }
220
221 int
222 Stem::get_center_distance (Score_element*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 (Score_element*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_elt_property ("default-neutral-direction"));
239 }
240
241 Real
242 Stem::get_default_stem_end_position (Score_element*me) 
243 {
244   bool grace_b = to_boolean (me->get_elt_property ("grace"));
245   SCM s;
246   Array<Real> a;
247
248   Real length_f = 0.;
249   SCM scm_len = me->get_elt_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_elt_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_elt_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
295    Real st = head_positions(me)[dir] + dir * length_f;
296   
297    bool no_extend_b = to_boolean (me->get_elt_property ("no-stem-extend"));
298    if (!grace_b && !no_extend_b && dir * st < 0) // junkme?
299       st = 0.0;
300
301   return st;
302 }
303
304 /*
305   Number of hooks on the flag, ie. the log of the duration.
306  */
307 int
308 Stem::flag_i (Score_element*me) 
309 {
310   SCM s = me->get_elt_property ("duration-log");
311   return  (gh_number_p (s)) ? gh_scm2int (s) : 2;
312 }
313
314 void
315 Stem::position_noteheads (Score_element*me)
316 {
317   if (!heads_i (me))
318     return;
319   
320   Link_array<Score_element> heads =
321     Pointer_group_interface__extract_elements (me, (Score_element*)0, "heads");
322
323   heads.sort (compare_position);
324   Direction dir =get_direction (me);
325   
326   if (dir < 0)
327     heads.reverse ();
328
329
330   Score_element *hed = support_head (me);
331   Real w = hed->extent (hed, X_AXIS)[dir];
332   for (int i=0; i < heads.size (); i++)
333     {
334       heads[i]->translate_axis (w - heads[i]->extent (heads[i], X_AXIS)[dir], X_AXIS);
335     }
336   
337   bool parity= true;            // todo: make me settable.
338   int lastpos = int (Staff_symbol_referencer::position_f (heads[0]));
339   for (int i=1; i < heads.size (); i ++)
340     {
341       Real p = Staff_symbol_referencer::position_f (heads[i]);
342       int dy =abs (lastpos- (int)p);
343
344       if (dy <= 1)
345         {
346           if (parity)
347             {
348               Real l = heads[i]->extent (heads[i], X_AXIS).length ();
349               heads[i]->translate_axis (l * get_direction (me), X_AXIS);
350             }
351           parity = !parity;
352         }
353       else
354         parity = true;
355       
356       lastpos = int (p);
357     }
358 }
359
360 MAKE_SCHEME_CALLBACK(Stem,before_line_breaking,1);
361 SCM
362 Stem::before_line_breaking (SCM smob)
363 {
364   Score_element*me = unsmob_element (smob);
365   stem_end_position (me);       // ugh. Trigger direction calc.
366   position_noteheads (me);
367
368   if (invisible_b (me))
369     {
370       me->remove_elt_property ("molecule-callback");
371       // suicide();
372     }
373   
374   set_spacing_hints (me);
375   return SCM_UNSPECIFIED;
376 }
377
378
379
380 /**
381    set stem directions for hinting the optical spacing correction.
382
383    Modifies DIR_LIST property of the Stem's Paper_column
384
385    TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
386  */
387 void
388 Stem::set_spacing_hints (Score_element*me) 
389 {
390   if (!invisible_b (me))
391     {
392       SCM scmdir  = gh_int2scm (get_direction (me));
393
394       Item* item = dynamic_cast<Item*> (me);
395       Item * col =  item->column_l ();
396       SCM dirlist =col->get_elt_property ("dir-list");
397       if (scm_sloppy_memq (scmdir, dirlist) == SCM_BOOL_F)
398         {
399           dirlist = gh_cons (scmdir, dirlist);
400           col->set_elt_property ("dir-list", dirlist);
401         }
402     }
403 }
404
405 Molecule
406 Stem::flag (Score_element*me)
407 {
408   String style;
409   SCM st = me->get_elt_property ("flag-style");
410   if ( gh_string_p (st))
411     {
412       style = ly_scm2string (st);
413     }
414
415   char c = (get_direction (me) == UP) ? 'u' : 'd';
416   Molecule m = me->lookup_l ()->afm_find (String ("flags-") + to_str (c) + 
417                                       to_str (flag_i (me)));
418   if (!style.empty_b ())
419     m.add_molecule(me->lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
420   return m;
421 }
422
423 MAKE_SCHEME_CALLBACK(Stem,dim_callback,2);
424 SCM
425 Stem::dim_callback (SCM e, SCM )
426 {
427    Score_element *se = unsmob_element (e);
428   Interval r (0, 0);
429   if (unsmob_element (se->get_elt_property ("beam")) || abs (flag_i (se)) <= 2)
430     ;   // TODO!
431   else
432     {
433       r = flag (se).extent (X_AXIS);
434     }
435   return ly_interval2scm ( r);
436 }
437
438
439 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh! Should be settable.
440
441
442 MAKE_SCHEME_CALLBACK(Stem,brew_molecule,1);
443
444 SCM
445 Stem::brew_molecule (SCM smob) 
446 {
447   Score_element*me = unsmob_element (smob);
448   Molecule mol;
449   Direction d = get_direction (me);
450   
451   
452   Real y1 = Staff_symbol_referencer::position_f (first_head (me));
453   Real y2 = stem_end_position (me);
454   
455   Interval stem_y(y1,y2);
456   stem_y.unite (Interval (y2,y1));
457
458   Real dy = Staff_symbol_referencer::staff_space (me)/2.0;
459   Real head_wid = 0;
460   
461   if (Score_element *hed = support_head (me))
462     head_wid = hed->extent (hed,X_AXIS).length ();
463   stem_y[Direction(-d)] += d * head_wid * tan(ANGLE)/(2*dy);
464   
465   if (!invisible_b (me))
466     {
467       Real stem_width = gh_scm2double (me->get_elt_property ("thickness")) * me->paper_l ()->get_var ("stafflinethickness");
468       Molecule ss =me->lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
469                                                  Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
470       mol.add_molecule (ss);
471     }
472
473   if (!beam_l (me) && abs (flag_i (me)) > 2)
474     {
475       Molecule fl = flag (me);
476       fl.translate_axis(stem_y[d]*dy, Y_AXIS);
477       mol.add_molecule (fl);
478     }
479
480   return mol.create_scheme();
481 }
482
483 MAKE_SCHEME_CALLBACK(Stem,off_callback,2);
484 SCM
485 Stem::off_callback (SCM element_smob, SCM )
486 {
487   Score_element *me = unsmob_element (element_smob);
488   
489   Real r=0;
490   if (Score_element * f = first_head (me))
491     {
492       Interval head_wid(0, f->extent (f,X_AXIS).length ());
493
494       if (to_boolean (me->get_elt_property ("stem-centered")))
495         return gh_double2scm ( head_wid.center ());
496       
497       Real rule_thick = gh_scm2double (me->get_elt_property ("thickness")) * me->paper_l ()->get_var ("stafflinethickness");
498       Direction d = get_direction (me);
499       r = head_wid[d] - d * rule_thick ;
500     }
501   return gh_double2scm (r);
502 }
503
504
505
506 Score_element*
507 Stem::beam_l (Score_element*me)
508 {
509   SCM b=  me->get_elt_property ("beam");
510   return unsmob_element (b);
511 }
512
513
514 // ugh still very long.
515 Stem_info
516 Stem::calc_stem_info (Score_element*me) 
517 {
518   Score_element * beam = beam_l (me);
519
520   Direction beam_dir = Directional_element_interface::get (beam);
521   if (!beam_dir)
522     {
523       programming_error ("Beam dir not set.");
524       beam_dir = UP;
525     }
526     
527
528   Real staff_space = Staff_symbol_referencer::staff_space (me);
529   Real half_space = staff_space / 2;
530   int multiplicity = Beam::get_multiplicity (beam);
531
532
533   SCM space_proc = beam->get_elt_property ("space-function");
534   SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
535   Real interbeam_f = gh_scm2double (space) * staff_space;
536
537   Real thick = gh_scm2double (beam->get_elt_property ("thickness"));
538   Stem_info info; 
539   info.idealy_f_ = chord_start_f (me);
540
541   // for simplicity, we calculate as if dir == UP
542   info.idealy_f_ *= beam_dir;
543   SCM grace_prop = me->get_elt_property ("grace");
544
545   bool grace_b = to_boolean (grace_prop);
546   
547   Array<Real> a;
548   SCM s;
549   
550   s = me->get_elt_property("beamed-minimum-lengths");
551   a.clear ();
552   for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
553     a.push (gh_scm2double (gh_car (q)));
554
555
556   Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
557   s = me->get_elt_property ("beamed-lengths");
558
559   a.clear();
560   for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
561     a.push (gh_scm2double (gh_car (q)));
562
563   Real stem_length =  a[multiplicity <? (a.size () - 1)] * staff_space;
564
565   if (!beam_dir || (beam_dir == Directional_element_interface::get (me)))
566     /* normal beamed stem */
567     {
568       if (multiplicity)
569         {
570           info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
571         }
572       info.miny_f_ = info.idealy_f_;
573       info.maxy_f_ = INT_MAX;
574
575       info.idealy_f_ += stem_length;
576       info.miny_f_ += minimum_length;
577
578       /*
579         lowest beam of (UP) beam must never be lower than second staffline
580
581         Hmm, reference (Wanske?)
582
583         Although this (additional) rule is probably correct,
584         I expect that highest beam (UP) should also never be lower
585         than middle staffline, just as normal stems.
586         
587       */
588       bool no_extend_b = to_boolean (me->get_elt_property ("no-stem-extend"));
589       if (!grace_b && !no_extend_b)
590         {
591           /* highest beam of (UP) beam must never be lower than middle
592              staffline
593              lowest beam of (UP) beam must never be lower than second staffline
594            */
595           info.miny_f_ =
596             info.miny_f_ >? 0
597             >? (- 2 * half_space - thick
598                 + (multiplicity > 0) * thick
599                 + interbeam_f * (multiplicity - 1));
600         }
601     }
602   else
603     /* knee */
604     {
605       info.idealy_f_ -= thick;
606       info.maxy_f_ = info.idealy_f_;
607       info.miny_f_ = -INT_MAX;
608
609       info.idealy_f_ -= stem_length;
610       info.maxy_f_ -= minimum_length;
611     }
612   
613   info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
614
615   s = beam->get_elt_property ("shorten");
616   if (gh_number_p (s))
617     info.idealy_f_ -= gh_scm2double (s);
618
619   Real interstaff_f = -beam_dir* calc_interstaff_dist (dynamic_cast<Item*> (me), dynamic_cast<Spanner*> (beam));
620
621   info.idealy_f_ += interstaff_f;
622   info.miny_f_ += interstaff_f;
623   info.maxy_f_ += interstaff_f ;
624
625   return info;
626 }
627
628 bool
629 Stem::has_interface (Score_element*m)
630 {
631   return m && m->has_interface (ly_symbol2scm ("stem-interface"));
632 }
633
634 void
635 Stem::set_interface (Score_element*me)
636 {    
637   me->set_interface (ly_symbol2scm ("stem-interface"));
638 }