2 bar-line.cc -- implement Bar_line
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "all-font-metrics.hh"
12 #include "font-interface.hh"
14 #include "output-def.hh"
15 #include "paper-column.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "line-interface.hh"
19 /* Get the extent of just the line part of the bar (ie. excluding any
22 Bar_line::bar_y_extent (Grob *me, Grob *refpoint)
24 SCM size = me->get_property ("bar-size");
25 if (!scm_is_number (size))
28 Real h = scm_to_double (size);
29 Interval iv (-h/2, h/2);
31 iv.translate (me->relative_coordinate (refpoint, Y_AXIS));
35 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
37 Bar_line::print (SCM smob)
39 Grob *me = unsmob_grob (smob);
41 SCM s = me->get_property ("glyph-name");
42 SCM barsize = me->get_property ("bar-size");
44 if (scm_is_string (s) && scm_is_number (barsize))
46 string str = ly_scm2string (s);
47 Real sz = robust_scm2double (barsize, 0);
51 return compound_barline (me, str, sz, false).smobbed_copy ();
57 Bar_line::compound_barline (Grob *me, string str, Real h,
60 Real kern = robust_scm2double (me->get_property ("kern"), 1);
61 Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
62 Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
63 Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
65 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
66 Real staff_space = Staff_symbol_referencer::staff_space (me);
69 thinkern *= staffline;
73 Stencil thin = simple_barline (me, hair, h, rounded);
74 Stencil thick = simple_barline (me, fatline, h, rounded);
75 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
77 int lines = Staff_symbol_referencer::line_count (me);
79 = ((lines & 1 || lines == 0)
81 : (staff_space < 2 ? 2 : .5)) * staff_space;
83 colon.translate_axis (dist, Y_AXIS);
84 colon.add_stencil (dot);
85 colon.translate_axis (-dist / 2, Y_AXIS);
92 return Lookup::blank (Box (Interval (0, 0), Interval (-h / 2, h / 2)));
95 else if (str == "|." || (h == 0 && str == ":|"))
97 m.add_at_edge (X_AXIS, LEFT, thick, 0);
98 m.add_at_edge (X_AXIS, LEFT, thin, kern);
100 else if (str == ".|" || (h == 0 && str == "|:"))
102 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
103 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
105 else if (str == ":|")
107 m.add_at_edge (X_AXIS, LEFT, thick, 0);
108 m.add_at_edge (X_AXIS, LEFT, thin, kern);
109 m.add_at_edge (X_AXIS, LEFT, colon, kern);
111 else if (str == "|:")
113 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
114 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
115 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
117 else if (str == ":|:")
119 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
120 m.add_at_edge (X_AXIS, LEFT, colon, kern);
121 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
122 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
124 else if (str == ".|.")
126 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
127 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
129 else if (str == "||")
132 should align to other side? this never appears
135 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
136 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
140 int c = (Staff_symbol_referencer::line_count (me));
142 for (int i = 0; i < c - 1; i++)
144 Real y = (- (c - 1.0) / 2 + 0.5 + i) * staff_space;
147 d.translate_axis (y, Y_AXIS);
151 else if (str == "dashed")
153 m = dashed_bar_line (me, h, hair);
163 Bar_line::simple_barline (Grob *me,
170 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
173 return Lookup::round_filled_box (Box (Interval (0, w),
174 Interval (-h / 2, h / 2)), blot);
177 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_size, 1);
179 Bar_line::calc_bar_size (SCM smob)
181 Grob *me = unsmob_grob (smob);
182 if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
184 Interval staff_y = staff->extent (staff, Y_AXIS);
185 return scm_from_double (staff_y.is_empty () ? 0.0 : staff_y.length ());
187 return scm_from_int (0);
192 Bar_line::dashed_bar_line (Grob *me, Real h, Real thick)
195 = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
197 this is a tad complex for what we want to achieve, but with a
198 simple line, the round blotting interferes with staff line
201 Real ss = Staff_symbol_referencer::staff_space (me);
202 int count = Staff_symbol_referencer::line_count (me);
203 Real line_thick = Staff_symbol_referencer::line_thickness (me);
205 if (fabs (line_thick + (count -1) * ss - h) < 0.1) // ugh.
208 me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
210 Real half_space = ss/2;
213 for (int i = (count-1); i >= -(count-1); i -= 2)
215 Real top_y = min ((i + dash_size) * half_space,
216 (count-1) * half_space + line_thick / 2);
217 Real bot_y = max ((i - dash_size) * half_space,
218 -(count-1) * half_space - line_thick/2);
220 bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
221 Interval (bot_y, top_y)),
229 We have to scale the dashing so it starts and ends with half a
232 int dashes = int (rint (h / ss));
233 Real total_dash_size = h / dashes;
234 Real factor = (dash_size - thick) / ss;
236 SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
237 scm_from_double (thick),
238 scm_from_double (factor * total_dash_size),
239 scm_from_double ((1-factor) * total_dash_size),
242 scm_from_double (factor * total_dash_size * 0.5),
246 box.add_point (Offset (0, 0));
247 box.add_point (Offset (0, h));
250 s.translate (Offset (thick/2, -h/2));
256 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
258 Bar_line::calc_anchor (SCM smob)
260 Grob *me = unsmob_grob (smob);
261 Real kern = robust_scm2double (me->get_property ("kern"), 1);
262 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
263 string str = robust_scm2string (me->get_property ("glyph-name"), "");
265 /* we put the anchor in the center of the barline, unless we are
266 a repeat bar, in which case we put the anchor in the center of
267 the barline without the dots. */
268 Interval ext = me->extent (me, X_AXIS);
270 return scm_from_double (0);
272 Real anchor = ext.center ();
274 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
275 Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
277 anchor -= dot_width / 2;
278 else if (str == ":|")
279 anchor += dot_width / 2;
281 return scm_from_double (anchor);
284 ADD_INTERFACE (Bar_line,
288 "Print a special bar symbol. It replaces the \n"
289 "regular bar symbol with a special\n"
290 "symbol. The argument @var{bartype} is a string which specifies the\n"
291 "kind of bar to print. Options are @code{:|}, \n"
292 "@code{|:}, @code{:|:},\n"
293 "@code{||}, @code{|.},\n"
294 "@code{.|}, and @code{.|.}. \n"
296 "These produce, respectively, a right repeat, a left repeat, a double\n"
297 "repeat, a double bar, a start bar, an end bar, and a thick double bar.\n"
298 "In addition, there is an option @code{||:} which is equivalent to\n"
299 "@code{|:} except at line breaks, where it produces a double bar (@code{||})\n"
300 "at the end of the line and a repeat sign (@code{|:}) at the beginning\n"
302 "If @var{bartype} is set to @code{empty} then nothing is printed,\n"
303 "but a line break is allowed at that spot.\n"
305 "@code{gap} is used for the gaps in dashed barlines."