2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "bar-line.hh"
22 #include "all-font-metrics.hh"
23 #include "font-interface.hh"
24 #include "line-interface.hh"
26 #include "output-def.hh"
27 #include "paper-column.hh"
28 #include "staff-symbol-referencer.hh"
30 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_extent, 1)
32 Bar_line::calc_bar_extent (SCM smob)
35 Grob *me = unsmob_grob (smob);
36 if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
38 result = staff->extent (staff, Y_AXIS);
40 /* Due to rounding problems, bar lines extending to the outermost edges
41 of the staff lines appear wrongly in on-screen display
42 (and, to a lesser extent, in print) - they stick out a pixel.
43 The solution is to extend bar lines only to the middle
44 of the staff line - unless they have different colors,
45 when it would be undesirable.
47 SCM bar_line_color = me->get_property ("color");
48 SCM staff_color = staff->get_property ("color");
49 Real radius = Staff_symbol_referencer::staff_radius (me);
50 if (bar_line_color == staff_color && radius)
51 result *= (1 - 0.5 * (Staff_symbol_referencer::line_thickness (me) / radius));
53 return ly_interval2scm (result);
57 Bar_line::bar_y_extent (Grob *me, Grob *refpoint)
59 Interval iv = robust_scm2interval (me->get_property ("bar-extent"), Interval ());
61 iv.translate (me->relative_coordinate (refpoint, Y_AXIS));
66 Bar_line::non_empty_barline (Grob *me)
68 return has_interface (me) && !me->extent (me, X_AXIS).is_empty ();
71 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
73 Bar_line::print (SCM smob)
75 Grob *me = unsmob_grob (smob);
77 SCM s = me->get_property ("glyph-name");
78 SCM extent = me->get_property ("bar-extent");
80 if (scm_is_string (s) && is_number_pair (extent))
82 string str = ly_scm2string (s);
83 Interval ex = ly_scm2interval (extent);
86 Stencil result = compound_barline (me, str, ex, false);
88 return result.smobbed_copy ();
95 Bar_line::compound_barline (Grob *me, string str, Interval const &extent,
98 Real kern = robust_scm2double (me->get_property ("kern"), 1);
99 Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
100 Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
101 Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
103 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
104 Real staff_space = Staff_symbol_referencer::staff_space (me);
107 thinkern *= staffline;
109 fatline *= staffline;
111 Stencil thin = simple_barline (me, hair, extent, rounded);
112 Stencil thick = simple_barline (me, fatline, extent, rounded);
113 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
115 int lines = Staff_symbol_referencer::line_count (me);
117 = ((lines & 1 || lines == 0)
119 : (staff_space < 2 ? 2 : .5)) * staff_space;
121 colon.translate_axis (dist, Y_AXIS);
122 colon.add_stencil (dot);
123 colon.translate_axis (-dist / 2, Y_AXIS);
125 Real const h = extent.length ();
131 if (str == "|S" || str == "S|")
136 Stencil empty = Lookup::blank (Box (Interval (0, 0), extent));
143 else if (str == "|." || (h == 0 && str == ":|"))
145 m.add_at_edge (X_AXIS, LEFT, thick, 0);
146 m.add_at_edge (X_AXIS, LEFT, thin, kern);
148 else if (str == ".|" || (h == 0 && str == "|:"))
150 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
151 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
153 else if (str == ":|")
155 m.add_at_edge (X_AXIS, LEFT, thick, 0);
156 m.add_at_edge (X_AXIS, LEFT, thin, kern);
157 m.add_at_edge (X_AXIS, LEFT, colon, kern);
159 else if (str == "|:")
161 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
162 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
163 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
165 else if (str == ":|:")
167 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
168 m.add_at_edge (X_AXIS, LEFT, colon, kern);
169 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
170 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
172 else if (str == ":|.|:")
174 m.add_at_edge (X_AXIS, LEFT, thick, 0);
175 m.add_at_edge (X_AXIS, LEFT, thin, kern);
176 m.add_at_edge (X_AXIS, LEFT, colon, kern);
177 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
178 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
180 else if (str == ":|.:")
182 m.add_at_edge (X_AXIS, LEFT, thick, 0);
183 m.add_at_edge (X_AXIS, LEFT, thin, kern);
184 m.add_at_edge (X_AXIS, LEFT, colon, kern);
185 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
187 else if (str == ".|.")
189 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
190 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
192 else if (str == "|.|")
194 m.add_at_edge (X_AXIS, LEFT, thick, 0);
195 m.add_at_edge (X_AXIS, LEFT, thin, kern);
196 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
198 else if (str == "||")
201 should align to other side? this never appears
203 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
204 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
206 m.add_at_edge (X_AXIS, LEFT, thin, thinkern);
207 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
209 else if (str.find ("S") != NPOS || str == "|._.|")
211 // Handle all varsegno stuff
213 segno.add_at_edge (X_AXIS, LEFT, thin, thinkern);
214 segno.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
215 segno.add_stencil (Font_interface::get_default_font (me)->find_by_name ("scripts.varsegno"));
218 m.add_stencil (segno);
219 else if (str == "S|:" || str == ".S|:")
221 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
222 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
223 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
224 m.add_at_edge (X_AXIS, LEFT, segno, thinkern);
226 else if (str == ":|S" || str == ":|S.")
228 m.add_at_edge (X_AXIS, LEFT, thick, 0);
229 m.add_at_edge (X_AXIS, LEFT, thin, kern);
230 m.add_at_edge (X_AXIS, LEFT, colon, kern);
231 m.add_at_edge (X_AXIS, RIGHT, segno, thinkern);
233 else if (str == ":|S|:" || str == ":|S.|:")
235 m.add_at_edge (X_AXIS, LEFT, thick, 0);
236 m.add_at_edge (X_AXIS, LEFT, thin, kern);
237 m.add_at_edge (X_AXIS, LEFT, colon, kern);
238 m.add_at_edge (X_AXIS, RIGHT, segno, thinkern);
239 m.add_at_edge (X_AXIS, RIGHT, thick, thinkern);
240 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
241 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
243 else if (str == "|._.|") // :|S|: or :|S.|: without segno and colon
245 // get the width of the segno sign
246 Real segno_width = segno.extent (X_AXIS).length ();
247 m.add_at_edge (X_AXIS, LEFT, thick, 0);
248 m.add_at_edge (X_AXIS, LEFT, thin, kern);
249 m.add_at_edge (X_AXIS, RIGHT, thick, segno_width + 2 * thinkern);
250 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
252 // end varsegno block
256 if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
258 Interval staff_extent = staff->extent (staff, Y_AXIS);
261 assume staff lines are disposed equally at unit space;
262 put a dot into each space within extent (may extend staff_extent).
264 staff_extent is an interval of two integers or two half-integers;
265 in the former case dots are to be placed at half-integers,
266 in the latter at integers.
268 these integers are not exact due to staff line thickness.
270 int const pos = int (rint (staff_extent.at (UP) * 2));
271 Real const correction = pos & 1 ? 0.0 : 0.5;
273 for (int i = int (rint (extent.at (DOWN) + (0.5 - correction))),
274 e = int (rint (extent.at (UP) + (0.5 - correction)));
280 d.translate_axis (i + correction, Y_AXIS);
285 else if (str == "dashed")
286 m = dashed_bar_line (me, extent, hair);
288 m = tick_bar_line (me, extent.at (UP), rounded);
289 else if (str == "kievan")
291 me->set_property ("layer", scm_from_int (1));
292 m.add_stencil (Font_interface::get_default_font (me)->find_by_name ("scripts.barline.kievan"));
293 m = *unsmob_stencil (scm_call_1 (ly_lily_module_constant ("stencil-whiteout"), m.smobbed_copy ()));
299 Bar_line::simple_barline (Grob *me,
301 Interval const &extent,
306 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
309 return Lookup::round_filled_box (Box (Interval (0, w), extent), blot);
313 Bar_line::tick_bar_line (Grob *me, Real h, bool rounded)
315 Real th = Staff_symbol_referencer::staff_space (me) / 2;
316 Real line_thick = Staff_symbol_referencer::line_thickness (me);
320 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
323 return Lookup::round_filled_box (Box (Interval (0, line_thick),
324 Interval (h - th, h + th)), blot);
328 Bar_line::dashed_bar_line (Grob *me, Interval const &extent, Real thick)
331 = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
333 this is a tad complex for what we want to achieve, but with a
334 simple line, the round blotting interferes with staff line
337 Real ss = Staff_symbol_referencer::staff_space (me);
338 Real const h = extent.length ();
339 int dashes = int (rint (h / ss));
342 there are two concerns:
343 1. one dash plus one space should be one staff space
344 2. the line should begin and end with half a dash
346 both can be satisfied, if the extent is (roughly) an integer
347 multiple of staff space.
349 if (fabs (h / ss - dashes) < 0.1)
352 = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
354 Real const half_dash = dash_size / 2;
357 for (int i = 0; i <= dashes; ++i)
359 Real top_y = extent.at (DOWN)
360 + (i == dashes ? h : (i + half_dash) * ss);
361 Real bot_y = extent.at (DOWN) + (i ? (i - half_dash) * ss : 0.0);
363 bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
364 Interval (bot_y, top_y)),
372 We have to scale the dashing so it starts and ends with half a
375 Real total_dash_size = h / dashes;
376 Real factor = (dash_size - thick) / ss;
378 SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
379 scm_from_double (thick),
380 scm_from_double (factor * total_dash_size),
381 scm_from_double ((1 - factor) * total_dash_size),
384 scm_from_double (factor * total_dash_size * 0.5),
388 box.add_point (Offset (0, 0));
389 box.add_point (Offset (0, h));
392 s.translate (Offset (thick / 2, extent.at (DOWN)));
398 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
400 Bar_line::calc_anchor (SCM smob)
402 Grob *me = unsmob_grob (smob);
403 Real kern = robust_scm2double (me->get_property ("kern"), 1);
404 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
405 string str = robust_scm2string (me->get_property ("glyph-name"), "");
407 /* we put the anchor in the center of the barline, unless we are
408 a repeat bar, in which case we put the anchor in the center of
409 the barline without the dots. */
410 Interval ext = me->extent (me, X_AXIS);
412 return scm_from_double (0);
414 Real anchor = ext.center ();
416 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
417 Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
419 anchor -= dot_width / 2;
420 else if (str == ":|")
421 anchor += dot_width / 2;
423 return scm_from_double (anchor);
426 ADD_INTERFACE (Bar_line,
429 "Print a special bar symbol. It replaces the regular bar"
430 " symbol with a special symbol. The argument @var{bartype}"
431 " is a string which specifies the kind of bar line to print."
432 " Options are @code{|}, @code{:|}, @code{|:}, @code{:|:}, @code{:|.|:},"
433 " @code{:|.:}, @code{.}, @code{||}, @code{|.}, @code{.|}, @code{.|.},"
434 " @code{|.|}, @code{:}, @code{dashed}, @code{'} and @code{S}.\n"
436 "These produce, respectively, a normal bar line, a right repeat, a left repeat,"
437 " a thick double repeat, a thin-thick-thin double repeat,"
438 " a thin-thick double repeat, a thick bar, a double bar, a start bar,"
439 " an end bar, a thick double bar, a thin-thick-thin bar,"
440 " a dotted bar, a dashed bar, a tick as bar line and a segno bar.\n"
442 "In addition, there is an option"
443 " @code{||:} which is equivalent to @code{|:} except at line"
444 " breaks, where it produces a double bar (@code{||}) at the"
445 " end of the line and a repeat sign (@code{|:}) at the"
446 " beginning of the new line.\n"
448 "For segno, @code{S} produces a segno sign except at line breaks,"
449 " where it produces a double bar (@code{||}) at the"
450 " end of the line and a segno sign at the beginning of the new line."
451 " @code{|S} is equivalent to @code{S} but produces a simple bar line"
452 " (@code{|}) instead of a double bar line (@code{||}) at line breaks."
453 " @code{S|} produces the segno sign at line breaks and starts the following"
454 " line without special bar lines.\n"
456 "@code{S|:} and @code{:|S} are used for repeat/segno combinations that are"
457 " separated at line breaks. Alternatively, @code{.S|:} and @code{:|S.}"
458 " may be used which combine repeat signs and segno at the same line in"
459 " case of a line break. @code{:|S|:} is a combination of a left repeat"
460 " (@code{:|}), a segno (@code{S}) and a right repeat @code{|:} which"
461 " splits before the segno at line breaks; @code{:|S.|:} splits after"
464 "If @var{bartype} is set to @code{empty} then nothing is"
465 " printed, but a line break is allowed at that spot.\n"
467 "@code{gap} is used for the gaps in dashed bar lines.",