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