]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
Web-ja: update introduction
[lilypond.git] / lily / hairpin.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 "dimensions.hh"
24 #include "directional-element-interface.hh"
25 #include "international.hh"
26 #include "line-interface.hh"
27 #include "output-def.hh"
28 #include "paper-column.hh"
29 #include "pointer-group-interface.hh"
30 #include "spanner.hh"
31 #include "staff-symbol-referencer.hh"
32 #include "text-interface.hh"
33 #include "note-column.hh"
34 #include "system.hh"
35 #include "warn.hh"
36
37 MAKE_SCHEME_CALLBACK (Hairpin, pure_height, 3);
38 SCM
39 Hairpin::pure_height (SCM smob, SCM, SCM)
40 {
41   Grob *me = unsmob<Grob> (smob);
42   Real height = robust_scm2double (me->get_property ("height"), 0.0)
43                 * Staff_symbol_referencer::staff_space (me);
44
45   Real thickness = robust_scm2double (me->get_property ("thickness"), 1)
46                    * Staff_symbol_referencer::line_thickness (me);
47
48   height += thickness / 2;
49   return ly_interval2scm (Interval (-height, height));
50 }
51
52 MAKE_SCHEME_CALLBACK (Hairpin, broken_bound_padding, 1);
53 SCM
54 Hairpin::broken_bound_padding (SCM smob)
55 {
56   Spanner *me = unsmob<Spanner> (smob);
57   Item *r_bound = me->get_bound (RIGHT);
58   if (r_bound->break_status_dir () != -1)
59     {
60       me->warning (_ ("Asking for broken bound padding at a non-broken bound."));
61       return scm_from_double (0.0);
62     }
63
64   System *sys = dynamic_cast<System *> (me->get_system ());
65   Direction dir = get_grob_direction (me->get_parent (Y_AXIS));
66   if (!dir)
67     return scm_from_double (0.0);
68
69   Grob *my_vertical_axis_group = Grob::get_vertical_axis_group (me);
70   Drul_array<Grob *> vertical_axis_groups;
71   for (DOWN_and_UP (d))
72     vertical_axis_groups[d] = d == dir
73                               ? sys->get_neighboring_staff (d, my_vertical_axis_group, Interval_t<int> (me->spanned_rank_interval ()))
74                               : my_vertical_axis_group;
75
76   if (!vertical_axis_groups[dir])
77     return scm_from_double (0.0);
78
79   Drul_array<Grob *> span_bars (0, 0);
80   for (DOWN_and_UP (d))
81     {
82       extract_grob_set (vertical_axis_groups[d], "elements", elts);
83       for (vsize i = elts.size (); i--;)
84         if (elts[i]->internal_has_interface (ly_symbol2scm ("bar-line-interface"))
85             && dynamic_cast<Item *> (elts[i])->break_status_dir () == -1)
86           {
87             SCM hsb = elts[i]->get_property ("has-span-bar");
88             if (!scm_is_pair (hsb))
89               break;
90
91             span_bars[d] = unsmob<Grob> ((d == UP ? scm_car : scm_cdr) (hsb));
92             break;
93           }
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   if (broken[RIGHT])
131     {
132       Spanner *next = me->broken_neighbor (RIGHT);
133       // Hairpin-parts suicide in after-line-breaking if they need not be drawn
134       if (next)
135         {
136           (void) next->get_property ("after-line-breaking");
137           broken[RIGHT] = next->is_live ();
138         }
139       else
140         broken[RIGHT] = false;
141     }
142
143   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
144   Drul_array<Real> x_points;
145
146   /*
147     Use the height and thickness of the hairpin when making a circled tip
148   */
149   bool circled_tip = ly_scm2bool (me->get_property ("circled-tip"));
150   Real height = robust_scm2double (me->get_property ("height"), 0.2)
151                 * Staff_symbol_referencer::staff_space (me);
152   /*
153     FIXME: 0.525 is still just a guess...
154   */
155   Real rad = height * 0.525;
156   Real thick = 1.0;
157   if (circled_tip)
158     thick = robust_scm2double (me->get_property ("thickness"), 1.0)
159             * Staff_symbol_referencer::line_thickness (me);
160   Drul_array<Real> shorten = robust_scm2interval (me->get_property ("shorten-pair"),
161                                                   Interval (0, 0));
162
163   for (LEFT_and_RIGHT (d))
164     {
165       Item *b = bounds[d];
166       Interval e = Axis_group_interface::generic_bound_extent (b, common, X_AXIS);
167
168       x_points[d] = b->relative_coordinate (common, X_AXIS);
169       if (broken [d])
170         {
171           if (d == LEFT)
172             x_points[d] = e[-d];
173           else
174             {
175               Real broken_bound_padding
176                 = robust_scm2double (me->get_property ("broken-bound-padding"), 0.0);
177               extract_grob_set (me, "concurrent-hairpins", chp);
178               for (vsize i = 0; i < chp.size (); i++)
179                 {
180                   Spanner *span_elt = dynamic_cast<Spanner *> (chp[i]);
181                   if (span_elt->get_bound (RIGHT)->break_status_dir () == LEFT)
182                     broken_bound_padding = max (broken_bound_padding,
183                                                 robust_scm2double (span_elt->get_property ("broken-bound-padding"), 0.0));
184                 }
185               x_points[d] -= d * broken_bound_padding;
186             }
187         }
188       else
189         {
190           if (has_interface<Text_interface> (b))
191             {
192               if (!e.is_empty ())
193                 x_points[d] = e[-d] - d * padding;
194             }
195           else
196             {
197               bool neighbor_found = false;
198               Spanner *adjacent = NULL;
199               extract_grob_set (me, "adjacent-spanners", neighbors);
200               for (vsize i = 0; i < neighbors.size (); i++)
201                 {
202                   /*
203                     FIXME: this will fuck up in case of polyphonic
204                     notes in other voices. Need to look at note-columns
205                     in the current staff/voice.
206                   */
207                   adjacent = dynamic_cast<Spanner *> (neighbors[i]);
208                   if (adjacent
209                       && (adjacent->get_bound (-d)->get_column ()
210                           == b->get_column ()))
211                     {
212                       neighbor_found = true;
213                       break;
214                     }
215                 }
216
217               if (neighbor_found)
218                 {
219                   if (has_interface<Hairpin> (adjacent))
220                     {
221                       /*
222                         Handle back-to-back hairpins with a circle in the middle
223                       */
224                       if (circled_tip && (grow_dir != d))
225                         {
226                           x_points[d] = e.center () + d * (rad - thick / 2.0);
227                           shorten[d] = 0.0;
228                         }
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 (d == RIGHT // end at the left edge of a rest
245                       && has_interface<Note_column> (b)
246                       && Note_column::has_rests (b))
247                     x_points[d] = e[-d];
248                   else
249                     x_points[d] = e[d];
250
251                   if (Item::is_non_musical (b))
252                     x_points[d] -= d * padding;
253                 }
254             }
255         }
256
257         x_points[d] -= shorten[d] * d;
258     }
259
260   Real width = x_points[RIGHT] - x_points[LEFT];
261
262   if (width < 0)
263     {
264       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
265                       : "crescendo too small"));
266       width = 0;
267     }
268
269   bool continued = broken[Direction (-grow_dir)];
270   bool continuing = broken[Direction (grow_dir)];
271
272   Real starth = 0;
273   Real endh = 0;
274   if (grow_dir < 0)
275     {
276       starth = continuing ? 2 * height / 3 : height;
277       endh = continued ? height / 3 : 0.0;
278     }
279   else
280     {
281       starth = continued ? height / 3 : 0.0;
282       endh = continuing ? 2 * height / 3 : height;
283     }
284
285   /*
286     should do relative to staff-symbol staff-space?
287   */
288   Stencil mol;
289   Real x = 0.0;
290
291   /*
292     Compensate for size of circle
293   */
294   Direction tip_dir = -grow_dir;
295   if (circled_tip && !broken[tip_dir])
296     {
297       if (grow_dir > 0)
298         x = rad * 2.0;
299       else if (grow_dir < 0)
300         width -= rad * 2.0;
301     }
302   mol = Line_interface::line (me, Offset (x, starth), Offset (width, endh));
303   mol.add_stencil (Line_interface::line (me,
304                                          Offset (x, -starth),
305                                          Offset (width, -endh)));
306
307   /*
308     Support al/del niente notation by putting a circle at the
309     tip of the (de)crescendo.
310   */
311   if (circled_tip)
312     {
313       Box extent (Interval (-rad, rad), Interval (-rad, rad));
314
315       /* Hmmm, perhaps we should have a Lookup::circle () method? */
316       Stencil circle (extent,
317                       scm_list_4 (ly_symbol2scm ("circle"),
318                                   scm_from_double (rad),
319                                   scm_from_double (thick),
320                                   SCM_BOOL_F));
321
322       /*
323         don't add another circle if the hairpin is broken
324       */
325       if (!broken[tip_dir])
326         mol.add_at_edge (X_AXIS, tip_dir, Stencil (circle), 0);
327     }
328
329   mol.translate_axis (x_points[LEFT]
330                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
331                       X_AXIS);
332   return mol.smobbed_copy ();
333 }
334
335 ADD_INTERFACE (Hairpin,
336                "A hairpin crescendo or decrescendo.",
337
338                /* properties */
339                "adjacent-spanners "
340                "circled-tip "
341                "concurrent-hairpins "
342                "broken-bound-padding "
343                "bound-padding "
344                "grow-direction "
345                "height "
346                "shorten-pair "
347               );