]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-aligment-interface.cc
66e71f0497083a98348dfe50e96ee7fefeec95f3
[lilypond.git] / lily / self-aligment-interface.cc
1 /*
2   self-alignment-interface.cc -- implement Self_alignment_interface
3  
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "self-alignment-interface.hh"
10
11 #include "warn.hh"
12 #include "paper-column.hh"
13 #include "grob.hh"
14
15 MAKE_SCHEME_CALLBACK (Self_alignment_interface, y_aligned_on_self, 1);
16 SCM
17 Self_alignment_interface::y_aligned_on_self (SCM element)
18 {
19   return aligned_on_self (unsmob_grob (element), Y_AXIS);
20 }
21
22 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_aligned_on_self, 1);
23 SCM
24 Self_alignment_interface::x_aligned_on_self (SCM element)
25 {
26   return aligned_on_self (unsmob_grob (element), X_AXIS);
27 }
28
29 SCM
30 Self_alignment_interface::aligned_on_self (Grob *me, Axis a)
31 {
32   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
33     : ly_symbol2scm ("self-alignment-Y");
34
35   SCM align (me->internal_get_property (sym));
36   if (scm_is_number (align))
37     {
38       Interval ext (me->extent (me, a));
39       if (ext.is_empty ())
40         programming_error ("cannot align on self: empty element");
41       else
42         return scm_from_double (- ext.linear_combination (scm_to_double (align)));
43     }
44   return scm_from_double (0.0);
45 }
46
47
48
49 SCM
50 Self_alignment_interface::centered_on_object (Grob *him, Axis a)
51 {
52   return scm_from_double (robust_relative_extent (him, him, a).center ());
53 }
54
55
56 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_x_parent, 1);
57 SCM
58 Self_alignment_interface::centered_on_x_parent (SCM smob)
59 {
60   return centered_on_object (unsmob_grob (smob)->get_parent (X_AXIS), X_AXIS);
61 }
62
63
64 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_y_parent, 1);
65 SCM
66 Self_alignment_interface::centered_on_y_parent (SCM smob)
67 {
68   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), Y_AXIS);
69 }
70
71
72 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_centered_on_y_parent, 1);
73 SCM
74 Self_alignment_interface::x_centered_on_y_parent (SCM smob)
75 {
76   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), X_AXIS);
77 }
78
79 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_x_parent,1);
80 SCM
81 Self_alignment_interface::aligned_on_x_parent (SCM smob)
82 {
83   return aligned_on_parent (unsmob_grob (smob), X_AXIS);
84 }
85
86 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_y_parent,1);
87 SCM
88 Self_alignment_interface::aligned_on_y_parent (SCM smob)
89 {
90   return aligned_on_parent (unsmob_grob (smob), Y_AXIS);
91 }
92
93 SCM
94 Self_alignment_interface::aligned_on_parent (Grob *me, Axis a)
95 {
96   Grob *him = me->get_parent (a);
97   if (Paper_column::has_interface (him))
98     return scm_from_double (0.0);
99     
100   Interval he = him->extent (him, a);
101
102   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
103     : ly_symbol2scm ("self-alignment-Y");
104   SCM align_prop (me->internal_get_property (sym));
105
106   if (!scm_is_number (align_prop))
107     return scm_from_int (0);
108
109   Real x = 0.0;
110   Real align = scm_to_double (align_prop);
111
112   Interval ext (me->extent (me, a));
113   if (ext.is_empty ())
114     programming_error ("cannot align on self: empty element");
115   else
116     x -= ext.linear_combination (align);
117
118   if (!he.is_empty ())
119     x += he.linear_combination (align);
120
121   return scm_from_double (x);
122 }
123
124 void
125 Self_alignment_interface::set_center_parent (Grob *me, Axis a)
126 {
127   add_offset_callback (me,
128                        (a==X_AXIS) ? centered_on_x_parent_proc : centered_on_y_parent_proc,
129                        a);
130 }
131
132 void
133 Self_alignment_interface::set_align_self (Grob *me, Axis a)
134 {
135   add_offset_callback (me,
136                        (a==X_AXIS) ? x_aligned_on_self_proc : y_aligned_on_self_proc,
137                        a);
138 }
139
140 ADD_INTERFACE (Self_alignment_interface,
141                "Position this object on itself and/or on its parent.  To this"
142                " end, the following functions are provided:\n"
143                "\n"
144                "@table @code\n"
145                "@item Self_alignment_interface::[xy]_aligned_on_self\n"
146                "Align self on reference point, using"
147                " @code{self-alignment-X} and @code{self-alignment-Y}."
148                "@item Self_alignment_interface::aligned_on_[xy]_parent\n"
149                "@item Self_alignment_interface::centered_on_[xy]_parent\n"
150                "Shift the object so its own reference point is centered on"
151                " the extent of the parent\n"
152                "@end table\n",
153
154                /* properties */
155                "self-alignment-X "
156                "self-alignment-Y "
157                );
158