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