]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
Nitpick run.
[lilypond.git] / lily / staff-symbol-referencer.cc
1 /*
2   staff-symbol-referencer.cc -- implement Staff_symbol_referencer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "staff-symbol-referencer.hh"
10
11 #include <math.h>
12
13 #include "staff-symbol.hh"
14 #include "output-def.hh"
15 #include "libc-extension.hh"
16
17 int
18 Staff_symbol_referencer::line_count (Grob *me)
19 {
20   Grob *st = get_staff_symbol (me);
21   return st ? Staff_symbol::line_count (st) : 0;
22 }
23
24 bool
25 Staff_symbol_referencer::on_staffline (Grob *me)
26 {
27   return on_staffline (me, (int) rint (get_position (me)));
28 }
29
30 /*
31   This does not take size into account.
32   maybe rename: on_virtual_staffline, on_staff_or_ledger_line?
33 */
34 bool
35 Staff_symbol_referencer::on_staffline (Grob *me, int pos)
36 {
37   int sz = line_count (me) - 1;
38   return ((pos + sz) % 2) == 0;
39 }
40
41 Grob *
42 Staff_symbol_referencer::get_staff_symbol (Grob *me)
43 {
44   if (Staff_symbol::has_interface (me))
45     return me;
46
47   SCM st = me->get_object ("staff-symbol");
48   return unsmob_grob (st);
49 }
50
51 Real
52 Staff_symbol_referencer::staff_space (Grob *me)
53 {
54   Grob *st = get_staff_symbol (me);
55   if (st)
56     return Staff_symbol::staff_space (st);
57   return 1.0;
58 }
59
60 Real
61 Staff_symbol_referencer::line_thickness (Grob *me)
62 {
63   Grob *st = get_staff_symbol (me);
64   if (st)
65     return Staff_symbol::get_line_thickness (st);
66   return me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
67 }
68
69 Real
70 Staff_symbol_referencer::get_position (Grob *me)
71 {
72   Real p = 0.0;
73   Grob *st = get_staff_symbol (me);
74   Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
75   if (st && c)
76     {
77       Real y = me->relative_coordinate (c, Y_AXIS)
78         - st->relative_coordinate (c, Y_AXIS);
79
80       p += 2.0 * y / Staff_symbol::staff_space (st);
81       return p;
82     }
83   else if (!st)
84     return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
85   return robust_scm2double (me->get_property ("staff-position"), p);
86 }
87
88 int
89 Staff_symbol_referencer::get_rounded_position (Grob *me)
90 {
91   return int (rint (get_position (me)));
92 }
93
94 LY_DEFINE (ly_grob_staff_position, "ly:grob-staff-position",
95            1, 0, 0, (SCM sg),
96            "Return the Y-position of @var{sg} relative to the staff.")
97 {
98   Grob *g = unsmob_grob (sg);
99
100   SCM_ASSERT_TYPE (g, sg, SCM_ARG1, __FUNCTION__, "grob");
101   Real pos = Staff_symbol_referencer::get_position (g);
102
103   if (fabs (rint (pos) -pos) < 1e-6) // ugh.
104     return scm_from_int ((int) my_round (pos));
105   else
106     return scm_from_double (pos);
107 }
108
109 /* should use offset callback!  */
110 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 2);
111 SCM
112 Staff_symbol_referencer::callback (SCM element_smob, SCM)
113 {
114   Grob *me = unsmob_grob (element_smob);
115
116   SCM pos = me->get_property ("staff-position");
117   Real off = 0.0;
118   if (scm_is_number (pos))
119     {
120       Real space = Staff_symbol_referencer::staff_space (me);
121       off = scm_to_double (pos) * space / 2.0;
122       me->set_property ("staff-position", scm_from_int (0));
123     }
124
125   return scm_from_double (off);
126 }
127
128 /*  This sets the position relative to the center of the staff symbol.
129
130 The function is hairy, because it can be called in two situations:
131
132 1. There is no staff yet; we must set staff-position
133
134 2. There is a staff, and perhaps someone even applied a
135 translate_axis (). Then we must compensate for the translation
136
137 In either case, we set a callback to be sure that our new position
138 will be extracted from staff-position */
139
140 void
141 Staff_symbol_referencer::set_position (Grob *me, Real p)
142 {
143   Grob *st = get_staff_symbol (me);
144   if (st && me->common_refpoint (st, Y_AXIS))
145     {
146       Real oldpos = get_position (me);
147       me->set_property ("staff-position", scm_from_double (p - oldpos));
148     }
149   else
150     me->set_property ("staff-position", scm_from_double (p));
151
152   me->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
153 }
154
155 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
156 Real
157 Staff_symbol_referencer::staff_radius (Grob *me)
158 {
159   return (line_count (me) - 1) / 2.0;
160 }
161
162 int
163 compare_position (Grob *const &a, Grob *const &b)
164 {
165   return sign (Staff_symbol_referencer::get_position ((Grob *)a)
166                - Staff_symbol_referencer::get_position ((Grob *) b));
167 }
168
169 ADD_INTERFACE (Staff_symbol_referencer, "staff-symbol-referencer-interface",
170                "An object whose Y position is meant relative to a staff "
171                "symbol. "
172                "These usually have @code{Staff_symbol_referencer::callback} "
173                "in their @code{Y-offset-callbacks}. ",
174                "staff-position");
175