]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest.cc
Merge branch 'lilypond/translation' into staging
[lilypond.git] / lily / rest.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "rest.hh"
21
22 #include "directional-element-interface.hh"
23 #include "dots.hh"
24 #include "font-interface.hh"
25 #include "international.hh"
26 #include "output-def.hh"
27 #include "paper-score.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "stencil.hh"
30 #include "grob.hh"
31
32 // -> offset callback
33 MAKE_SCHEME_CALLBACK (Rest, y_offset_callback, 1);
34 SCM
35 Rest::y_offset_callback (SCM smob)
36 {
37   Grob *me = unsmob_grob (smob);
38   int duration_log = scm_to_int (me->get_property ("duration-log"));
39   int line_count = Staff_symbol_referencer::line_count (me);
40   Real ss = Staff_symbol_referencer::staff_space (me);
41
42   bool position_override = scm_is_number (me->get_property ("staff-position"));
43   Real amount;
44
45   if (position_override)
46     {
47       amount =
48         robust_scm2double (me->get_property ("staff-position"), 0) * 0.5 * ss;
49
50       /*
51         semibreve rests were always positioned one off
52       */
53       if (duration_log == 0)
54         amount += ss;
55
56       /*
57         trust the client on good positioning;
58         would be tempting to adjust position of rests longer than a quarter
59         to be properly aligned to staff lines,
60         but custom rest shapes may not need that sort of care.
61       */
62     }
63   else
64     {
65       int pos = 4 * get_grob_direction (me);
66
67       /*
68         make a semibreve rest hang from the next line,
69         except for a single line staff;
70         assume the next line being integer steps away
71       */
72       if (duration_log == 0 && line_count > 1)
73         ++pos;
74
75       /*
76         make sure rest is aligned to a staff line
77       */
78       while (!Staff_symbol_referencer::on_line (me, pos))
79         ++pos;
80
81       amount = ss * 0.5 * pos;
82     }
83
84   return scm_from_double (amount);
85 }
86
87 /* A rest might lie under a beam, in which case it should be cross-staff if
88    the beam is cross-staff because the rest's position depends on the
89    formatting of the beam. */
90 MAKE_SCHEME_CALLBACK (Rest, calc_cross_staff, 1);
91 SCM
92 Rest::calc_cross_staff (SCM smob)
93 {
94   Grob *me = unsmob_grob (smob);
95   Grob *stem = unsmob_grob (me->get_object ("stem"));
96
97   if (!stem)
98     return SCM_BOOL_F;
99
100   return stem->get_property ("cross-staff");
101 }
102
103 /*
104   make this function easily usable in C++
105 */
106 string
107 Rest::glyph_name (Grob *me, int durlog, string style, bool try_ledgers)
108 {
109   bool is_ledgered = false;
110   if (try_ledgers && (durlog == -1 || durlog == 0 || durlog == 1))
111     {
112       int const pos = int (Staff_symbol_referencer::get_position (me));
113
114       /*
115         half rests need ledger if not lying on a staff line,
116         whole rests need ledger if not hanging from a staff line,
117         breve rests need ledger if neither lying on nor hanging from a staff line
118       */
119       if (-1 <= durlog && durlog <= 1)
120         is_ledgered = !Staff_symbol_referencer::on_staff_line (me, pos)
121           && !(durlog == -1
122                && Staff_symbol_referencer::on_staff_line (me, pos + 2));
123     }
124
125   string actual_style (style.c_str ());
126
127   if ((style == "mensural") || (style == "neomensural"))
128     {
129
130       /*
131         FIXME: Currently, ancient font does not provide ledgered rests;
132         hence the "o" suffix in the glyph name is bogus.  But do we need
133         ledgered rests at all now that we can draw ledger lines with
134         variable width, length and blotdiameter? -- jr
135       */
136       is_ledgered = 0;
137
138       /*
139         There are no 32th/64th/128th mensural/neomensural rests.  In
140         these cases, revert back to default style.
141       */
142       if (durlog > 4)
143         actual_style = "";
144     }
145
146   if ((style == "classical") && (durlog != 2))
147     {
148       /*
149         classical style: revert back to default style for any rest other
150         than quarter rest
151       */
152       actual_style = "";
153     }
154
155   if (style == "default")
156     {
157       /*
158         Some parts of lily still prefer style "default" over "".
159         Correct this here. -- jr
160       */
161       actual_style = "";
162     }
163
164   return ("rests." + to_string (durlog) + (is_ledgered ? "o" : "")
165           + actual_style);
166 }
167
168 MAKE_SCHEME_CALLBACK (Rest, print, 1);
169 SCM
170 Rest::brew_internal_stencil (Grob *me, bool ledgered)
171 {
172   SCM durlog_scm = me->get_property ("duration-log");
173   if (!scm_is_number (durlog_scm))
174     return Stencil ().smobbed_copy ();
175
176   int durlog = scm_to_int (durlog_scm);
177
178   string style = robust_symbol2string (me->get_property ("style"), "default");
179
180   Font_metric *fm = Font_interface::get_default_font (me);
181   string font_char = glyph_name (me, durlog, style, ledgered);
182   Stencil out = fm->find_by_name (font_char);
183   if (out.is_empty ())
184     me->warning (_f ("rest `%s' not found", font_char.c_str ()));
185
186   return out.smobbed_copy ();
187 }
188
189 /**
190    translate the rest vertically by amount DY, but only if
191    it doesn't have staff-position set.
192 */
193 void
194 Rest::translate (Grob *me, int dy)
195 {
196   if (!scm_is_number (me->get_property ("staff-position")))
197     {
198       me->translate_axis (dy * Staff_symbol_referencer::staff_space (me) / 2.0, Y_AXIS);
199       Grob *p = me->get_parent (Y_AXIS);
200       p->flush_extent_cache (Y_AXIS);
201     }
202 }
203
204 SCM
205 Rest::print (SCM smob)
206 {
207   return brew_internal_stencil (unsmob_grob (smob), true);
208 }
209
210 MAKE_SCHEME_CALLBACK (Rest, width, 1);
211 /*
212   We need the callback. The real stencil has ledgers depending on
213   Y-position. The Y-position is known only after line breaking.  */
214 SCM
215 Rest::width (SCM smob)
216 {
217   return generic_extent_callback (unsmob_grob (smob), X_AXIS);
218 }
219
220 MAKE_SCHEME_CALLBACK (Rest, height, 1);
221 SCM
222 Rest::height (SCM smob)
223 {
224   return generic_extent_callback (unsmob_grob (smob), Y_AXIS);
225 }
226
227 /*
228   We need the callback. The real stencil has ledgers depending on
229   Y-position. The Y-position is known only after line breaking.  */
230 SCM
231 Rest::generic_extent_callback (Grob *me, Axis a)
232 {
233   /*
234     Don't want ledgers: ledgers depend on Y position, which depends on
235     rest collision, which depends on stem size which depends on beam
236     slop of opposite note column.
237
238     consequence: we get too small extents and potential collisions
239     with ledgered rests.
240   */
241   SCM m = brew_internal_stencil (me, a != X_AXIS);
242   return ly_interval2scm (unsmob_stencil (m)->extent (a));
243 }
244
245 MAKE_SCHEME_CALLBACK (Rest, pure_height, 3);
246 SCM
247 Rest::pure_height (SCM smob,
248                    SCM /* start */,
249                    SCM /* end */)
250 {
251   Grob *me = unsmob_grob (smob);
252   SCM m = brew_internal_stencil (me, false);
253   return ly_interval2scm (unsmob_stencil (m)->extent (Y_AXIS));
254 }
255
256 ADD_INTERFACE (Rest,
257                "A rest symbol.  The property @code{style} can be"
258                " @code{default}, @code{mensural}, @code{neomensural} or"
259                " @code{classical}.",
260
261                /* properties */
262                "direction "
263                "minimum-distance "
264                "style "
265               );