]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
* lily/instrument-name-engraver.cc (stop_translation_timestep):
[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   consider_suicide (me);
62   SCM s = me->get_property ("grow-direction");
63   if (!is_direction (s))
64     {
65       me->suicide ();
66       return SCM_EOL;
67     }
68
69   Direction grow_dir = to_dir (s);
70   Real padding = robust_scm2double (me->get_property ("bound-padding"), 0.5);
71
72   Drul_array<bool> broken;
73   Drul_array<Item *> bounds;
74   Direction d = LEFT;
75   do
76     {
77       bounds[d] = me->get_bound (d);
78       broken[d] = bounds[d]->break_status_dir () != CENTER;
79     }
80   while (flip (&d) != LEFT);
81   if (broken[RIGHT])
82     {
83       Spanner *orig = dynamic_cast<Spanner*> (me->original ());
84       if (me->get_break_index ()
85           < orig->broken_intos_.size () - 1)
86         {
87           Spanner *next = orig->broken_intos_[me->get_break_index () + 1];
88           Stencil *s = next->get_stencil ();
89           if (!s || s->is_empty ())
90             broken[RIGHT] = false;
91         }
92     }
93
94   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
95   Drul_array<Real> x_points;
96
97   do
98     {
99       Item *b = bounds[d];
100       x_points[d] = b->relative_coordinate (common, X_AXIS);
101       if (broken [d])
102         {
103           if (d == LEFT)
104             x_points[d] = b->extent (common, X_AXIS)[RIGHT];
105         }
106       else
107         {
108           if (Text_interface::has_interface (b))
109             {
110               Interval e = b->extent (common, X_AXIS);
111               if (!e.is_empty ())
112                 x_points[d] = e[-d] - d * padding;
113             }
114           else
115             {
116               bool neighbor_found = false;
117               extract_grob_set (me, "adjacent-hairpins", pins);
118               for (vsize i = 0; i < pins.size (); i++)
119                 {
120                   /*
121                     FIXME: this will fuck up in case of polyphonic
122                     notes in other voices. Need to look at note-columns
123                     in the current staff/voice.
124                   */
125
126                   Spanner *pin = dynamic_cast<Spanner *> (pins[i]);
127                   if (pin
128                       && (pin->get_bound (LEFT)->get_column () == b->get_column ()
129                           || pin->get_bound (RIGHT)->get_column () == b->get_column ()))
130                     neighbor_found = true;
131                 }
132
133               /*
134                 If we're hung on a paper column, that means we're not
135                 adjacent to a text-dynamic, and we may move closer. We
136                 make the padding a little smaller, here.
137               */
138               Interval e = robust_relative_extent (b, common, X_AXIS);
139               x_points[d]
140                 = neighbor_found ? e.center () - d * padding / 3 : e[d];
141             }
142         }
143     }
144   while (flip (&d) != LEFT);
145
146   Real width = x_points[RIGHT] - x_points[LEFT];
147   if (width < 0)
148     {
149       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
150                       : "crescendo too small"));
151       width = 0;
152     }
153
154   bool continued = broken[Direction (-grow_dir)];
155   Real height = robust_scm2double (me->get_property ("height"), 0.2) *
156     Staff_symbol_referencer::staff_space (me);
157
158   Real starth, endh;
159   if (grow_dir < 0)
160     {
161       starth = height;
162       endh = continued ? height / 2 : 0.0;
163     }
164   else
165     {
166       starth = continued ? height / 2 : 0.0;
167       endh = height;
168     }
169
170   /*
171     should do relative to staff-symbol staff-space?
172   */
173
174   Stencil mol;
175   mol = Line_interface::line (me, Offset (0, starth), Offset (width, endh));
176   mol.add_stencil (Line_interface::line (me,
177                                          Offset (0, -starth),
178                                          Offset (width, -endh)));
179
180   mol.translate_axis (x_points[LEFT]
181                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
182                       X_AXIS);
183   return mol.smobbed_copy ();
184 }
185
186 ADD_INTERFACE (Hairpin, "hairpin-interface",
187                "A hairpin crescendo/decrescendo.",
188                "adjacent-hairpins "
189                "bound-padding "
190                "grow-direction "
191                "height "
192                );
193