]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar-line.cc
Issue 1080: corrected spacing in double bar lines
[lilypond.git] / lily / bar-line.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "bar-line.hh"
21
22 #include "all-font-metrics.hh"
23 #include "font-interface.hh"
24 #include "lookup.hh"
25 #include "output-def.hh"
26 #include "paper-column.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "line-interface.hh"
29
30 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_extent, 1)
31 SCM
32 Bar_line::calc_bar_extent (SCM smob)
33 {
34   Grob *me = unsmob_grob (smob);
35
36   SCM size = me->get_property ("bar-size");
37
38   if (!scm_is_number (size)
39       || !Staff_symbol_referencer::get_staff_symbol (me))
40     return ly_interval2scm (Interval ());
41
42   Real h = scm_to_double (size);
43   return ly_interval2scm (Interval (-h/2, h/2));
44 }
45
46 Interval
47 Bar_line::bar_y_extent (Grob *me, Grob *refpoint)
48 {
49   Interval iv = robust_scm2interval (me->get_property ("bar-extent"), Interval ());
50
51   iv.translate (me->relative_coordinate (refpoint, Y_AXIS));
52   return iv;
53 }
54
55 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
56 SCM
57 Bar_line::print (SCM smob)
58 {
59   Grob *me = unsmob_grob (smob);
60
61   SCM s = me->get_property ("glyph-name");
62   SCM barsize = me->get_property ("bar-size");
63
64   if (scm_is_string (s) && scm_is_number (barsize))
65     {
66       string str = ly_scm2string (s);
67       Real sz = robust_scm2double (barsize, 0);
68       if (sz <= 0)
69         return SCM_EOL;
70
71       return compound_barline (me, str, sz, false).smobbed_copy ();
72     }
73   return SCM_EOL;
74 }
75
76 Stencil
77 Bar_line::compound_barline (Grob *me, string str, Real h,
78                             bool rounded)
79 {
80   Real kern = robust_scm2double (me->get_property ("kern"), 1);
81   Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
82   Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
83   Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
84
85   Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
86   Real staff_space = Staff_symbol_referencer::staff_space (me);
87
88   kern *= staffline;
89   thinkern *= staffline;
90   hair *= staffline;
91   fatline *= staffline;
92
93   Stencil thin = simple_barline (me, hair, h, rounded);
94   Stencil thick = simple_barline (me, fatline, h, rounded);
95   Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
96
97   int lines = Staff_symbol_referencer::line_count (me);
98   Real dist
99     = ((lines & 1 || lines == 0)
100        ? 1
101        : (staff_space < 2 ? 2 : .5)) * staff_space;
102   Stencil colon (dot);
103   colon.translate_axis (dist, Y_AXIS);
104   colon.add_stencil (dot);
105   colon.translate_axis (-dist / 2, Y_AXIS);
106
107   Stencil m;
108   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
109   Real center = 0;
110   if (staff)
111     {
112       Interval staff_extent = staff->extent (staff, Y_AXIS);
113       center = staff_extent.center ();
114     }
115
116   if (str == "||:")
117     str = "|:";
118
119   if (str == "|S" || str == "S|")
120     str = "S";
121
122   if (str == "")
123     {
124       Stencil empty =  Lookup::blank (Box (Interval (0, 0), Interval (-h / 2, h / 2)));
125       empty.translate_axis (center, Y_AXIS);
126       return empty;
127     }
128   else if (str == "|")
129     {
130       thin.translate_axis (center, Y_AXIS);
131       return thin;
132     }
133   else if (str == ".")
134     {
135       thick.translate_axis (center, Y_AXIS);
136       return thick;
137     }
138   else if (str == "|." || (h == 0 && str == ":|"))
139     {
140       m.add_at_edge (X_AXIS, LEFT, thick, 0);
141       m.add_at_edge (X_AXIS, LEFT, thin, kern);
142     }
143   else if (str == ".|" || (h == 0 && str == "|:"))
144     {
145       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
146       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
147     }
148   else if (str == ":|")
149     {
150       m.add_at_edge (X_AXIS, LEFT, thick, 0);
151       m.add_at_edge (X_AXIS, LEFT, thin, kern);
152       m.add_at_edge (X_AXIS, LEFT, colon, kern);
153     }
154   else if (str == "|:")
155     {
156       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
157       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
158       m.add_at_edge (X_AXIS, RIGHT, colon, kern);
159     }
160   else if (str == ":|:")
161     {
162       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
163       m.add_at_edge (X_AXIS, LEFT, colon, kern);
164       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
165       m.add_at_edge (X_AXIS, RIGHT, colon, kern);
166     }
167   else if (str == ":|.|:")
168     {
169       m.add_at_edge (X_AXIS, LEFT, thick, 0);
170       m.add_at_edge (X_AXIS, LEFT, thin, kern);
171       m.add_at_edge (X_AXIS, LEFT, colon, kern);
172       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
173       m.add_at_edge (X_AXIS, RIGHT, colon, kern);
174
175     }
176   else if (str == ":|.:")
177     {
178       m.add_at_edge (X_AXIS, LEFT, thick, 0);
179       m.add_at_edge (X_AXIS, LEFT, thin, kern);
180       m.add_at_edge (X_AXIS, LEFT, colon, kern);
181       m.add_at_edge (X_AXIS, RIGHT, colon, kern);
182     }
183   else if (str == ".|.")
184     {
185       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
186       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
187     }
188   else if (str == "|.|")
189     {
190       m.add_at_edge (X_AXIS, LEFT, thick, 0);
191       m.add_at_edge (X_AXIS, LEFT, thin, kern);
192       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
193     }
194   else if (str == "||")
195     {
196       /*
197         should align to other side? this never appears
198         on the system-start?
199       m.add_at_edge (X_AXIS, RIGHT, thin, 0);
200       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
201       */
202       m.add_at_edge (X_AXIS, LEFT, thin, thinkern);
203       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
204     }
205   else if (str.find ("S") != NPOS || str == "|._.|")
206     {
207       //  Handle all varsegno stuff
208       Stencil segno;
209       segno.add_at_edge (X_AXIS, LEFT, thin, thinkern);
210       segno.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
211       segno.add_stencil (Font_interface::get_default_font (me)->find_by_name ("scripts.varsegno"));
212
213       if (str == "S")
214         {
215           m.add_stencil (segno);
216         }
217       else if (str == "S|:" || str == ".S|:")
218         {
219           m.add_at_edge (X_AXIS, RIGHT, thick, 0);
220           m.add_at_edge (X_AXIS, RIGHT, thin, kern);
221           m.add_at_edge (X_AXIS, RIGHT, colon, kern);
222           m.add_at_edge (X_AXIS, LEFT, segno, thinkern);
223         }
224       else if (str == ":|S" || str == ":|S.")
225         {
226           m.add_at_edge (X_AXIS, LEFT, thick, 0);
227           m.add_at_edge (X_AXIS, LEFT, thin, kern);
228           m.add_at_edge (X_AXIS, LEFT, colon, kern);
229           m.add_at_edge (X_AXIS, RIGHT, segno, thinkern);
230         }
231       else if (str == ":|S|:" || str == ":|S.|:")
232         {
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, LEFT, colon, kern);
236           m.add_at_edge (X_AXIS, RIGHT, segno, thinkern);
237           m.add_at_edge (X_AXIS, RIGHT, thick, thinkern);
238           m.add_at_edge (X_AXIS, RIGHT, thin, kern);
239           m.add_at_edge (X_AXIS, RIGHT, colon, kern);
240         }
241       else if (str == "|._.|") // :|S|: or :|S.|: without segno and colon
242         {
243           // get the width of the segno sign
244           Real segno_width = segno.extent (X_AXIS).length ();
245           m.add_at_edge (X_AXIS, LEFT, thick, 0);
246           m.add_at_edge (X_AXIS, LEFT, thin, kern);
247           m.add_at_edge (X_AXIS, RIGHT, thick, segno_width + 2 * thinkern);
248           m.add_at_edge (X_AXIS, RIGHT, thin, kern);
249         }
250       // end varsegno block
251     }
252   else if (str == ":")
253     {
254       int c = (Staff_symbol_referencer::line_count (me));
255
256       for (int i = 0; i < c - 1; i++)
257         {
258           Real y = (- (c - 1.0) / 2 + 0.5 + i) * staff_space;
259           Stencil d (dot);
260
261           d.translate_axis (y, Y_AXIS);
262           m.add_stencil (d);
263         }
264     }
265   else if (str == "dashed")
266     {
267       m = dashed_bar_line (me, h, hair);
268     }
269   else if (str == "'")
270     {
271       m = tick_bar_line (me, h, rounded);
272     }
273
274   m.translate_axis (center, Y_AXIS);
275   return m;
276 }
277
278 Stencil
279 Bar_line::simple_barline (Grob *me,
280                           Real w,
281                           Real h,
282                           bool rounded)
283 {
284   Real blot
285     = rounded
286     ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
287     : 0.0;
288
289   return Lookup::round_filled_box (Box (Interval (0, w),
290                                         Interval (-h / 2, h / 2)), blot);
291 }
292
293 Stencil
294 Bar_line::tick_bar_line (Grob *me, Real h, bool rounded)
295 {
296   Real th = Staff_symbol_referencer::staff_space (me) / 2;
297   Real line_thick = Staff_symbol_referencer::line_thickness (me);
298
299   Real blot
300     = rounded
301     ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
302     : 0.0;
303
304   return Lookup::round_filled_box (Box (Interval (0, line_thick),
305                                         Interval (h / 2 - th, h / 2 + th)), blot);
306 }
307
308
309 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_size, 1);
310 SCM
311 Bar_line::calc_bar_size (SCM smob)
312 {
313   Grob *me = unsmob_grob (smob);
314   if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
315     {
316       Interval staff_y = staff->extent (staff, Y_AXIS);
317       return scm_from_double (staff_y.is_empty () ? 0.0 : staff_y.length ());
318     }
319   return scm_from_int (0);
320 }
321
322
323 Stencil
324 Bar_line::dashed_bar_line (Grob *me, Real h, Real thick)
325 {
326   Real dash_size
327     = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
328   /*
329     this is a tad complex for what we want to achieve, but with a
330     simple line, the round blotting interferes with staff line
331     connections.
332       */
333   Real ss = Staff_symbol_referencer::staff_space (me);
334   int count = Staff_symbol_referencer::line_count (me);
335       Real line_thick = Staff_symbol_referencer::line_thickness (me);
336
337   if (fabs (line_thick + (count -1) * ss - h) <   0.1) // ugh.
338     {
339       Real blot =
340         me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
341
342       Real half_space = ss/2;
343       Stencil bar;
344
345       for (int i = (count-1); i >= -(count-1); i -= 2)
346         {
347           Real top_y = min ((i + dash_size) * half_space,
348                             (count-1) * half_space +  line_thick / 2);
349           Real bot_y = max ((i - dash_size) * half_space,
350                             -(count-1) * half_space - line_thick/2);
351
352           bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
353                                                           Interval (bot_y, top_y)),
354                                                      blot));
355         }
356       return bar;
357     }
358   else
359     {
360       /*
361         We have to scale the dashing so it starts and ends with half a
362         dash exactly.
363        */
364       int dashes = int (rint (h / ss));
365       Real total_dash_size = h / dashes;
366       Real factor = (dash_size - thick) / ss;
367
368       SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
369                            scm_from_double (thick),
370                            scm_from_double (factor * total_dash_size),
371                            scm_from_double ((1-factor) * total_dash_size),
372                            scm_from_double (0),
373                            scm_from_double (h),
374                            scm_from_double (factor * total_dash_size * 0.5),
375                            SCM_UNDEFINED);
376
377       Box box;
378       box.add_point (Offset (0, 0));
379       box.add_point (Offset (0, h));
380
381       Stencil s (box, at);
382       s.translate (Offset (thick/2, -h/2));
383       return s;
384     }
385   return Stencil ();
386 }
387
388 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
389 SCM
390 Bar_line::calc_anchor (SCM smob)
391 {
392   Grob *me = unsmob_grob (smob);
393   Real kern = robust_scm2double (me->get_property ("kern"), 1);
394   Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
395   string str = robust_scm2string (me->get_property ("glyph-name"), "");
396
397   /* we put the anchor in the center of the barline, unless we are
398      a repeat bar, in which case we put the anchor in the center of
399      the barline without the dots. */
400   Interval ext = me->extent (me, X_AXIS);
401   if (ext.is_empty ())
402     return scm_from_double (0);
403
404   Real anchor = ext.center ();
405
406   Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
407   Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
408   if (str == "|:")
409     anchor -= dot_width / 2;
410   else if (str == ":|")
411     anchor += dot_width / 2;
412
413   return scm_from_double (anchor);
414 }
415
416 ADD_INTERFACE (Bar_line,
417                "Bar line.\n"
418                "\n"
419                "Print a special bar symbol.  It replaces the regular bar"
420                " symbol with a special symbol.  The argument @var{bartype}"
421                " is a string which specifies the kind of bar line to print."
422                "  Options are @code{|}, @code{:|}, @code{|:}, @code{:|:}, @code{:|.|:},"
423                " @code{:|.:}, @code{.}, @code{||}, @code{|.}, @code{.|}, @code{.|.},"
424                " @code{|.|}, @code{:}, @code{dashed}, @code{'} and @code{S}.\n"
425                "\n"
426                "These produce, respectively, a normal bar line, a right repeat, a left repeat,"
427                " a thick double repeat, a thin-thick-thin double repeat,"
428                " a thin-thick double repeat, a thick bar, a double bar, a start bar,"
429                " an end bar, a thick double bar, a thin-thick-thin bar,"
430                " a dotted bar, a dashed bar, a tick as bar line and a segno bar.\n"
431                "\n"
432                "In addition, there is an option"
433                " @code{||:} which is equivalent to @code{|:} except at line"
434                " breaks, where it produces a double bar (@code{||}) at the"
435                " end of the line and a repeat sign (@code{|:}) at the"
436                " beginning of the new line.\n"
437                "\n"
438                "For segno, @code{S} produces a segno sign except at line breaks,"
439                " where it produces a double bar (@code{||}) at the"
440                " end of the line and a segno sign at the beginning of the new line."
441                " @code{|S} is equivalent to @code{S} but produces a simple bar line"
442                " (@code{|}) instead of a double bar line (@code{||}) at line breaks."
443                " @code{S|} produces the segno sign at line breaks and starts the following"
444                " line without special bar lines.\n"
445                "\n"
446                "@code{S|:} and @code{:|S} are used for repeat/segno combinations that are"
447                " separated at line breaks. Alternatively, @code{.S|:} and @code{:|S.}"
448                " may be used which combine repeat signs and segno at the same line in"
449                " case of a line break. @code{:|S|:} is a combination of a left repeat"
450                " (@code{:|}), a segno (@code{S}) and a right repeat @code{|:} which"
451                " splits before the segno at line breaks; @code{:|S.|:} splits after"
452                " the segno sign.\n"
453                "\n"
454                "If @var{bartype} is set to @code{empty} then nothing is"
455                " printed, but a line break is allowed at that spot.\n"
456                "\n"
457                "@code{gap} is used for the gaps in dashed bar lines.",
458
459                /* properties */
460                "allow-span-bar "
461                "gap "
462                "kern "
463                "thin-kern "
464                "hair-thickness "
465                "thick-thickness "
466                "glyph "
467                "glyph-name "
468                "bar-size "
469                "bar-extent "
470                );