]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
799616bf1495cdba098a643a8f25fb8d7588b7aa
[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
21 MAKE_SCHEME_CALLBACK (Hairpin, print, 1);
22
23 SCM
24 Hairpin::print (SCM smob) 
25 {
26   Grob *me= unsmob_grob (smob);
27   Spanner *spanner = dynamic_cast<Spanner*> (me);
28
29   SCM s = me->get_property ("grow-direction");
30   if (!is_direction (s))
31     {
32       me->suicide ();
33       return SCM_EOL;
34     }
35   
36   Direction grow_dir = to_dir (s);
37
38   Real padding = scm_to_double (me->get_property ("bound-padding"));
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 (dynamic_cast<Paper_column*> (b))
65             {
66               /*
67                 If we're hung on a paper column, that means we're not
68                 adjacent to a text-dynamic, and we may move closer. We
69                 make the padding a little smaller, here.
70               */
71               Interval e = b->extent (common, X_AXIS);
72               if (e.is_empty ())
73                 e = Interval (0,0) + b->relative_coordinate (common, X_AXIS);
74               
75               x_points[d] = e.center () - d  * padding / 3; // ugh.
76             }
77           else
78             {
79               Interval e = b->extent (common, X_AXIS);
80               if (!e.is_empty ())
81                 x_points[d] = e[-d] - d*padding;
82             }
83         }
84     }
85   while (flip (&d) != LEFT);
86
87
88   Real width = x_points[RIGHT] - x_points[LEFT];
89
90   if (width < 0)
91     {
92       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
93                   : "crescendo too small"));
94       width = 0;
95     }
96
97   bool continued = broken[Direction (-grow_dir)];
98   Real height = robust_scm2double (me->get_property ("height"), 0.2) *
99     Staff_symbol_referencer::staff_space (me);
100
101   Real starth, endh;
102   if (grow_dir < 0)
103     {
104       starth = height;
105       endh = continued ? height/2 : 0.0;
106     }
107   else
108     {
109       starth = continued ? height/2 : 0.0;
110       endh = height;
111     }
112
113   /*
114     should do relative to staff-symbol staff-space?
115   */
116
117   Stencil mol;
118   mol  = Line_interface::line (me, Offset (0, starth), Offset (width, endh));
119   mol.add_stencil (Line_interface::line (me,
120                                          Offset (0, -starth),
121                                          Offset (width, -endh)));
122
123   mol.translate_axis (x_points[LEFT]
124                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
125                       X_AXIS);
126   return mol.smobbed_copy ();
127 }
128
129
130
131 ADD_INTERFACE (Hairpin, "hairpin-interface",
132   "A hairpin (de)crescendo.",
133   "grow-direction height bound-padding");
134