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