]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
7639ddd78e774210ebcea9de2da6b772a493564a
[lilypond.git] / lily / hairpin.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "hairpin.hh"
21
22 #include "axis-group-interface.hh"
23 #include "bar-line.hh"
24 #include "dimensions.hh"
25 #include "directional-element-interface.hh"
26 #include "international.hh"
27 #include "line-interface.hh"
28 #include "output-def.hh"
29 #include "paper-column.hh"
30 #include "pointer-group-interface.hh"
31 #include "spanner.hh"
32 #include "staff-symbol-referencer.hh"
33 #include "text-interface.hh"
34 #include "note-column.hh"
35 #include "system.hh"
36 #include "warn.hh"
37
38 MAKE_SCHEME_CALLBACK (Hairpin, pure_height, 3);
39 SCM
40 Hairpin::pure_height (SCM smob, SCM, SCM)
41 {
42   Grob *me = unsmob_grob (smob);
43   Real height = robust_scm2double (me->get_property ("height"), 0.0)
44                 * Staff_symbol_referencer::staff_space (me);
45
46   Real thickness = robust_scm2double (me->get_property ("thickness"), 1)
47                    * Staff_symbol_referencer::line_thickness (me);
48
49   height += thickness / 2;
50   return ly_interval2scm (Interval (-height, height));
51 }
52
53 MAKE_SCHEME_CALLBACK (Hairpin, broken_bound_padding, 1);
54 SCM
55 Hairpin::broken_bound_padding (SCM smob)
56 {
57   Spanner *me = unsmob_spanner (smob);
58   Item *r_bound = me->get_bound (RIGHT);
59   if (r_bound->break_status_dir () != -1)
60     {
61       me->warning ("Asking for broken bound padding at a non-broken bound.");
62       return scm_from_double (0.0);
63     }
64
65   System *sys = dynamic_cast<System *> (me->get_system ());
66   Direction dir = get_grob_direction (me->get_parent (Y_AXIS));
67   if (!dir)
68     return scm_from_double (0.0);
69
70   Grob *my_vertical_axis_group = Grob::get_vertical_axis_group (me);
71   Drul_array<Grob *> vertical_axis_groups;
72   for (DOWN_and_UP (d))
73     vertical_axis_groups[d] = d == dir
74                               ? sys->get_neighboring_staff (d, my_vertical_axis_group, Interval_t<int> (me->spanned_rank_interval ()))
75                               : my_vertical_axis_group;
76
77   if (!vertical_axis_groups[dir])
78     return scm_from_double (0.0);
79
80   Drul_array<Grob *> span_bars (0, 0);
81   for (DOWN_and_UP (d))
82     {
83       extract_grob_set (vertical_axis_groups[d], "elements", elts);
84       for (vsize i = elts.size (); i--;)
85         if (Bar_line::has_interface (elts[i])
86             && dynamic_cast<Item *> (elts[i])->break_status_dir () == -1)
87           {
88             SCM hsb = elts[i]->get_property ("has-span-bar");
89             if (!scm_is_pair (hsb))
90               break;
91
92             span_bars[d] = unsmob_grob ((d == UP ? scm_car : scm_cdr) (hsb));
93             break;
94           }
95       if (!span_bars[d])
96         return scm_from_double (0.0);
97     }
98
99   if (span_bars[DOWN] != span_bars[UP])
100     return scm_from_double (0.0);
101
102   return scm_from_double (robust_scm2double (me->get_property ("bound-padding"), 0.5)
103                           / 2.0);
104 }
105
106 MAKE_SCHEME_CALLBACK (Hairpin, print, 1);
107 SCM
108 Hairpin::print (SCM smob)
109 {
110   Spanner *me = unsmob_spanner (smob);
111
112   SCM s = me->get_property ("grow-direction");
113   if (!is_direction (s))
114     {
115       me->suicide ();
116       return SCM_EOL;
117     }
118
119   Direction grow_dir = to_dir (s);
120   Real padding = robust_scm2double (me->get_property ("bound-padding"), 0.5);
121
122   Drul_array<bool> broken;
123   Drul_array<Item *> bounds;
124   for (LEFT_and_RIGHT (d))
125     {
126       bounds[d] = me->get_bound (d);
127       broken[d] = bounds[d]->break_status_dir () != CENTER;
128     }
129
130   broken[RIGHT] = broken[RIGHT] && me->broken_neighbor (RIGHT);
131   broken[RIGHT] = broken[RIGHT] && me->broken_neighbor (RIGHT)->is_live ();
132
133   if (broken[RIGHT])
134     {
135       Spanner *next = me->broken_neighbor (RIGHT);
136       Stencil *s = next->get_stencil ();
137       if (!s || s->is_empty ())
138         broken[RIGHT] = false;
139     }
140
141   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
142   Drul_array<Real> x_points;
143
144   /*
145     Use the height and thickness of the hairpin when making a circled tip
146   */
147   bool circled_tip = ly_scm2bool (me->get_property ("circled-tip"));
148   Real height = robust_scm2double (me->get_property ("height"), 0.2)
149                 * Staff_symbol_referencer::staff_space (me);
150   /*
151     FIXME: 0.525 is still just a guess...
152   */
153   Real rad = height * 0.525;
154   Real thick = 1.0;
155   if (circled_tip)
156     thick = robust_scm2double (me->get_property ("thickness"), 1.0)
157             * Staff_symbol_referencer::line_thickness (me);
158
159   for (LEFT_and_RIGHT (d))
160     {
161       Item *b = bounds[d];
162       Interval e = (Paper_column::has_interface (b) && b->break_status_dir ())
163                    ? Axis_group_interface::generic_bound_extent (b, common, X_AXIS)
164                    : robust_relative_extent (b, common, X_AXIS);
165
166       x_points[d] = b->relative_coordinate (common, X_AXIS);
167       if (broken [d])
168         {
169           if (d == LEFT)
170             x_points[d] = e[-d];
171           else
172             {
173               Real broken_bound_padding
174                 = robust_scm2double (me->get_property ("broken-bound-padding"), 0.0);
175               extract_grob_set (me, "concurrent-hairpins", chp);
176               for (vsize i = 0; i < chp.size (); i++)
177                 {
178                   Spanner *span_elt = dynamic_cast<Spanner *> (chp[i]);
179                   if (span_elt->get_bound (RIGHT)->break_status_dir () == LEFT)
180                     broken_bound_padding = max (broken_bound_padding,
181                                                 robust_scm2double (span_elt->get_property ("broken-bound-padding"), 0.0));
182                 }
183               x_points[d] -= d * broken_bound_padding;
184             }
185         }
186       else
187         {
188           if (Text_interface::has_interface (b))
189             {
190               if (!e.is_empty ())
191                 x_points[d] = e[-d] - d * padding;
192             }
193           else
194             {
195               bool neighbor_found = false;
196               Spanner *adjacent = NULL;
197               extract_grob_set (me, "adjacent-spanners", neighbors);
198               for (vsize i = 0; i < neighbors.size (); i++)
199                 {
200                   /*
201                     FIXME: this will fuck up in case of polyphonic
202                     notes in other voices. Need to look at note-columns
203                     in the current staff/voice.
204                   */
205                   adjacent = dynamic_cast<Spanner *> (neighbors[i]);
206                   if (adjacent
207                       && (adjacent->get_bound (-d)->get_column ()
208                           == b->get_column ()))
209                     {
210                       neighbor_found = true;
211                       break;
212                     }
213                 }
214
215               if (neighbor_found)
216                 {
217                   if (Hairpin::has_interface (adjacent))
218                     {
219                       /*
220                         Handle back-to-back hairpins with a circle in the middle
221                       */
222                       if (circled_tip && (grow_dir != d))
223                         x_points[d] = e.center () + d * (rad - thick / 2.0);
224                       /*
225                         If we're hung on a paper column, that means we're not
226                         adjacent to a text-dynamic, and we may move closer. We
227                         make the padding a little smaller, here.
228                       */
229                       else
230                         x_points[d] = e.center () - d * padding / 3;
231                     }
232                   // Our neighbor is a dynamic text spanner.
233                   // If we end on the text, pad as for text dynamics
234                   else if (d == RIGHT)
235                     x_points[d] = e[-d] - d * padding;
236                 }
237               else
238                 {
239                   if (Note_column::has_interface (b)
240                       && Note_column::has_rests (b))
241                     x_points[d] = e[-d];
242                   else
243                     x_points[d] = e[d];
244
245                   if (Item::is_non_musical (b))
246                     x_points[d] -= d * padding;
247                 }
248             }
249         }
250     }
251
252   Real width = x_points[RIGHT] - x_points[LEFT];
253
254   if (width < 0)
255     {
256       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
257                       : "crescendo too small"));
258       width = 0;
259     }
260
261   bool continued = broken[Direction (-grow_dir)];
262   bool continuing = broken[Direction (grow_dir)];
263
264   Real starth = 0;
265   Real endh = 0;
266   if (grow_dir < 0)
267     {
268       starth = continuing ? 2 * height / 3 : height;
269       endh = continued ? height / 3 : 0.0;
270     }
271   else
272     {
273       starth = continued ? height / 3 : 0.0;
274       endh = continuing ? 2 * height / 3 : height;
275     }
276
277   /*
278     should do relative to staff-symbol staff-space?
279   */
280   Stencil mol;
281   Real x = 0.0;
282
283   /*
284     Compensate for size of circle
285   */
286   Direction tip_dir = -grow_dir;
287   if (circled_tip && !broken[tip_dir])
288     {
289       if (grow_dir > 0)
290         x = rad * 2.0;
291       else if (grow_dir < 0)
292         width -= rad * 2.0;
293     }
294   mol = Line_interface::line (me, Offset (x, starth), Offset (width, endh));
295   mol.add_stencil (Line_interface::line (me,
296                                          Offset (x, -starth),
297                                          Offset (width, -endh)));
298
299   /*
300     Support al/del niente notation by putting a circle at the
301     tip of the (de)crescendo.
302   */
303   if (circled_tip)
304     {
305       Box extent (Interval (-rad, rad), Interval (-rad, rad));
306
307       /* Hmmm, perhaps we should have a Lookup::circle () method? */
308       Stencil circle (extent,
309                       scm_list_4 (ly_symbol2scm ("circle"),
310                                   scm_from_double (rad),
311                                   scm_from_double (thick),
312                                   SCM_BOOL_F));
313
314       /*
315         don't add another circle if the hairpin is broken
316       */
317       if (!broken[tip_dir])
318         mol.add_at_edge (X_AXIS, tip_dir, Stencil (circle), 0);
319     }
320
321   mol.translate_axis (x_points[LEFT]
322                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
323                       X_AXIS);
324   return mol.smobbed_copy ();
325 }
326
327 ADD_INTERFACE (Hairpin,
328                "A hairpin crescendo or decrescendo.",
329
330                /* properties */
331                "adjacent-spanners "
332                "circled-tip "
333                "concurrent-hairpins "
334                "broken-bound-padding "
335                "bound-padding "
336                "grow-direction "
337                "height "
338               );