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