]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-hyphen.cc
*** empty log message ***
[lilypond.git] / lily / lyric-hyphen.cc
1 /*
2   hyphen-spanner.cc -- implement Hyphen_spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2003--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "lyric-hyphen.hh"
10
11
12 #include "lookup.hh"
13 #include "output-def.hh"
14 #include "paper-column.hh"
15 #include "moment.hh"
16
17 MAKE_SCHEME_CALLBACK (Hyphen_spanner, print, 1)
18   SCM
19 Hyphen_spanner::print (SCM smob)
20 {
21   Spanner *me = unsmob_spanner (smob);
22   Drul_array<Item *> bounds (me->get_bound (LEFT),
23                              me->get_bound (RIGHT));
24
25   if (bounds[LEFT]->break_status_dir ()
26       && Paper_column::when_mom (bounds[LEFT]) == Paper_column::when_mom (bounds[RIGHT]->get_column ()))
27     return SCM_EOL;
28
29   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
30
31   Interval span_points;
32   Direction d = LEFT;
33   Drul_array<bool> broken;
34   do
35     {
36       Interval iv = bounds[d]->extent (common, X_AXIS);
37
38       span_points[d] = iv.is_empty ()
39         ? bounds[d]->relative_coordinate (common, X_AXIS)
40         : iv[-d];
41     }
42   while (flip (&d) != LEFT);
43
44   Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
45   Real th = robust_scm2double (me->get_property ("thickness"), 1) * lt;
46   Real h = robust_scm2double (me->get_property ("height"), 0.5);
47
48   // interval?
49
50   Real dash_period = robust_scm2double (me->get_property ("dash-period"), 1.0);
51   Real dash_length = robust_scm2double (me->get_property ("length"), .5);
52   Real padding = robust_scm2double (me->get_property ("padding"), 0.1);
53
54   if (dash_period < dash_length)
55     dash_period = 1.5 * dash_length;
56
57   Real l = span_points.length ();
58
59   int n = int (ceil (l / dash_period - 0.5));
60   if (n <= 0)
61     n = 1;
62
63   if (l < dash_length + 2 * padding
64       && !bounds[RIGHT]->break_status_dir ())
65     {
66       Real minimum_length = robust_scm2double (me->get_property ("minimum-length"), .3);
67       dash_length = max ((l - 2 * padding), minimum_length);
68     }
69
70   Real space_left = l - dash_length - (n - 1) * dash_period;
71
72   /*
73     If there is not enough space, the hyphen should disappear, but not
74     at the end of the line.
75   */
76   if (space_left < 0.0
77       && !bounds[RIGHT]->break_status_dir ())
78     return SCM_EOL;
79
80   space_left = max (space_left, 0.0);
81
82   Box b (Interval (0, dash_length), Interval (h, h + th));
83   Stencil dash_mol (Lookup::round_filled_box (b, 0.8 * lt));
84
85   Stencil total;
86   for (int i = 0; i < n; i++)
87     {
88       Stencil m (dash_mol);
89       m.translate_axis (span_points[LEFT] + i * dash_period + space_left / 2, X_AXIS);
90       total.add_stencil (m);
91     }
92
93   total.translate_axis (-me->relative_coordinate (common, X_AXIS), X_AXIS);
94   return total.smobbed_copy ();
95 }
96
97 MAKE_SCHEME_CALLBACK (Hyphen_spanner, set_spacing_rods, 1);
98 SCM
99 Hyphen_spanner::set_spacing_rods (SCM smob)
100 {
101   Grob *me = unsmob_grob (smob);
102
103   Rod r;
104   Spanner *sp = dynamic_cast<Spanner *> (me);
105   r.distance_
106     = robust_scm2double (me->get_property ("minimum-length"), 0);
107
108   if (r.distance_ > 0.0)
109     {
110       Real padding = robust_scm2double (me->get_property ("padding"), 0.1);
111       r.distance_ += 2*padding;
112       Direction d = LEFT;
113       do
114         {
115           r.item_drul_[d] = sp->get_bound (d);
116           if (r.item_drul_[d])
117             r.distance_ += r.item_drul_[d]->extent (r.item_drul_[d], X_AXIS)[-d];
118         }
119       while (flip (&d) != LEFT);
120
121       if (r.item_drul_[LEFT]
122           && r.item_drul_[RIGHT])
123         r.add_to_cols ();
124     }
125
126   return SCM_UNSPECIFIED;
127 }
128
129 ADD_INTERFACE (Hyphen_spanner, "lyric-hyphen-interface",
130                "A centred hyphen is a simple line between lyrics used to divide syllables",
131                "padding thickness height dash-period minimum-length length");
132