2 self-alignment-interface.cc -- implement Self_alignment_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "self-alignment-interface.hh"
12 #include "paper-column.hh"
15 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, y_aligned_on_self
, 1);
17 Self_alignment_interface::y_aligned_on_self (SCM element
)
19 return aligned_on_self (unsmob_grob (element
), Y_AXIS
, false, 0, 0);
22 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, x_aligned_on_self
, 1);
24 Self_alignment_interface::x_aligned_on_self (SCM element
)
26 return aligned_on_self (unsmob_grob (element
), X_AXIS
, false, 0, 0);
29 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, pure_y_aligned_on_self
, 3);
31 Self_alignment_interface::pure_y_aligned_on_self (SCM smob
, SCM start
, SCM end
)
33 return aligned_on_self (unsmob_grob (smob
), Y_AXIS
, true, robust_scm2int (start
, 0), robust_scm2int (end
, INT_MAX
));
37 Self_alignment_interface::aligned_on_self (Grob
*me
, Axis a
, bool pure
, int start
, int end
)
39 SCM sym
= (a
== X_AXIS
) ? ly_symbol2scm ("self-alignment-X")
40 : ly_symbol2scm ("self-alignment-Y");
42 SCM
align (me
->internal_get_property (sym
));
43 if (scm_is_number (align
))
45 Interval
ext (me
->maybe_pure_extent (me
, a
, pure
, start
, end
));
47 programming_error ("cannot align on self: empty element");
49 return scm_from_double (- ext
.linear_combination (scm_to_double (align
)));
51 return scm_from_double (0.0);
57 Self_alignment_interface::centered_on_object (Grob
*him
, Axis a
)
59 return scm_from_double (robust_relative_extent (him
, him
, a
).center ());
63 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, centered_on_x_parent
, 1);
65 Self_alignment_interface::centered_on_x_parent (SCM smob
)
67 return centered_on_object (unsmob_grob (smob
)->get_parent (X_AXIS
), X_AXIS
);
71 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, centered_on_y_parent
, 1);
73 Self_alignment_interface::centered_on_y_parent (SCM smob
)
75 return centered_on_object (unsmob_grob (smob
)->get_parent (Y_AXIS
), Y_AXIS
);
79 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, x_centered_on_y_parent
, 1);
81 Self_alignment_interface::x_centered_on_y_parent (SCM smob
)
83 return centered_on_object (unsmob_grob (smob
)->get_parent (Y_AXIS
), X_AXIS
);
86 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, aligned_on_x_parent
,1);
88 Self_alignment_interface::aligned_on_x_parent (SCM smob
)
90 return aligned_on_parent (unsmob_grob (smob
), X_AXIS
);
93 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, aligned_on_y_parent
,1);
95 Self_alignment_interface::aligned_on_y_parent (SCM smob
)
97 return aligned_on_parent (unsmob_grob (smob
), Y_AXIS
);
101 Self_alignment_interface::aligned_on_parent (Grob
*me
, Axis a
)
103 Grob
*him
= me
->get_parent (a
);
104 if (Paper_column::has_interface (him
))
105 return scm_from_double (0.0);
107 Interval he
= him
->extent (him
, a
);
109 SCM sym
= (a
== X_AXIS
) ? ly_symbol2scm ("self-alignment-X")
110 : ly_symbol2scm ("self-alignment-Y");
111 SCM
align_prop (me
->internal_get_property (sym
));
113 if (!scm_is_number (align_prop
))
114 return scm_from_int (0);
117 Real align
= scm_to_double (align_prop
);
119 Interval
ext (me
->extent (me
, a
));
121 programming_error ("cannot align on self: empty element");
123 x
-= ext
.linear_combination (align
);
126 x
+= he
.linear_combination (align
);
128 return scm_from_double (x
);
132 Self_alignment_interface::set_center_parent (Grob
*me
, Axis a
)
134 add_offset_callback (me
,
135 (a
==X_AXIS
) ? centered_on_x_parent_proc
: centered_on_y_parent_proc
,
140 Self_alignment_interface::set_align_self (Grob
*me
, Axis a
)
142 add_offset_callback (me
,
143 (a
==X_AXIS
) ? x_aligned_on_self_proc
: y_aligned_on_self_proc
,
147 ADD_INTERFACE (Self_alignment_interface
,
148 "Position this object on itself and/or on its parent. To this"
149 " end, the following functions are provided:\n"
152 "@item Self_alignment_interface::[xy]_aligned_on_self\n"
153 "Align self on reference point, using"
154 " @code{self-alignment-X} and @code{self-alignment-Y}."
155 "@item Self_alignment_interface::aligned_on_[xy]_parent\n"
156 "@item Self_alignment_interface::centered_on_[xy]_parent\n"
157 "Shift the object so its own reference point is centered on"
158 " the extent of the parent\n"