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