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