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