]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-alignment-interface.cc
Update d/copyright
[lilypond.git] / lily / self-alignment-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "self-alignment-interface.hh"
21
22 #include "directional-element-interface.hh"
23 #include "grob.hh"
24 #include "grob-array.hh"
25 #include "interval-minefield.hh"
26 #include "note-column.hh"
27 #include "paper-column.hh"
28 #include "pointer-group-interface.hh"
29 #include "warn.hh"
30
31 MAKE_SCHEME_CALLBACK (Self_alignment_interface, y_aligned_on_self, 1);
32 SCM
33 Self_alignment_interface::y_aligned_on_self (SCM element)
34 {
35   return aligned_on_self (unsmob_grob (element), Y_AXIS, false, 0, 0);
36 }
37
38 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_aligned_on_self, 1);
39 SCM
40 Self_alignment_interface::x_aligned_on_self (SCM element)
41 {
42   return aligned_on_self (unsmob_grob (element), X_AXIS, false, 0, 0);
43 }
44
45 MAKE_SCHEME_CALLBACK (Self_alignment_interface, pure_y_aligned_on_self, 3);
46 SCM
47 Self_alignment_interface::pure_y_aligned_on_self (SCM smob, SCM start, SCM end)
48 {
49   return aligned_on_self (unsmob_grob (smob), Y_AXIS, true, robust_scm2int (start, 0), robust_scm2int (end, INT_MAX));
50 }
51
52 SCM
53 Self_alignment_interface::aligned_on_self (Grob *me, Axis a, bool pure, int start, int end)
54 {
55   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
56             : ly_symbol2scm ("self-alignment-Y");
57
58   SCM align (me->internal_get_property (sym));
59   if (scm_is_number (align))
60     {
61       Interval ext (me->maybe_pure_extent (me, a, pure, start, end));
62       // Empty extent doesn't mean an error - we simply don't align such grobs.
63       // However, empty extent and non-empty stencil would be suspicious.
64       if (!ext.is_empty ())
65         return scm_from_double (- ext.linear_combination (scm_to_double (align)));
66       else if (me->get_stencil ())
67         warning (me->name () + " has empty extent and non-empty stencil.");
68     }
69   return scm_from_double (0.0);
70 }
71
72 SCM
73 Self_alignment_interface::centered_on_object (Grob *him, Axis a)
74 {
75   return scm_from_double (robust_relative_extent (him, him, a).center ());
76 }
77
78 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_x_parent, 1);
79 SCM
80 Self_alignment_interface::centered_on_x_parent (SCM smob)
81 {
82   return centered_on_object (unsmob_grob (smob)->get_parent (X_AXIS), X_AXIS);
83 }
84
85 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_note_columns, 1);
86 SCM
87 Self_alignment_interface::centered_on_note_columns (SCM smob)
88 {
89   Item *it = unsmob_item (smob)->get_column ();
90   if (!it)
91     return scm_from_double (0.0);
92
93   extract_grob_set (it, "elements", elts);
94   vector<Grob *> ncs;
95   Interval centers;
96   for (vsize i = 0; i < elts.size (); i++)
97     if (Note_column::has_interface (elts[i]))
98       centers.add_point (scm_to_double (centered_on_object (elts[i], X_AXIS)));
99
100   if (centers.is_empty ())
101     return scm_from_double (0.0);
102
103   return scm_from_double (centers.center ());
104 }
105
106 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_y_parent, 1);
107 SCM
108 Self_alignment_interface::centered_on_y_parent (SCM smob)
109 {
110   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), Y_AXIS);
111 }
112
113 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_centered_on_y_parent, 1);
114 SCM
115 Self_alignment_interface::x_centered_on_y_parent (SCM smob)
116 {
117   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), X_AXIS);
118 }
119
120 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_x_parent, 1);
121 SCM
122 Self_alignment_interface::aligned_on_x_parent (SCM smob)
123 {
124   return aligned_on_parent (unsmob_grob (smob), X_AXIS);
125 }
126
127 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_y_parent, 1);
128 SCM
129 Self_alignment_interface::aligned_on_y_parent (SCM smob)
130 {
131   return aligned_on_parent (unsmob_grob (smob), Y_AXIS);
132 }
133
134 SCM
135 Self_alignment_interface::aligned_on_parent (Grob *me, Axis a)
136 {
137   Grob *him = me->get_parent (a);
138   if (Paper_column::has_interface (him))
139     return scm_from_double (0.0);
140
141   Interval he = him->extent (him, a);
142
143   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
144             : ly_symbol2scm ("self-alignment-Y");
145   SCM align_prop (me->internal_get_property (sym));
146
147   if (!scm_is_number (align_prop))
148     return scm_from_int (0);
149
150   Real x = 0.0;
151   Real align = scm_to_double (align_prop);
152
153   Interval ext (me->extent (me, a));
154
155   // Empty extent doesn't mean an error - we simply don't align such grobs.
156   // However, empty extent and non-empty stencil would be suspicious.
157   if (!ext.is_empty ())
158     x -= ext.linear_combination (align);
159   else if (me->get_stencil ())
160     warning (me->name () + " has empty extent and non-empty stencil.");
161
162   // See comment above.
163   if (!he.is_empty ())
164     x += he.linear_combination (align);
165   else if (him->get_stencil ())
166     warning (him->name () + " has empty extent and non-empty stencil.");
167
168   return scm_from_double (x);
169 }
170
171 void
172 Self_alignment_interface::set_center_parent (Grob *me, Axis a)
173 {
174   add_offset_callback (me,
175                        (a == X_AXIS) ? centered_on_x_parent_proc : centered_on_y_parent_proc,
176                        a);
177 }
178
179 void
180 Self_alignment_interface::set_align_self (Grob *me, Axis a)
181 {
182   add_offset_callback (me,
183                        (a == X_AXIS) ? x_aligned_on_self_proc : y_aligned_on_self_proc,
184                        a);
185 }
186
187 ADD_INTERFACE (Self_alignment_interface,
188                "Position this object on itself and/or on its parent.  To this"
189                " end, the following functions are provided:\n"
190                "\n"
191                "@table @code\n"
192                "@item Self_alignment_interface::[xy]_aligned_on_self\n"
193                "Align self on reference point, using"
194                " @code{self-alignment-X} and @code{self-alignment-Y}."
195                "@item Self_alignment_interface::aligned_on_[xy]_parent\n"
196                "@item Self_alignment_interface::centered_on_[xy]_parent\n"
197                "Shift the object so its own reference point is centered on"
198                " the extent of the parent\n"
199                "@end table\n",
200
201                /* properties */
202                "collision-bias "
203                "collision-padding "
204                "potential-X-colliding-grobs "
205                "self-alignment-X "
206                "self-alignment-Y "
207                "X-colliding-grobs "
208                "Y-colliding-grobs "
209               );