]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
Doc-de: fixing linkage
[lilypond.git] / lily / stem-tremolo.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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 "stem-tremolo.hh"
21
22 #include "spanner.hh"
23 #include "beam.hh"
24 #include "directional-element-interface.hh"
25 #include "item.hh"
26 #include "lookup.hh"
27 #include "output-def.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "stem.hh"
30 #include "warn.hh"
31
32 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_slope, 1)
33 SCM
34 Stem_tremolo::calc_slope (SCM smob)
35 {
36   Grob *me = unsmob_grob (smob);
37   Grob *stem = unsmob_grob (me->get_object ("stem"));
38   Spanner *beam = Stem::get_beam (stem);
39
40   if (beam)
41     {
42       Real dy = 0;
43       SCM s = beam->get_property ("quantized-positions");
44       if (is_number_pair (s))
45         dy = - scm_to_double (scm_car (s)) + scm_to_double (scm_cdr (s));
46
47       Grob *s2 = Beam::last_normal_stem (beam);
48       Grob *s1 = Beam::first_normal_stem (beam);
49
50       Grob *common = s1->common_refpoint (s2, X_AXIS);
51       Real dx = s2->relative_coordinate (common, X_AXIS)
52                 - s1->relative_coordinate (common, X_AXIS);
53
54       return scm_from_double (dx ? dy / dx : 0);
55     }
56   else
57     /* down stems with flags should have more sloped trems (helps avoid
58        flag/stem collisions without making the stem very long) */
59     return scm_from_double ((Stem::duration_log (stem) >= 3 && get_grob_direction (stem) == DOWN)
60                             ? 0.40 : 0.25);
61 }
62
63 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_width, 1)
64 SCM
65 Stem_tremolo::calc_width (SCM smob)
66 {
67   Grob *me = unsmob_grob (smob);
68   Grob *stem = unsmob_grob (me->get_object ("stem"));
69   Direction stemdir = get_grob_direction (stem);
70   bool beam = Stem::get_beam (stem);
71   bool flag = Stem::duration_log (stem) >= 3 && !beam;
72
73   /* beamed stems and up-stems with flags have shorter tremolos */
74   return scm_from_double (((stemdir == UP && flag) || beam) ? 1.0 : 1.5);
75 }
76
77 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_style, 1)
78 SCM
79 Stem_tremolo::calc_style (SCM smob)
80 {
81   Grob *me = unsmob_grob (smob);
82   Grob *stem = unsmob_grob (me->get_object ("stem"));
83   Direction stemdir = get_grob_direction (stem);
84   bool beam = Stem::get_beam (stem);
85   bool flag = Stem::duration_log (stem) >= 3 && !beam;
86
87   return ly_symbol2scm (((stemdir == UP && flag) || beam) ? "rectangle" : "default");
88 }
89
90 Real
91 Stem_tremolo::get_beam_translation (Grob *me)
92 {
93   Grob *stem = unsmob_grob (me->get_object ("stem"));
94   Spanner *beam = Stem::get_beam (stem);
95
96   return (beam && beam->is_live ())
97          ? Beam::get_beam_translation (beam)
98          : (Staff_symbol_referencer::staff_space (me)
99             * robust_scm2double (me->get_property ("length-fraction"), 1.0) * 0.81);
100 }
101
102 Stencil
103 Stem_tremolo::raw_stencil (Grob *me, Real slope, Direction stemdir)
104 {
105   Real ss = Staff_symbol_referencer::staff_space (me);
106   Real thick = robust_scm2double (me->get_property ("beam-thickness"), 1);
107   Real width = robust_scm2double (me->get_property ("beam-width"), 1);
108   Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
109   SCM style = me->get_property ("style");
110   if (!scm_is_symbol (style))
111     style = ly_symbol2scm ("default");
112
113   width *= ss;
114   thick *= ss;
115
116   Stencil a;
117   if (style == ly_symbol2scm ("rectangle"))
118     a = Lookup::rotated_box (slope, width, thick, blot);
119   else
120     a = Lookup::beam (slope, width, thick, blot);
121
122   a.align_to (X_AXIS, CENTER);
123   a.align_to (Y_AXIS, CENTER);
124
125   int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
126   if (!tremolo_flags)
127     {
128       programming_error ("no tremolo flags");
129
130       me->suicide ();
131       return Stencil ();
132     }
133
134   Real beam_translation = get_beam_translation (me);
135
136   Stencil mol;
137   for (int i = 0; i < tremolo_flags; i++)
138     {
139       Stencil b (a);
140       b.translate_axis (beam_translation * i * stemdir * -1, Y_AXIS);
141       mol.add_stencil (b);
142     }
143   return mol;
144 }
145
146 MAKE_SCHEME_CALLBACK (Stem_tremolo, pure_height, 3);
147 SCM
148 Stem_tremolo::pure_height (SCM smob, SCM, SCM)
149 {
150   Grob *me = unsmob_grob (smob);
151
152   /*
153     Cannot use the real slope, since it looks at the Beam.
154    */
155   Stencil s1 (untranslated_stencil (me, 0.35));
156
157   return ly_interval2scm (s1.extent (Y_AXIS));
158 }
159
160 MAKE_SCHEME_CALLBACK (Stem_tremolo, width, 1);
161 SCM
162 Stem_tremolo::width (SCM smob)
163 {
164   Grob *me = unsmob_grob (smob);
165
166   /*
167     Cannot use the real slope, since it looks at the Beam.
168    */
169   Stencil s1 (untranslated_stencil (me, 0.35));
170
171   return ly_interval2scm (s1.extent (X_AXIS));
172 }
173
174 Real
175 Stem_tremolo::vertical_length (Grob *me)
176 {
177   return untranslated_stencil (me, 0.35).extent (Y_AXIS).length ();
178 }
179
180 Stencil
181 Stem_tremolo::untranslated_stencil (Grob *me, Real slope)
182 {
183   Grob *stem = unsmob_grob (me->get_object ("stem"));
184   if (!stem)
185     {
186       programming_error ("no stem for stem-tremolo");
187       return Stencil ();
188     }
189
190   Direction stemdir = get_grob_direction (stem);
191   if (!stemdir)
192     stemdir = UP;
193
194   bool whole_note = Stem::duration_log (stem) <= 0;
195
196   /* for a whole note, we position relative to the notehead, so we want the
197      stencil aligned on the flag closest to the head */
198   Direction stencil_dir = whole_note ? -stemdir : stemdir;
199   return raw_stencil (me, slope, stencil_dir);
200 }
201
202 Stencil
203 Stem_tremolo::translated_stencil (Grob *me, Real slope)
204 {
205   Stencil mol = untranslated_stencil (me, slope);
206
207   Grob *stem = unsmob_grob (me->get_object ("stem"));
208   if (!stem)
209     return Stencil ();
210
211   Direction stemdir = get_grob_direction (stem);
212   if (stemdir == 0)
213     stemdir = UP;
214
215   Spanner *beam = Stem::get_beam (stem);
216   Real beam_translation = get_beam_translation (me);
217
218   int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
219   Real ss = Staff_symbol_referencer::staff_space (me);
220
221   Real end_y
222     = Stem::stem_end_position (stem) * ss / 2
223       - stemdir * max (beam_count, 1) * beam_translation;
224
225   if (!beam && Stem::duration_log (stem) >= 3)
226     {
227       end_y -= stemdir * (Stem::duration_log (stem) - 2) * beam_translation;
228       if (stemdir == UP)
229         end_y -= stemdir * beam_translation * 0.5;
230     }
231
232   bool whole_note = Stem::duration_log (stem) <= 0;
233   if (whole_note)
234     {
235       /* we shouldn't position relative to the end of the stem since the stem
236          is invisible */
237       vector<int> nhp = Stem::note_head_positions (stem);
238       Real note_head = (stemdir == UP ? nhp.back () : nhp[0]) * ss / 2;
239       end_y = note_head + stemdir * 1.5;
240     }
241   mol.translate_axis (end_y, Y_AXIS);
242
243   return mol;
244 }
245
246 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
247 SCM
248 Stem_tremolo::print (SCM grob)
249 {
250   Grob *me = unsmob_grob (grob);
251
252   Stencil s = translated_stencil (me, robust_scm2double (me->get_property ("slope"), 0.25));
253   return s.smobbed_copy ();
254 }
255
256 ADD_INTERFACE (Stem_tremolo,
257                "A beam slashing a stem to indicate a tremolo.  The property"
258                " @code{style} can be @code{default} or @code{rectangle}.",
259
260                /* properties */
261                "beam-thickness "
262                "beam-width "
263                "flag-count "
264                "length-fraction "
265                "stem "
266                "style "
267                "slope "
268               );