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 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_extent, 1)
21 Bar_line::calc_bar_extent (SCM smob)
23 Grob *me = unsmob_grob (smob);
25 SCM size = me->get_property ("bar-size");
27 if (!scm_is_number (size))
28 return ly_interval2scm (Interval ());
30 Real h = scm_to_double (size);
31 return ly_interval2scm (Interval (-h/2, h/2));
35 Bar_line::bar_y_extent (Grob *me, Grob *refpoint)
37 Interval iv = robust_scm2interval (me->get_property ("bar-extent"), Interval ());
39 iv.translate (me->relative_coordinate (refpoint, Y_AXIS));
43 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
45 Bar_line::print (SCM smob)
47 Grob *me = unsmob_grob (smob);
49 SCM s = me->get_property ("glyph-name");
50 SCM barsize = me->get_property ("bar-size");
52 if (scm_is_string (s) && scm_is_number (barsize))
54 string str = ly_scm2string (s);
55 Real sz = robust_scm2double (barsize, 0);
59 return compound_barline (me, str, sz, false).smobbed_copy ();
65 Bar_line::compound_barline (Grob *me, string str, Real h,
68 Real kern = robust_scm2double (me->get_property ("kern"), 1);
69 Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
70 Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
71 Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
73 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
74 Real staff_space = Staff_symbol_referencer::staff_space (me);
77 thinkern *= staffline;
81 Stencil thin = simple_barline (me, hair, h, rounded);
82 Stencil thick = simple_barline (me, fatline, h, rounded);
83 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
85 int lines = Staff_symbol_referencer::line_count (me);
87 = ((lines & 1 || lines == 0)
89 : (staff_space < 2 ? 2 : .5)) * staff_space;
91 colon.translate_axis (dist, Y_AXIS);
92 colon.add_stencil (dot);
93 colon.translate_axis (-dist / 2, Y_AXIS);
100 return Lookup::blank (Box (Interval (0, 0), Interval (-h / 2, h / 2)));
103 else if (str == "|." || (h == 0 && str == ":|"))
105 m.add_at_edge (X_AXIS, LEFT, thick, 0);
106 m.add_at_edge (X_AXIS, LEFT, thin, kern);
108 else if (str == ".|" || (h == 0 && str == "|:"))
110 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
111 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
113 else if (str == ":|")
115 m.add_at_edge (X_AXIS, LEFT, thick, 0);
116 m.add_at_edge (X_AXIS, LEFT, thin, kern);
117 m.add_at_edge (X_AXIS, LEFT, colon, kern);
119 else if (str == "|:")
121 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
122 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
123 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
125 else if (str == ":|:")
127 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
128 m.add_at_edge (X_AXIS, LEFT, colon, kern);
129 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
130 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
132 else if (str == ".|.")
134 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
135 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
137 else if (str == "||")
140 should align to other side? this never appears
143 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
144 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
148 int c = (Staff_symbol_referencer::line_count (me));
150 for (int i = 0; i < c - 1; i++)
152 Real y = (- (c - 1.0) / 2 + 0.5 + i) * staff_space;
155 d.translate_axis (y, Y_AXIS);
159 else if (str == "dashed")
161 m = dashed_bar_line (me, h, hair);
171 Bar_line::simple_barline (Grob *me,
178 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
181 return Lookup::round_filled_box (Box (Interval (0, w),
182 Interval (-h / 2, h / 2)), blot);
185 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_size, 1);
187 Bar_line::calc_bar_size (SCM smob)
189 Grob *me = unsmob_grob (smob);
190 if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
192 Interval staff_y = staff->extent (staff, Y_AXIS);
193 return scm_from_double (staff_y.is_empty () ? 0.0 : staff_y.length ());
195 return scm_from_int (0);
200 Bar_line::dashed_bar_line (Grob *me, Real h, Real thick)
203 = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
205 this is a tad complex for what we want to achieve, but with a
206 simple line, the round blotting interferes with staff line
209 Real ss = Staff_symbol_referencer::staff_space (me);
210 int count = Staff_symbol_referencer::line_count (me);
211 Real line_thick = Staff_symbol_referencer::line_thickness (me);
213 if (fabs (line_thick + (count -1) * ss - h) < 0.1) // ugh.
216 me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
218 Real half_space = ss/2;
221 for (int i = (count-1); i >= -(count-1); i -= 2)
223 Real top_y = min ((i + dash_size) * half_space,
224 (count-1) * half_space + line_thick / 2);
225 Real bot_y = max ((i - dash_size) * half_space,
226 -(count-1) * half_space - line_thick/2);
228 bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
229 Interval (bot_y, top_y)),
237 We have to scale the dashing so it starts and ends with half a
240 int dashes = int (rint (h / ss));
241 Real total_dash_size = h / dashes;
242 Real factor = (dash_size - thick) / ss;
244 SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
245 scm_from_double (thick),
246 scm_from_double (factor * total_dash_size),
247 scm_from_double ((1-factor) * total_dash_size),
250 scm_from_double (factor * total_dash_size * 0.5),
254 box.add_point (Offset (0, 0));
255 box.add_point (Offset (0, h));
258 s.translate (Offset (thick/2, -h/2));
264 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
266 Bar_line::calc_anchor (SCM smob)
268 Grob *me = unsmob_grob (smob);
269 Real kern = robust_scm2double (me->get_property ("kern"), 1);
270 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
271 string str = robust_scm2string (me->get_property ("glyph-name"), "");
273 /* we put the anchor in the center of the barline, unless we are
274 a repeat bar, in which case we put the anchor in the center of
275 the barline without the dots. */
276 Interval ext = me->extent (me, X_AXIS);
278 return scm_from_double (0);
280 Real anchor = ext.center ();
282 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
283 Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
285 anchor -= dot_width / 2;
286 else if (str == ":|")
287 anchor += dot_width / 2;
289 return scm_from_double (anchor);
292 ADD_INTERFACE (Bar_line,
296 "Print a special bar symbol. It replaces the \n"
297 "regular bar symbol with a special\n"
298 "symbol. The argument @var{bartype} is a string which specifies the\n"
299 "kind of bar to print. Options are @code{:|}, \n"
300 "@code{|:}, @code{:|:},\n"
301 "@code{||}, @code{|.},\n"
302 "@code{.|}, and @code{.|.}. \n"
304 "These produce, respectively, a right repeat, a left repeat, a double\n"
305 "repeat, a double bar, a start bar, an end bar, and a thick double bar.\n"
306 "In addition, there is an option @code{||:} which is equivalent to\n"
307 "@code{|:} except at line breaks, where it produces a double bar (@code{||})\n"
308 "at the end of the line and a repeat sign (@code{|:}) at the beginning\n"
310 "If @var{bartype} is set to @code{empty} then nothing is printed,\n"
311 "but a line break is allowed at that spot.\n"
313 "@code{gap} is used for the gaps in dashed barlines."