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