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