]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
*** empty log message ***
[lilypond.git] / lily / hairpin.cc
1 /*
2   hairpin.cc -- implement Hairpin
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "hairpin.hh"
10
11 #include "dimensions.hh"
12 #include "font-interface.hh"
13 #include "international.hh"
14 #include "line-interface.hh"
15 #include "lookup.hh"
16 #include "output-def.hh"
17 #include "paper-column.hh"
18 #include "pointer-group-interface.hh"
19 #include "spanner.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "text-interface.hh"
22 #include "warn.hh"
23
24 MAKE_SCHEME_CALLBACK (Hairpin, after_line_breaking, 1);
25 SCM
26 Hairpin::after_line_breaking (SCM smob)
27 {
28   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
29   consider_suicide (me);
30
31   return SCM_UNSPECIFIED;
32 }
33
34 void
35 Hairpin::consider_suicide (Spanner*me)
36 {
37   Drul_array<bool> broken;
38   Drul_array<Item *> bounds;
39   Direction d = LEFT;
40   do
41     {
42       bounds[d] = me->get_bound (d);
43       broken[d] = bounds[d]->break_status_dir () != CENTER;
44     }
45   while (flip (&d) != LEFT);
46
47   if (broken[LEFT]
48       && ly_is_equal (bounds[RIGHT]->get_column ()->get_property ("when"),
49                       bounds[LEFT]->get_property ("when")))
50     me->suicide ();
51   
52 }
53
54 MAKE_SCHEME_CALLBACK (Hairpin, print, 1);
55
56 SCM
57 Hairpin::print (SCM smob)
58 {
59   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
60
61   consider_suicide (me);
62   SCM s = me->get_property ("grow-direction");
63   if (!is_direction (s))
64     {
65       me->suicide ();
66       return SCM_EOL;
67     }
68
69   Direction grow_dir = to_dir (s);
70   Real padding = robust_scm2double (me->get_property ("bound-padding"), 0.5);
71
72   Drul_array<bool> broken;
73   Drul_array<Item *> bounds;
74   Direction d = LEFT;
75   do
76     {
77       bounds[d] = me->get_bound (d);
78       broken[d] = bounds[d]->break_status_dir () != CENTER;
79     }
80   while (flip (&d) != LEFT);
81   if (broken[RIGHT])
82     {
83       Spanner *orig = dynamic_cast<Spanner*> (me->original ());
84       if (me->get_break_index ()
85           < orig->broken_intos_.size () - 1)
86         {
87           Spanner *next = orig->broken_intos_[me->get_break_index () + 1];
88           Stencil *s = next->get_stencil ();
89           if (!s || s->is_empty ())
90             broken[RIGHT] = false;
91         }
92     }
93
94   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
95   Drul_array<Real> x_points;
96
97   /*
98     Use the height and thickness of the hairpin when making a circled tip
99   */
100   bool circled_tip = ly_scm2bool (me->get_property ("circled-tip"));
101   Real height = robust_scm2double (me->get_property ("height"), 0.2) *
102                 Staff_symbol_referencer::staff_space (me);
103   /*
104     FIXME: 0.65 is just a guess...
105   */
106   Real rad = height * 0.65;
107   Real thick = 1.0;
108   if (circled_tip)
109     thick = robust_scm2double (me->get_property ("thickness"), 1.0)
110             * Staff_symbol_referencer::line_thickness (me);
111
112   do
113     {
114       Item *b = bounds[d];
115       x_points[d] = b->relative_coordinate (common, X_AXIS);
116       if (broken [d])
117         {
118           if (d == LEFT)
119             x_points[d] = b->extent (common, X_AXIS)[RIGHT];
120         }
121       else
122         {
123           if (Text_interface::has_interface (b))
124             {
125               Interval e = b->extent (common, X_AXIS);
126               if (!e.is_empty ())
127                 x_points[d] = e[-d] - d * padding;
128             }
129           else
130             {
131               bool neighbor_found = false;
132               extract_grob_set (me, "adjacent-hairpins", pins);
133               for (vsize i = 0; i < pins.size (); i++)
134                 {
135                   /*
136                     FIXME: this will fuck up in case of polyphonic
137                     notes in other voices. Need to look at note-columns
138                     in the current staff/voice.
139                   */
140
141                   Spanner *pin = dynamic_cast<Spanner *> (pins[i]);
142                   if (pin
143                       && (pin->get_bound (LEFT)->get_column () == b->get_column ()
144                           || pin->get_bound (RIGHT)->get_column () == b->get_column ()))
145                     neighbor_found = true;
146                 }
147
148               Interval e = robust_relative_extent (b, common, X_AXIS);
149               if (neighbor_found)
150                 {
151                   /*
152                     Handle back-to-back hairpins with a circle in the middle
153                   */
154                   if (circled_tip && (grow_dir != d))
155                     x_points[d] = e.center () + d * (rad - thick / 2.0);
156                   /*
157                     If we're hung on a paper column, that means we're not
158                     adjacent to a text-dynamic, and we may move closer. We
159                     make the padding a little smaller, here.
160                   */
161                   else
162                     x_points[d] = e.center () - d * padding / 3;
163                 }
164               else
165                 x_points[d] = e[d];
166             }
167         }
168     }
169   while (flip (&d) != LEFT);
170
171   Real width = x_points[RIGHT] - x_points[LEFT];
172   if (width < 0)
173     {
174       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
175                       : "crescendo too small"));
176       width = 0;
177     }
178
179   bool continued = broken[Direction (-grow_dir)];
180
181   Real starth, endh;
182   if (grow_dir < 0)
183     {
184       starth = height;
185       endh = continued ? height / 2 : 0.0;
186     }
187   else
188     {
189       starth = continued ? height / 2 : 0.0;
190       endh = height;
191     }
192
193   /*
194     should do relative to staff-symbol staff-space?
195   */
196
197   Stencil mol;
198   Real x = 0.0;
199
200   /*
201     Compensate for size of circle
202   */
203   Direction tip_dir = -grow_dir;
204   if (circled_tip && !broken[tip_dir])
205     {
206       if (grow_dir > 0)
207         x = rad * 2.0;
208       else if (grow_dir < 0)
209         width -= rad *2.0;
210     }
211   mol = Line_interface::line (me, Offset (x, starth), Offset (width, endh));
212   mol.add_stencil (Line_interface::line (me,
213                                          Offset (x, -starth),
214                                          Offset (width, -endh)));
215
216   /*
217     Support al/del niente notation by putting a circle at the
218     tip of the (de)crescendo.
219   */
220   if (circled_tip)
221     {
222       Box extent (Interval (-rad, rad), Interval (-rad, rad));
223       
224       /* Hmmm, perhaps we should have a Lookup::circle () method? */
225       Stencil circle(extent,
226                      scm_list_4 (ly_symbol2scm ("circle"),
227                                  scm_from_double (rad),
228                                  scm_from_double (thick),
229                                  SCM_BOOL_F));
230
231       /*
232         don't add another circle the hairpin is broken
233       */
234       if (!broken[tip_dir])
235         mol.add_at_edge (X_AXIS, tip_dir, Stencil (circle), 0, 0);
236     }
237
238   mol.translate_axis (x_points[LEFT]
239                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
240                       X_AXIS);
241   return mol.smobbed_copy ();
242 }
243
244 ADD_INTERFACE (Hairpin, "hairpin-interface",
245                "A hairpin crescendo/decrescendo.",
246                "adjacent-hairpins "
247                "circled-tip "
248                "bound-padding "
249                "grow-direction "
250                "height "
251                );