]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / hairpin.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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   Direction d = DOWN;
73   do
74     vertical_axis_groups[d] = d == dir
75                               ? sys->get_neighboring_staff (d, my_vertical_axis_group, Interval_t<int> (me->spanned_rank_interval ()))
76                               : my_vertical_axis_group;
77   while (flip (&d) != DOWN);
78
79   if (!vertical_axis_groups[dir])
80     return scm_from_double (0.0);
81
82   Drul_array<Grob *> span_bars (0, 0);
83   d = DOWN;
84   do
85     {
86       extract_grob_set (vertical_axis_groups[d], "elements", elts);
87       for (vsize i = elts.size (); i--;)
88         if (Bar_line::has_interface (elts[i])
89             && dynamic_cast<Item *> (elts[i])->break_status_dir () == -1)
90           {
91             SCM hsb = elts[i]->get_property ("has-span-bar");
92             if (!scm_is_pair (hsb))
93               break;
94
95             span_bars[d] = unsmob_grob ((d == UP ? scm_car : scm_cdr) (hsb));
96             break;
97           }
98       if (!span_bars[d])
99         return scm_from_double (0.0);
100     }
101   while (flip (&d) != DOWN);
102
103   if (span_bars[DOWN] != span_bars[UP])
104     return scm_from_double (0.0);
105
106   return scm_from_double (robust_scm2double (me->get_property ("bound-padding"), 0.5)
107                           / 2.0);
108 }
109
110 MAKE_SCHEME_CALLBACK (Hairpin, print, 1);
111 SCM
112 Hairpin::print (SCM smob)
113 {
114   Spanner *me = unsmob_spanner (smob);
115
116   SCM s = me->get_property ("grow-direction");
117   if (!is_direction (s))
118     {
119       me->suicide ();
120       return SCM_EOL;
121     }
122
123   Direction grow_dir = to_dir (s);
124   Real padding = robust_scm2double (me->get_property ("bound-padding"), 0.5);
125
126   Drul_array<bool> broken;
127   Drul_array<Item *> bounds;
128   Direction d = LEFT;
129   do
130     {
131       bounds[d] = me->get_bound (d);
132       broken[d] = bounds[d]->break_status_dir () != CENTER;
133     }
134   while (flip (&d) != LEFT);
135
136   broken[RIGHT] = broken[RIGHT] && me->broken_neighbor (RIGHT);
137   broken[RIGHT] = broken[RIGHT] && me->broken_neighbor (RIGHT)->is_live ();
138
139   if (broken[RIGHT])
140     {
141       Spanner *next = me->broken_neighbor (RIGHT);
142       Stencil *s = next->get_stencil ();
143       if (!s || s->is_empty ())
144         broken[RIGHT] = false;
145     }
146
147   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
148   Drul_array<Real> x_points;
149
150   /*
151     Use the height and thickness of the hairpin when making a circled tip
152   */
153   bool circled_tip = ly_scm2bool (me->get_property ("circled-tip"));
154   Real height = robust_scm2double (me->get_property ("height"), 0.2)
155                 * Staff_symbol_referencer::staff_space (me);
156   /*
157     FIXME: 0.525 is still just a guess...
158   */
159   Real rad = height * 0.525;
160   Real thick = 1.0;
161   if (circled_tip)
162     thick = robust_scm2double (me->get_property ("thickness"), 1.0)
163             * Staff_symbol_referencer::line_thickness (me);
164
165   do
166     {
167       Item *b = bounds[d];
168       Interval e = (Axis_group_interface::has_interface (b)
169                     ? Axis_group_interface::generic_bound_extent (b, common, X_AXIS)
170                     : robust_relative_extent (b, common, X_AXIS));
171
172       x_points[d] = b->relative_coordinate (common, X_AXIS);
173       if (broken [d])
174         {
175           if (d == LEFT)
176             x_points[d] = e[-d];
177           else
178             {
179               Real broken_bound_padding = 0.0;
180               extract_grob_set (me, "concurrent-hairpins", chp);
181               for (vsize i = 0; i < chp.size (); i++)
182                 {
183                   Spanner *span_elt = dynamic_cast<Spanner *> (chp[i]);
184                   if (span_elt->get_bound (RIGHT)->break_status_dir () == LEFT)
185                     broken_bound_padding = max (broken_bound_padding,
186                                                 robust_scm2double (span_elt->get_property ("broken-bound-padding"), 0.0));
187                 }
188               x_points[d] -= d * broken_bound_padding;
189             }
190         }
191       else
192         {
193           if (Text_interface::has_interface (b))
194             {
195               if (!e.is_empty ())
196                 x_points[d] = e[-d] - d * padding;
197             }
198           else
199             {
200               bool neighbor_found = false;
201               Spanner *adjacent = NULL;
202               extract_grob_set (me, "adjacent-spanners", neighbors);
203               for (vsize i = 0; i < neighbors.size (); i++)
204                 {
205                   /*
206                     FIXME: this will fuck up in case of polyphonic
207                     notes in other voices. Need to look at note-columns
208                     in the current staff/voice.
209                   */
210                   adjacent = dynamic_cast<Spanner *> (neighbors[i]);
211                   if (adjacent
212                       && (adjacent->get_bound (-d)->get_column ()
213                           == b->get_column ()))
214                     {
215                       neighbor_found = true;
216                       break;
217                     }
218                 }
219
220               if (neighbor_found)
221                 {
222                   if (Hairpin::has_interface (adjacent))
223                     {
224                       /*
225                         Handle back-to-back hairpins with a circle in the middle
226                       */
227                       if (circled_tip && (grow_dir != d))
228                         x_points[d] = e.center () + d * (rad - thick / 2.0);
229                       /*
230                         If we're hung on a paper column, that means we're not
231                         adjacent to a text-dynamic, and we may move closer. We
232                         make the padding a little smaller, here.
233                       */
234                       else
235                         x_points[d] = e.center () - d * padding / 3;
236                     }
237                   // Our neighbor is a dynamic text spanner.
238                   // If we end on the text, pad as for text dynamics
239                   else if (d == RIGHT)
240                     x_points[d] = e[-d] - d * padding;
241                 }
242               else
243                 {
244                   if (Note_column::has_interface (b)
245                       && Note_column::has_rests (b))
246                     x_points[d] = e[-d];
247                   else
248                     x_points[d] = e[d];
249
250                   if (Item::is_non_musical (b))
251                     x_points[d] -= d * padding;
252                 }
253             }
254         }
255     }
256   while (flip (&d) != LEFT);
257
258   Real width = x_points[RIGHT] - x_points[LEFT];
259
260   if (width < 0)
261     {
262       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
263                       : "crescendo too small"));
264       width = 0;
265     }
266
267   bool continued = broken[Direction (-grow_dir)];
268
269   Real starth = 0;
270   Real endh = 0;
271   if (grow_dir < 0)
272     {
273       starth = height;
274       endh = continued ? height / 2 : 0.0;
275     }
276   else
277     {
278       starth = continued ? height / 2 : 0.0;
279       endh = height;
280     }
281
282   /*
283     should do relative to staff-symbol staff-space?
284   */
285   Stencil mol;
286   Real x = 0.0;
287
288   /*
289     Compensate for size of circle
290   */
291   Direction tip_dir = -grow_dir;
292   if (circled_tip && !broken[tip_dir])
293     {
294       if (grow_dir > 0)
295         x = rad * 2.0;
296       else if (grow_dir < 0)
297         width -= rad * 2.0;
298     }
299   mol = Line_interface::line (me, Offset (x, starth), Offset (width, endh));
300   mol.add_stencil (Line_interface::line (me,
301                                          Offset (x, -starth),
302                                          Offset (width, -endh)));
303
304   /*
305     Support al/del niente notation by putting a circle at the
306     tip of the (de)crescendo.
307   */
308   if (circled_tip)
309     {
310       Box extent (Interval (-rad, rad), Interval (-rad, rad));
311
312       /* Hmmm, perhaps we should have a Lookup::circle () method? */
313       Stencil circle (extent,
314                       scm_list_4 (ly_symbol2scm ("circle"),
315                                   scm_from_double (rad),
316                                   scm_from_double (thick),
317                                   SCM_BOOL_F));
318
319       /*
320         don't add another circle if the hairpin is broken
321       */
322       if (!broken[tip_dir])
323         mol.add_at_edge (X_AXIS, tip_dir, Stencil (circle), 0);
324     }
325
326   mol.translate_axis (x_points[LEFT]
327                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
328                       X_AXIS);
329   return mol.smobbed_copy ();
330 }
331
332 ADD_INTERFACE (Hairpin,
333                "A hairpin crescendo or decrescendo.",
334
335                /* properties */
336                "adjacent-spanners "
337                "circled-tip "
338                "concurrent-hairpins "
339                "broken-bound-padding "
340                "bound-padding "
341                "grow-direction "
342                "height "
343               );