2 score-elem.cc -- implement Score_element
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "libc-extension.hh"
14 #include "group-interface.hh"
16 #include "paper-score.hh"
17 #include "paper-def.hh"
19 #include "molecule.hh"
20 #include "score-element.hh"
23 #include "line-of-score.hh"
25 #include "paper-column.hh"
26 #include "molecule.hh"
28 #include "paper-outputter.hh"
29 #include "dimension-cache.hh"
30 #include "side-position-interface.hh"
36 remove dynamic_cast<Spanner,Item> and put this code into respective
41 #define INFINITY_MSG "Infinity or NaN encountered"
43 Score_element::Score_element(SCM basicprops
)
45 set_extent_callback (molecule_extent
, X_AXIS
);
46 set_extent_callback (molecule_extent
, Y_AXIS
);
53 immutable_property_alist_
= basicprops
;
54 mutable_property_alist_
= SCM_EOL
;
57 set_elt_property ("dependencies", SCM_EOL
);
59 if (get_elt_property ("interfaces") == SCM_UNDEFINED
)
60 set_elt_property ("interfaces", SCM_EOL
);
64 Score_element::Score_element (Score_element
const&s
)
65 : dim_cache_ (s
.dim_cache_
)
68 original_l_
=(Score_element
*) &s
;
69 immutable_property_alist_
= s
.immutable_property_alist_
;
70 mutable_property_alist_
= SCM_EOL
;
72 status_i_
= s
.status_i_
;
73 lookup_l_
= s
.lookup_l_
;
74 pscore_l_
= s
.pscore_l_
;
79 Score_element::~Score_element()
81 // this goes awry when score-elements are copied...
84 Kijk goed naar hoe smobify en unsmobify werken. unsmobify_self
85 is te gebruiken wanneer C++ geheugen beheer weer overneemt van
91 Score_element::get_elt_property (const char *nm
) const
93 SCM sym
= ly_symbol2scm (nm
);
94 return get_elt_property (sym
);
98 Score_element::get_elt_property (SCM sym
) const
100 SCM s
= scm_assq(sym
, mutable_property_alist_
);
104 s
= scm_assq (sym
, immutable_property_alist_
);
105 return (s
== SCM_BOOL_F
) ? SCM_UNDEFINED
: gh_cdr (s
);
109 Remove the value associated with KEY, and return it. The result is
110 that a next call will yield SCM_UNDEFINED (and not the underlying
114 Score_element::remove_elt_property (const char* key
)
116 SCM val
= get_elt_property (key
);
117 if (val
!= SCM_UNDEFINED
)
118 set_elt_property (key
, SCM_UNDEFINED
);
123 Score_element::set_elt_property (const char* k
, SCM v
)
125 SCM s
= ly_symbol2scm (k
);
126 set_elt_property (s
, v
);
130 Puts the k, v in the immutable_property_alist_, which is convenient for
131 storing variables that are needed during the breaking process. (eg.
132 Line_of_score::rank : int )
135 Score_element::set_immutable_elt_property (const char*k
, SCM v
)
137 SCM s
= ly_symbol2scm (k
);
138 set_immutable_elt_property (s
, v
);
142 Score_element::set_immutable_elt_property (SCM s
, SCM v
)
144 immutable_property_alist_
= gh_cons (gh_cons (s
,v
), mutable_property_alist_
);
145 mutable_property_alist_
= scm_assq_remove_x (mutable_property_alist_
, s
);
148 Score_element::set_elt_property (SCM s
, SCM v
)
150 mutable_property_alist_
= scm_assq_set_x (mutable_property_alist_
, s
, v
);
155 Score_element::molecule_extent (Score_element
*s
, Axis a
)
157 Molecule m
= s
->get_molecule ();
162 Score_element::preset_extent (Score_element
*s
, Axis a
)
164 SCM ext
= s
->get_elt_property ((a
== X_AXIS
)
170 Real l
= gh_scm2double (gh_car (ext
));
171 Real r
= gh_scm2double (gh_cdr (ext
));
172 l
*= s
->paper_l ()->get_var ("staffspace");
173 r
*= s
->paper_l ()->get_var ("staffspace");
174 return Interval (l
, r
);
183 Score_element::paper_l () const
185 return pscore_l_
? pscore_l_
->paper_l_
: 0;
189 Score_element::lookup_l () const
193 Score_element
* urg
= (Score_element
*)this;
194 SCM sz
= urg
->remove_elt_property ("fontsize");
195 int i
= (gh_number_p (sz
))
199 urg
->lookup_l_
= (Lookup
*)pscore_l_
->paper_l_
->lookup_l (i
);
205 Score_element::add_processing()
207 assert (status_i_
>=0);
216 Score_element::calculate_dependencies (int final
, int busy
, SCM funcname
)
218 assert (status_i_
>=0);
220 if (status_i_
>= final
)
223 if (status_i_
== busy
)
225 programming_error ("Element is busy, come back later");
231 for (SCM d
= get_elt_property ("dependencies"); gh_pair_p (d
); d
= gh_cdr (d
))
233 unsmob_element (gh_car (d
))
234 ->calculate_dependencies (final
, busy
, funcname
);
238 String s
= ly_symbol2string (funcname
);
239 SCM proc
= get_elt_property (s
.ch_C());
240 if (gh_procedure_p (proc
))
241 gh_call1 (proc
, this->self_scm_
);
248 Score_element::get_molecule () const
250 SCM proc
= get_elt_property ("molecule-callback");
251 if (gh_procedure_p (proc
))
252 return create_molecule (gh_apply (proc
, gh_list (this->self_scm_
, SCM_UNDEFINED
)));
264 Score_element::do_break_processing()
269 Score_element::do_space_processing ()
274 Score_element::do_add_processing()
279 MAKE_SCHEME_CALLBACK(Score_element
,brew_molecule
)
285 Score_element::brew_molecule (SCM smob
)
287 Score_element
* sc
= unsmob_element (smob
);
288 SCM glyph
= sc
->get_elt_property ("glyph");
289 if (gh_string_p (glyph
))
291 return sc
->lookup_l ()->afm_find (String (ly_scm2string (glyph
))).create_scheme ();
301 Score_element::line_l() const
307 Score_element::add_dependency (Score_element
*e
)
311 Pointer_group_interface
gi (this, "dependencies");
315 programming_error ("Null dependency added");
322 Do break substitution in S, using CRITERION. Return new value.
323 CRITERION is either a SMOB pointer to the desired line, or a number
324 representing the break direction. Do not modify SRC.
327 Score_element::handle_broken_smobs (SCM src
, SCM criterion
)
330 Score_element
*sc
= unsmob_element (src
);
333 if (gh_number_p (criterion
))
335 Item
* i
= dynamic_cast<Item
*> (sc
);
336 Direction d
= to_dir (criterion
);
337 if (i
&& i
->break_status_dir () != d
)
339 Item
*br
= i
->find_prebroken_piece (d
);
340 return (br
) ? br
->self_scm_
: SCM_UNDEFINED
;
346 = dynamic_cast<Line_of_score
*> (unsmob_element (criterion
));
347 if (sc
->line_l () != line
)
349 sc
= sc
->find_broken_piece (line
);
353 /* now: !sc || (sc && sc->line_l () == line) */
355 return SCM_UNDEFINED
;
357 /* now: sc && sc->line_l () == line */
359 || (sc
->common_refpoint (line
, X_AXIS
)
360 && sc
->common_refpoint (line
, Y_AXIS
)))
362 return sc
->self_scm_
;
364 return SCM_UNDEFINED
;
367 else if (gh_pair_p (src
))
369 SCM oldcar
=gh_car (src
);
371 UGH! breaks on circular lists.
373 SCM newcar
= handle_broken_smobs (oldcar
, criterion
);
374 SCM oldcdr
= gh_cdr (src
);
376 if (newcar
== SCM_UNDEFINED
377 && (gh_pair_p (oldcdr
) || oldcdr
== SCM_EOL
))
380 This is tail-recursion, ie.
382 return handle_broken_smobs (cdr, criterion);
384 We don't want to rely on the compiler to do this. Without
385 tail-recursion, this easily crashes with a stack overflow. */
390 SCM newcdr
= handle_broken_smobs (oldcdr
, criterion
);
391 return gh_cons (newcar
, newcdr
);
400 Score_element::handle_broken_dependencies()
402 Spanner
* s
= dynamic_cast<Spanner
*> (this);
403 if (original_l_
&& s
)
408 for (int i
= 0; i
< s
->broken_into_l_arr_
.size (); i
++)
410 Score_element
* sc
= s
->broken_into_l_arr_
[i
];
411 Line_of_score
* l
= sc
->line_l ();
412 sc
->mutable_property_alist_
=
413 handle_broken_smobs (mutable_property_alist_
,
414 l
? l
->self_scm_
: SCM_UNDEFINED
);
419 Line_of_score
*line
= line_l();
421 if (line
&& common_refpoint (line
, X_AXIS
) && common_refpoint (line
, Y_AXIS
))
423 mutable_property_alist_
424 = handle_broken_smobs (mutable_property_alist_
,
425 line
? line
->self_scm_
: SCM_UNDEFINED
);
427 else if (dynamic_cast <Line_of_score
*> (this))
429 mutable_property_alist_
= handle_broken_smobs (mutable_property_alist_
,
435 This element is `invalid'; it has been removed from all
436 dependencies, so let's junk the element itself.
438 do not do this for Line_of_score, since that would remove
439 references to the originals of score-elts, which get then GC'd
447 Note that we still want references to this element to be
448 rearranged, and not silently thrown away, so we keep pointers
449 like {broken_into_{drul,array}, original}
452 Score_element::suicide ()
454 mutable_property_alist_
= SCM_EOL
;
455 immutable_property_alist_
= SCM_EOL
;
456 set_extent_callback (0, Y_AXIS
);
457 set_extent_callback (0, X_AXIS
);
459 for (int a
= X_AXIS
; a
<= Y_AXIS
; a
++)
461 dim_cache_
[a
].off_callbacks_
.clear ();
466 Score_element::handle_prebroken_dependencies()
471 Score_element::find_broken_piece (Line_of_score
*) const
477 Score_element::translate_axis (Real y
, Axis a
)
479 if (isinf (y
) || isnan (y
))
480 programming_error (_(INFINITY_MSG
));
483 dim_cache_
[a
].offset_
+= y
;
488 Score_element::relative_coordinate (Score_element
const*refp
, Axis a
) const
494 We catch PARENT_L_ == nil case with this, but we crash if we did
495 not ask for the absolute coordinate (ie. REFP == nil.)
498 if (refp
== dim_cache_
[a
].parent_l_
)
499 return get_offset (a
);
501 return get_offset (a
) + dim_cache_
[a
].parent_l_
->relative_coordinate (refp
, a
);
505 Score_element::get_offset (Axis a
) const
507 Score_element
*me
= (Score_element
*) this;
508 while (dim_cache_
[a
].off_callbacks_
.size ())
510 Offset_callback c
= dim_cache_
[a
].off_callbacks_
[0];
511 me
->dim_cache_
[a
].off_callbacks_
.del (0);
512 Real r
= (*c
) (me
,a
);
513 if (isinf (r
) || isnan (r
))
516 programming_error (INFINITY_MSG
);
518 me
->dim_cache_
[a
].offset_
+=r
;
520 return dim_cache_
[a
].offset_
;
525 Score_element::point_dimension_callback (Score_element
* , Axis
)
527 return Interval (0,0);
531 Score_element::empty_b (Axis a
)const
533 return !dim_cache_
[a
].extent_callback_l_
;
537 Score_element::extent (Axis a
) const
539 Dimension_cache
* d
= (Dimension_cache
*)&dim_cache_
[a
];
540 if (!d
->extent_callback_l_
)
542 d
->dim_
.set_empty ();
544 else if (!d
->valid_b_
)
546 d
->dim_
= (*d
->extent_callback_l_
) ((Score_element
*)this, a
);
550 Interval ext
= d
->dim_
;
555 SCM extra
= get_elt_property (a
== X_AXIS
562 Real s
= paper_l ()->get_var ("staffspace");
563 if (gh_pair_p (extra
))
565 ext
[BIGGER
] += s
* gh_scm2double (gh_cdr (extra
));
566 ext
[SMALLER
] += s
* gh_scm2double (gh_car (extra
));
569 extra
= get_elt_property (a
== X_AXIS
571 : "minimum-extent-Y");
572 if (gh_pair_p (extra
))
574 ext
.unite (Interval (s
* gh_scm2double (gh_car (extra
)),
575 s
* gh_scm2double (gh_cdr (extra
))));
583 Score_element::parent_l (Axis a
) const
585 return dim_cache_
[a
].parent_l_
;
589 Score_element::common_refpoint (Score_element
const* s
, Axis a
) const
592 I don't like the quadratic aspect of this code, but I see no other
593 way. The largest chain of parents might be 10 high or so, so
594 it shouldn't be a real issue. */
595 for (Score_element
const *c
= this; c
; c
= c
->dim_cache_
[a
].parent_l_
)
596 for (Score_element
const * d
= s
; d
; d
= d
->dim_cache_
[a
].parent_l_
)
598 return (Score_element
*)d
;
605 Score_element::common_refpoint (SCM elist
, Axis a
) const
607 Score_element
* common
= (Score_element
*) this;
608 for (; gh_pair_p (elist
); elist
= gh_cdr (elist
))
610 Score_element
* s
= unsmob_element (gh_car (elist
));
612 common
= common
->common_refpoint (s
, a
);
619 Score_element::name () const
621 return classname (this);
625 Score_element::add_offset_callback (Offset_callback cb
, Axis a
)
627 dim_cache_
[a
].off_callbacks_
.push (cb
);
631 Score_element::has_extent_callback_b (Extent_callback cb
, Axis a
)const
633 return cb
== dim_cache_
[a
].extent_callback_l_
;
637 Score_element::has_offset_callback_b (Offset_callback cb
, Axis a
)const
639 for (int i
= dim_cache_
[a
].off_callbacks_
.size (); i
--;)
641 if (dim_cache_
[a
].off_callbacks_
[i
] == cb
)
648 Score_element::set_extent_callback (Dim_cache_callback dc
, Axis a
)
650 dim_cache_
[a
].extent_callback_l_
= dc
;
655 Score_element::set_parent (Score_element
*g
, Axis a
)
657 dim_cache_
[a
].parent_l_
= g
;
660 MAKE_SCHEME_CALLBACK(Score_element
,fixup_refpoint
);
662 Score_element::fixup_refpoint (SCM smob
)
664 Score_element
*me
= unsmob_element (smob
);
665 for (int a
= X_AXIS
; a
< NO_AXES
; a
++)
668 Score_element
* parent
= me
->parent_l (ax
);
673 if (parent
->line_l () != me
->line_l () && me
->line_l ())
675 Score_element
* newparent
= parent
->find_broken_piece (me
->line_l ());
676 me
->set_parent (newparent
, ax
);
679 if (Item
* i
= dynamic_cast<Item
*> (me
))
681 Item
*parenti
= dynamic_cast<Item
*> (parent
);
685 Direction my_dir
= i
->break_status_dir () ;
686 if (my_dir
!= parenti
->break_status_dir())
688 Item
*newparent
= parenti
->find_prebroken_piece (my_dir
);
689 me
->set_parent (newparent
, ax
);
699 /****************************************************
701 ****************************************************/
703 #include "ly-smobs.icc"
705 IMPLEMENT_UNSMOB(Score_element
, element
);
706 IMPLEMENT_SMOBS(Score_element
);
709 Score_element::mark_smob (SCM ses
)
711 Score_element
* s
= SMOB_TO_TYPE (Score_element
, ses
);
712 if (s
->self_scm_
!= ses
)
714 programming_error ("SMOB marking gone awry");
717 scm_gc_mark ( s
->immutable_property_alist_
);
719 s
->do_derived_mark ();
720 if (s
->parent_l (Y_AXIS
))
721 scm_gc_mark (s
->parent_l (Y_AXIS
)->self_scm_
);
722 if (s
->parent_l (X_AXIS
))
723 scm_gc_mark (s
->parent_l (X_AXIS
)->self_scm_
);
725 return s
->mutable_property_alist_
;
729 Score_element::print_smob (SCM s
, SCM port
, scm_print_state
*)
731 Score_element
*sc
= (Score_element
*) gh_cdr (s
);
733 scm_puts ("#<Score_element ", port
);
734 scm_puts ((char *)sc
->name (), port
);
737 don't try to print properties, that is too much hassle.
739 scm_puts (" >", port
);
744 Score_element::do_derived_mark ()
749 Score_element::do_smobify_self ()
754 Score_element::equal_p (SCM a
, SCM b
)
756 return gh_cdr(a
) == gh_cdr(b
) ? SCM_BOOL_T
: SCM_BOOL_F
;
761 ly_set_elt_property (SCM elt
, SCM sym
, SCM val
)
763 Score_element
* sc
= unsmob_element (elt
);
765 if (!gh_symbol_p (sym
))
767 error ("Not a symbol");
768 ly_display_scm (sym
);
769 return SCM_UNDEFINED
;
774 sc
->set_elt_property (sym
, val
);
778 error ("Not a score element");
779 ly_display_scm (elt
);
782 return SCM_UNDEFINED
;
787 ly_get_elt_property (SCM elt
, SCM sym
)
789 Score_element
* sc
= unsmob_element (elt
);
793 return sc
->get_elt_property (sym
);
797 error ("Not a score element");
798 ly_display_scm (elt
);
800 return SCM_UNDEFINED
;
805 Score_element::discretionary_processing()
812 spanner_get_bound (SCM slur
, SCM dir
)
814 return dynamic_cast<Spanner
*> (unsmob_element (slur
))->get_bound (to_dir (dir
))->self_scm_
;
819 static SCM interfaces_sym
;
824 interfaces_sym
= scm_permanent_object (ly_symbol2scm ("interfaces"));
826 scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))ly_get_elt_property
);
827 scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))ly_set_elt_property
);
828 scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (SCM(*)(...)) spanner_get_bound
);
832 Score_element::has_interface (SCM k
)
834 if (mutable_property_alist_
== SCM_EOL
)
837 SCM ifs
= get_elt_property (interfaces_sym
);
839 return scm_memq (k
, ifs
) != SCM_BOOL_F
;
843 Score_element::set_interface (SCM k
)
845 if (has_interface (k
))
849 set_elt_property (interfaces_sym
,
850 gh_cons (k
, get_elt_property (interfaces_sym
)));
855 ADD_SCM_INIT_FUNC(scoreelt
, init_functions
);