Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / bar-line.cc
bloba2d6588726bb823c03075ca65dc35b663ecffaa3
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 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"
25 #include "lookup.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)
31 SCM
32 Bar_line::calc_bar_extent (SCM smob)
34 Interval result;
35 Grob *me = unsmob_grob (smob);
36 if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
37 result = staff->extent (staff, Y_AXIS);
39 return ly_interval2scm (result);
42 Interval
43 Bar_line::bar_y_extent (Grob *me, Grob *refpoint)
45 Interval iv = robust_scm2interval (me->get_property ("bar-extent"), Interval ());
47 iv.translate (me->relative_coordinate (refpoint, Y_AXIS));
48 return iv;
51 bool
52 Bar_line::non_empty_barline (Grob *me)
54 return has_interface (me) && !me->extent (me, X_AXIS).is_empty ();
57 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
58 SCM
59 Bar_line::print (SCM smob)
61 Grob *me = unsmob_grob (smob);
63 SCM s = me->get_property ("glyph-name");
64 SCM extent = me->get_property ("bar-extent");
66 if (scm_is_string (s) && is_number_pair (extent))
68 string str = ly_scm2string (s);
69 Interval ex = ly_scm2interval (extent);
70 if (ex.length () > 0)
72 Stencil result = compound_barline (me, str, ex, false);
74 return result.smobbed_copy ();
77 return SCM_EOL;
80 Stencil
81 Bar_line::compound_barline (Grob *me, string str, Interval const &extent,
82 bool rounded)
84 Real kern = robust_scm2double (me->get_property ("kern"), 1);
85 Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
86 Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
87 Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
89 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
90 Real staff_space = Staff_symbol_referencer::staff_space (me);
92 kern *= staffline;
93 thinkern *= staffline;
94 hair *= staffline;
95 fatline *= staffline;
97 Stencil thin = simple_barline (me, hair, extent, rounded);
98 Stencil thick = simple_barline (me, fatline, extent, rounded);
99 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
101 int lines = Staff_symbol_referencer::line_count (me);
102 Real dist
103 = ((lines & 1 || lines == 0)
105 : (staff_space < 2 ? 2 : .5)) * staff_space;
106 Stencil colon (dot);
107 colon.translate_axis (dist, Y_AXIS);
108 colon.add_stencil (dot);
109 colon.translate_axis (-dist / 2, Y_AXIS);
111 Real const h = extent.length ();
112 Stencil m;
114 if (str == "||:")
115 str = "|:";
117 if (str == "|S" || str == "S|")
118 str = "S";
120 if (str == "")
122 Stencil empty = Lookup::blank (Box (Interval (0, 0), extent));
123 return empty;
125 else if (str == "|")
126 return thin;
127 else if (str == ".")
128 return thick;
129 else if (str == "|." || (h == 0 && str == ":|"))
131 m.add_at_edge (X_AXIS, LEFT, thick, 0);
132 m.add_at_edge (X_AXIS, LEFT, thin, kern);
134 else if (str == ".|" || (h == 0 && str == "|:"))
136 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
137 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
139 else if (str == ":|")
141 m.add_at_edge (X_AXIS, LEFT, thick, 0);
142 m.add_at_edge (X_AXIS, LEFT, thin, kern);
143 m.add_at_edge (X_AXIS, LEFT, colon, kern);
145 else if (str == "|:")
147 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
148 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
149 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
151 else if (str == ":|:")
153 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
154 m.add_at_edge (X_AXIS, LEFT, colon, kern);
155 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
156 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
158 else if (str == ":|.|:")
160 m.add_at_edge (X_AXIS, LEFT, thick, 0);
161 m.add_at_edge (X_AXIS, LEFT, thin, kern);
162 m.add_at_edge (X_AXIS, LEFT, colon, kern);
163 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
164 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
166 else if (str == ":|.:")
168 m.add_at_edge (X_AXIS, LEFT, thick, 0);
169 m.add_at_edge (X_AXIS, LEFT, thin, kern);
170 m.add_at_edge (X_AXIS, LEFT, colon, kern);
171 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
173 else if (str == ".|.")
175 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
176 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
178 else if (str == "|.|")
180 m.add_at_edge (X_AXIS, LEFT, thick, 0);
181 m.add_at_edge (X_AXIS, LEFT, thin, kern);
182 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
184 else if (str == "||")
187 should align to other side? this never appears
188 on the system-start?
189 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
190 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
192 m.add_at_edge (X_AXIS, LEFT, thin, thinkern);
193 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
195 else if (str.find ("S") != NPOS || str == "|._.|")
197 // Handle all varsegno stuff
198 Stencil segno;
199 segno.add_at_edge (X_AXIS, LEFT, thin, thinkern);
200 segno.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
201 segno.add_stencil (Font_interface::get_default_font (me)->find_by_name ("scripts.varsegno"));
203 if (str == "S")
204 m.add_stencil (segno);
205 else if (str == "S|:" || str == ".S|:")
207 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
208 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
209 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
210 m.add_at_edge (X_AXIS, LEFT, segno, thinkern);
212 else if (str == ":|S" || str == ":|S.")
214 m.add_at_edge (X_AXIS, LEFT, thick, 0);
215 m.add_at_edge (X_AXIS, LEFT, thin, kern);
216 m.add_at_edge (X_AXIS, LEFT, colon, kern);
217 m.add_at_edge (X_AXIS, RIGHT, segno, thinkern);
219 else if (str == ":|S|:" || str == ":|S.|:")
221 m.add_at_edge (X_AXIS, LEFT, thick, 0);
222 m.add_at_edge (X_AXIS, LEFT, thin, kern);
223 m.add_at_edge (X_AXIS, LEFT, colon, kern);
224 m.add_at_edge (X_AXIS, RIGHT, segno, thinkern);
225 m.add_at_edge (X_AXIS, RIGHT, thick, thinkern);
226 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
227 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
229 else if (str == "|._.|") // :|S|: or :|S.|: without segno and colon
231 // get the width of the segno sign
232 Real segno_width = segno.extent (X_AXIS).length ();
233 m.add_at_edge (X_AXIS, LEFT, thick, 0);
234 m.add_at_edge (X_AXIS, LEFT, thin, kern);
235 m.add_at_edge (X_AXIS, RIGHT, thick, segno_width + 2 * thinkern);
236 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
238 // end varsegno block
240 else if (str == ":")
242 if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
244 Interval staff_extent = staff->extent (staff, Y_AXIS);
247 assume staff lines are disposed equally at unit space;
248 put a dot into each space within extent (may extend staff_extent).
250 staff_extent is an interval of two integers or two half-integers;
251 in the former case dots are to be placed at half-integers,
252 in the latter at integers.
254 these integers are not exact due to staff line thickness.
256 int const pos = int (rint (staff_extent.at (UP) * 2));
257 Real const correction = pos & 1 ? 0.0 : 0.5;
259 for (int i = int (rint (extent.at (DOWN) + (0.5 - correction))),
260 e = int (rint (extent.at (UP) + (0.5 - correction)));
261 i < e;
262 ++i)
264 Stencil d (dot);
266 d.translate_axis (i + correction, Y_AXIS);
267 m.add_stencil (d);
271 else if (str == "dashed")
272 m = dashed_bar_line (me, extent, hair);
273 else if (str == "'")
274 m = tick_bar_line (me, extent.at (UP), rounded);
276 return m;
279 Stencil
280 Bar_line::simple_barline (Grob *me,
281 Real w,
282 Interval const &extent,
283 bool rounded)
285 Real blot
286 = rounded
287 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
288 : 0.0;
290 return Lookup::round_filled_box (Box (Interval (0, w), extent), blot);
293 Stencil
294 Bar_line::tick_bar_line (Grob *me, Real h, bool rounded)
296 Real th = Staff_symbol_referencer::staff_space (me) / 2;
297 Real line_thick = Staff_symbol_referencer::line_thickness (me);
299 Real blot
300 = rounded
301 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
302 : 0.0;
304 return Lookup::round_filled_box (Box (Interval (0, line_thick),
305 Interval (h - th, h + th)), blot);
308 Stencil
309 Bar_line::dashed_bar_line (Grob *me, Interval const &extent, Real thick)
311 Real dash_size
312 = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
314 this is a tad complex for what we want to achieve, but with a
315 simple line, the round blotting interferes with staff line
316 connections.
318 Real ss = Staff_symbol_referencer::staff_space (me);
319 Real const h = extent.length ();
320 int dashes = int (rint (h / ss));
323 there are two concerns:
324 1. one dash plus one space should be one staff space
325 2. the line should begin and end with half a dash
327 both can be satisfied, if the extent is (roughly) an integer
328 multiple of staff space.
330 if (fabs (h / ss - dashes) < 0.1)
332 Real blot
333 = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
335 Real const half_dash = dash_size / 2;
336 Stencil bar;
338 for (int i = 0; i <= dashes; ++i)
340 Real top_y = extent.at (DOWN)
341 + (i == dashes ? h : (i + half_dash) * ss);
342 Real bot_y = extent.at (DOWN) + (i ? (i - half_dash) * ss : 0.0);
344 bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
345 Interval (bot_y, top_y)),
346 blot));
348 return bar;
350 else
353 We have to scale the dashing so it starts and ends with half a
354 dash exactly.
356 Real total_dash_size = h / dashes;
357 Real factor = (dash_size - thick) / ss;
359 SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
360 scm_from_double (thick),
361 scm_from_double (factor * total_dash_size),
362 scm_from_double ((1 - factor) * total_dash_size),
363 scm_from_double (0),
364 scm_from_double (h),
365 scm_from_double (factor * total_dash_size * 0.5),
366 SCM_UNDEFINED);
368 Box box;
369 box.add_point (Offset (0, 0));
370 box.add_point (Offset (0, h));
372 Stencil s (box, at);
373 s.translate (Offset (thick / 2, extent.at (DOWN)));
374 return s;
376 return Stencil ();
379 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
381 Bar_line::calc_anchor (SCM smob)
383 Grob *me = unsmob_grob (smob);
384 Real kern = robust_scm2double (me->get_property ("kern"), 1);
385 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
386 string str = robust_scm2string (me->get_property ("glyph-name"), "");
388 /* we put the anchor in the center of the barline, unless we are
389 a repeat bar, in which case we put the anchor in the center of
390 the barline without the dots. */
391 Interval ext = me->extent (me, X_AXIS);
392 if (ext.is_empty ())
393 return scm_from_double (0);
395 Real anchor = ext.center ();
397 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
398 Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
399 if (str == "|:")
400 anchor -= dot_width / 2;
401 else if (str == ":|")
402 anchor += dot_width / 2;
404 return scm_from_double (anchor);
407 ADD_INTERFACE (Bar_line,
408 "Bar line.\n"
409 "\n"
410 "Print a special bar symbol. It replaces the regular bar"
411 " symbol with a special symbol. The argument @var{bartype}"
412 " is a string which specifies the kind of bar line to print."
413 " Options are @code{|}, @code{:|}, @code{|:}, @code{:|:}, @code{:|.|:},"
414 " @code{:|.:}, @code{.}, @code{||}, @code{|.}, @code{.|}, @code{.|.},"
415 " @code{|.|}, @code{:}, @code{dashed}, @code{'} and @code{S}.\n"
416 "\n"
417 "These produce, respectively, a normal bar line, a right repeat, a left repeat,"
418 " a thick double repeat, a thin-thick-thin double repeat,"
419 " a thin-thick double repeat, a thick bar, a double bar, a start bar,"
420 " an end bar, a thick double bar, a thin-thick-thin bar,"
421 " a dotted bar, a dashed bar, a tick as bar line and a segno bar.\n"
422 "\n"
423 "In addition, there is an option"
424 " @code{||:} which is equivalent to @code{|:} except at line"
425 " breaks, where it produces a double bar (@code{||}) at the"
426 " end of the line and a repeat sign (@code{|:}) at the"
427 " beginning of the new line.\n"
428 "\n"
429 "For segno, @code{S} produces a segno sign except at line breaks,"
430 " where it produces a double bar (@code{||}) at the"
431 " end of the line and a segno sign at the beginning of the new line."
432 " @code{|S} is equivalent to @code{S} but produces a simple bar line"
433 " (@code{|}) instead of a double bar line (@code{||}) at line breaks."
434 " @code{S|} produces the segno sign at line breaks and starts the following"
435 " line without special bar lines.\n"
436 "\n"
437 "@code{S|:} and @code{:|S} are used for repeat/segno combinations that are"
438 " separated at line breaks. Alternatively, @code{.S|:} and @code{:|S.}"
439 " may be used which combine repeat signs and segno at the same line in"
440 " case of a line break. @code{:|S|:} is a combination of a left repeat"
441 " (@code{:|}), a segno (@code{S}) and a right repeat @code{|:} which"
442 " splits before the segno at line breaks; @code{:|S.|:} splits after"
443 " the segno sign.\n"
444 "\n"
445 "If @var{bartype} is set to @code{empty} then nothing is"
446 " printed, but a line break is allowed at that spot.\n"
447 "\n"
448 "@code{gap} is used for the gaps in dashed bar lines.",
450 /* properties */
451 "allow-span-bar "
452 "gap "
453 "kern "
454 "thin-kern "
455 "hair-thickness "
456 "thick-thickness "
457 "glyph "
458 "glyph-name "
459 "bar-extent "