]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-alignment-interface.cc
Imported Upstream version 2.16.0
[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       if (!ext.is_empty ())
63         return scm_from_double (- ext.linear_combination (scm_to_double (align)));
64     }
65   return scm_from_double (0.0);
66 }
67
68 SCM
69 Self_alignment_interface::centered_on_object (Grob *him, Axis a)
70 {
71   return scm_from_double (robust_relative_extent (him, him, a).center ());
72 }
73
74 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_x_parent, 1);
75 SCM
76 Self_alignment_interface::centered_on_x_parent (SCM smob)
77 {
78   return centered_on_object (unsmob_grob (smob)->get_parent (X_AXIS), X_AXIS);
79 }
80
81 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_note_columns, 1);
82 SCM
83 Self_alignment_interface::centered_on_note_columns (SCM smob)
84 {
85   Item *it = unsmob_item (smob)->get_column ();
86   if (!it)
87     return scm_from_double (0.0);
88
89   extract_grob_set (it, "elements", elts);
90   vector<Grob *> ncs;
91   Interval centers;
92   for (vsize i = 0; i < elts.size (); i++)
93     if (Note_column::has_interface (elts[i]))
94       centers.add_point (scm_to_double (centered_on_object (elts[i], X_AXIS)));
95
96   if (centers.is_empty ())
97     return scm_from_double (0.0);
98
99   return scm_from_double (centers.center ());
100 }
101
102 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_y_parent, 1);
103 SCM
104 Self_alignment_interface::centered_on_y_parent (SCM smob)
105 {
106   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), Y_AXIS);
107 }
108
109 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_centered_on_y_parent, 1);
110 SCM
111 Self_alignment_interface::x_centered_on_y_parent (SCM smob)
112 {
113   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), X_AXIS);
114 }
115
116 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_x_parent, 1);
117 SCM
118 Self_alignment_interface::aligned_on_x_parent (SCM smob)
119 {
120   return aligned_on_parent (unsmob_grob (smob), X_AXIS);
121 }
122
123 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_y_parent, 1);
124 SCM
125 Self_alignment_interface::aligned_on_y_parent (SCM smob)
126 {
127   return aligned_on_parent (unsmob_grob (smob), Y_AXIS);
128 }
129
130 SCM
131 Self_alignment_interface::aligned_on_parent (Grob *me, Axis a)
132 {
133   Grob *him = me->get_parent (a);
134   if (Paper_column::has_interface (him))
135     return scm_from_double (0.0);
136
137   Interval he = him->extent (him, a);
138
139   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
140             : ly_symbol2scm ("self-alignment-Y");
141   SCM align_prop (me->internal_get_property (sym));
142
143   if (!scm_is_number (align_prop))
144     return scm_from_int (0);
145
146   Real x = 0.0;
147   Real align = scm_to_double (align_prop);
148
149   Interval ext (me->extent (me, a));
150   if (ext.is_empty ())
151     programming_error ("cannot align on self: empty element");
152   else
153     x -= ext.linear_combination (align);
154
155   if (!he.is_empty ())
156     x += he.linear_combination (align);
157
158   return scm_from_double (x);
159 }
160
161 MAKE_SCHEME_CALLBACK (Self_alignment_interface, avoid_x_colliding_grobs, 2);
162 SCM
163 Self_alignment_interface::avoid_x_colliding_grobs (SCM smob, SCM o)
164 {
165   SCM avoided = avoid_colliding_grobs (unsmob_grob (smob), X_AXIS, robust_scm2double (o, 0.0));
166   return scm_is_null (avoided) ? o : avoided;
167 }
168
169 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_colliding_grobs, 1);
170 SCM
171 Self_alignment_interface::x_colliding_grobs (SCM smob)
172 {
173   Grob *me = unsmob_grob (smob);
174   extract_grob_set (me, "potential-X-colliding-grobs", pot);
175   vector<Grob *> act;
176   Direction d = get_grob_direction (me->get_parent (Y_AXIS));
177   for (vsize i = 0; i < pot.size (); i++)
178     if (d == get_grob_direction (pot[i])
179         && to_boolean (pot[i]->get_property ("cross-staff")))
180       act.push_back (pot[i]);
181
182   SCM grobs_scm = Grob_array::make_array ();
183   unsmob_grob_array (grobs_scm)->set_array (act);
184
185   return grobs_scm;
186 }
187
188 SCM
189 Self_alignment_interface::avoid_colliding_grobs (Grob *me, Axis a, Real offset)
190 {
191   extract_grob_set (me, a == X_AXIS ? "X-colliding-grobs" : "Y-colliding-grobs", colls);
192   if (!colls.size ())
193     return SCM_EOL;
194   vector<Interval> ivs;
195
196   Item *refp = dynamic_cast<Item *> (common_refpoint_of_array (colls, me, a));
197   if (!refp)
198     return SCM_EOL;
199
200   Interval iv = me->extent (me, a) + offset;
201   for (vsize i = 0; i < colls.size (); i++)
202     ivs.push_back (colls[i]->extent (refp, a));
203
204   Interval_minefield minefield (Interval (iv.center (), iv.center ()), iv.length ());
205   for (vsize i = 0; i < ivs.size (); i++)
206     minefield.add_forbidden_interval (ivs[i]);
207   minefield.solve ();
208   Interval pos = minefield.feasible_placements ();
209
210   if (pos[LEFT] == pos[RIGHT])
211     return SCM_EOL;
212
213   Direction col_dir = ((abs (pos[LEFT] - iv.center ())
214                         + robust_scm2double (me->get_property ("collision-bias"), 0.0))
215                        > abs (pos[RIGHT] - iv.center ()))
216                       ? RIGHT
217                       : LEFT;
218
219   return scm_from_double ((pos[col_dir] - (iv.length () / 2)
220                            + col_dir
221                            * robust_scm2double (me->get_property ("collision-padding"), 0.0)));
222 }
223
224 void
225 Self_alignment_interface::set_center_parent (Grob *me, Axis a)
226 {
227   add_offset_callback (me,
228                        (a == X_AXIS) ? centered_on_x_parent_proc : centered_on_y_parent_proc,
229                        a);
230 }
231
232 void
233 Self_alignment_interface::avoid_x_collisions (Grob *me)
234 {
235   chain_offset_callback (me, avoid_x_colliding_grobs_proc, X_AXIS);
236 }
237
238 void
239 Self_alignment_interface::set_align_self (Grob *me, Axis a)
240 {
241   add_offset_callback (me,
242                        (a == X_AXIS) ? x_aligned_on_self_proc : y_aligned_on_self_proc,
243                        a);
244 }
245
246 ADD_INTERFACE (Self_alignment_interface,
247                "Position this object on itself and/or on its parent.  To this"
248                " end, the following functions are provided:\n"
249                "\n"
250                "@table @code\n"
251                "@item Self_alignment_interface::[xy]_aligned_on_self\n"
252                "Align self on reference point, using"
253                " @code{self-alignment-X} and @code{self-alignment-Y}."
254                "@item Self_alignment_interface::aligned_on_[xy]_parent\n"
255                "@item Self_alignment_interface::centered_on_[xy]_parent\n"
256                "Shift the object so its own reference point is centered on"
257                " the extent of the parent\n"
258                "@end table\n",
259
260                /* properties */
261                "collision-bias "
262                "collision-padding "
263                "potential-X-colliding-grobs "
264                "self-alignment-X "
265                "self-alignment-Y "
266                "X-colliding-grobs "
267                "Y-colliding-grobs "
268               );