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