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