]> git.donarmstrong.com Git - lilypond.git/blob - lily/hyphen-spanner.cc
* lily/lyric-phrasing-engraver.cc: move from
[lilypond.git] / lily / hyphen-spanner.cc
1 /*
2   hyphen-spanner.cc -- implement Hyphen_spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1999--2003 Glen Prideaux <glenprideaux@iname.com>
7
8  (adapted from lyric-extender)
9 */
10
11 #include <math.h>
12
13 #include "box.hh"
14 #include "lookup.hh"
15 #include "molecule.hh"
16 #include "paper-def.hh"
17 #include "hyphen-spanner.hh"
18 #include "paper-column.hh"
19 #include "spanner.hh"
20 #include "item.hh"
21
22
23 MAKE_SCHEME_CALLBACK (Hyphen_spanner,set_spacing_rods,1);
24 SCM
25 Hyphen_spanner::set_spacing_rods (SCM smob)
26 {
27   Grob*me = unsmob_grob (smob);
28
29   Rod rod;
30   Spanner*sp = dynamic_cast<Spanner*> (me);
31   Item * l = sp->get_bound (LEFT);
32   Item * r =  sp->get_bound (RIGHT);
33   rod.item_l_drul_[LEFT] = l;
34   rod.item_l_drul_[RIGHT] =r;
35   rod.distance_ =
36     gh_scm2double (me->get_grob_property ("minimum-length"))
37     + l->extent (l, X_AXIS)[RIGHT]
38     - r->extent (r, X_AXIS)[LEFT];
39
40   rod.add_to_cols ();
41   return SCM_UNSPECIFIED;
42 }
43
44 MAKE_SCHEME_CALLBACK (Hyphen_spanner,brew_molecule,1)
45 SCM 
46 Hyphen_spanner::brew_molecule (SCM smob)
47 {
48   Spanner * sp = unsmob_spanner (smob);
49
50   Grob * common = sp;
51   Direction d = LEFT;
52   do
53     {
54       common = common->common_refpoint (sp->get_bound (d), X_AXIS);
55     }
56   while (flip (&d) != LEFT);
57   Interval bounds;
58   
59   do
60     {
61       Interval iv = sp->get_bound (d)->extent (common, X_AXIS);
62
63       bounds[d] = iv.is_empty ()
64         ? sp->get_bound (d)->relative_coordinate (common, X_AXIS)
65         : iv[-d];
66     }
67   while (flip (&d) != LEFT);
68   
69   Real lt = sp->get_paper ()->get_realvar (ly_symbol2scm ("linethickness"));
70   Real th = gh_scm2double (sp->get_grob_property ("thickness")) * lt ;
71   Real h = gh_scm2double (sp->get_grob_property ("height"));
72
73   // interval?
74   Real x = gh_scm2double (sp->get_grob_property ("maximum-length"));
75   SCM space =  sp->get_bound (LEFT)->get_grob_property ("word-space");
76
77   Real word_space  = 1.0;
78   if (gh_number_p (space))
79     {
80       word_space = gh_scm2double (space);
81     }
82
83   /*
84     We remove word space from the distance so it doesn't look like an extender.
85     
86    */
87   Real l = (gh_scm2double (sp->get_grob_property ("minimum-length"))
88     - word_space ) >? word_space;
89   
90   
91   /*
92     we should probably do something more intelligent when bounds is
93     empty, but at least this doesn't crash.
94   */      
95   Real w  = bounds.is_empty () ? 0 : bounds.length ();
96   
97   /* for length, use a geometric mean of the available space and some minimum
98    */
99   if (l < w)
100     {
101       l = sqrt (l*w);
102       if (l > x)
103         l = x;
104     }
105   else
106     {
107       /* OK, we have a problem. Usually this means that we're on the
108          first column, and we have a long lyric which extends to near
109          the offset for stuff */
110       /* This test for being on the first column has been shamelessly
111          ripped from spanner.cc */
112       Paper_column *sc = dynamic_cast<Paper_column*> (sp->get_bound (LEFT)->get_column ());
113       if (sc != NULL &&
114           sc->break_status_dir () == RIGHT)
115         {
116           /* We are on the first column, so it's probably harmless to
117              get the minimum length back by extending leftwards into
118              the space under the clef/key sig/time sig */
119           bounds[LEFT] = bounds[RIGHT] - l;
120         }
121       else 
122         {
123           /* We can't get the length desired. Maybe we should warn. */
124           l = w;
125         }
126     }
127   Box b (Interval (-l/2,l/2), Interval (h,h+th));
128   Molecule mol (Lookup::filledbox (b));
129   Real ct = bounds.is_empty () ? 0 : bounds.center () ;
130   mol.translate_axis (ct -sp->relative_coordinate (common, X_AXIS), X_AXIS);
131   return mol.smobbed_copy ();
132 }
133   
134 void
135 Hyphen_spanner::set_textitem (Direction d, Grob* b)
136 {
137   elt_->set_bound (d, b);
138   elt_->add_dependency (b);
139 }
140
141 Hyphen_spanner::Hyphen_spanner (Spanner*s)
142 {
143   elt_ = s;
144 }
145
146
147
148 ADD_INTERFACE (Hyphen_spanner, "lyric-hyphen-interface",
149                "A centred hyphen is a simple line between lyrics used to divide syllables",
150                "thickness height minimum-length maximum-length word-space");
151