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