]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-alignment-interface.cc
New upstream version 2.19.80
[lilypond.git] / lily / self-alignment-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2015 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 "note-column.hh"
24 #include "paper-column.hh"
25 #include "pointer-group-interface.hh"
26 #include "stencil.hh"
27 #include "warn.hh"
28
29 MAKE_SCHEME_CALLBACK (Self_alignment_interface, y_aligned_on_self, 1);
30 SCM
31 Self_alignment_interface::y_aligned_on_self (SCM element)
32 {
33   return aligned_on_self (unsmob<Grob> (element), Y_AXIS, false, 0, 0);
34 }
35
36 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_aligned_on_self, 1);
37 SCM
38 Self_alignment_interface::x_aligned_on_self (SCM element)
39 {
40   return aligned_on_self (unsmob<Grob> (element), X_AXIS, false, 0, 0);
41 }
42
43 MAKE_SCHEME_CALLBACK (Self_alignment_interface, pure_y_aligned_on_self, 3);
44 SCM
45 Self_alignment_interface::pure_y_aligned_on_self (SCM smob, SCM start, SCM end)
46 {
47   return aligned_on_self (unsmob<Grob> (smob), Y_AXIS, true, robust_scm2int (start, 0), robust_scm2int (end, INT_MAX));
48 }
49
50 SCM
51 Self_alignment_interface::aligned_on_self (Grob *me, Axis a, bool pure, int start, int end)
52 {
53   SCM align = (a == X_AXIS)
54           ? me->get_property ("self-alignment-X")
55           : me->get_property ("self-alignment-Y");
56   if (scm_is_number (align))
57     {
58       Interval ext (me->maybe_pure_extent (me, a, pure, start, end));
59       // Empty extent doesn't mean an error - we simply don't align such grobs.
60       // However, empty extent and non-empty stencil would be suspicious.
61       if (!ext.is_empty ())
62         return scm_from_double (- ext.linear_combination (scm_to_double (align)));
63       else
64         {
65           Stencil *st = me->get_stencil ();
66           if (st && !st->is_empty ())
67             warning (me->name () + " has empty extent and non-empty stencil.");
68         }
69     }
70   return scm_from_double (0.0);
71 }
72
73 SCM
74 Self_alignment_interface::centered_on_object (Grob *him, Axis a)
75 {
76   return scm_from_double (robust_relative_extent (him, him, a).center ());
77 }
78
79 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_x_parent, 1);
80 SCM
81 Self_alignment_interface::centered_on_x_parent (SCM smob)
82 {
83   return centered_on_object (unsmob<Grob> (smob)->get_parent (X_AXIS), X_AXIS);
84 }
85
86 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_y_parent, 1);
87 SCM
88 Self_alignment_interface::centered_on_y_parent (SCM smob)
89 {
90   return centered_on_object (unsmob<Grob> (smob)->get_parent (Y_AXIS), Y_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   Interval he;
112   if (has_interface<Paper_column> (him))
113       /*
114         PaperColumn extents aren't reliable (they depend on size and alignment
115         of PaperColumn's children), so we align on NoteColumn instead.
116         This happens e.g. for lyrics without associatedVoice.
117       */
118     he = Paper_column::get_interface_extent
119               (him, ly_symbol2scm ("note-column-interface"), a);
120   else
121     {
122       if (to_boolean (me->get_property ("X-align-on-main-noteheads"))
123           && has_interface<Note_column> (him))
124         he = Note_column::calc_main_extent(him);
125       else
126         he = him->extent (him, a);
127     }
128
129   SCM self_align = (a == X_AXIS)
130           ? me->get_property ("self-alignment-X")
131           : me->get_property ("self-alignment-Y");
132
133   SCM par_align = (a == X_AXIS)
134           ? me->get_property ("parent-alignment-X")
135           : me->get_property ("parent-alignment-Y");
136
137   if (scm_is_null (par_align))
138       par_align = self_align;
139
140   Real x = 0.0;
141   Interval ext (me->extent (me, a));
142
143   if (scm_is_number (self_align))
144     {
145       // Empty extent doesn't mean an error - we simply don't align such grobs.
146       // However, empty extent and non-empty stencil would be suspicious.
147       if (!ext.is_empty ())
148         x -= ext.linear_combination (scm_to_double (self_align));
149       else
150         {
151           Stencil *st = me->get_stencil ();
152           if (st && !st->is_empty ())
153             warning (me->name () + " has empty extent and non-empty stencil.");
154         }
155     }
156
157   if (scm_is_number (par_align))
158     {
159       // See comment above.
160       if (!he.is_empty ())
161         x += he.linear_combination (scm_to_double (par_align));
162       else
163         {
164           Stencil *st = him->get_stencil ();
165           if (st && !st->is_empty ())
166             warning (him->name () + " has empty extent and non-empty stencil.");
167         }
168     }
169
170   return scm_from_double (x);
171 }
172
173 void
174 Self_alignment_interface::set_aligned_on_parent (Grob *me, Axis a)
175 {
176   add_offset_callback (me,
177                        (a == X_AXIS) ? aligned_on_x_parent_proc : aligned_on_y_parent_proc,
178                        a);
179 }
180
181 ADD_INTERFACE (Self_alignment_interface,
182                "Position this object on itself and/or on its parent.  To this"
183                " end, the following functions are provided:\n"
184                "\n"
185                "@table @code\n"
186                "@item Self_alignment_interface::[xy]_aligned_on_self\n"
187                "Align self on reference point, using"
188                " @code{self-alignment-X} and @code{self-alignment-Y}."
189                "@item Self_alignment_interface::aligned_on_[xy]_parent\n"
190                "@item Self_alignment_interface::centered_on_[xy]_parent\n"
191                "Shift the object so its own reference point is centered on"
192                " the extent of the parent\n"
193                "@end table\n",
194
195                /* properties */
196                "parent-alignment-X "
197                "parent-alignment-Y "
198                "self-alignment-X "
199                "self-alignment-Y "
200                "X-align-on-main-noteheads "
201               );