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