]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
* lily/hairpin.cc: use Line_interface
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "molecule.hh"
10 #include "line-interface.hh"
11 #include "hairpin.hh"
12 #include "spanner.hh"
13 #include "font-interface.hh"
14 #include "dimensions.hh"
15 #include "paper-def.hh"
16 #include "warn.hh"
17 #include "paper-column.hh"
18 #include "lookup.hh"
19
20 MAKE_SCHEME_CALLBACK (Hairpin, brew_molecule, 1);
21
22 SCM
23 Hairpin::brew_molecule (SCM smob) 
24 {
25   Grob *me= unsmob_grob (smob);
26   Spanner *spanner = dynamic_cast<Spanner*> (me);
27
28   Real line = me->get_paper ()->get_realvar (ly_symbol2scm ("linethickness"));  
29   
30   SCM s = me->get_grob_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
40   /* Ugh, must be same as Text_spanner::brew_molecule.  */
41
42   /*
43     Ugh. property name is not general.
44    */
45   Real padding = gh_scm2double (me->get_grob_property ("if-text-padding"));
46  
47   Drul_array<bool> broken;
48   Drul_array<Item*> bounds ;
49   Direction d = LEFT;
50   do
51     {
52       bounds[d] =spanner->get_bound (d);
53       broken[d] = bounds[d]->break_status_dir () != CENTER;
54     }
55   while (flip (&d) != LEFT);
56
57   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
58   Drul_array<Real> x_points ;
59   
60   do
61     {
62       Item *b = bounds[d];
63       x_points[d]  = b->relative_coordinate (common, X_AXIS);
64       if (broken [d])
65         {
66           if (d == LEFT)
67             x_points[d] = b->extent (common,X_AXIS)[RIGHT] ;
68         }
69       else
70         {
71           if (dynamic_cast<Paper_column*> (b))
72             {
73               /*
74                 If we're hung on a paper column, that means we're not
75                 adjacent to a text-dynamic, and we may move closer. We
76                 make the padding a little smaller, here.
77               */
78               Interval e =b->extent (common, X_AXIS);
79               if (e.is_empty ())
80                 e = Interval (0,0) + b->relative_coordinate (common, X_AXIS);
81               
82               x_points[d] = e.center () - d  * padding /3; // ugh.
83             }
84           else
85             {
86               Interval e =b->extent (common, X_AXIS);
87               if (!e.is_empty ())
88                 x_points[d] = e[-d] - d*padding;
89             }
90         }
91     }
92   while (flip (&d) != LEFT);
93
94
95   Real width = x_points[RIGHT] - x_points[LEFT];
96
97   if (width < 0)
98     {
99       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
100                   : "crescendo too small"));
101       width = 0;
102     }
103
104   bool continued = broken[Direction (-grow_dir)];
105   Real height = gh_scm2double (me->get_grob_property ("height"));
106   Real thick = line * gh_scm2double (me->get_grob_property ("thickness"));
107
108   Real starth, endh;
109   if (grow_dir < 0)
110     {
111       starth = height;
112       endh = continued ? height/2 : 0.0;
113     }
114   else
115     {
116       starth = continued ? height/2 : 0.0;
117       endh = height;
118     }
119
120   /*
121     should do relative to staff-symbol staff-space?
122    */
123
124   Molecule mol;
125   mol  = Line_interface::dashed_line (me,
126                                       thick,
127                                       Offset (0, starth),
128                                       Offset (width, endh));
129   mol.add_molecule (Line_interface::dashed_line (me,
130                                                  thick,
131                                                  Offset (0, -starth),
132                                                  Offset (width, -endh)));
133
134   mol.translate_axis (x_points[LEFT]
135                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
136                       X_AXIS);
137   return mol.smobbed_copy ();
138 }
139
140
141
142 ADD_INTERFACE (Hairpin, "hairpin-interface",
143   "hairpin crescendo.",
144   "grow-direction height if-text-padding");
145