Compile fix.
[lilypond.git] / lily / bar-line.cc
blob4d2cbf9b5968b46b80f3f9c7b557379a09fde3d2
1 /*
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>
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 == "|." || (h == 0 && str == ":|"))
121 m.add_at_edge (X_AXIS, LEFT, thick, 0);
122 m.add_at_edge (X_AXIS, LEFT, thin, kern);
124 else if (str == ".|" || (h == 0 && str == "|:"))
126 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
127 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
129 else if (str == ":|")
131 m.add_at_edge (X_AXIS, LEFT, thick, 0);
132 m.add_at_edge (X_AXIS, LEFT, thin, kern);
133 m.add_at_edge (X_AXIS, LEFT, colon, kern);
135 else if (str == "|:")
137 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
138 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
139 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
141 else if (str == ":|:")
143 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
144 m.add_at_edge (X_AXIS, LEFT, colon, kern);
145 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
146 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
148 else if (str == ".|.")
150 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
151 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
153 else if (str == "||")
156 should align to other side? this never appears
157 on the system-start?
159 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
160 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
162 else if (str == ":")
164 int c = (Staff_symbol_referencer::line_count (me));
166 for (int i = 0; i < c - 1; i++)
168 Real y = (- (c - 1.0) / 2 + 0.5 + i) * staff_space;
169 Stencil d (dot);
171 d.translate_axis (y, Y_AXIS);
172 m.add_stencil (d);
175 else if (str == "dashed")
177 m = dashed_bar_line (me, h, hair);
179 else if (str == ".")
181 m = dot;
184 m.translate_axis (center, Y_AXIS);
185 return m;
188 Stencil
189 Bar_line::simple_barline (Grob *me,
190 Real w,
191 Real h,
192 bool rounded)
194 Real blot
195 = rounded
196 ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
197 : 0.0;
199 return Lookup::round_filled_box (Box (Interval (0, w),
200 Interval (-h / 2, h / 2)), blot);
203 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_size, 1);
205 Bar_line::calc_bar_size (SCM smob)
207 Grob *me = unsmob_grob (smob);
208 if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
210 Interval staff_y = staff->extent (staff, Y_AXIS);
211 return scm_from_double (staff_y.is_empty () ? 0.0 : staff_y.length ());
213 return scm_from_int (0);
217 Stencil
218 Bar_line::dashed_bar_line (Grob *me, Real h, Real thick)
220 Real dash_size
221 = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
223 this is a tad complex for what we want to achieve, but with a
224 simple line, the round blotting interferes with staff line
225 connections.
227 Real ss = Staff_symbol_referencer::staff_space (me);
228 int count = Staff_symbol_referencer::line_count (me);
229 Real line_thick = Staff_symbol_referencer::line_thickness (me);
231 if (fabs (line_thick + (count -1) * ss - h) < 0.1) // ugh.
233 Real blot =
234 me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
236 Real half_space = ss/2;
237 Stencil bar;
239 for (int i = (count-1); i >= -(count-1); i -= 2)
241 Real top_y = min ((i + dash_size) * half_space,
242 (count-1) * half_space + line_thick / 2);
243 Real bot_y = max ((i - dash_size) * half_space,
244 -(count-1) * half_space - line_thick/2);
246 bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
247 Interval (bot_y, top_y)),
248 blot));
250 return bar;
252 else
255 We have to scale the dashing so it starts and ends with half a
256 dash exactly.
258 int dashes = int (rint (h / ss));
259 Real total_dash_size = h / dashes;
260 Real factor = (dash_size - thick) / ss;
262 SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
263 scm_from_double (thick),
264 scm_from_double (factor * total_dash_size),
265 scm_from_double ((1-factor) * total_dash_size),
266 scm_from_double (0),
267 scm_from_double (h),
268 scm_from_double (factor * total_dash_size * 0.5),
269 SCM_UNDEFINED);
271 Box box;
272 box.add_point (Offset (0, 0));
273 box.add_point (Offset (0, h));
275 Stencil s (box, at);
276 s.translate (Offset (thick/2, -h/2));
277 return s;
279 return Stencil ();
282 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
284 Bar_line::calc_anchor (SCM smob)
286 Grob *me = unsmob_grob (smob);
287 Real kern = robust_scm2double (me->get_property ("kern"), 1);
288 Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
289 string str = robust_scm2string (me->get_property ("glyph-name"), "");
291 /* we put the anchor in the center of the barline, unless we are
292 a repeat bar, in which case we put the anchor in the center of
293 the barline without the dots. */
294 Interval ext = me->extent (me, X_AXIS);
295 if (ext.is_empty ())
296 return scm_from_double (0);
298 Real anchor = ext.center ();
300 Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
301 Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
302 if (str == "|:")
303 anchor -= dot_width / 2;
304 else if (str == ":|")
305 anchor += dot_width / 2;
307 return scm_from_double (anchor);
310 ADD_INTERFACE (Bar_line,
311 "Bar line.\n"
312 "\n"
313 "Print a special bar symbol. It replaces the regular bar"
314 " symbol with a special symbol. The argument @var{bartype}"
315 " is a string which specifies the kind of bar to print."
316 " Options are @code{:|}, @code{|:}, @code{:|:}, @code{||},"
317 " @code{|.}, @code{.|}, and @code{.|.}.\n"
318 "\n"
319 "These produce, respectively, a right repeat, a left repeat,"
320 " a double repeat, a double bar, a start bar, an end bar, and"
321 " a thick double bar. In addition, there is an option"
322 " @code{||:} which is equivalent to @code{|:} except at line"
323 " breaks, where it produces a double bar (@code{||}) at the"
324 " end of the line and a repeat sign (@code{|:}) at the"
325 " beginning of the new line.\n"
326 "\n"
327 "If @var{bartype} is set to @code{empty} then nothing is"
328 " printed, but a line break is allowed at that spot.\n"
329 "\n"
330 "@code{gap} is used for the gaps in dashed barlines.",
332 /* properties */
333 "allow-span-bar "
334 "gap "
335 "kern "
336 "thin-kern "
337 "hair-thickness "
338 "thick-thickness "
339 "glyph "
340 "glyph-name "
341 "bar-size "
342 "bar-extent "