2 stem.cc -- implement Stem
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
9 TODO: This is way too hairy
12 #include "dimension-cache.hh"
15 #include "paper-def.hh"
16 #include "note-head.hh"
18 #include "molecule.hh"
19 #include "paper-column.hh"
23 #include "group-interface.hh"
24 #include "cross-staff.hh"
25 #include "staff-symbol-referencer.hh"
29 Stem::set_beaming (int i, Direction d )
31 SCM pair = get_elt_property ("beaming");
33 if (!gh_pair_p (pair))
35 pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
36 set_elt_property ("beaming", pair);
38 index_set_cell (pair, d, gh_int2scm (i));
42 Stem::beam_count (Direction d) const
44 SCM p=get_elt_property ("beaming");
46 return gh_scm2int (index_cell (p,d));
52 Stem::head_positions () const
55 Mysterious FreeBSD fix by John Galbraith. Somehow, the empty intervals
56 trigger FP exceptions on FreeBSD. Fix: do not return infinity
61 return Interval_t<int> (100,-100);
64 Link_array<Note_head> head_l_arr =
65 Group_interface__extract_elements (this, (Note_head*)0, "heads");
68 for (int i =0; i < head_l_arr.size (); i++)
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;
80 Stem::chord_start_f () const
82 return head_positions()[get_direction ()]
83 * Staff_symbol_referencer_interface (this).staff_space ()/2.0;
87 Stem::stem_end_position () const
89 SCM p =get_elt_property ("stem-end-position");
93 Stem * me = (Stem*) this;
94 len = get_default_stemlen ();
96 // FIXME: len != position
97 me->set_elt_property ("stem-end-position", gh_double2scm (len));
100 len = gh_scm2double (p);
106 Stem::set_stemend (Real se)
109 Direction d= get_direction ();
111 if (d && d * head_positions()[get_direction ()] >= se*d)
112 warning (_ ("Weird stem size; check for narrow beams"));
114 set_elt_property ("stem-end-position", gh_double2scm (se));
118 Stem::type_i () const
120 return first_head () ? first_head ()->balltype_i () : 2;
126 Note head that determines hshift for upstems
129 Stem::support_head ()const
131 SCM h = get_elt_property ("support-head");
132 Score_element * nh = unsmob_element (h);
136 return first_head ();
141 The note head which forms one end of the stem.
144 Stem::first_head () const
146 const int inf = 1000000;
148 Direction dir = get_direction ();
151 for (SCM s = get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
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 ());
167 Stem::add_head (Rhythmic_head *n)
169 n->set_elt_property ("stem", this->self_scm_);
170 n->add_dependency (this); // ?
173 Group_interface gi (this);
174 if (Note_head *nh = dynamic_cast<Note_head *> (n))
184 set_elt_property ("heads", SCM_EOL);
185 set_elt_property ("rests", SCM_EOL);
189 Stem::invisible_b () const
191 return !(first_head () && first_head()->balltype_i () >= 1);
195 Stem::get_center_distance (Direction d) const
197 int staff_center = 0;
198 int distance = d*(head_positions()[d] - staff_center);
199 return distance >? 0;
203 Stem::get_default_dir () const
205 int du = get_center_distance (UP);
206 int dd = get_center_distance (DOWN);
209 return Direction (sign (dd -du));
211 return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
215 Stem::get_default_stemlen () const
218 SCM scm_len = get_elt_property("length");
219 if (gh_number_p (scm_len))
221 length_f = gh_scm2double (scm_len);
224 length_f = paper_l ()->get_var ("stem_length0");
226 bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
227 String type_str = grace_b ? "grace_" : "";
229 Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
232 'set-default-stemlen' sets direction too
234 Direction dir = get_direction ();
237 Stem * me = (Stem*) this;
238 dir = get_default_dir ();
239 me->set_direction (dir);
243 stems in unnatural (forced) direction should be shortened,
244 according to [Roush & Gourlay]
246 if (((int)chord_start_f ())
247 && (get_direction () != get_default_dir ()))
248 length_f -= shorten_f;
260 Real st = head_positions()[-dir] + dir * length_f;
262 bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
263 if (!grace_b && !no_extend_b && dir * st < 0)
270 Stem::flag_i () const
272 SCM s = get_elt_property ("duration-log");
273 return (gh_number_p (s)) ? gh_scm2int (s) : 2;
277 Stem::position_noteheads ()
282 Link_array<Score_element> heads =
283 Group_interface__extract_elements (this, (Score_element*)0, "heads");
285 heads.sort (compare_position);
286 Direction dir =get_direction ();
292 Real w = support_head ()->extent (X_AXIS)[dir];
293 for (int i=0; i < heads.size (); i++)
295 heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
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 ++)
302 Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
303 int dy =abs (lastpos- (int)p);
309 Real l = heads[i]->extent (X_AXIS).length ();
310 heads[i]->translate_axis (l * get_direction (), X_AXIS);
322 Stem::do_pre_processing ()
324 get_default_stemlen (); // ugh. Trigger direction calc.
325 position_noteheads ();
329 set_elt_property ("transparent", SCM_BOOL_T);
334 set_spacing_hints ();
340 set stem directions for hinting the optical spacing correction.
342 Modifies DIR_LIST property of the Stem's Paper_column
344 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
347 Stem::set_spacing_hints ()
351 SCM scmdir = gh_int2scm (get_direction ());
352 SCM dirlist = column_l ()->get_elt_property ("dir-list");
353 if (dirlist == SCM_UNDEFINED)
356 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
358 dirlist = gh_cons (scmdir, dirlist);
359 column_l ()->set_elt_property ("dir-list", dirlist);
368 SCM st = get_elt_property ("style");
369 if ( st != SCM_UNDEFINED)
371 style = ly_scm2string (st);
374 char c = (get_direction () == UP) ? 'u' : 'd';
375 Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) +
377 if (!style.empty_b ())
378 m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
383 Stem::dim_callback (Dimension_cache const* c)
385 Stem * s = dynamic_cast<Stem*> (c->element_l ());
388 if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
392 r = s->flag ().dim_.x ();
393 r += s->note_delta_f ();
399 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
402 Stem::do_brew_molecule_p () const
404 Molecule *mol_p =new Molecule;
406 Staff_symbol_referencer_interface si (first_head ());
408 Real y1 = si.position_f();
409 Real y2 = stem_end_position ();
411 Interval stem_y(y1,y2);
412 stem_y.unite (Interval (y2,y1));
414 Real dy = staff_symbol_referencer_interface (this)
419 head_wid = support_head ()->extent (X_AXIS).length ();
420 stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
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);
430 if (!beam_l () && abs (flag_i ()) > 2)
432 Molecule fl = flag ();
433 fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
434 mol_p->add_molecule (fl);
439 mol_p->translate_axis (note_delta_f (), X_AXIS);
445 Stem::note_delta_f () const
450 Interval head_wid(0, first_head()->extent (X_AXIS).length ());
451 Real rule_thick = paper_l ()->get_var ("stemthickness");
453 Interval stem_wid(-rule_thick/2, rule_thick/2);
454 if (get_direction () == CENTER)
455 r = head_wid.center ();
457 r = head_wid[get_direction ()] - stem_wid[get_direction ()];
463 Stem::hpos_f () const
465 return note_delta_f () + Item::hpos_f ();
472 SCM b= get_elt_property ("beam");
473 return dynamic_cast<Beam*> (unsmob_element (b));
477 // ugh still very long.
479 Stem::calc_stem_info () const
483 Direction beam_dir = beam_l ()->get_direction ();
486 programming_error ("Beam dir not set.");
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"));
496 info.idealy_f_ = chord_start_f ();
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);
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;
512 if (!beam_dir || (beam_dir == get_direction ()))
513 /* normal beamed stem */
515 if (beam_l ()->get_multiplicity ())
517 info.idealy_f_ += beam_f
518 + (beam_l ()->get_multiplicity () - 1) * interbeam_f;
520 info.miny_f_ = info.idealy_f_;
521 info.maxy_f_ = INT_MAX;
523 info.idealy_f_ += stem_f;
524 info.miny_f_ += min_stem_f;
527 lowest beam of (UP) beam must never be lower than second staffline
529 Hmm, reference (Wanske?)
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.
536 if (!grace_b && !no_extend_b)
538 /* highest beam of (UP) beam must never be lower than middle staffline
540 lowest beam of (UP) beam must never be lower than second staffline
544 >? (- 2 * internote_f - beam_f
545 + (beam_l ()->get_multiplicity () > 0) * beam_f
546 + interbeam_f * (beam_l ()->get_multiplicity () - 1));
552 info.idealy_f_ -= beam_f;
553 info.maxy_f_ = info.idealy_f_;
554 info.miny_f_ = -INT_MAX;
556 info.idealy_f_ -= stem_f;
557 info.maxy_f_ -= min_stem_f;
560 info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
562 SCM s = beam_l ()->get_elt_property ("shorten");
564 info.idealy_f_ -= gh_double2scm (s);
566 Real interstaff_f = beam_dir* calc_interstaff_dist (this, beam_l ());
568 info.idealy_f_ += interstaff_f;
569 info.miny_f_ += interstaff_f;
570 info.maxy_f_ += interstaff_f ;