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
11 #include "dimension-cache.hh"
14 #include "paper-def.hh"
15 #include "note-head.hh"
17 #include "molecule.hh"
18 #include "paper-column.hh"
22 #include "group-interface.hh"
23 #include "cross-staff.hh"
27 beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1;
28 yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
33 Stem::head_positions () const
36 Mysterious FreeBSD fix by John Galbraith. Somehow, the empty intervals
37 trigger FP exceptions on FreeBSD. Fix: do not return infinity
42 return Interval_t<int> (100,-100);
45 Link_array<Note_head> head_l_arr =
46 Group_interface__extract_elements (this, (Note_head*)0, "heads");
49 for (int i =0; i < head_l_arr.size (); i++)
51 int p = (int)head_l_arr[i]->position_f ();
52 r[BIGGER] = r[BIGGER] >? p;
53 r[SMALLER] = r[SMALLER] <? p;
59 Stem::do_print () const
62 DEBUG_OUT << "flag "<< flag_i_;
67 Stem::stem_length_f () const
69 return yextent_drul_[UP]-yextent_drul_[DOWN] ;
73 Stem::stem_begin_f () const
75 return yextent_drul_[Direction(-get_direction ())];
79 Stem::chord_start_f () const
81 return head_positions()[get_direction ()] * staff_line_leading_f ()/2.0;
85 Stem::stem_end_f () const
87 return yextent_drul_[get_direction ()];
91 Stem::set_stemend (Real se)
94 if (get_direction () && get_direction () * head_positions()[get_direction ()] >= se*get_direction ())
95 warning (_ ("Weird stem size; check for narrow beams"));
98 yextent_drul_[get_direction ()] = se;
99 yextent_drul_[Direction(-get_direction ())] = head_positions()[-get_direction ()];
103 Stem::type_i () const
106 return first_head ()->balltype_i_;
110 Stem::first_head () const
112 SCM h =get_elt_property ("heads");
116 Score_element * sc = unsmob_element (gh_car (h));
118 return dynamic_cast<Note_head*> (sc);
122 Stem::add_head (Rhythmic_head *n)
124 n->set_elt_property ("stem", this->self_scm_);
125 n->add_dependency (this); // ?
128 Group_interface gi (this);
129 if (Note_head *nh = dynamic_cast<Note_head *> (n))
138 Stem::invisible_b () const
140 return !(first_head () && first_head()->balltype_i_ >= 1);
144 Stem::get_center_distance (Direction d) const
146 int staff_center = 0;
147 int distance = d*(head_positions()[d] - staff_center);
148 return distance >? 0;
152 Stem::get_default_dir () const
154 int du = get_center_distance (UP);
155 int dd = get_center_distance (DOWN);
158 return Direction (sign (dd -du));
160 return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
166 Stem::set_default_stemlen ()
169 SCM scm_len = get_elt_property("length");
170 if (scm_len != SCM_UNDEFINED)
172 length_f = gh_scm2double (scm_len);
175 length_f = paper_l ()->get_var ("stem_length0");
177 bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
178 String type_str = grace_b ? "grace_" : "";
180 Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
182 if (!get_direction ())
183 set_direction (get_default_dir ());
186 stems in unnatural (forced) direction should be shortened,
187 according to [Roush & Gourlay]
189 if (((int)chord_start_f ())
190 && (get_direction () != get_default_dir ()))
191 length_f -= shorten_f;
198 set_stemend ((get_direction () > 0) ? head_positions()[BIGGER] + length_f:
199 head_positions()[SMALLER] - length_f);
201 bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
202 if (!grace_b && !no_extend_b && (get_direction () * stem_end_f () < 0))
208 Stem::set_default_extents ()
210 if (!stem_length_f ())
211 set_default_stemlen ();
216 Stem::set_noteheads ()
222 Link_array<Note_head> head_l_arr =
223 Group_interface__extract_elements (this, (Note_head*)0, "heads");
225 head_l_arr.sort (Note_head::compare);
226 if (get_direction () < 0)
227 head_l_arr.reverse ();
229 Note_head * beginhead = first_head ();
230 beginhead->set_elt_property ("extremal", SCM_BOOL_T);
231 if (beginhead != head_l_arr.top ())
232 head_l_arr.top ()->set_elt_property ("extremal", SCM_BOOL_T);
235 int lastpos = int (beginhead->position_f ());
236 for (int i=1; i < head_l_arr.size (); i ++)
238 int dy =abs (lastpos- (int)head_l_arr[i]->position_f ());
243 head_l_arr[i]->flip_around_stem (get_direction ());
248 lastpos = int (head_l_arr[i]->position_f ());
253 Stem::do_pre_processing ()
255 if (yextent_drul_[DOWN]== yextent_drul_[UP])
256 set_default_extents ();
261 set_elt_property ("transparent", SCM_BOOL_T);
267 set_spacing_hints ();
273 set stem directions for hinting the optical spacing correction.
275 Modifies DIR_LIST property of the Stem's Score_column
277 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
281 Stem::set_spacing_hints ()
285 SCM scmdir = gh_int2scm (get_direction ());
286 SCM dirlist = column_l ()->get_elt_property ("dir-list");
287 if (dirlist == SCM_UNDEFINED)
290 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
292 dirlist = gh_cons (scmdir, dirlist);
293 column_l ()->set_elt_property ("dir-list", dirlist);
302 SCM st = get_elt_property ("style");
303 if ( st != SCM_UNDEFINED)
305 style = ly_scm2string (st);
308 char c = (get_direction () == UP) ? 'u' : 'd';
309 Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) +
311 if (!style.empty_b ())
312 m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
317 Stem::dim_callback (Dimension_cache const* c)
319 Stem * s = dynamic_cast<Stem*> (c->element_l ());
322 if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i_) <= 2)
326 r = s->flag ().dim_.x ();
327 r += s->note_delta_f ();
335 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
338 Stem::do_brew_molecule_p () const
340 Molecule *mol_p =new Molecule;
341 Drul_array<Real> stem_y = yextent_drul_;
342 Real dy = staff_line_leading_f ()/2.0;
346 head_wid = first_head ()->extent (X_AXIS).length ();
347 stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
351 Real stem_width = paper_l ()->get_var ("stemthickness");
352 Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
353 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
354 mol_p->add_molecule (ss);
357 if (get_elt_property ("beam") == SCM_UNDEFINED
358 && abs (flag_i_) > 2)
360 Molecule fl = flag ();
361 fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
362 mol_p->add_molecule (fl);
367 mol_p->translate_axis (note_delta_f (), X_AXIS);
373 Stem::note_delta_f () const
378 Interval head_wid(0, first_head()->extent (X_AXIS).length ());
379 Real rule_thick = paper_l ()->get_var ("stemthickness");
381 Interval stem_wid(-rule_thick/2, rule_thick/2);
382 if (get_direction () == CENTER)
383 r = head_wid.center ();
385 r = head_wid[get_direction ()] - stem_wid[get_direction ()];
391 Stem::hpos_f () const
393 return note_delta_f () + Item::hpos_f ();
400 SCM b= get_elt_property ("beam");
401 return dynamic_cast<Beam*> (unsmob_element (b));
406 stupid name: we're calculating and setting (caching??)
407 some stem length parameters for beamed stem
414 SCM bd = remove_elt_property ("beam-dir");
415 Real internote_f = staff_line_leading_f ()/2;
423 if (gh_number_p (bd))
425 beam_dir = (Direction)gh_scm2int (bd);
429 programming_error ("Beam direction not set.");
430 beam_dir = UP; // GURAUGRNAGURAGU! urg !
433 Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->multiplicity_i_);
434 Real beam_f = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
436 idealy_f = chord_start_f ();
438 // for simplicity, we calculate as if dir == UP
439 idealy_f *= beam_dir;
441 bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
442 bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
444 int stem_max = (int)rint(paper_l ()->get_var ("stem_max"));
445 String type_str = grace_b ? "grace_" : "";
446 Real min_stem_f = paper_l ()->get_var (type_str + "minimum_stem_length"
447 + to_str (beam_l ()->multiplicity_i_ <? stem_max)) * internote_f;
448 Real stem_f = paper_l ()->get_var (type_str + "stem_length"
449 + to_str (beam_l ()->multiplicity_i_ <? stem_max)) * internote_f;
451 if (!beam_dir || (beam_dir == get_direction ()))
452 /* normal beamed stem */
454 if (beam_l ()->multiplicity_i_)
457 idealy_f += (beam_l ()->multiplicity_i_ - 1) * interbeam_f;
463 miny_f += min_stem_f;
466 lowest beam of (UP) beam must never be lower than second staffline
468 Hmm, reference (Wanske?)
470 Although this (additional) rule is probably correct,
471 I expect that highest beam (UP) should also never be lower
472 than middle staffline, just as normal stems.
475 if (!grace_b && !no_extend_b)
477 //highest beam of (UP) beam must never be lower than middle staffline
478 miny_f = miny_f >? 0;
479 //lowest beam of (UP) beam must never be lower than second staffline
480 miny_f = miny_f >? (- 2 * internote_f - beam_f
481 + (beam_l ()->multiplicity_i_ > 0) * beam_f + interbeam_f * (beam_l ()->multiplicity_i_ - 1));
492 maxy_f -= min_stem_f;
495 idealy_f = maxy_f <? idealy_f;
496 idealy_f = miny_f >? idealy_f;
498 interstaff_f = calc_interstaff_dist (this, beam_l ());
499 idealy_f += interstaff_f * beam_dir;
500 miny_f += interstaff_f * beam_dir;
501 maxy_f += interstaff_f * beam_dir;
503 set_elt_property ("interstaff-f", gh_double2scm (interstaff_f));
504 set_elt_property ("idealy-f", gh_double2scm (idealy_f));
505 set_elt_property ("miny-f", gh_double2scm (miny_f));
506 set_real ("maxy-f", gh_double2scm (maxy_f));