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