2 grob.cc -- implement Grob
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
13 #include "align-interface.hh"
15 #include "international.hh"
20 #include "output-def.hh"
21 #include "pointer-group-interface.hh"
23 #include "stream-event.hh"
27 #include "ly-smobs.icc"
30 Grob::clone (int count) const
32 return new Grob (*this, count);
35 Grob::Grob (SCM basicprops,
36 Object_key const *key)
40 /* FIXME: default should be no callback. */
44 interfaces_ = SCM_EOL;
45 immutable_property_alist_ = basicprops;
46 mutable_property_alist_ = SCM_EOL;
47 object_alist_ = SCM_EOL;
49 /* We do smobify_self () as the first step. Since the object lives
50 on the heap, none of its SCM variables are protected from
51 GC. After smobify_self (), they are. */
55 We always get a new key object for a new grob.
58 ((Object_key *)key_)->unprotect ();
60 SCM meta = get_property ("meta");
61 if (scm_is_pair (meta))
62 interfaces_ = scm_cdr (scm_assq (ly_symbol2scm ("interfaces"), meta));
64 if (get_property_data ("X-extent") == SCM_EOL)
65 set_property ("X-extent", Grob::stencil_width_proc);
66 if (get_property_data ("Y-extent") == SCM_EOL)
67 set_property ("Y-extent", Grob::stencil_height_proc);
70 Grob::Grob (Grob const &s, int copy_index)
71 : dim_cache_ (s.dim_cache_)
73 key_ = (use_object_keys) ? new Copied_key (s.key_, copy_index) : 0;
74 original_ = (Grob *) & s;
77 immutable_property_alist_ = s.immutable_property_alist_;
78 mutable_property_alist_ = ly_deep_copy (s.mutable_property_alist_);
79 interfaces_ = s.interfaces_;
80 object_alist_ = SCM_EOL;
86 ((Object_key *)key_)->unprotect ();
92 /****************************************************************
94 ****************************************************************/
97 Grob::get_stencil () const
102 SCM stil = get_property ("stencil");
103 return unsmob_stencil (stil);
107 Grob::get_print_stencil () const
109 SCM stil = get_property ("stencil");
112 if (Stencil *m = unsmob_stencil (stil))
115 if (to_boolean (get_property ("transparent")))
116 retval = Stencil (m->extent_box (), SCM_EOL);
119 SCM expr = m->expr ();
120 expr = scm_list_3 (ly_symbol2scm ("grob-cause"),
123 retval = Stencil (m->extent_box (), expr);
126 SCM rot = get_property ("rotation");
127 if (scm_is_pair (rot))
129 Real angle = scm_to_double (scm_car (rot));
130 Real x = scm_to_double (scm_cadr (rot));
131 Real y = scm_to_double (scm_caddr (rot));
133 retval.rotate (angle, Offset (x, y));
136 /* color support... see interpret_stencil_expression () for more... */
137 SCM color = get_property ("color");
138 if (scm_is_pair (color))
140 SCM expr = scm_list_3 (ly_symbol2scm ("color"),
144 retval = Stencil (retval.extent_box (), expr);
152 /****************************************************************
154 ****************************************************************/
156 Grob::do_break_processing ()
161 Grob::discretionary_processing ()
166 Grob::get_system () const
173 Grob::handle_broken_dependencies ()
175 Spanner *sp = dynamic_cast<Spanner *> (this);
176 if (original () && sp)
180 /* THIS, SP is the original spanner. We use a special function
181 because some Spanners have enormously long lists in their
182 properties, and a special function fixes FOO */
184 for (SCM s = object_alist_; scm_is_pair (s); s = scm_cdr (s))
185 sp->substitute_one_mutable_property (scm_caar (s), scm_cdar (s));
187 System *system = get_system ();
191 && common_refpoint (system, X_AXIS)
192 && common_refpoint (system, Y_AXIS))
193 substitute_object_links (system->self_scm (), object_alist_);
194 else if (dynamic_cast<System *> (this))
195 substitute_object_links (SCM_UNDEFINED, object_alist_);
197 /* THIS element is `invalid'; it has been removed from all
198 dependencies, so let's junk the element itself.
200 Do not do this for System, since that would remove references
201 to the originals of score-grobs, which get then GC'd (a bad
206 /* Note that we still want references to this element to be
207 rearranged, and not silently thrown away, so we keep pointers like
208 {broken_into_{drul, array}, original}
216 for (int a = X_AXIS; a < NO_AXES; a++)
217 dim_cache_[a].clear ();
219 mutable_property_alist_ = SCM_EOL;
220 object_alist_ = SCM_EOL;
221 immutable_property_alist_ = SCM_EOL;
222 interfaces_ = SCM_EOL;
226 Grob::handle_prebroken_dependencies ()
228 /* Don't do this in the derived method, since we want to keep access to
229 object_alist_ centralized. */
232 Item *it = dynamic_cast<Item *> (this);
233 substitute_object_links (scm_from_int (it->break_status_dir ()),
234 original ()->object_alist_);
239 Grob::find_broken_piece (System *) const
244 /****************************************************************
246 ****************************************************************/
249 Grob::translate_axis (Real y, Axis a)
251 if (isinf (y) || isnan (y))
253 programming_error (_ ("Infinity or NaN encountered"));
257 if (!dim_cache_[a].offset_)
258 dim_cache_[a].offset_ = new Real (y);
260 *dim_cache_[a].offset_ += y;
263 /* Find the offset relative to D. If D equals THIS, then it is 0.
264 Otherwise, it recursively defd as
266 OFFSET_ + PARENT_L_->relative_coordinate (D) */
268 Grob::relative_coordinate (Grob const *refp, Axis a) const
273 /* We catch PARENT_L_ == nil case with this, but we crash if we did
274 not ask for the absolute coordinate (ie. REFP == nil.) */
275 Real off = get_offset (a);
276 if (refp == dim_cache_[a].parent_)
279 off += dim_cache_[a].parent_->relative_coordinate (refp, a);
285 Grob::pure_relative_y_coordinate (Grob const *refp, int start, int end)
292 if (dim_cache_[Y_AXIS].offset_)
293 off = *dim_cache_[Y_AXIS].offset_;
296 SCM proc = get_property_data ("Y-offset");
298 dim_cache_[Y_AXIS].offset_ = new Real (0.0);
299 off = robust_scm2double (call_pure_function (proc,
300 scm_list_1 (self_scm ()),
303 delete dim_cache_[Y_AXIS].offset_;
304 dim_cache_[Y_AXIS].offset_ = 0;
307 /* we simulate positioning-done if we are the child of a VerticalAlignment,
308 but only if we don't have a cached offset. If we do have a cached offset,
309 it probably means that the Alignment was fixed and it has already been
312 Grob *p = get_parent (Y_AXIS);
314 if (Align_interface::has_interface (p) && !dim_cache_[Y_AXIS].offset_)
315 trans = Align_interface::get_pure_child_y_translation (p, this, start, end);
318 + dim_cache_[Y_AXIS].parent_->pure_relative_y_coordinate (refp, start, end);
321 /* Invoke callbacks to get offset relative to parent. */
323 Grob::get_offset (Axis a) const
325 if (dim_cache_[a].offset_)
326 return *dim_cache_[a].offset_;
328 Grob *me = (Grob *) this;
330 SCM sym = axis_offset_symbol (a);
331 me->dim_cache_[a].offset_ = new Real (0.0);
334 UGH: can't fold next 2 statements together. Apparently GCC thinks
335 dim_cache_[a].offset_ is unaliased.
337 Real off = robust_scm2double (internal_get_property (sym), 0.0);
338 if (me->dim_cache_[a].offset_)
340 *me->dim_cache_[a].offset_ += off;
341 me->del_property (sym);
342 return *me->dim_cache_[a].offset_;
349 Grob::maybe_pure_coordinate (Grob const *refp, Axis a, bool pure, int start, int end)
351 if (pure && a != Y_AXIS)
352 programming_error ("tried to get pure X-offset");
353 return (pure && a == Y_AXIS) ? pure_relative_y_coordinate (refp, start, end)
354 : relative_coordinate (refp, a);
357 /****************************************************************
359 ****************************************************************/
362 Grob::flush_extent_cache (Axis axis)
364 if (dim_cache_[axis].extent_)
367 Ugh, this is not accurate; will flush property, causing
368 callback to be called if.
370 del_property ((axis == X_AXIS) ? ly_symbol2scm ("X-extent") : ly_symbol2scm ("Y-extent"));
371 delete dim_cache_[axis].extent_;
372 dim_cache_[axis].extent_ = 0;
373 if (get_parent (axis))
374 get_parent (axis)->flush_extent_cache (axis);
380 Grob::extent (Grob *refp, Axis a) const
382 Real offset = relative_coordinate (refp, a);
384 if (dim_cache_[a].extent_)
386 real_ext = *dim_cache_[a].extent_;
391 Order is significant: ?-extent may trigger suicide.
395 ? ly_symbol2scm ("X-extent")
396 : ly_symbol2scm ("Y-extent");
398 SCM ext = internal_get_property (ext_sym);
399 if (is_number_pair (ext))
400 real_ext.unite (ly_scm2interval (ext));
404 ? ly_symbol2scm ("minimum-X-extent")
405 : ly_symbol2scm ("minimum-Y-extent");
406 SCM min_ext = internal_get_property (min_ext_sym);
407 if (is_number_pair (min_ext))
408 real_ext.unite (ly_scm2interval (min_ext));
410 ((Grob*)this)->dim_cache_[a].extent_ = new Interval (real_ext);
413 real_ext.translate (offset);
419 Grob::pure_height (Grob *refp, int start, int end)
421 SCM proc = get_property_data ( ly_symbol2scm ("Y-extent"));
422 Interval iv = robust_scm2interval (call_pure_function (proc,
423 scm_list_1 (self_scm ()),
426 Real offset = pure_relative_y_coordinate (refp, start, end);
428 SCM min_ext = get_property ("minimum-Y-extent");
430 /* we don't add minimum-Y-extent if the extent is empty. This solves
431 a problem with Hara-kiri spanners. They would request_suicide and
432 return empty extents, but we would force them here to be large. */
433 if (!iv.is_empty () && is_number_pair (min_ext))
434 iv.unite (ly_scm2interval (min_ext));
436 iv.translate (offset);
441 Grob::maybe_pure_extent (Grob *refp, Axis a, bool pure, int start, int end)
443 if (pure && a != Y_AXIS)
444 programming_error ("tried to get pure width");
445 return (pure && a == Y_AXIS) ? pure_height (refp, start, end) : extent (refp, a);
449 Grob::spanned_rank_iv ()
451 return Interval_t<int> (-1, 0);
454 /****************************************************************
456 ****************************************************************/
458 /* Find the group-element which has both #this# and #s# */
460 Grob::common_refpoint (Grob const *s, Axis a) const
462 /* I don't like the quadratic aspect of this code, but I see no
463 other way. The largest chain of parents might be 10 high or so,
464 so it shouldn't be a real issue. */
465 for (Grob const *c = this; c; c = c->dim_cache_[a].parent_)
466 for (Grob const *d = s; d; d = d->dim_cache_[a].parent_)
474 Grob::set_parent (Grob *g, Axis a)
476 dim_cache_[a].parent_ = g;
480 Grob::get_parent (Axis a) const
482 return dim_cache_[a].parent_;
487 Grob::fixup_refpoint ()
489 for (int a = X_AXIS; a < NO_AXES; a++)
492 Grob *parent = get_parent (ax);
497 if (parent->get_system () != get_system () && get_system ())
499 Grob *newparent = parent->find_broken_piece (get_system ());
500 set_parent (newparent, ax);
503 if (Item *i = dynamic_cast<Item *> (this))
505 Item *parenti = dynamic_cast<Item *> (parent);
509 Direction my_dir = i->break_status_dir ();
510 if (my_dir != parenti->break_status_dir ())
512 Item *newparent = parenti->find_prebroken_piece (my_dir);
513 set_parent (newparent, ax);
521 /****************************************************************
523 ****************************************************************/
525 Grob::warning (string s) const
527 SCM cause = self_scm ();
528 while (Grob *g = unsmob_grob (cause))
529 cause = g->get_property ("cause");
531 /* ES TODO: cause can't be Music*/
532 if (Music *m = unsmob_music (cause))
533 m->origin ()->warning (s);
534 else if (Stream_event *ev = unsmob_stream_event (cause))
535 ev->origin ()->warning (s);
544 SCM meta = get_property ("meta");
545 SCM nm = scm_assq (ly_symbol2scm ("name"), meta);
546 nm = (scm_is_pair (nm)) ? scm_cdr (nm) : SCM_EOL;
547 return scm_is_symbol (nm) ? ly_symbol2string (nm) : this->class_name ();
551 Grob::programming_error (string s) const
553 SCM cause = self_scm ();
554 while (Grob *g = unsmob_grob (cause))
555 cause = g->get_property ("cause");
557 s = _f ("programming error: %s", s);
559 /* ES TODO: cause can't be Music*/
560 if (Music *m = unsmob_music (cause))
561 m->origin ()->message (s);
562 else if (Stream_event *ev = unsmob_stream_event (cause))
563 ev->origin ()->warning (s);
569 ADD_INTERFACE (Grob, "grob-interface",
570 "A grob represents a piece of music notation\n"
572 "All grobs have an X and Y-position on the page. These X and Y positions\n"
573 "are stored in a relative format, so they can easily be combined by\n"
574 "stacking them, hanging one grob to the side of another, and coupling\n"
575 "them into a grouping objects.\n"
577 "Each grob has a reference point (a.k.a. parent): the position of a grob\n"
578 "is stored relative to that reference point. For example the X-reference\n"
579 "point of a staccato dot usually is the note head that it applies\n"
580 "to. When the note head is moved, the staccato dot moves along\n"
583 "A grob is often associated with a symbol, but some grobs do not print\n"
584 "any symbols. They take care of grouping objects. For example, there is a\n"
585 "separate grob that stacks staves vertically. The @ref{NoteCollision}\n"
586 "is also an abstract grob: it only moves around chords, but doesn't print\n"
589 "Grobs have a properties: Scheme variables, that can be read and set. "
590 "They have two types. Immutable variables "
591 "define the default style and behavior. They are shared between many objects. "
592 "They can be changed using @code{\\override} and @code{\\revert}. "
594 "Mutable properties are variables that are specific to one grob. Typically, "
595 "lists of other objects, or results from computations are stored in"
596 "mutable properties: every call to set-grob-property (or its C++ equivalent) "
597 "sets a mutable property. "
599 "The properties @code{after-line-breaking} and @code{before-line-breaking} "
600 "are dummies that are not user-serviceable. "
609 "after-line-breaking "
611 "axis-group-parent-X "
612 "axis-group-parent-Y "
613 "before-line-breaking "
635 /****************************************************************
637 ****************************************************************/
641 grob_stencil_extent (Grob *me, Axis a)
643 Stencil *m = me->get_stencil ();
647 return ly_interval2scm (e);
651 MAKE_SCHEME_CALLBACK (Grob, stencil_height, 1);
653 Grob::stencil_height (SCM smob)
655 Grob *me = unsmob_grob (smob);
656 return grob_stencil_extent (me, Y_AXIS);
659 MAKE_SCHEME_CALLBACK(Grob, y_parent_positioning, 1);
661 Grob::y_parent_positioning (SCM smob)
663 Grob *me = unsmob_grob (smob);
664 Grob *par = me->get_parent (Y_AXIS);
666 (void) par->get_property ("positioning-done");
668 return scm_from_double (0.0);
672 MAKE_SCHEME_CALLBACK(Grob, x_parent_positioning, 1);
674 Grob::x_parent_positioning (SCM smob)
676 Grob *me = unsmob_grob (smob);
678 Grob *par = me->get_parent (X_AXIS);
680 (void) par->get_property ("positioning-done");
682 return scm_from_double (0.0);
685 MAKE_SCHEME_CALLBACK (Grob, stencil_width, 1);
687 Grob::stencil_width (SCM smob)
689 Grob *me = unsmob_grob (smob);
690 return grob_stencil_extent (me, X_AXIS);
696 common_refpoint_of_list (SCM elist, Grob *common, Axis a)
698 for (; scm_is_pair (elist); elist = scm_cdr (elist))
699 if (Grob *s = unsmob_grob (scm_car (elist)))
702 common = common->common_refpoint (s, a);
711 common_refpoint_of_array (vector<Grob*> const &arr, Grob *common, Axis a)
713 for (vsize i = arr.size (); i--;)
714 if (Grob *s = arr[i])
717 common = common->common_refpoint (s, a);
726 robust_relative_extent (Grob *me, Grob *refpoint, Axis a)
728 Interval ext = me->extent (refpoint, a);
730 ext.add_point (me->relative_coordinate (refpoint, a));