]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
patch::: 1.3.15.jcn3
[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, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8
9   TODO: This is way too hairy
10 */
11
12 #include "dimension-cache.hh"
13 #include "stem.hh"
14 #include "debug.hh"
15 #include "paper-def.hh"
16 #include "note-head.hh"
17 #include "lookup.hh"
18 #include "molecule.hh"
19 #include "paper-column.hh"
20 #include "misc.hh"
21 #include "beam.hh"
22 #include "rest.hh"
23 #include "group-interface.hh"
24 #include "cross-staff.hh"
25 #include "staff-symbol-referencer.hh"
26 #include "lily-guile.icc"
27
28
29 void
30 Stem::set_beaming (int i,  Direction d )
31 {
32   SCM pair = get_elt_property ("beaming");
33   
34   if (!gh_pair_p (pair))
35     {
36       pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
37       set_elt_property ("beaming", pair);
38     }
39   index_set_cell (pair, d, gh_int2scm (i));
40 }
41
42 int
43 Stem::beam_count (Direction d) const
44 {
45   SCM p=get_elt_property ("beaming");
46   if (gh_pair_p (p))
47     return gh_scm2int (index_cell (p,d));
48   else
49     return 0;
50 }
51
52 Interval_t<int>
53 Stem::head_positions () const
54 {
55   /* 
56     Mysterious FreeBSD fix by John Galbraith.  Somehow, the empty intervals 
57     trigger FP exceptions on FreeBSD.  Fix: do not return infinity 
58
59    */
60   if (!first_head ())
61     {
62       return Interval_t<int> (100,-100);        
63     }
64
65   Link_array<Note_head> head_l_arr =
66     Group_interface__extract_elements (this, (Note_head*)0, "heads");
67   
68   Interval_t<int> r;
69   for (int i =0; i < head_l_arr.size (); i++)
70     {
71       Staff_symbol_referencer_interface si (head_l_arr[i]);
72       int p = (int)si.position_f ();
73       r[BIGGER] = r[BIGGER] >? p;
74       r[SMALLER] = r[SMALLER] <? p;
75     }
76   return r;
77 }
78
79
80 Real
81 Stem::chord_start_f () const
82 {
83   return head_positions()[get_direction ()]
84     * Staff_symbol_referencer_interface (this).staff_space ()/2.0;
85 }
86
87 Real
88 Stem::stem_end_position () const
89 {
90   SCM p =get_elt_property ("stem-end-position");
91   Real len;
92   if (!gh_number_p (p))
93     {
94       Stem * me = (Stem*) this;
95       len = get_default_stemlen ();
96
97       // FIXME: len != position
98       me->set_elt_property ("stem-end-position", gh_double2scm (len));
99     }
100   else
101     len = gh_scm2double (p);
102
103   return len;
104 }
105
106 void
107 Stem::set_stemend (Real se)
108 {
109   // todo: margins
110   Direction d= get_direction ();
111   
112   if (d && d * head_positions()[get_direction ()] >= se*d)
113     warning (_ ("Weird stem size; check for narrow beams"));
114
115   set_elt_property ("stem-end-position", gh_double2scm (se));
116 }
117
118 int
119 Stem::type_i () const
120 {
121   return first_head () ?  first_head ()->balltype_i () : 2;
122 }
123
124
125
126 /*
127   Note head that determines hshift for upstems
128  */ 
129 Score_element*
130 Stem::support_head ()const
131 {
132   SCM h = get_elt_property ("support-head");
133   Score_element * nh = unsmob_element (h);
134   if (nh)
135     return nh;
136   else
137     return first_head ();
138 }
139
140
141 /*
142   The note head which forms one end of the stem.  
143  */
144 Note_head*
145 Stem::first_head () const
146 {
147   const int inf = 1000000;
148   int pos = -inf;               
149   Direction dir = get_direction ();
150
151   Note_head *nh =0;
152   for (SCM s = get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
153     {
154       Note_head * n = dynamic_cast<Note_head*> (unsmob_element (gh_car (s)));
155       Staff_symbol_referencer_interface si (n);
156       int p = dir * int(si.position_f ());
157       if (p > pos)
158         {
159           nh = n;
160           pos = p;
161         }
162     }
163
164   return nh;
165 }
166
167 void
168 Stem::add_head (Rhythmic_head *n)
169 {
170   n->set_elt_property ("stem", this->self_scm_);
171   n->add_dependency (this);     // ?
172   
173
174   Group_interface gi (this);
175   if (Note_head *nh = dynamic_cast<Note_head *> (n))
176     gi.name_ = "heads";
177   else
178     gi.name_ = "rests";
179
180   gi.add_element (n);
181 }
182
183 Stem::Stem ()
184 {
185   set_elt_property ("heads", SCM_EOL);
186   set_elt_property ("rests", SCM_EOL);
187 }
188
189 bool
190 Stem::invisible_b () const
191 {
192   return !(first_head () && first_head()->balltype_i () >= 1);
193 }
194
195 int
196 Stem::get_center_distance (Direction d) const
197 {
198   int staff_center = 0;
199   int distance = d*(head_positions()[d] - staff_center);
200   return distance >? 0;
201 }
202
203 Direction
204 Stem::get_default_dir () const
205 {
206   int du = get_center_distance (UP);
207   int dd = get_center_distance (DOWN);
208
209   if (sign (dd - du))
210     return Direction (sign (dd -du));
211
212   return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
213 }
214
215 Real
216 Stem::get_default_stemlen () const
217 {
218   bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
219   String type_str = grace_b ? "grace-" : "";
220   SCM s;
221   Array<Real> a;
222
223   Real length_f = 0.;
224   SCM scm_len = get_elt_property("length");
225   if (gh_number_p (scm_len))
226     {
227       length_f = gh_scm2double (scm_len);
228     }
229   else
230     {
231       s = ly_eval_str (type_str + "stem-length");
232       scm_to_array (s, &a);
233       // stem uses half-spaces
234       length_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
235     }
236
237
238   s = ly_eval_str (type_str + "stem-shorten");
239   scm_to_array (s, &a);
240
241   // stem uses half-spaces
242   Real shorten_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
243
244   /* URGURGURG
245      'set-default-stemlen' sets direction too
246    */
247   Direction dir = get_direction ();
248   if (!dir)
249     {
250       Stem * me = (Stem*) this;
251       dir = get_default_dir ();
252       me->set_direction (dir);
253     }
254   
255   /* 
256     stems in unnatural (forced) direction should be shortened, 
257     according to [Roush & Gourlay]
258    */
259   if (((int)chord_start_f ())
260       && (get_direction () != get_default_dir ()))
261     length_f -= shorten_f;
262
263 #if 0
264   /*
265     UGK.!
266    */
267  if (flag_i () >= 5)
268     length_f += 2.0;
269   if (flag_i () >= 6)
270     length_f += 1.0;
271 #endif
272   
273   Real st_f = head_positions()[-dir] + dir * length_f;
274
275   bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
276   if (!grace_b && !no_extend_b && dir * st_f < 0)
277     st_f = 0.0;
278
279   return st_f;
280 }
281
282 /*
283   FIXME: wrong name
284  */
285 int
286 Stem::flag_i () const
287 {
288   SCM s = get_elt_property ("duration-log");
289   return  (gh_number_p (s)) ? gh_scm2int (s) : 2;
290 }
291
292 void
293 Stem::position_noteheads ()
294 {
295   if (!first_head ())
296     return;
297   
298   Link_array<Score_element> heads =
299     Group_interface__extract_elements (this, (Score_element*)0, "heads");
300
301   heads.sort (compare_position);
302   Direction dir =get_direction ();
303   
304   if (dir < 0)
305     heads.reverse ();
306
307
308   Real w = support_head ()->extent (X_AXIS)[dir];
309   for (int i=0; i < heads.size (); i++)
310     {
311       heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
312     }
313   
314   bool parity= true;            // todo: make this settable.
315   int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ());
316   for (int i=1; i < heads.size (); i ++)
317     {
318       Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
319       int dy =abs (lastpos- (int)p);
320
321       if (dy <= 1)
322         {
323           if (parity)
324             {
325               Real l  = heads[i]->extent (X_AXIS).length ();
326               heads[i]->translate_axis (l * get_direction (), X_AXIS);
327             }
328           parity = !parity;
329         }
330       else
331         parity = true;
332       
333       lastpos = int (p);
334     }
335 }
336
337 void
338 Stem::do_pre_processing ()
339 {
340   get_default_stemlen ();       // ugh. Trigger direction calc.
341   position_noteheads ();
342
343   if (invisible_b ())
344     {
345       set_elt_property ("transparent", SCM_BOOL_T);
346       set_empty (Y_AXIS);      
347       set_empty (X_AXIS);      
348     }
349
350   set_spacing_hints ();
351 }
352
353
354
355 /**
356    set stem directions for hinting the optical spacing correction.
357
358    Modifies DIR_LIST property of the Stem's Paper_column
359
360    TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
361  */
362 void
363 Stem::set_spacing_hints () 
364 {
365   if (!invisible_b ())
366     {
367       SCM scmdir  = gh_int2scm (get_direction ());
368       SCM dirlist = column_l ()->get_elt_property ("dir-list");
369       if (dirlist == SCM_UNDEFINED)
370         dirlist = SCM_EOL;
371
372       if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
373         {
374           dirlist = gh_cons (scmdir, dirlist);
375           column_l ()->set_elt_property ("dir-list", dirlist);
376         }
377     }
378 }
379
380 Molecule
381 Stem::flag () const
382 {
383   String style;
384   SCM st = get_elt_property ("style");
385   if ( st != SCM_UNDEFINED)
386     {
387       style = ly_scm2string (st);
388     }
389
390   char c = (get_direction () == UP) ? 'u' : 'd';
391   Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) + 
392                                       to_str (flag_i ()));
393   if (!style.empty_b ())
394     m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
395   return m;
396 }
397
398 Interval
399 Stem::dim_callback (Dimension_cache const* c) 
400 {
401   Stem * s = dynamic_cast<Stem*> (c->element_l ());
402   
403   Interval r (0, 0);
404   if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
405     ;   // TODO!
406   else
407     {
408       r = s->flag ().dim_.x ();
409       r += s->note_delta_f ();
410     }
411   return r;
412 }
413
414
415 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
416
417 Molecule*
418 Stem::do_brew_molecule_p () const
419 {
420   Molecule *mol_p =new Molecule;
421
422   Staff_symbol_referencer_interface si (first_head ());
423   
424   Real y1 = si.position_f();
425   Real y2 = stem_end_position ();
426   
427   Interval stem_y(y1,y2);
428   stem_y.unite (Interval (y2,y1));
429
430   Real dy = staff_symbol_referencer_interface (this)
431     .staff_space ()/2.0;
432
433   Real head_wid = 0;
434   if (support_head ())
435     head_wid = support_head ()->extent (X_AXIS).length ();
436   stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
437   
438   if (!invisible_b ())
439     {
440       Real stem_width = paper_l ()->get_var ("stemthickness");
441       Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
442                                                  Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
443       mol_p->add_molecule (ss);
444     }
445
446   if (!beam_l () && abs (flag_i ()) > 2)
447     {
448       Molecule fl = flag ();
449       fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
450       mol_p->add_molecule (fl);
451     }
452
453   if (first_head ())
454     {
455       mol_p->translate_axis (note_delta_f (), X_AXIS);
456     }
457   return mol_p;
458 }
459
460 Real
461 Stem::note_delta_f () const
462 {
463   Real r=0;
464   if (first_head ())
465     {
466       Interval head_wid(0,  first_head()->extent (X_AXIS).length ());
467          Real rule_thick = paper_l ()->get_var ("stemthickness");
468
469       Interval stem_wid(-rule_thick/2, rule_thick/2);
470       if (get_direction () == CENTER)
471         r = head_wid.center ();
472       else
473         r = head_wid[get_direction ()] - stem_wid[get_direction ()];
474     }
475   return r;
476 }
477
478 Real
479 Stem::hpos_f () const
480 {
481   return note_delta_f () + Item::hpos_f ();
482 }
483
484
485 Beam*
486 Stem::beam_l ()const
487 {
488   SCM b=  get_elt_property ("beam");
489   return dynamic_cast<Beam*> (unsmob_element (b));
490 }
491
492
493 // ugh still very long.
494 Stem_info
495 Stem::calc_stem_info () const
496 {
497   assert (beam_l ());
498
499   Direction beam_dir = beam_l ()->get_direction ();
500   if (!beam_dir)
501     {
502       programming_error ("Beam dir not set.");
503       beam_dir = UP;
504     }
505     
506   Staff_symbol_referencer_interface st (this);
507   Real staff_space = st.staff_space ();
508   Real half_space = staff_space / 2;
509   Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
510   Real thick = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
511   int multiplicity = beam_l ()->get_multiplicity ();
512
513   Stem_info info; 
514   info.idealy_f_ = chord_start_f ();
515
516   // for simplicity, we calculate as if dir == UP
517   info.idealy_f_ *= beam_dir;
518   SCM grace_prop = get_elt_property ("grace");
519
520   bool grace_b = gh_boolean_p (grace_prop) && gh_scm2bool (grace_prop);
521   
522   Array<Real> a;
523   SCM s;
524   String type_str = grace_b ? "grace-" : "";
525   
526   s = ly_eval_str (type_str + "beamed-stem-minimum-length");
527   scm_to_array (s, &a);
528   Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
529
530   s = ly_eval_str (type_str + "beamed-stem-length");
531   scm_to_array (s, &a);
532   Real stem_length =  a[multiplicity <? (a.size () - 1)] * staff_space;
533
534   if (!beam_dir || (beam_dir == get_direction ()))
535     /* normal beamed stem */
536     {
537       if (multiplicity)
538         {
539           info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
540         }
541       info.miny_f_ = info.idealy_f_;
542       info.maxy_f_ = INT_MAX;
543
544       info.idealy_f_ += stem_length;
545       info.miny_f_ += minimum_length;
546
547       /*
548         lowest beam of (UP) beam must never be lower than second staffline
549
550         Hmm, reference (Wanske?)
551
552         Although this (additional) rule is probably correct,
553         I expect that highest beam (UP) should also never be lower
554         than middle staffline, just as normal stems.
555         
556       */
557       SCM extend_prop = get_elt_property ("no-stem-extend");
558       bool no_extend_b = gh_boolean_p (extend_prop)
559         && gh_scm2bool (extend_prop);
560       if (!grace_b && !no_extend_b)
561         {
562           /* highest beam of (UP) beam must never be lower than middle
563              staffline
564              lowest beam of (UP) beam must never be lower than second staffline
565            */
566           info.miny_f_ =
567             info.miny_f_ >? 0
568             >? (- 2 * half_space - thick
569                 + (multiplicity > 0) * thick
570                 + interbeam_f * (multiplicity - 1));
571         }
572     }
573   else
574     /* knee */
575     {
576       info.idealy_f_ -= thick;
577       info.maxy_f_ = info.idealy_f_;
578       info.miny_f_ = -INT_MAX;
579
580       info.idealy_f_ -= stem_length;
581       info.maxy_f_ -= minimum_length;
582     }
583   
584   info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
585
586   s = beam_l ()->get_elt_property ("shorten");
587   if (gh_number_p (s))
588     info.idealy_f_ -= gh_double2scm (s);
589
590   Real interstaff_f = -beam_dir* calc_interstaff_dist (this, beam_l ());
591
592   info.idealy_f_ += interstaff_f;
593   info.miny_f_ += interstaff_f;
594   info.maxy_f_ += interstaff_f ;
595
596   return info;
597 }
598