]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-tremolo.cc
(class Paper_book):
[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 /* TODO: lengthen stem if necessary  */
22
23 MAKE_SCHEME_CALLBACK (Stem_tremolo, dim_callback, 1);
24
25 /* todo: init with cons.  */
26 SCM
27 Stem_tremolo::dim_callback (SCM e)
28 {
29   Grob *se = unsmob_grob (e);
30
31   Real space = Staff_symbol_referencer::staff_space (se);
32   return ly_interval2scm (Interval (-space, space));
33 }
34
35
36 MAKE_SCHEME_CALLBACK (Stem_tremolo, calc_slope, 1)
37 SCM
38 Stem_tremolo::calc_slope (SCM smob)
39 {
40   Grob *me = unsmob_grob (smob);
41   Grob *stem = unsmob_grob (me->get_object ("stem"));
42   Spanner *beam = Stem::get_beam (stem);
43
44   if (beam)
45     {
46       Real dy = 0;
47       SCM s = beam->get_property ("quantized-positions");
48       if (is_number_pair (s))
49         dy = - scm_to_double (scm_car (s)) + scm_to_double (scm_cdr (s));
50
51       Grob *s2 = Beam::last_visible_stem (beam);
52       Grob *s1 = Beam::first_visible_stem (beam);
53       
54       Grob *common = s1->common_refpoint (s2, X_AXIS);
55       Real dx = s2->relative_coordinate (common, X_AXIS) -
56         s1->relative_coordinate (common, X_AXIS);
57
58       return scm_from_double (dx ? dy / dx : 0);
59     }
60   else
61     return scm_from_double (0.25);
62 }
63
64 Real
65 Stem_tremolo::get_beam_translation (Grob *me)
66 {
67   Grob *stem = unsmob_grob (me->get_object ("stem"));
68   Spanner *beam = Stem::get_beam (stem);
69
70   return  beam ? Beam::get_beam_translation (beam) : 0.81;
71 }
72
73 Stencil
74 Stem_tremolo::raw_stencil (Grob *me, Real slope)
75 {
76   Real ss = Staff_symbol_referencer::staff_space (me);
77   Real thick = robust_scm2double (me->get_property ("beam-thickness"), 1);
78   Real width = robust_scm2double (me->get_property ("beam-width"), 1);
79   Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
80
81   width *= ss;
82   thick *= ss;
83
84   Stencil a (Lookup::beam (slope, width, thick, blot));
85   a.translate (Offset (-width * 0.5, width * 0.5 * slope));
86
87   int tremolo_flags = robust_scm2int (me->get_property ("flag-count"), 0);
88   if (!tremolo_flags)
89     {
90       programming_error ("no tremolo flags");
91
92       me->suicide ();
93       return Stencil ();
94     }
95
96   /* Who the fuck is 0.81 ? --hwn.   */
97   Real beam_translation = get_beam_translation(me);
98
99   Stencil mol;
100   for (int i = 0; i < tremolo_flags; i++)
101     {
102       Stencil b (a);
103       b.translate_axis (beam_translation * i, Y_AXIS);
104       mol.add_stencil (b);
105     }
106   return mol;
107 }
108
109
110
111 MAKE_SCHEME_CALLBACK (Stem_tremolo, height, 1);
112 SCM
113 Stem_tremolo::height (SCM smob)
114 {
115   Grob *me = unsmob_grob (smob);
116
117   /*
118     Cannot use the real slope, since it looks at the Beam.
119    */
120   Stencil s1 (raw_stencil (me, 0.35));
121
122   return ly_interval2scm (s1.extent (Y_AXIS));
123 }
124
125
126 MAKE_SCHEME_CALLBACK (Stem_tremolo, print, 1);
127 SCM
128 Stem_tremolo::print (SCM grob)
129 {
130   Grob *me = unsmob_grob (grob);
131   Grob *stem = unsmob_grob (me->get_object ("stem"));
132   if (!stem)
133     {
134       programming_error ("no stem for stem-tremolo");
135       return SCM_EOL;
136     }
137
138   Spanner *beam = Stem::get_beam (stem);
139   Direction stemdir = get_grob_direction (stem);
140   if (stemdir == 0)
141     stemdir = UP;
142
143   Real beam_translation
144     = (beam && beam->is_live ())
145     ? Beam::get_beam_translation (beam)
146     : 0.81;
147
148   Stencil mol = raw_stencil (me, robust_scm2double (me->get_property ("slope"),
149                                                     0.25));
150   Interval mol_ext = mol.extent (Y_AXIS);
151   Real ss = Staff_symbol_referencer::staff_space (me);
152
153   // ugh, rather calc from Stem_tremolo_req
154   int beam_count = beam ? (Stem::beam_multiplicity (stem).length () + 1) : 0;
155
156   Real beamthickness = 0.0;
157   SCM sbt = (beam) ? beam->get_property ("thickness") : SCM_EOL;
158   if (scm_is_number (sbt))
159     beamthickness = scm_to_double (sbt) * ss;
160
161   Real end_y
162     = Stem::stem_end_position (stem) * ss / 2
163     - stemdir * (beam_count * beamthickness
164                  + (max (beam_count -1, 0) * beam_translation));
165
166   /* FIXME: the 0.33 ss is to compensate for the size of the note head.  */
167   Real chord_start_y = Stem::chord_start_y (stem) + 0.33 * ss * stemdir;
168
169   Real padding = beam_translation;
170
171   /* if there is a flag, just above/below the notehead.
172      if there is not enough space, center on remaining space,
173      else one beamspace away from stem end.  */
174   if (!beam && Stem::duration_log (stem) >= 3)
175     {
176       mol.align_to (Y_AXIS, -stemdir);
177       mol.translate_axis (chord_start_y + 0.5 * stemdir, Y_AXIS);
178     }
179   else if (stemdir * (end_y - chord_start_y) - 2 * padding - mol_ext.length ()
180            < 0.0)
181     mol.translate_axis (0.5 * (end_y + chord_start_y) - mol_ext.center (),
182                         Y_AXIS);
183   else
184     mol.translate_axis (end_y - stemdir * beam_translation -mol_ext [stemdir],
185                         Y_AXIS);
186
187   return mol.smobbed_copy ();
188 }
189
190 ADD_INTERFACE (Stem_tremolo, "stem-tremolo-interface",
191                "A beam slashing a stem to indicate a tremolo.",
192                "stem "
193                "slope "
194                "beam-width "
195                "beam-thickness "
196                "flag-count");