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