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