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