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