]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
*** empty log message ***
[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
30   Drul_array<bool> broken;
31   Drul_array<Item *> bounds;
32   Direction d = LEFT;
33   do
34     {
35       bounds[d] = me->get_bound (d);
36       broken[d] = bounds[d]->break_status_dir () != CENTER;
37     }
38   while (flip (&d) != LEFT);
39
40   if (broken[LEFT]
41       && ly_is_equal (bounds[RIGHT]->get_column ()->get_property ("when"),
42                       bounds[LEFT]->get_property ("when")))
43     me->suicide ();
44   return SCM_UNSPECIFIED;
45 }
46
47 MAKE_SCHEME_CALLBACK (Hairpin, print, 1);
48
49 SCM
50 Hairpin::print (SCM smob)
51 {
52   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
53
54   SCM s = me->get_property ("grow-direction");
55   if (!is_direction (s))
56     {
57       me->suicide ();
58       return SCM_EOL;
59     }
60
61   Direction grow_dir = to_dir (s);
62   Real padding = robust_scm2double (me->get_property ("bound-padding"), 0.5);
63
64   Drul_array<bool> broken;
65   Drul_array<Item *> bounds;
66   Direction d = LEFT;
67   do
68     {
69       bounds[d] = me->get_bound (d);
70       broken[d] = bounds[d]->break_status_dir () != CENTER;
71     }
72   while (flip (&d) != LEFT);
73
74   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
75   Drul_array<Real> x_points;
76
77   do
78     {
79       Item *b = bounds[d];
80       x_points[d] = b->relative_coordinate (common, X_AXIS);
81       if (broken [d])
82         {
83           if (d == LEFT)
84             x_points[d] = b->extent (common, X_AXIS)[RIGHT];
85         }
86       else
87         {
88           if (Text_interface::has_interface (b))
89             {
90               Interval e = b->extent (common, X_AXIS);
91               if (!e.is_empty ())
92                 x_points[d] = e[-d] - d * padding;
93             }
94           else
95             {
96               bool neighbor_found = false;
97               extract_grob_set (me, "adjacent-hairpins", pins);
98               for (int i = 0; i < pins.size (); i++)
99                 {
100                   /*
101                     FIXME: this will fuck up in case of polyphonic
102                     notes in other voices. Need to look at note-columns
103                     in the current staff/voice.
104                   */
105
106                   Spanner *pin = dynamic_cast<Spanner *> (pins[i]);
107                   if (pin
108                       && (pin->get_bound (LEFT)->get_column () == b->get_column ()
109                           || pin->get_bound (RIGHT)->get_column () == b->get_column ()))
110                     neighbor_found = true;
111                 }
112
113               /*
114                 If we're hung on a paper column, that means we're not
115                 adjacent to a text-dynamic, and we may move closer. We
116                 make the padding a little smaller, here.
117               */
118               Interval e = robust_relative_extent (b, common, X_AXIS);
119               x_points[d]
120                 = neighbor_found ? e.center () - d * padding / 3 : e[d];
121             }
122         }
123     }
124   while (flip (&d) != LEFT);
125
126   Real width = x_points[RIGHT] - x_points[LEFT];
127   if (width < 0)
128     {
129       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
130                       : "crescendo too small"));
131       width = 0;
132     }
133
134   bool continued = broken[Direction (-grow_dir)];
135   Real height = robust_scm2double (me->get_property ("height"), 0.2) *
136     Staff_symbol_referencer::staff_space (me);
137
138   Real starth, endh;
139   if (grow_dir < 0)
140     {
141       starth = height;
142       endh = continued ? height / 2 : 0.0;
143     }
144   else
145     {
146       starth = continued ? height / 2 : 0.0;
147       endh = height;
148     }
149
150   /*
151     should do relative to staff-symbol staff-space?
152   */
153
154   Stencil mol;
155   mol = Line_interface::line (me, Offset (0, starth), Offset (width, endh));
156   mol.add_stencil (Line_interface::line (me,
157                                          Offset (0, -starth),
158                                          Offset (width, -endh)));
159
160   mol.translate_axis (x_points[LEFT]
161                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
162                       X_AXIS);
163   return mol.smobbed_copy ();
164 }
165
166 ADD_INTERFACE (Hairpin, "hairpin-interface",
167                "A hairpin crescendo/decrescendo.",
168                "adjacent-hairpins "
169                "bound-padding "
170                "grow-direction "
171                "height "
172                );
173