]> git.donarmstrong.com Git - lilypond.git/blob - lily/lyric-hyphen.cc
Web-ja: update introduction
[lilypond.git] / lily / lyric-hyphen.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2003--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "lyric-hyphen.hh"
21
22 #include "axis-group-interface.hh"
23 #include "lookup.hh"
24 #include "output-def.hh"
25 #include "paper-column.hh"
26 #include "moment.hh"
27 #include "spanner.hh"
28
29 /*
30   TODO: should extract hyphen dimensions or hyphen glyph from the
31   font.
32  */
33
34 MAKE_SCHEME_CALLBACK (Lyric_hyphen, print, 1);
35 SCM
36 Lyric_hyphen::print (SCM smob)
37 {
38   Spanner *me = unsmob<Spanner> (smob);
39   Drul_array<Item *> bounds (me->get_bound (LEFT),
40                              me->get_bound (RIGHT));
41
42   if (bounds[LEFT]->break_status_dir ()
43       && (Paper_column::when_mom (bounds[LEFT])
44           == Paper_column::when_mom (bounds[RIGHT]->get_column ())))
45     return SCM_EOL;
46
47   Grob *common = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
48
49   Interval span_points;
50   for (LEFT_and_RIGHT (d))
51     {
52       Interval iv = Axis_group_interface::generic_bound_extent (bounds[d], common, X_AXIS);
53
54       span_points[d] = iv.is_empty ()
55                        ? bounds[d]->relative_coordinate (common, X_AXIS)
56                        : iv[-d];
57     }
58
59   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
60   Real th = robust_scm2double (me->get_property ("thickness"), 1) * lt;
61   Real font_size_step = robust_scm2double (me->get_property ("font-size"), 0.0);
62   Real h = robust_scm2double (me->get_property ("height"), 0.5)
63            * pow (2.0, font_size_step / 6.0);
64
65   // interval?
66
67   Real dash_period = robust_scm2double (me->get_property ("dash-period"), 1.0);
68   Real dash_length = robust_scm2double (me->get_property ("length"), .5);
69   Real padding = robust_scm2double (me->get_property ("padding"), 0.1);
70   Real whiteout = robust_scm2double (me->get_property ("whiteout"), -1);
71
72   if (dash_period < dash_length)
73     dash_period = 1.5 * dash_length;
74
75   Real l = span_points.length ();
76
77   int n = int (ceil (l / dash_period - 0.5));
78   if (n <= 0)
79     n = 1;
80
81   if (l < dash_length + 2 * padding
82       && !bounds[RIGHT]->break_status_dir ())
83     {
84       Real minimum_length = robust_scm2double (me->get_property ("minimum-length"), .3);
85       dash_length = max ((l - 2 * padding), minimum_length);
86     }
87
88   Real space_left = l - dash_length - (n - 1) * dash_period;
89
90   /*
91     If there is not enough space, the hyphen should disappear, but not
92     at the end of the line.
93   */
94   if (space_left < 0.0
95       && !bounds[RIGHT]->break_status_dir ())
96     return SCM_EOL;
97
98   space_left = max (space_left, 0.0);
99
100   Box b (Interval (0, dash_length), Interval (h, h + th));
101   Stencil dash_mol (Lookup::round_filled_box (b, 0.8 * lt));
102
103   Stencil total;
104   for (int i = 0; i < n; i++)
105     {
106       Stencil m (dash_mol);
107       m = m.in_color (0.0, 0.0, 0.0);
108       m.translate_axis (span_points[LEFT] + i * dash_period +
109                         space_left / 2, X_AXIS);
110       total.add_stencil (m);
111       if (whiteout > 0.0 )
112         {
113           Box c (Interval (0, dash_length + 2 * whiteout * lt),
114                  Interval (h - whiteout * lt, h + th + whiteout * lt));
115           Stencil w (Lookup::round_filled_box (c, 0.8 * lt));
116           w = w.in_color (1.0, 1.0, 1.0);
117           w.translate_axis (span_points[LEFT] + i * dash_period +
118                             space_left / 2 - whiteout * lt, X_AXIS);
119           total.add_stencil (w);
120         }
121     }
122
123   total.translate_axis (-me->relative_coordinate (common, X_AXIS), X_AXIS);
124   return total.smobbed_copy ();
125 }
126
127 MAKE_SCHEME_CALLBACK (Lyric_hyphen, set_spacing_rods, 1);
128 SCM
129 Lyric_hyphen::set_spacing_rods (SCM smob)
130 {
131   Grob *me = unsmob<Grob> (smob);
132
133   Rod r;
134   Spanner *sp = dynamic_cast<Spanner *> (me);
135
136   r.distance_ = robust_scm2double (me->get_property ("minimum-distance"), 0);
137   for (LEFT_and_RIGHT (d))
138     {
139       r.item_drul_[d] = sp->get_bound (d);
140       if (r.item_drul_[d])
141         r.distance_ += -d * r.item_drul_[d]->extent (r.item_drul_[d], X_AXIS)[-d];
142     }
143
144   if (r.item_drul_[LEFT]
145       && r.item_drul_[RIGHT])
146     r.add_to_cols ();
147
148   return SCM_UNSPECIFIED;
149 }
150
151 ADD_INTERFACE (Lyric_hyphen,
152                "A centered hyphen is simply a line between lyrics used to"
153                " divide syllables.",
154
155                /* properties */
156                "dash-period "
157                "height "
158                "length "
159                "minimum-distance "
160                "minimum-length "
161                "padding "
162                "thickness "
163               );