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