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