]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
38c39e04fe1851ec00dbc26893937040e065acee
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "staff-symbol-referencer.hh"
10
11 #include "staff-symbol.hh"
12 #include "grob.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_line (Grob *me, int pos)
25 {
26   Grob *st = get_staff_symbol (me);
27   return st ? Staff_symbol::on_line (st, pos) : false;
28 }
29
30 bool
31 Staff_symbol_referencer::on_staff_line (Grob *me, int pos)
32 {
33   return on_line (me, pos) && abs (pos) <= 2 * staff_radius (me);
34 }
35
36 Grob *
37 Staff_symbol_referencer::get_staff_symbol (Grob *me)
38 {
39   if (Staff_symbol::has_interface (me))
40     return me;
41
42   SCM st = me->get_object ("staff-symbol");
43   return unsmob_grob (st);
44 }
45
46 Real
47 Staff_symbol_referencer::staff_space (Grob *me)
48 {
49   Grob *st = get_staff_symbol (me);
50   if (st)
51     return Staff_symbol::staff_space (st);
52   return 1.0;
53 }
54
55 Real
56 Staff_symbol_referencer::line_thickness (Grob *me)
57 {
58   Grob *st = get_staff_symbol (me);
59   if (st)
60     return Staff_symbol::get_line_thickness (st);
61   return me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
62 }
63
64 Real
65 Staff_symbol_referencer::get_position (Grob *me)
66 {
67   Real p = 0.0;
68   Grob *st = get_staff_symbol (me);
69   Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
70   if (st && c)
71     {
72       Real y = me->relative_coordinate (c, Y_AXIS)
73         - st->relative_coordinate (c, Y_AXIS);
74
75       p += 2.0 * y / Staff_symbol::staff_space (st);
76       return p;
77     }
78   else if (!st)
79     return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
80   return robust_scm2double (me->get_property ("staff-position"), p);
81 }
82
83
84 Interval
85 Staff_symbol_referencer::extent_in_staff (Grob *me)
86 {
87   Grob *st = get_staff_symbol (me);
88   Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
89
90   Interval retval;
91   if (st && c)
92     {
93       retval  = me->extent (c, Y_AXIS)
94         - st->relative_coordinate (c, Y_AXIS);
95     }
96
97   return retval;
98 }
99
100 int
101 Staff_symbol_referencer::get_rounded_position (Grob *me)
102 {
103   return int (rint (get_position (me)));
104 }
105
106 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 1);
107 SCM
108 Staff_symbol_referencer::callback (SCM smob)
109 {
110   Grob *me = unsmob_grob (smob);
111
112   SCM pos = me->get_property ("staff-position");
113   Real off = 0.0;
114   if (scm_is_number (pos))
115     {
116       Real space = Staff_symbol_referencer::staff_space (me);
117       off = scm_to_double (pos) * space / 2.0;
118     }
119
120   return scm_from_double (off);
121 }
122
123 /*  This sets the position relative to the center of the staff symbol.
124
125 The function is hairy, because it can be called in two situations:
126
127 1. There is no staff yet; we must set staff-position
128
129 2. There is a staff, and perhaps someone even applied a
130 translate_axis (). Then we must compensate for the translation
131
132 In either case, we set a callback to be sure that our new position
133 will be extracted from staff-position */
134
135 void
136 Staff_symbol_referencer::set_position (Grob *me, Real p)
137 {
138   Grob *st = get_staff_symbol (me);
139   Real oldpos = 0.0;
140   if (st && me->common_refpoint (st, Y_AXIS))
141     {
142       oldpos = get_position (me);
143     }
144
145   
146   Real ss = Staff_symbol_referencer::staff_space (me);
147   me->translate_axis ((p  - oldpos) * ss * 0.5, Y_AXIS);
148 }
149
150 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
151 Real
152 Staff_symbol_referencer::staff_radius (Grob *me)
153 {
154   return (line_count (me) - 1) / 2.0;
155 }
156
157 int
158 compare_position (Grob *const &a, Grob *const &b)
159 {
160   return sign (Staff_symbol_referencer::get_position ((Grob *)a)
161                - Staff_symbol_referencer::get_position ((Grob *) b));
162 }
163
164 bool
165 position_less (Grob *const &a, Grob *const &b)
166 {
167   return Staff_symbol_referencer::get_position (a)
168     < Staff_symbol_referencer::get_position (b);
169 }
170
171 ADD_INTERFACE (Staff_symbol_referencer,
172                "An object whose Y@tie{}position is meant relative to a staff"
173                " symbol.  These usually"
174                " have @code{Staff_symbol_referencer::callback} in their"
175                " @code{Y-offset-callbacks}.",
176
177                /* properties */
178                "staff-position "
179                );