]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
patch::: 1.3.15.jcn4
[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   Real st_f = head_positions()[-dir] + dir * length_f;
264
265   bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
266   if (!grace_b && !no_extend_b && dir * st_f < 0)
267     st_f = 0.0;
268
269   return st_f;
270 }
271
272 /*
273   FIXME: wrong name
274  */
275 int
276 Stem::flag_i () const
277 {
278   SCM s = get_elt_property ("duration-log");
279   return  (gh_number_p (s)) ? gh_scm2int (s) : 2;
280 }
281
282 void
283 Stem::position_noteheads ()
284 {
285   if (!first_head ())
286     return;
287   
288   Link_array<Score_element> heads =
289     Group_interface__extract_elements (this, (Score_element*)0, "heads");
290
291   heads.sort (compare_position);
292   Direction dir =get_direction ();
293   
294   if (dir < 0)
295     heads.reverse ();
296
297
298   Real w = support_head ()->extent (X_AXIS)[dir];
299   for (int i=0; i < heads.size (); i++)
300     {
301       heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
302     }
303   
304   bool parity= true;            // todo: make this settable.
305   int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ());
306   for (int i=1; i < heads.size (); i ++)
307     {
308       Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
309       int dy =abs (lastpos- (int)p);
310
311       if (dy <= 1)
312         {
313           if (parity)
314             {
315               Real l  = heads[i]->extent (X_AXIS).length ();
316               heads[i]->translate_axis (l * get_direction (), X_AXIS);
317             }
318           parity = !parity;
319         }
320       else
321         parity = true;
322       
323       lastpos = int (p);
324     }
325 }
326
327 void
328 Stem::do_pre_processing ()
329 {
330   get_default_stemlen ();       // ugh. Trigger direction calc.
331   position_noteheads ();
332
333   if (invisible_b ())
334     {
335       set_elt_property ("transparent", SCM_BOOL_T);
336       set_empty (Y_AXIS);      
337       set_empty (X_AXIS);      
338     }
339
340   set_spacing_hints ();
341 }
342
343
344
345 /**
346    set stem directions for hinting the optical spacing correction.
347
348    Modifies DIR_LIST property of the Stem's Paper_column
349
350    TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
351  */
352 void
353 Stem::set_spacing_hints () 
354 {
355   if (!invisible_b ())
356     {
357       SCM scmdir  = gh_int2scm (get_direction ());
358       SCM dirlist = column_l ()->get_elt_property ("dir-list");
359       if (dirlist == SCM_UNDEFINED)
360         dirlist = SCM_EOL;
361
362       if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
363         {
364           dirlist = gh_cons (scmdir, dirlist);
365           column_l ()->set_elt_property ("dir-list", dirlist);
366         }
367     }
368 }
369
370 Molecule
371 Stem::flag () const
372 {
373   String style;
374   SCM st = get_elt_property ("style");
375   if ( st != SCM_UNDEFINED)
376     {
377       style = ly_scm2string (st);
378     }
379
380   char c = (get_direction () == UP) ? 'u' : 'd';
381   Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) + 
382                                       to_str (flag_i ()));
383   if (!style.empty_b ())
384     m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
385   return m;
386 }
387
388 Interval
389 Stem::dim_callback (Dimension_cache const* c) 
390 {
391   Stem * s = dynamic_cast<Stem*> (c->element_l ());
392   
393   Interval r (0, 0);
394   if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
395     ;   // TODO!
396   else
397     {
398       r = s->flag ().dim_.x ();
399       r += s->note_delta_f ();
400     }
401   return r;
402 }
403
404
405 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
406
407 Molecule*
408 Stem::do_brew_molecule_p () const
409 {
410   Molecule *mol_p =new Molecule;
411
412   Staff_symbol_referencer_interface si (first_head ());
413   
414   Real y1 = si.position_f();
415   Real y2 = stem_end_position ();
416   
417   Interval stem_y(y1,y2);
418   stem_y.unite (Interval (y2,y1));
419
420   Real dy = staff_symbol_referencer_interface (this)
421     .staff_space ()/2.0;
422
423   Real head_wid = 0;
424   if (support_head ())
425     head_wid = support_head ()->extent (X_AXIS).length ();
426   stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
427   
428   if (!invisible_b ())
429     {
430       Real stem_width = paper_l ()->get_var ("stemthickness");
431       Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
432                                                  Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
433       mol_p->add_molecule (ss);
434     }
435
436   if (!beam_l () && abs (flag_i ()) > 2)
437     {
438       Molecule fl = flag ();
439       fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
440       mol_p->add_molecule (fl);
441     }
442
443   if (first_head ())
444     {
445       mol_p->translate_axis (note_delta_f (), X_AXIS);
446     }
447   return mol_p;
448 }
449
450 Real
451 Stem::note_delta_f () const
452 {
453   Real r=0;
454   if (first_head ())
455     {
456       Interval head_wid(0,  first_head()->extent (X_AXIS).length ());
457          Real rule_thick = paper_l ()->get_var ("stemthickness");
458
459       Interval stem_wid(-rule_thick/2, rule_thick/2);
460       if (get_direction () == CENTER)
461         r = head_wid.center ();
462       else
463         r = head_wid[get_direction ()] - stem_wid[get_direction ()];
464     }
465   return r;
466 }
467
468 Real
469 Stem::hpos_f () const
470 {
471   return note_delta_f () + Item::hpos_f ();
472 }
473
474
475 Beam*
476 Stem::beam_l ()const
477 {
478   SCM b=  get_elt_property ("beam");
479   return dynamic_cast<Beam*> (unsmob_element (b));
480 }
481
482
483 // ugh still very long.
484 Stem_info
485 Stem::calc_stem_info () const
486 {
487   assert (beam_l ());
488
489   Direction beam_dir = beam_l ()->get_direction ();
490   if (!beam_dir)
491     {
492       programming_error ("Beam dir not set.");
493       beam_dir = UP;
494     }
495     
496   Staff_symbol_referencer_interface st (this);
497   Real staff_space = st.staff_space ();
498   Real half_space = staff_space / 2;
499   Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
500   Real thick = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
501   int multiplicity = beam_l ()->get_multiplicity ();
502
503   Stem_info info; 
504   info.idealy_f_ = chord_start_f ();
505
506   // for simplicity, we calculate as if dir == UP
507   info.idealy_f_ *= beam_dir;
508   SCM grace_prop = get_elt_property ("grace");
509
510   bool grace_b = gh_boolean_p (grace_prop) && gh_scm2bool (grace_prop);
511   
512   Array<Real> a;
513   SCM s;
514   String type_str = grace_b ? "grace-" : "";
515   
516   s = ly_eval_str (type_str + "beamed-stem-minimum-length");
517   scm_to_array (s, &a);
518   Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
519
520   s = ly_eval_str (type_str + "beamed-stem-length");
521   scm_to_array (s, &a);
522   Real stem_length =  a[multiplicity <? (a.size () - 1)] * staff_space;
523
524   if (!beam_dir || (beam_dir == get_direction ()))
525     /* normal beamed stem */
526     {
527       if (multiplicity)
528         {
529           info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
530         }
531       info.miny_f_ = info.idealy_f_;
532       info.maxy_f_ = INT_MAX;
533
534       info.idealy_f_ += stem_length;
535       info.miny_f_ += minimum_length;
536
537       /*
538         lowest beam of (UP) beam must never be lower than second staffline
539
540         Hmm, reference (Wanske?)
541
542         Although this (additional) rule is probably correct,
543         I expect that highest beam (UP) should also never be lower
544         than middle staffline, just as normal stems.
545         
546       */
547       SCM extend_prop = get_elt_property ("no-stem-extend");
548       bool no_extend_b = gh_boolean_p (extend_prop)
549         && gh_scm2bool (extend_prop);
550       if (!grace_b && !no_extend_b)
551         {
552           /* highest beam of (UP) beam must never be lower than middle
553              staffline
554              lowest beam of (UP) beam must never be lower than second staffline
555            */
556           info.miny_f_ =
557             info.miny_f_ >? 0
558             >? (- 2 * half_space - thick
559                 + (multiplicity > 0) * thick
560                 + interbeam_f * (multiplicity - 1));
561         }
562     }
563   else
564     /* knee */
565     {
566       info.idealy_f_ -= thick;
567       info.maxy_f_ = info.idealy_f_;
568       info.miny_f_ = -INT_MAX;
569
570       info.idealy_f_ -= stem_length;
571       info.maxy_f_ -= minimum_length;
572     }
573   
574   info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
575
576   s = beam_l ()->get_elt_property ("shorten");
577   if (gh_number_p (s))
578     info.idealy_f_ -= gh_double2scm (s);
579
580   Real interstaff_f = -beam_dir* calc_interstaff_dist (this, beam_l ());
581
582   info.idealy_f_ += interstaff_f;
583   info.miny_f_ += interstaff_f;
584   info.maxy_f_ += interstaff_f ;
585
586   return info;
587 }
588