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