]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-alignment-interface.cc
Merge branch 'release/unstable' into HEAD
[lilypond.git] / lily / self-alignment-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2011 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 "paper-column.hh"
27 #include "pointer-group-interface.hh"
28 #include "warn.hh"
29
30 MAKE_SCHEME_CALLBACK (Self_alignment_interface, y_aligned_on_self, 1);
31 SCM
32 Self_alignment_interface::y_aligned_on_self (SCM element)
33 {
34   return aligned_on_self (unsmob_grob (element), Y_AXIS, false, 0, 0);
35 }
36
37 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_aligned_on_self, 1);
38 SCM
39 Self_alignment_interface::x_aligned_on_self (SCM element)
40 {
41   return aligned_on_self (unsmob_grob (element), X_AXIS, false, 0, 0);
42 }
43
44 MAKE_SCHEME_CALLBACK (Self_alignment_interface, pure_y_aligned_on_self, 3);
45 SCM
46 Self_alignment_interface::pure_y_aligned_on_self (SCM smob, SCM start, SCM end)
47 {
48   return aligned_on_self (unsmob_grob (smob), Y_AXIS, true, robust_scm2int (start, 0), robust_scm2int (end, INT_MAX));
49 }
50
51 SCM
52 Self_alignment_interface::aligned_on_self (Grob *me, Axis a, bool pure, int start, int end)
53 {
54   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
55             : ly_symbol2scm ("self-alignment-Y");
56
57   SCM align (me->internal_get_property (sym));
58   if (scm_is_number (align))
59     {
60       Interval ext (me->maybe_pure_extent (me, a, pure, start, end));
61       if (ext.is_empty ())
62         programming_error ("cannot align on self: empty element");
63       else
64         return scm_from_double (- ext.linear_combination (scm_to_double (align)));
65     }
66   return scm_from_double (0.0);
67 }
68
69 SCM
70 Self_alignment_interface::centered_on_object (Grob *him, Axis a)
71 {
72   return scm_from_double (robust_relative_extent (him, him, a).center ());
73 }
74
75 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_x_parent, 1);
76 SCM
77 Self_alignment_interface::centered_on_x_parent (SCM smob)
78 {
79   return centered_on_object (unsmob_grob (smob)->get_parent (X_AXIS), X_AXIS);
80 }
81
82 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_y_parent, 1);
83 SCM
84 Self_alignment_interface::centered_on_y_parent (SCM smob)
85 {
86   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), Y_AXIS);
87 }
88
89 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_centered_on_y_parent, 1);
90 SCM
91 Self_alignment_interface::x_centered_on_y_parent (SCM smob)
92 {
93   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), X_AXIS);
94 }
95
96 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_x_parent, 1);
97 SCM
98 Self_alignment_interface::aligned_on_x_parent (SCM smob)
99 {
100   return aligned_on_parent (unsmob_grob (smob), X_AXIS);
101 }
102
103 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_y_parent, 1);
104 SCM
105 Self_alignment_interface::aligned_on_y_parent (SCM smob)
106 {
107   return aligned_on_parent (unsmob_grob (smob), Y_AXIS);
108 }
109
110 SCM
111 Self_alignment_interface::aligned_on_parent (Grob *me, Axis a)
112 {
113   Grob *him = me->get_parent (a);
114   if (Paper_column::has_interface (him))
115     return scm_from_double (0.0);
116
117   Interval he = him->extent (him, a);
118
119   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
120             : ly_symbol2scm ("self-alignment-Y");
121   SCM align_prop (me->internal_get_property (sym));
122
123   if (!scm_is_number (align_prop))
124     return scm_from_int (0);
125
126   Real x = 0.0;
127   Real align = scm_to_double (align_prop);
128
129   Interval ext (me->extent (me, a));
130   if (ext.is_empty ())
131     programming_error ("cannot align on self: empty element");
132   else
133     x -= ext.linear_combination (align);
134
135   if (!he.is_empty ())
136     x += he.linear_combination (align);
137
138   return scm_from_double (x);
139 }
140
141 MAKE_SCHEME_CALLBACK (Self_alignment_interface, avoid_x_colliding_grobs, 2);
142 SCM
143 Self_alignment_interface::avoid_x_colliding_grobs (SCM smob, SCM o)
144 {
145   SCM avoided = avoid_colliding_grobs (unsmob_grob (smob), X_AXIS, robust_scm2double (o, 0.0));
146   return scm_is_null (avoided) ? o : avoided;
147 }
148
149 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_colliding_grobs, 1);
150 SCM
151 Self_alignment_interface::x_colliding_grobs (SCM smob)
152 {
153   Grob *me = unsmob_grob (smob);
154   extract_grob_set (me, "potential-X-colliding-grobs", pot);
155   vector<Grob *> act;
156   Direction d = get_grob_direction (me->get_parent (Y_AXIS));
157   for (vsize i = 0; i < pot.size (); i++)
158     if (d == get_grob_direction (pot[i])
159         && to_boolean (pot[i]->get_property ("cross-staff")))
160       act.push_back (pot[i]);
161
162   SCM grobs_scm = Grob_array::make_array ();
163   unsmob_grob_array (grobs_scm)->set_array (act);
164
165   return grobs_scm;
166 }
167
168 SCM
169 Self_alignment_interface::avoid_colliding_grobs (Grob *me, Axis a, Real offset)
170 {
171   extract_grob_set (me, a == X_AXIS ? "X-colliding-grobs" : "Y-colliding-grobs", colls);
172   if (!colls.size ())
173     return SCM_EOL;
174   vector<Interval> ivs;
175
176   Item *refp = dynamic_cast<Item *> (common_refpoint_of_array (colls, me, a));
177   if (!refp)
178     return SCM_EOL;
179
180   Interval iv = me->extent (me, a) + offset;
181   for (vsize i = 0; i < colls.size (); i++)
182     ivs.push_back (colls[i]->extent (refp, a));
183
184   Interval_minefield minefield (Interval (iv.center (), iv.center ()), iv.length ());
185   for (vsize i = 0; i < ivs.size (); i++)
186     minefield.add_forbidden_interval (ivs[i]);
187   minefield.solve ();
188   Interval pos = minefield.feasible_placements ();
189
190   if (pos[LEFT] == pos[RIGHT])
191     return SCM_EOL;
192
193   Direction col_dir = ((abs (pos[LEFT] - iv.center ())
194                         + robust_scm2double (me->get_property ("collision-bias"), 0.0))
195                        > abs (pos[RIGHT] - iv.center ()))
196                       ? RIGHT
197                       : LEFT;
198
199   return scm_from_double ((pos[col_dir] - (iv.length () / 2)
200                           + col_dir
201                           * robust_scm2double (me->get_property ("collision-padding"), 0.0)));
202 }
203
204 void
205 Self_alignment_interface::set_center_parent (Grob *me, Axis a)
206 {
207   add_offset_callback (me,
208                        (a == X_AXIS) ? centered_on_x_parent_proc : centered_on_y_parent_proc,
209                        a);
210 }
211
212 void
213 Self_alignment_interface::avoid_x_collisions (Grob *me)
214 {
215   chain_offset_callback (me, avoid_x_colliding_grobs_proc, X_AXIS);
216 }
217
218 void
219 Self_alignment_interface::set_align_self (Grob *me, Axis a)
220 {
221   add_offset_callback (me,
222                        (a == X_AXIS) ? x_aligned_on_self_proc : y_aligned_on_self_proc,
223                        a);
224 }
225
226 ADD_INTERFACE (Self_alignment_interface,
227                "Position this object on itself and/or on its parent.  To this"
228                " end, the following functions are provided:\n"
229                "\n"
230                "@table @code\n"
231                "@item Self_alignment_interface::[xy]_aligned_on_self\n"
232                "Align self on reference point, using"
233                " @code{self-alignment-X} and @code{self-alignment-Y}."
234                "@item Self_alignment_interface::aligned_on_[xy]_parent\n"
235                "@item Self_alignment_interface::centered_on_[xy]_parent\n"
236                "Shift the object so its own reference point is centered on"
237                " the extent of the parent\n"
238                "@end table\n",
239
240                /* properties */
241                "collision-bias "
242                "collision-padding "
243                "potential-X-colliding-grobs "
244                "self-alignment-X "
245                "self-alignment-Y "
246                "X-colliding-grobs "
247                "Y-colliding-grobs "
248               );