]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
Imported sources
[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 "molecule.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 "paper-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_grob_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
39   /* Ugh, must be same as Text_spanner::print.  */
40
41   /*
42     Ugh. property name is not general.
43    */
44   Real padding = gh_scm2double (me->get_grob_property ("if-text-padding"));
45  
46   Drul_array<bool> broken;
47   Drul_array<Item*> bounds ;
48   Direction d = LEFT;
49   do
50     {
51       bounds[d] =spanner->get_bound (d);
52       broken[d] = bounds[d]->break_status_dir () != CENTER;
53     }
54   while (flip (&d) != LEFT);
55
56   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
57   Drul_array<Real> x_points ;
58   
59   do
60     {
61       Item *b = bounds[d];
62       x_points[d]  = b->relative_coordinate (common, X_AXIS);
63       if (broken [d])
64         {
65           if (d == LEFT)
66             x_points[d] = b->extent (common,X_AXIS)[RIGHT] ;
67         }
68       else
69         {
70           if (dynamic_cast<Paper_column*> (b))
71             {
72               /*
73                 If we're hung on a paper column, that means we're not
74                 adjacent to a text-dynamic, and we may move closer. We
75                 make the padding a little smaller, here.
76               */
77               Interval e =b->extent (common, X_AXIS);
78               if (e.is_empty ())
79                 e = Interval (0,0) + b->relative_coordinate (common, X_AXIS);
80               
81               x_points[d] = e.center () - d  * padding /3; // ugh.
82             }
83           else
84             {
85               Interval e =b->extent (common, X_AXIS);
86               if (!e.is_empty ())
87                 x_points[d] = e[-d] - d*padding;
88             }
89         }
90     }
91   while (flip (&d) != LEFT);
92
93
94   Real width = x_points[RIGHT] - x_points[LEFT];
95
96   if (width < 0)
97     {
98       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
99                   : "crescendo too small"));
100       width = 0;
101     }
102
103   bool continued = broken[Direction (-grow_dir)];
104   Real height = robust_scm2double (me->get_grob_property ("height"), 0.2) *
105     Staff_symbol_referencer::staff_space (me);
106
107   Real starth, endh;
108   if (grow_dir < 0)
109     {
110       starth = height;
111       endh = continued ? height/2 : 0.0;
112     }
113   else
114     {
115       starth = continued ? height/2 : 0.0;
116       endh = height;
117     }
118
119   /*
120     should do relative to staff-symbol staff-space?
121    */
122
123   Molecule mol;
124   mol  = Line_interface::line (me, Offset (0, starth), Offset (width, endh));
125   mol.add_molecule (Line_interface::line (me,
126                                                  Offset (0, -starth),
127                                                  Offset (width, -endh)));
128
129   mol.translate_axis (x_points[LEFT]
130                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
131                       X_AXIS);
132   return mol.smobbed_copy ();
133 }
134
135
136
137 ADD_INTERFACE (Hairpin, "hairpin-interface",
138   "hairpin crescendo.",
139   "grow-direction height if-text-padding");
140