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