Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / lily / bar-line.cc
blobb6424111fdf39f299d7fada453fc7182e44cd2ad
1 /*
2 bar-line.cc -- implement Bar_line
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "bar-line.hh"
11 #include "all-font-metrics.hh"
12 #include "font-interface.hh"
13 #include "lookup.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)
20 SCM
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 || !Staff_symbol_referencer::get_staff_symbol (me))
29 return ly_interval2scm (Interval ());
31 Real h = scm_to_double (size);
32 return ly_interval2scm (Interval (-h/2, h/2));
35 Interval
36 Bar_line::bar_y_extent (Grob *me, Grob *refpoint)
38 Interval iv = robust_scm2interval (me->get_property ("bar-extent"), Interval ());
40 iv.translate (me->relative_coordinate (refpoint, Y_AXIS));
41 return iv;
44 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
45 SCM
46 Bar_line::print (SCM smob)
48 Grob *me = unsmob_grob (smob);
50 SCM s = me->get_property ("glyph-name");
51 SCM barsize = me->get_property ("bar-size");
53 if (scm_is_string (s) && scm_is_number (barsize))
55 string str = ly_scm2string (s);
56 Real sz = robust_scm2double (barsize, 0);
57 if (sz <= 0)
58 return SCM_EOL;
60 return compound_barline (me, str, sz, false).smobbed_copy ();
62 return SCM_EOL;
65 Stencil
66 Bar_line::compound_barline (Grob *me, string str, Real h,
67 bool rounded)
69 Real kern = robust_scm2double (me->get_property ("kern"), 1);
70 Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
71 Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
72 Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
74 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
75 Real staff_space = Staff_symbol_referencer::staff_space (me);
77 kern *= staffline;
78 thinkern *= staffline;
79 hair *= staffline;
80 fatline *= staffline;
82 Stencil thin = simple_barline (me, hair, h, rounded);
83 Stencil thick = simple_barline (me, fatline, h, rounded);
84 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
86 int lines = Staff_symbol_referencer::line_count (me);
87 Real dist
88 = ((lines & 1 || lines == 0)
89 ? 1
90 : (staff_space < 2 ? 2 : .5)) * staff_space;
91 Stencil colon (dot);
92 colon.translate_axis (dist, Y_AXIS);
93 colon.add_stencil (dot);
94 colon.translate_axis (-dist / 2, Y_AXIS);
96 Stencil m;
97 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
98 Real center = 0;
99 if (staff)
101 Interval staff_extent = staff->extent (staff, Y_AXIS);
102 center = staff_extent.center ();
105 if (str == "||:")
106 str = "|:";
108 if (str == "")
110 Stencil empty = Lookup::blank (Box (Interval (0, 0), Interval (-h / 2, h / 2)));
111 empty.translate_axis (center, Y_AXIS);
112 return empty;
114 else if (str == "|")
116 thin.translate_axis (center, Y_AXIS);
117 return thin;
119 else if (str == ".")
121 thick.translate_axis (center, Y_AXIS);
122 return thick;
124 else if (str == "|." || (h == 0 && str == ":|"))
126 m.add_at_edge (X_AXIS, LEFT, thick, 0);
127 m.add_at_edge (X_AXIS, LEFT, thin, kern);
129 else if (str == ".|" || (h == 0 && str == "|:"))
131 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
132 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
134 else if (str == ":|")
136 m.add_at_edge (X_AXIS, LEFT, thick, 0);
137 m.add_at_edge (X_AXIS, LEFT, thin, kern);
138 m.add_at_edge (X_AXIS, LEFT, colon, kern);
140 else if (str == "|:")
142 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
143 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
144 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
146 else if (str == ":|:")
148 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
149 m.add_at_edge (X_AXIS, LEFT, colon, kern);
150 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
151 m.add_at_edge (X_AXIS, RIGHT, colon, 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);
158 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
159 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
162 else if (str == ":|.:")
164 m.add_at_edge (X_AXIS, LEFT, thick, 0);
165 m.add_at_edge (X_AXIS, LEFT, thin, kern);
166 m.add_at_edge (X_AXIS, LEFT, colon, kern);
167 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
169 else if (str == ".|.")
171 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
172 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
174 else if (str == "|.|")
176 m.add_at_edge (X_AXIS, LEFT, thick, 0);
177 m.add_at_edge (X_AXIS, LEFT, thin, kern);
178 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
180 else if (str == "||")
183 should align to other side? this never appears
184 on the system-start?
186 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
187 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
189 else if (str == ":")
191 int c = (Staff_symbol_referencer::line_count (me));
193 for (int i = 0; i < c - 1; i++)
195 Real y = (- (c - 1.0) / 2 + 0.5 + i) * staff_space;
196 Stencil d (dot);
198 d.translate_axis (y, Y_AXIS);
199 m.add_stencil (d);
202 else if (str == "dashed")
204 m = dashed_bar_line (me, h, hair);
206 else if (str == "'")
208 m = tick_bar_line (me, h, rounded);
211 m.translate_axis (center, Y_AXIS);
212 return m;
215 Stencil
216 Bar_line::simple_barline (Grob *me,
217 Real w,
218 Real h,
219 bool rounded)
221 Real blot
222 = rounded
223 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
224 : 0.0;
226 return Lookup::round_filled_box (Box (Interval (0, w),
227 Interval (-h / 2, h / 2)), blot);
230 Stencil
231 Bar_line::tick_bar_line (Grob *me, Real h, bool rounded)
233 Real th = Staff_symbol_referencer::staff_space (me) / 2;
234 Real line_thick = Staff_symbol_referencer::line_thickness (me);
236 Real blot
237 = rounded
238 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
239 : 0.0;
241 return Lookup::round_filled_box (Box (Interval (0, line_thick),
242 Interval (h / 2 - th, h / 2 + th)), blot);
246 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_size, 1);
248 Bar_line::calc_bar_size (SCM smob)
250 Grob *me = unsmob_grob (smob);
251 if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
253 Interval staff_y = staff->extent (staff, Y_AXIS);
254 return scm_from_double (staff_y.is_empty () ? 0.0 : staff_y.length ());
256 return scm_from_int (0);
260 Stencil
261 Bar_line::dashed_bar_line (Grob *me, Real h, Real thick)
263 Real dash_size
264 = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
266 this is a tad complex for what we want to achieve, but with a
267 simple line, the round blotting interferes with staff line
268 connections.
270 Real ss = Staff_symbol_referencer::staff_space (me);
271 int count = Staff_symbol_referencer::line_count (me);
272 Real line_thick = Staff_symbol_referencer::line_thickness (me);
274 if (fabs (line_thick + (count -1) * ss - h) < 0.1) // ugh.
276 Real blot =
277 me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
279 Real half_space = ss/2;
280 Stencil bar;
282 for (int i = (count-1); i >= -(count-1); i -= 2)
284 Real top_y = min ((i + dash_size) * half_space,
285 (count-1) * half_space + line_thick / 2);
286 Real bot_y = max ((i - dash_size) * half_space,
287 -(count-1) * half_space - line_thick/2);
289 bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
290 Interval (bot_y, top_y)),
291 blot));
293 return bar;
295 else
298 We have to scale the dashing so it starts and ends with half a
299 dash exactly.
301 int dashes = int (rint (h / ss));
302 Real total_dash_size = h / dashes;
303 Real factor = (dash_size - thick) / ss;
305 SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
306 scm_from_double (thick),
307 scm_from_double (factor * total_dash_size),
308 scm_from_double ((1-factor) * total_dash_size),
309 scm_from_double (0),
310 scm_from_double (h),
311 scm_from_double (factor * total_dash_size * 0.5),
312 SCM_UNDEFINED);
314 Box box;
315 box.add_point (Offset (0, 0));
316 box.add_point (Offset (0, h));
318 Stencil s (box, at);
319 s.translate (Offset (thick/2, -h/2));
320 return s;
322 return Stencil ();
325 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
327 Bar_line::calc_anchor (SCM smob)
329 Grob *me = unsmob_grob (smob);
330 Real kern = robust_scm2double (me->get_property ("kern"), 1);
331 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
332 string str = robust_scm2string (me->get_property ("glyph-name"), "");
334 /* we put the anchor in the center of the barline, unless we are
335 a repeat bar, in which case we put the anchor in the center of
336 the barline without the dots. */
337 Interval ext = me->extent (me, X_AXIS);
338 if (ext.is_empty ())
339 return scm_from_double (0);
341 Real anchor = ext.center ();
343 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
344 Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
345 if (str == "|:")
346 anchor -= dot_width / 2;
347 else if (str == ":|")
348 anchor += dot_width / 2;
350 return scm_from_double (anchor);
353 ADD_INTERFACE (Bar_line,
354 "Bar line.\n"
355 "\n"
356 "Print a special bar symbol. It replaces the regular bar"
357 " symbol with a special symbol. The argument @var{bartype}"
358 " is a string which specifies the kind of bar line to print."
359 " Options are @code{|}, @code{:|}, @code{|:}, @code{:|:}, @code{:|.|:},"
360 " @code{:|.:}, @code{.}, @code{||}, @code{|.}, @code{.|}, @code{.|.},"
361 " @code{|.|}, @code{:}, @code{dashed} and @code{'}.\n"
362 "\n"
363 "These produce, respectively, a normal bar line, a right repeat, a left repeat,"
364 " a thick double repeat, a thin-thick-thin double repeat,"
365 " a thin-thick double repeat, a thick bar, a double bar, a start bar,"
366 " an end bar, a thick double bar, a thin-thick-thin bar,"
367 " a dotted bar, a dashed bar and a tick as bar bar line."
368 " In addition, there is an option"
369 " @code{||:} which is equivalent to @code{|:} except at line"
370 " breaks, where it produces a double bar (@code{||}) at the"
371 " end of the line and a repeat sign (@code{|:}) at the"
372 " beginning of the new line.\n"
373 "\n"
374 "If @var{bartype} is set to @code{empty} then nothing is"
375 " printed, but a line break is allowed at that spot.\n"
376 "\n"
377 "@code{gap} is used for the gaps in dashed bar lines.",
379 /* properties */
380 "allow-span-bar "
381 "gap "
382 "kern "
383 "thin-kern "
384 "hair-thickness "
385 "thick-thickness "
386 "glyph "
387 "glyph-name "
388 "bar-size "
389 "bar-extent "