]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol.cc
6fefc48010750e7daa9b2f9ed0078305047eff4b
[lilypond.git] / lily / staff-symbol.cc
1 /*
2   staff-symbol.cc -- implement Staff_symbol
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "staff-symbol.hh"
10
11 #include "lookup.hh"
12 #include "dimensions.hh"
13 #include "output-def.hh"
14 #include "warn.hh"
15 #include "item.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "spanner.hh"
18
19 MAKE_SCHEME_CALLBACK (Staff_symbol, print, 1);
20
21 SCM
22 Staff_symbol::print (SCM smob)
23 {
24   Grob *me = unsmob_grob (smob);
25   Spanner *sp = dynamic_cast<Spanner *> (me);
26   Grob *common
27     = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
28
29   Interval span_points (0, 0);
30
31   /*
32     For raggedright without ragged staves, simply set width to the linewidth.
33
34     (ok -- lousy UI, since width is in staff spaces)
35
36     --hwn.
37   */
38   Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
39   t *= robust_scm2double (me->get_property ("thickness"), 1.0);
40
41   Direction d = LEFT;
42   do
43     {
44       SCM width_scm = me->get_property ("width");
45       if (d == RIGHT && scm_is_number (width_scm))
46         {
47           /*
48             don't multiply by Staff_symbol_referencer::staff_space (me),
49             since that would make aligning staff symbols of different sizes to
50             one right margin hell.
51           */
52           span_points[RIGHT] = scm_to_double (width_scm);
53         }
54       else
55         {
56           Item *x = sp->get_bound (d);
57
58           span_points[d] = x->relative_coordinate (common, X_AXIS);
59           if (!x->break_status_dir ()
60               && !x->extent (x, X_AXIS).is_empty ())
61             span_points[d] += x->extent (x, X_AXIS)[d];
62         }
63
64       span_points[d] -= d* t / 2;
65     }
66   while (flip (&d) != LEFT);
67
68   Stencil m;
69
70   SCM line_positions = me->get_property ("line-positions");
71   Stencil line
72     = Lookup::horizontal_line (span_points
73                                -me->relative_coordinate (common, X_AXIS),
74                                t);
75
76   Real space = staff_space (me);
77   if (scm_is_pair (line_positions))
78     {
79       for (SCM s = line_positions; scm_is_pair (s);
80            s = scm_cdr (s))
81         {
82           Stencil b (line);
83           b.translate_axis (scm_to_double (scm_car (s))
84                             * 0.5 * space, Y_AXIS);
85           m.add_stencil (b);
86         }
87     }
88   else
89     {
90       int l = Staff_symbol::line_count (me);
91       Real height = (l - 1) * staff_space (me) / 2;
92       for (int i = 0; i < l; i++)
93         {
94           Stencil b (line);
95           b.translate_axis (height - i * space, Y_AXIS);
96           m.add_stencil (b);
97         }
98     }
99   return m.smobbed_copy ();
100 }
101
102
103 int
104 Staff_symbol::get_steps (Grob *me)
105 {
106   return line_count (me) * 2;
107 }
108
109 int
110 Staff_symbol::line_count (Grob *me)
111 {
112   SCM c = me->get_property ("line-count");
113   if (scm_is_number (c))
114     return scm_to_int (c);
115   else
116     return 0;
117 }
118
119 Real
120 Staff_symbol::staff_space (Grob *me)
121 {
122   return robust_scm2double (me->get_property ("staff-space"), 1.0);
123 }
124
125 Real
126 Staff_symbol::get_line_thickness (Grob *me)
127 {
128   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
129
130   return robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
131 }
132
133 Real
134 Staff_symbol::get_ledger_line_thickness (Grob *me)
135 {
136   SCM lt_pair = me->get_property ("ledger-line-thickness");
137   Offset z = robust_scm2offset (lt_pair, Offset (1.0, 0.1));
138
139   return z[X_AXIS] * get_line_thickness (me) + z[Y_AXIS] * staff_space (me);
140 }
141
142 MAKE_SCHEME_CALLBACK (Staff_symbol, height,1);
143 SCM
144 Staff_symbol::height  (SCM smob)
145 {
146   Grob *me = unsmob_grob (smob);
147   Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
148   t *= robust_scm2double (me->get_property ("thickness"), 1.0);
149   
150   SCM line_positions = me->get_property ("line-positions");
151
152   Interval y_ext;
153   Real space = staff_space (me);
154   if (scm_is_pair (line_positions))
155     {
156       for (SCM s = line_positions; scm_is_pair (s);
157            s = scm_cdr (s))
158         y_ext.add_point (scm_to_double (scm_car (s)) * 0.5 * space);
159     }
160   else
161     {
162       int l = Staff_symbol::line_count (me);
163       Real height = (l - 1) * staff_space (me) / 2;
164       y_ext = Interval (-height, height);
165     }
166   y_ext.widen (t/2);
167   return ly_interval2scm (y_ext);
168 }
169
170 bool
171 Staff_symbol::on_line (Grob *me, int pos)
172 {
173   SCM line_positions = me->get_property ("line-positions");
174   if (scm_is_pair (line_positions))
175     {
176       Real min_line = HUGE_VAL;
177       Real max_line = -HUGE_VAL;
178       for (SCM s = line_positions; scm_is_pair (s); s = scm_cdr (s))
179         {
180           Real current_line = scm_to_double (scm_car (s));
181           if (pos == current_line)
182             return true;
183           if (current_line > max_line)
184             max_line = current_line;
185           if (current_line < min_line)
186             min_line = current_line;
187         
188         }
189       if (pos < min_line)
190         return (( (int) (rint (pos - min_line)) % 2) == 0);
191       if (pos > max_line)
192         return (( (int) (rint (pos - max_line)) % 2) == 0);
193
194       return false;
195     }
196   else
197     return ((abs (pos + line_count (me)) % 2) == 1);
198 }
199
200 ADD_INTERFACE (Staff_symbol,
201                "This spanner draws the lines of a staff.  A staff symbol"
202                " defines a vertical unit, the @emph{staff space}.  Quantities"
203                " that go by a half staff space are called @emph{positions}."
204                "  The center (i.e., middle line or space) is position@tie{}0."
205                " The length of the symbol may be set by hand through the"
206                " @code{width} property.",
207
208                /* properties */
209                "ledger-line-thickness "
210                "line-count "
211                "line-positions "
212                "staff-space "
213                "thickness "
214                "width "
215                );