]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-hyphen.cc
* The grand 2005-2006 replace.
[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--2006 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 /*
18   TODO: should extract hyphen dimensions or hyphen glyph from the
19   font.
20  */
21
22 MAKE_SCHEME_CALLBACK (Hyphen_spanner, print, 1);
23 SCM
24 Hyphen_spanner::print (SCM smob)
25 {
26   Spanner *me = unsmob_spanner (smob);
27   Drul_array<Item *> bounds (me->get_bound (LEFT),
28                              me->get_bound (RIGHT));
29
30   if (bounds[LEFT]->break_status_dir ()
31       && (Paper_column::when_mom (bounds[LEFT])
32           == Paper_column::when_mom (bounds[RIGHT]->get_column ())))
33     return SCM_EOL;
34
35   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
36
37   Interval span_points;
38   Direction d = LEFT;
39   Drul_array<bool> broken;
40   do
41     {
42       Interval iv = bounds[d]->extent (common, X_AXIS);
43
44       span_points[d] = iv.is_empty ()
45         ? bounds[d]->relative_coordinate (common, X_AXIS)
46         : iv[-d];
47     }
48   while (flip (&d) != LEFT);
49
50   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
51   Real th = robust_scm2double (me->get_property ("thickness"), 1) * lt;
52   Real font_size_step = robust_scm2double (me->get_property ("font-size"), 0.0);
53   Real h = robust_scm2double (me->get_property ("height"), 0.5)
54     * pow (2.0, font_size_step / 6.0);
55
56   // interval?
57
58   Real dash_period = robust_scm2double (me->get_property ("dash-period"), 1.0);
59   Real dash_length = robust_scm2double (me->get_property ("length"), .5);
60   Real padding = robust_scm2double (me->get_property ("padding"), 0.1);
61
62   if (dash_period < dash_length)
63     dash_period = 1.5 * dash_length;
64
65   Real l = span_points.length ();
66
67   int n = int (ceil (l / dash_period - 0.5));
68   if (n <= 0)
69     n = 1;
70
71   if (l < dash_length + 2 * padding
72       && !bounds[RIGHT]->break_status_dir ())
73     {
74       Real minimum_length = robust_scm2double (me->get_property ("minimum-length"), .3);
75       dash_length = max ((l - 2 * padding), minimum_length);
76     }
77
78   Real space_left = l - dash_length - (n - 1) * dash_period;
79
80   /*
81     If there is not enough space, the hyphen should disappear, but not
82     at the end of the line.
83   */
84   if (space_left < 0.0
85       && !bounds[RIGHT]->break_status_dir ())
86     return SCM_EOL;
87
88   space_left = max (space_left, 0.0);
89
90   Box b (Interval (0, dash_length), Interval (h, h + th));
91   Stencil dash_mol (Lookup::round_filled_box (b, 0.8 * lt));
92
93   Stencil total;
94   for (int i = 0; i < n; i++)
95     {
96       Stencil m (dash_mol);
97       m.translate_axis (span_points[LEFT] + i * dash_period + space_left / 2, X_AXIS);
98       total.add_stencil (m);
99     }
100
101   total.translate_axis (-me->relative_coordinate (common, X_AXIS), X_AXIS);
102   return total.smobbed_copy ();
103 }
104
105 MAKE_SCHEME_CALLBACK (Hyphen_spanner, set_spacing_rods, 1);
106 SCM
107 Hyphen_spanner::set_spacing_rods (SCM smob)
108 {
109   Grob *me = unsmob_grob (smob);
110
111   Rod r;
112   Spanner *sp = dynamic_cast<Spanner *> (me);
113
114   r.distance_ = robust_scm2double (me->get_property ("minimum-distance"), 0);
115   Direction d = LEFT;
116   do
117     {
118       r.item_drul_[d] = sp->get_bound (d);
119       if (r.item_drul_[d])
120         r.distance_ += r.item_drul_[d]->extent (r.item_drul_[d], X_AXIS)[-d];
121     }
122   while (flip (&d) != LEFT);
123
124   if (r.item_drul_[LEFT]
125       && r.item_drul_[RIGHT])
126     r.add_to_cols ();
127
128   return SCM_UNSPECIFIED;
129 }
130
131 ADD_INTERFACE (Hyphen_spanner, "lyric-hyphen-interface",
132                "A centered hyphen is a simple line between lyrics used to divide syllables",
133
134                /* props */
135                "padding "
136                "thickness "
137                "height "
138                "dash-period "
139                "minimum-length "
140                "minimum-distance "
141                "length");
142