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