2 self-alignment-interface.cc
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "self-alignment-interface.hh"
12 /* Position centered on parent. */
13 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, centered_on_parent
, 2);
15 Self_alignment_interface::centered_on_parent (SCM element_smob
, SCM axis
)
17 Grob
*me
= unsmob_grob (element_smob
);
18 Axis a
= (Axis
) ly_scm2int (axis
);
19 Grob
*him
= me
->get_parent (a
);
20 Interval he
= him
->extent (him
, a
);
22 return scm_make_real (he
.is_empty () ? 0.0 : he
.center ());
25 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, aligned_on_parent
, 2);
27 Self_alignment_interface::aligned_on_parent (SCM element_smob
, SCM axis
)
29 Grob
*me
= unsmob_grob (element_smob
);
30 Axis a
= (Axis
) ly_scm2int (axis
);
31 Grob
*him
= me
->get_parent (a
);
32 Interval he
= him
->extent (him
, a
);
34 SCM sym
= (a
== X_AXIS
) ? ly_symbol2scm ("self-alignment-X")
35 : ly_symbol2scm ("self-alignment-Y");
36 SCM
align_prop (me
->internal_get_property (sym
));
38 if (!ly_c_number_p (align_prop
))
39 return scm_int2num (0);
42 Real align
= ly_scm2double (align_prop
);
44 Interval
ext (me
->extent (me
, a
));
46 programming_error ("I'm empty. Can't align on self");
48 x
-= ext
.linear_combination (align
) ;
51 x
+= he
.linear_combination (align
);
53 return scm_make_real (x
);
56 /* Position centered on parent. */
57 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, centered_on_other_axis_parent
, 2);
59 Self_alignment_interface::centered_on_other_axis_parent (SCM element_smob
,
62 Grob
*me
= unsmob_grob (element_smob
);
63 Axis a
= (Axis
) ly_scm2int (axis
);
64 Grob
*him
= me
->get_parent (other_axis (a
));
65 Interval he
= him
->extent (him
, a
);
67 return scm_make_real (he
.is_empty () ? 0.0 : he
.center ());
70 /** callback that centers the element on itself
71 Requires that self-alignment-{X, Y} be set. */
72 MAKE_SCHEME_CALLBACK (Self_alignment_interface
, aligned_on_self
, 2);
74 Self_alignment_interface::aligned_on_self (SCM element_smob
, SCM axis
)
76 Grob
*me
= unsmob_grob (element_smob
);
77 Axis a
= (Axis
) ly_scm2int (axis
);
79 SCM sym
= (a
== X_AXIS
) ? ly_symbol2scm ("self-alignment-X")
80 : ly_symbol2scm ("self-alignment-Y");
82 SCM
align (me
->internal_get_property (sym
));
83 if (ly_c_number_p (align
))
85 Interval
ext (me
->extent (me
, a
));
87 programming_error ("I'm empty. Can't align on self");
89 return scm_make_real (- ext
.linear_combination (ly_scm2double (align
)));
91 return scm_make_real (0.0);
95 ADD_INTERFACE (Self_alignment_interface
, "self-alignment-interface",
96 "Position this object on itself and/or on its parent. To this end, the following functions "
99 "@item Self_alignment_interface::aligned_on_self\n"
100 " Align self on reference point, using @code{self-alignment-X} and "
101 "@code{self-alignment-Y}."
102 "@item Self_alignment_interface::aligned_on_parent\n"
103 "@item Self_alignment_interface::centered_on_parent\n"
104 " Shift the object so its own reference point is centered on the "
105 " extent of the parent \n"
106 "@item Self_alignment_interface::centered_on_other_axis_parent\n"
107 " For X-axis, center on the Y-parent, and vice versa.\n "
109 "self-alignment-X self-alignment-Y");