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