lilypond-1.3.146
[lilypond.git] / lily / stem.cc
blob165848acd7d3e73c30581d305d1ed53f1f558500
1 /*
2 stem.cc -- implement Stem
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
9 TODO: This is way too hairy
11 #include <math.h> // m_pi
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 "staff-symbol-referencer.hh"
28 #include "spanner.hh"
31 void
32 Stem::set_beaming (Grob*me ,int i, Direction d)
34 SCM pair = me->get_grob_property ("beaming");
36 if (!gh_pair_p (pair))
38 pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
39 me-> set_grob_property ("beaming", pair);
41 index_set_cell (pair, d, gh_int2scm (i));
44 int
45 Stem::beam_count (Grob*me,Direction d)
47 SCM p=me->get_grob_property ("beaming");
48 if (gh_pair_p (p))
49 return gh_scm2int (index_cell (p,d));
50 else
51 return 0;
54 Interval
55 Stem::head_positions (Grob*me)
57 if (!heads_i (me))
59 Interval iv;
60 return iv;
63 Drul_array<Grob*> e (extremal_heads (me));
65 return Interval (Staff_symbol_referencer::position_f (e[DOWN]),
66 Staff_symbol_referencer::position_f (e[UP]));
70 Real
71 Stem::chord_start_f (Grob*me)
73 return head_positions (me)[get_direction (me)]
74 * Staff_symbol_referencer::staff_space (me)/2.0;
77 Real
78 Stem::stem_end_position (Grob*me)
80 SCM p =me->get_grob_property ("stem-end-position");
81 Real pos;
82 if (!gh_number_p (p))
85 pos = get_default_stem_end_position (me);
86 me->set_grob_property ("stem-end-position", gh_double2scm (pos));
88 else
89 pos = gh_scm2double (p);
91 return pos;
94 Direction
95 Stem::get_direction (Grob*me)
97 Direction d = Directional_element_interface::get (me);
99 if (!d)
101 d = get_default_dir (me);
102 // urg, AAARGH!
103 Directional_element_interface::set (me, d);
105 return d ;
109 void
110 Stem::set_stemend (Grob*me, Real se)
112 // todo: margins
113 Direction d= get_direction (me);
115 if (d && d * head_positions (me)[get_direction (me)] >= se*d)
116 warning (_ ("Weird stem size; check for narrow beams"));
118 me->set_grob_property ("stem-end-position", gh_double2scm (se));
122 Stem::type_i (Grob*me)
124 return first_head (me) ? Rhythmic_head::balltype_i (first_head (me)) : 2;
128 Note head that determines hshift for upstems
130 Grob*
131 Stem::support_head (Grob*me)
133 SCM h = me->get_grob_property ("support-head");
134 Grob * nh = unsmob_grob (h);
135 if (nh)
136 return nh;
137 else if (heads_i (me) == 1)
140 UGH.
143 return unsmob_grob (gh_car (me->get_grob_property ("heads")));
145 else
146 return first_head (me);
151 Stem::heads_i (Grob*me)
153 return Pointer_group_interface::count (me, "heads");
157 The note head which forms one end of the stem.
159 Grob*
160 Stem::first_head (Grob*me)
162 return extremal_heads (me)[-get_direction (me)];
166 START is part where stem reaches `last' head.
168 Drul_array<Grob*>
169 Stem::extremal_heads (Grob*me)
171 const int inf = 1000000;
172 Drul_array<int> extpos;
173 extpos[DOWN] = inf;
174 extpos[UP] = -inf;
176 Drul_array<Grob *> exthead;
177 exthead[LEFT] = exthead[RIGHT] =0;
179 for (SCM s = me->get_grob_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
181 Grob * n = unsmob_grob (gh_car (s));
184 int p = int (Staff_symbol_referencer::position_f (n));
186 Direction d = LEFT;
187 do {
188 if (d* p > d* extpos[d])
190 exthead[d] = n;
191 extpos[d] = p;
193 } while (flip (&d) != DOWN);
196 return exthead;
199 void
200 Stem::add_head (Grob*me, Grob *n)
202 n->set_grob_property ("stem", me->self_scm ());
203 n->add_dependency (me);
205 if (Note_head::has_interface (n))
207 Pointer_group_interface::add_element (me, "heads",n);
209 else
211 n->set_grob_property ("rest", n->self_scm ());
215 bool
216 Stem::invisible_b (Grob*me)
218 return ! (heads_i (me) && Rhythmic_head::balltype_i (support_head (me)) >= 1);
222 Stem::get_center_distance (Grob*me, Direction d)
224 int staff_center = 0;
225 int distance = (int) (d* (head_positions (me)[d] - staff_center));
226 return distance >? 0;
229 Direction
230 Stem::get_default_dir (Grob*me)
232 int du = get_center_distance (me,UP);
233 int dd = get_center_distance (me,DOWN);
235 if (sign (dd - du))
236 return Direction (sign (dd -du));
238 return to_dir (me->get_grob_property ("default-neutral-direction"));
241 Real
242 Stem::get_default_stem_end_position (Grob*me)
244 bool grace_b = to_boolean (me->get_grob_property ("grace"));
245 SCM s;
246 Array<Real> a;
248 Real length_f = 0.;
249 SCM scm_len = me->get_grob_property ("length");
250 if (gh_number_p (scm_len))
252 length_f = gh_scm2double (scm_len);
254 else
256 s = me->get_grob_property ("lengths");
257 for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
258 a.push (gh_scm2double (gh_car (q)));
260 // stem uses half-spaces
261 length_f = a[ ((flag_i (me) - 2) >? 0) <? (a.size () - 1)] * 2;
265 a.clear ();
266 s = me->get_grob_property ("stem-shorten");
267 for (SCM q = s; gh_pair_p (q); q = gh_cdr (q))
268 a.push (gh_scm2double (gh_car (q)));
271 // stem uses half-spaces
273 // fixme: use gh_list_ref () iso. array[]
274 Real shorten_f = a[ ((flag_i (me) - 2) >? 0) <? (a.size () - 1)] * 2;
276 /* URGURGURG
277 'set-default-stemlen' sets direction too
279 Direction dir = get_direction (me);
280 if (!dir)
282 dir = get_default_dir (me);
283 Directional_element_interface::set (me, dir);
287 stems in unnatural (forced) direction should be shortened,
288 according to [Roush & Gourlay]
290 if (( (int)chord_start_f (me))
291 && (get_direction (me) != get_default_dir (me)))
292 length_f -= shorten_f;
295 Real st = head_positions (me)[dir] + dir * length_f;
297 bool no_extend_b = to_boolean (me->get_grob_property ("no-stem-extend"));
298 if (!grace_b && !no_extend_b && dir * st < 0) // junkme?
299 st = 0.0;
301 return st;
305 Number of hooks on the flag, ie. the log of the duration.
308 Stem::flag_i (Grob*me)
310 SCM s = me->get_grob_property ("duration-log");
311 return (gh_number_p (s)) ? gh_scm2int (s) : 2;
314 void
315 Stem::position_noteheads (Grob*me)
317 if (!heads_i (me))
318 return;
320 Link_array<Grob> heads =
321 Pointer_group_interface__extract_elements (me, (Grob*)0, "heads");
323 heads.sort (compare_position);
324 Direction dir =get_direction (me);
326 if (dir < 0)
327 heads.reverse ();
330 Grob *hed = support_head (me);
331 Real w = hed->extent (hed, X_AXIS)[dir];
332 for (int i=0; i < heads.size (); i++)
334 heads[i]->translate_axis (w - heads[i]->extent (heads[i], X_AXIS)[dir], X_AXIS);
337 bool parity= true; // todo: make me settable.
338 int lastpos = int (Staff_symbol_referencer::position_f (heads[0]));
339 for (int i=1; i < heads.size (); i ++)
341 Real p = Staff_symbol_referencer::position_f (heads[i]);
342 int dy =abs (lastpos- (int)p);
344 if (dy <= 1)
346 if (parity)
348 Real l = heads[i]->extent (heads[i], X_AXIS).length ();
349 heads[i]->translate_axis (l * get_direction (me), X_AXIS);
351 parity = !parity;
353 else
354 parity = true;
356 lastpos = int (p);
360 MAKE_SCHEME_CALLBACK (Stem,before_line_breaking,1);
362 Stem::before_line_breaking (SCM smob)
364 Grob*me = unsmob_grob (smob);
365 stem_end_position (me); // ugh. Trigger direction calc.
366 position_noteheads (me);
368 if (invisible_b (me))
370 me->remove_grob_property ("molecule-callback");
371 // suicide ();
374 set_spacing_hints (me);
375 return SCM_UNSPECIFIED;
380 ugh.
381 When in a beam with tuplet brackets, brew_mol is called early,
382 caching a wrong value.
384 MAKE_SCHEME_CALLBACK (Stem, height, 2);
386 Stem::height (SCM smob, SCM ax)
388 Axis a = (Axis)gh_scm2int (ax);
389 Grob * me = unsmob_grob (smob);
390 assert (a == Y_AXIS);
392 SCM mol = me->get_uncached_molecule ();
393 Interval iv;
394 if (mol != SCM_EOL)
395 iv = unsmob_molecule (mol)->extent (a);
396 return ly_interval2scm (iv);
401 set stem directions for hinting the optical spacing correction.
403 Modifies DIR_LIST property of the Stem's Paper_column
405 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
407 void
408 Stem::set_spacing_hints (Grob*me)
410 if (!invisible_b (me))
412 SCM scmdir = gh_int2scm (get_direction (me));
414 Item* item = dynamic_cast<Item*> (me);
415 Item * col = item->column_l ();
416 SCM dirlist =col->get_grob_property ("dir-list");
417 if (scm_sloppy_memq (scmdir, dirlist) == SCM_BOOL_F)
419 dirlist = gh_cons (scmdir, dirlist);
420 col->set_grob_property ("dir-list", dirlist);
425 Molecule
426 Stem::flag (Grob*me)
428 String style;
429 SCM st = me->get_grob_property ("flag-style");
430 if (gh_string_p (st))
432 style = ly_scm2string (st);
435 char c = (get_direction (me) == UP) ? 'u' : 'd';
436 Molecule m = Font_interface::get_default_font (me)->find_by_name (String ("flags-") + to_str (c) +
437 to_str (flag_i (me)));
438 if (!style.empty_b ())
439 m.add_molecule (Font_interface::get_default_font (me)->find_by_name (String ("flags-") + to_str (c) + style));
440 return m;
443 MAKE_SCHEME_CALLBACK (Stem,dim_callback,2);
445 Stem::dim_callback (SCM e, SCM ax)
447 Axis a = (Axis) gh_scm2int (ax);
448 assert (a == X_AXIS);
449 Grob *se = unsmob_grob (e);
450 Interval r (0, 0);
451 if (unsmob_grob (se->get_grob_property ("beam")) || abs (flag_i (se)) <= 2)
452 ; // TODO!
453 else
455 r = flag (se).extent (X_AXIS);
457 return ly_interval2scm (r);
462 MAKE_SCHEME_CALLBACK (Stem,brew_molecule,1);
465 Stem::brew_molecule (SCM smob)
467 Grob*me = unsmob_grob (smob);
468 Molecule mol;
469 Direction d = get_direction (me);
472 Real y1 = Staff_symbol_referencer::position_f (first_head (me));
473 Real y2 = stem_end_position (me);
475 Interval stem_y (y1,y2);
476 stem_y.unite (Interval (y2,y1));
478 Real dy = Staff_symbol_referencer::staff_space (me)/2.0;
479 Real head_wid = 0;
481 if (Grob *hed = support_head (me))
483 Real slope =0.0;
485 Interval head_height = hed->extent (hed,Y_AXIS);
486 Real y_attach = Note_head::stem_attachment_coordinate ( hed, Y_AXIS);
488 y_attach = head_height.linear_combination (y_attach);
489 stem_y[Direction (-d)] += d * 2*y_attach;
493 if (!invisible_b (me))
495 Real stem_width = gh_scm2double (me->get_grob_property ("thickness")) * me->paper_l ()->get_var ("stafflinethickness");
496 Molecule ss =Lookup::filledbox (Box (Interval (-stem_width/2, stem_width/2),
497 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
498 mol.add_molecule (ss);
501 if (!beam_l (me) && abs (flag_i (me)) > 2)
503 Molecule fl = flag (me);
504 fl.translate_axis (stem_y[d]*dy, Y_AXIS);
505 mol.add_molecule (fl);
508 return mol.smobbed_copy ();
512 move the stem to right of the notehead if it is up.
514 MAKE_SCHEME_CALLBACK (Stem,off_callback,2);
516 Stem::off_callback (SCM element_smob, SCM)
518 Grob *me = unsmob_grob (element_smob);
520 Real r=0;
521 if (Grob * f = first_head (me))
523 Interval head_wid = f->extent (f,X_AXIS);
525 Real attach =
526 Note_head::stem_attachment_coordinate(f, X_AXIS);
528 Direction d = get_direction (me);
530 Real real_attach = head_wid.linear_combination (d * attach);
532 r = real_attach;
535 If not centered: correct for stem thickness.
537 if (attach)
539 Real rule_thick
540 = gh_scm2double (me->get_grob_property ("thickness"))
541 * me->paper_l ()->get_var ("stafflinethickness");
544 r += - d * rule_thick * 0.5;
547 return gh_double2scm (r);
552 Grob*
553 Stem::beam_l (Grob*me)
555 SCM b= me->get_grob_property ("beam");
556 return unsmob_grob (b);
560 // ugh still very long.
561 Stem_info
562 Stem::calc_stem_info (Grob*me)
564 Grob * beam = beam_l (me);
566 Direction beam_dir = Directional_element_interface::get (beam);
567 if (!beam_dir)
569 programming_error ("Beam dir not set.");
570 beam_dir = UP;
574 Real staff_space = Staff_symbol_referencer::staff_space (me);
575 Real half_space = staff_space / 2;
576 int multiplicity = Beam::get_multiplicity (beam);
579 SCM space_proc = beam->get_grob_property ("space-function");
580 SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
581 Real interbeam_f = gh_scm2double (space) * staff_space;
583 Real thick = gh_scm2double (beam->get_grob_property ("thickness"));
584 Stem_info info;
585 info.idealy_f_ = chord_start_f (me);
587 // for simplicity, we calculate as if dir == UP
588 info.idealy_f_ *= beam_dir;
589 SCM grace_prop = me->get_grob_property ("grace");
591 bool grace_b = to_boolean (grace_prop);
593 Array<Real> a;
594 SCM s;
596 s = me->get_grob_property ("beamed-minimum-lengths");
597 a.clear ();
598 for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
599 a.push (gh_scm2double (gh_car (q)));
602 Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
603 s = me->get_grob_property ("beamed-lengths");
605 a.clear ();
606 for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
607 a.push (gh_scm2double (gh_car (q)));
609 Real stem_length = a[multiplicity <? (a.size () - 1)] * staff_space;
611 if (!beam_dir || (beam_dir == Directional_element_interface::get (me)))
612 /* normal beamed stem */
614 if (multiplicity)
616 info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
618 info.miny_f_ = info.idealy_f_;
619 info.maxy_f_ = INT_MAX;
621 info.idealy_f_ += stem_length;
622 info.miny_f_ += minimum_length;
625 lowest beam of (UP) beam must never be lower than second staffline
627 Hmm, reference (Wanske?)
629 Although this (additional) rule is probably correct,
630 I expect that highest beam (UP) should also never be lower
631 than middle staffline, just as normal stems.
634 bool no_extend_b = to_boolean (me->get_grob_property ("no-stem-extend"));
635 if (!grace_b && !no_extend_b)
637 /* highest beam of (UP) beam must never be lower than middle
638 staffline
639 lowest beam of (UP) beam must never be lower than second staffline
641 info.miny_f_ =
642 info.miny_f_ >? 0
643 >? (- 2 * half_space - thick
644 + (multiplicity > 0) * thick
645 + interbeam_f * (multiplicity - 1));
648 else
649 /* knee */
651 info.idealy_f_ -= thick;
652 info.maxy_f_ = info.idealy_f_;
653 info.miny_f_ = -INT_MAX;
655 info.idealy_f_ -= stem_length;
656 info.maxy_f_ -= minimum_length;
659 info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
661 s = beam->get_grob_property ("shorten");
662 if (gh_number_p (s))
663 info.idealy_f_ -= gh_scm2double (s);
665 Grob *common = me->common_refpoint (beam, Y_AXIS);
666 Real interstaff_f = beam_dir *
667 (me->relative_coordinate (common, Y_AXIS)
668 - beam->relative_coordinate (common, Y_AXIS));
670 info.idealy_f_ += interstaff_f;
671 info.miny_f_ += interstaff_f;
672 info.maxy_f_ += interstaff_f ;
674 return info;
677 bool
678 Stem::has_interface (Grob*m)
680 return m && m->has_interface (ly_symbol2scm ("stem-interface"));
683 void
684 Stem::set_interface (Grob*me)
686 me->set_interface (ly_symbol2scm ("stem-interface"));