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