]> git.donarmstrong.com Git - lilypond.git/blob - lily/self-aligment-interface.cc
* lily/rest.cc (y_offset_callback): merge function of 3 callbacks.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "self-alignment-interface.hh"
10 #include "warn.hh"
11
12 MAKE_SCHEME_CALLBACK (Self_alignment_interface, y_aligned_on_self, 1);
13 SCM
14 Self_alignment_interface::y_aligned_on_self (SCM element)
15 {
16   return aligned_on_self (unsmob_grob (element), Y_AXIS);
17 }
18
19 MAKE_SCHEME_CALLBACK (Self_alignment_interface, x_aligned_on_self, 1);
20 SCM
21 Self_alignment_interface::x_aligned_on_self (SCM element)
22 {
23   return aligned_on_self (unsmob_grob (element), X_AXIS);
24 }
25
26 SCM
27 Self_alignment_interface::aligned_on_self (Grob *me, Axis a)
28 {
29   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
30     : ly_symbol2scm ("self-alignment-Y");
31
32   SCM align (me->internal_get_property (sym));
33   if (scm_is_number (align))
34     {
35       Interval ext (me->extent (me, a));
36       if (ext.is_empty ())
37         programming_error ("can't align on self: empty element");
38       else
39         return scm_from_double (- ext.linear_combination (scm_to_double (align)));
40     }
41   return scm_from_double (0.0);
42 }
43
44
45
46
47 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_x_parent, 1);
48 SCM
49 Self_alignment_interface::centered_on_x_parent (SCM smob)
50 {
51   return centered_on_parent (unsmob_grob (smob), X_AXIS);
52 }
53
54
55 MAKE_SCHEME_CALLBACK (Self_alignment_interface, centered_on_y_parent, 1);
56 SCM
57 Self_alignment_interface::centered_on_y_parent (SCM smob)
58 {
59   return centered_on_parent (unsmob_grob (smob), Y_AXIS);
60 }
61
62 SCM
63 Self_alignment_interface::centered_on_parent (Grob *me, Axis a)
64 {
65   Grob *him = me->get_parent (a);
66   Interval he = him->extent (him, a);
67
68   return scm_from_double (he.is_empty () ? 0.0 : he.center ());
69 }
70
71 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_x_parent,1);
72 SCM
73 Self_alignment_interface::aligned_on_x_parent (SCM smob)
74 {
75   return aligned_on_parent (unsmob_grob (smob), X_AXIS);
76 }
77
78 MAKE_SCHEME_CALLBACK (Self_alignment_interface, aligned_on_y_parent,1);
79 SCM
80 Self_alignment_interface::aligned_on_y_parent (SCM smob)
81 {
82   return aligned_on_parent (unsmob_grob (smob), Y_AXIS);
83 }
84
85 SCM
86 Self_alignment_interface::aligned_on_parent (Grob *me, Axis a)
87 {
88   Grob *him = me->get_parent (a);
89   Interval he = him->extent (him, a);
90
91   SCM sym = (a == X_AXIS) ? ly_symbol2scm ("self-alignment-X")
92     : ly_symbol2scm ("self-alignment-Y");
93   SCM align_prop (me->internal_get_property (sym));
94
95   if (!scm_is_number (align_prop))
96     return scm_from_int (0);
97
98   Real x = 0.0;
99   Real align = scm_to_double (align_prop);
100
101   Interval ext (me->extent (me, a));
102   if (ext.is_empty ())
103     programming_error ("can't align on self: empty element");
104   else
105     x -= ext.linear_combination (align);
106
107   if (!he.is_empty ())
108     x += he.linear_combination (align);
109
110   return scm_from_double (x);
111 }
112
113 ADD_INTERFACE (Self_alignment_interface, "self-alignment-interface",
114                "Position this object on itself and/or on its parent. To this end, the following functions "
115                " are provided: \n"
116                "@table @code \n"
117                "@item Self_alignment_interface::aligned_on_self\n"
118                "  Align self on reference point, using @code{self-alignment-X} and "
119                "@code{self-alignment-Y}."
120                "@item Self_alignment_interface::aligned_on_parent\n"
121                "@item Self_alignment_interface::centered_on_parent\n"
122                "  Shift the object so its own reference point is centered on the  "
123                " extent of the parent \n"
124                "@item Self_alignment_interface::centered_on_other_axis_parent\n"
125                " For X-axis, center on the Y-parent, and vice versa.\n "
126                "@end table\n",
127
128
129                /* porperties */
130                "self-alignment-X "
131                "self-alignment-Y ");
132