]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-alignment-interface.cc
Merge branch 'master' into lilypond/translation
[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 "grob.hh"
23 #include "paper-column.hh"
24 #include "warn.hh"
25
26 MAKE_SCHEME_CALLBACK (Self_alignment_interface, y_aligned_on_self, 1);
27 SCM
28 Self_alignment_interface::y_aligned_on_self (SCM element)
29 {
30   return aligned_on_self (unsmob_grob (element), Y_AXIS, false, 0, 0);
31 }
32
33 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_aligned_on_self, 1);
34 SCM
35 Self_alignment_interface::x_aligned_on_self (SCM element)
36 {
37   return aligned_on_self (unsmob_grob (element), X_AXIS, false, 0, 0);
38 }
39
40 MAKE_SCHEME_CALLBACK (Self_alignment_interface, pure_y_aligned_on_self, 3);
41 SCM
42 Self_alignment_interface::pure_y_aligned_on_self (SCM smob, SCM start, SCM end)
43 {
44   return aligned_on_self (unsmob_grob (smob), Y_AXIS, true, robust_scm2int (start, 0), robust_scm2int (end, INT_MAX));
45 }
46
47 SCM
48 Self_alignment_interface::aligned_on_self (Grob *me, Axis a, bool pure, int start, int end)
49 {
50   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
51     : ly_symbol2scm ("self-alignment-Y");
52
53   SCM align (me->internal_get_property (sym));
54   if (scm_is_number (align))
55     {
56       Interval ext (me->maybe_pure_extent (me, a, pure, start, end));
57       if (ext.is_empty ())
58         programming_error ("cannot align on self: empty element");
59       else
60         return scm_from_double (- ext.linear_combination (scm_to_double (align)));
61     }
62   return scm_from_double (0.0);
63 }
64
65
66 SCM
67 Self_alignment_interface::centered_on_object (Grob *him, Axis a)
68 {
69   return scm_from_double (robust_relative_extent (him, him, a).center ());
70 }
71
72 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_x_parent, 1);
73 SCM
74 Self_alignment_interface::centered_on_x_parent (SCM smob)
75 {
76   return centered_on_object (unsmob_grob (smob)->get_parent (X_AXIS), X_AXIS);
77 }
78
79 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_y_parent, 1);
80 SCM
81 Self_alignment_interface::centered_on_y_parent (SCM smob)
82 {
83   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), Y_AXIS);
84 }
85
86 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_centered_on_y_parent, 1);
87 SCM
88 Self_alignment_interface::x_centered_on_y_parent (SCM smob)
89 {
90   return centered_on_object (unsmob_grob (smob)->get_parent (Y_AXIS), X_AXIS);
91 }
92
93 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_x_parent,1);
94 SCM
95 Self_alignment_interface::aligned_on_x_parent (SCM smob)
96 {
97   return aligned_on_parent (unsmob_grob (smob), X_AXIS);
98 }
99
100 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_y_parent,1);
101 SCM
102 Self_alignment_interface::aligned_on_y_parent (SCM smob)
103 {
104   return aligned_on_parent (unsmob_grob (smob), Y_AXIS);
105 }
106
107 SCM
108 Self_alignment_interface::aligned_on_parent (Grob *me, Axis a)
109 {
110   Grob *him = me->get_parent (a);
111   if (Paper_column::has_interface (him))
112     return scm_from_double (0.0);
113
114   Interval he = him->extent (him, a);
115
116   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
117     : ly_symbol2scm ("self-alignment-Y");
118   SCM align_prop (me->internal_get_property (sym));
119
120   if (!scm_is_number (align_prop))
121     return scm_from_int (0);
122
123   Real x = 0.0;
124   Real align = scm_to_double (align_prop);
125
126   Interval ext (me->extent (me, a));
127   if (ext.is_empty ())
128     programming_error ("cannot align on self: empty element");
129   else
130     x -= ext.linear_combination (align);
131
132   if (!he.is_empty ())
133     x += he.linear_combination (align);
134
135   return scm_from_double (x);
136 }
137
138 void
139 Self_alignment_interface::set_center_parent (Grob *me, Axis a)
140 {
141   add_offset_callback (me,
142                        (a == X_AXIS) ? centered_on_x_parent_proc : centered_on_y_parent_proc,
143                        a);
144 }
145
146 void
147 Self_alignment_interface::set_align_self (Grob *me, Axis a)
148 {
149   add_offset_callback (me,
150                        (a == X_AXIS) ? x_aligned_on_self_proc : y_aligned_on_self_proc,
151                        a);
152 }
153
154 ADD_INTERFACE (Self_alignment_interface,
155                "Position this object on itself and/or on its parent.  To this"
156                " end, the following functions are provided:\n"
157                "\n"
158                "@table @code\n"
159                "@item Self_alignment_interface::[xy]_aligned_on_self\n"
160                "Align self on reference point, using"
161                " @code{self-alignment-X} and @code{self-alignment-Y}."
162                "@item Self_alignment_interface::aligned_on_[xy]_parent\n"
163                "@item Self_alignment_interface::centered_on_[xy]_parent\n"
164                "Shift the object so its own reference point is centered on"
165                " the extent of the parent\n"
166                "@end table\n",
167
168                /* properties */
169                "self-alignment-X "
170                "self-alignment-Y "
171                );