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