]> git.donarmstrong.com Git - lilypond.git/blob - lily/hairpin.cc
release: 1.5.47
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "molecule.hh"
10 #include "hairpin.hh"
11 #include "spanner.hh"
12 #include "font-interface.hh"
13 #include "dimensions.hh"
14 #include "paper-def.hh"
15 #include "debug.hh"
16 #include "paper-column.hh"
17
18 MAKE_SCHEME_CALLBACK (Hairpin, brew_molecule, 1);
19
20 SCM
21 Hairpin::brew_molecule (SCM smob) 
22 {
23   Grob *me= unsmob_grob (smob);
24   Spanner *spanner = dynamic_cast<Spanner*> (me);
25
26   Real line = me->paper_l ()->get_var ("stafflinethickness");  
27   
28   SCM s = me->get_grob_property ("grow-direction");
29   if (!ly_dir_p (s))
30     {
31       me->suicide ();
32       return SCM_EOL;
33     }
34   
35   Direction grow_dir = to_dir (s);
36
37
38   /* Ugh, must be same as Text_spanner::brew_molecule.  */  
39   Real padding = gh_scm2double (me->get_grob_property ("if-text-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 (dynamic_cast<Paper_column*> (b))
66             {
67               /*
68                 If we're hung on a paper column, that means we're not
69                 adjacent to a text-dynamic, and we may move closer. We
70                 make the padding a little smaller, here.
71               */
72               Interval e =b->extent (common, X_AXIS);
73               if (e.empty_b ())
74                 e = Interval (0,0) + b->relative_coordinate (common, X_AXIS);
75               
76               x_points[d] = e.center () - d  * padding /3; // ugh.
77             }
78           else
79             {
80               Interval e =b->extent (common, X_AXIS);
81               if (!e.empty_b ())
82                 x_points[d] = e[-d] - d*padding;
83             }
84         }
85     }
86   while (flip (&d) != LEFT);
87
88
89   Real width = x_points[RIGHT] - x_points[LEFT];
90
91   if (width < 0)
92     {
93       me->warning (_ ((grow_dir < 0) ? "decrescendo too small"
94                   : "crescendo too small"));
95       width = 0;
96     }
97
98   bool continued = broken[Direction (-grow_dir)];
99   Real height = gh_scm2double (me->get_grob_property ("height"));
100   Real thick = line * gh_scm2double (me->get_grob_property ("thickness"));
101
102   Real starth, endh;
103   if (grow_dir < 0)
104     {
105       starth = height;
106       endh = continued ? height/2 : 0.0;
107     }
108   else
109     {
110       starth = continued ? height/2 : 0.0;
111       endh = height;
112     }
113   
114   /*
115     TODO: junk this and, make a general
116
117     Lookup::line  (XY1, XY2).
118   */
119   SCM hairpin = scm_list_n (ly_symbol2scm ("hairpin"),
120                          gh_double2scm (thick),
121                          gh_double2scm (width),
122                          gh_double2scm (starth),
123                          gh_double2scm (endh),
124                          SCM_UNDEFINED);
125
126   /*
127     We make the hairpin too large in Y direction, so it stays at
128     proper distance from the staff.
129   */
130   Interval yext = 2* height  * Interval (-1,1);
131   Box b (Interval (0, width), yext);
132   Molecule mol (b, hairpin);
133   
134   mol.translate_axis (x_points[LEFT]
135                       - bounds[LEFT]->relative_coordinate (common, X_AXIS),
136                       X_AXIS);
137
138   return mol.smobbed_copy ();
139 }
140
141
142
143 ADD_INTERFACE (Hairpin, "hairpin-interface",
144   "hairpin crescendo.
145
146 padding -- horizontal padding. This is useful if a crescendo is set next to a text like `mf'
147
148 ",
149   "grow-direction thickness height padding");
150