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