]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar-line.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / bar-line.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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
290   return m;
291 }
292
293 Stencil
294 Bar_line::simple_barline (Grob *me,
295                           Real w,
296                           Interval const &extent,
297                           bool rounded)
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, w), extent), blot);
305 }
306
307 Stencil
308 Bar_line::tick_bar_line (Grob *me, Real h, bool rounded)
309 {
310   Real th = Staff_symbol_referencer::staff_space (me) / 2;
311   Real line_thick = Staff_symbol_referencer::line_thickness (me);
312
313   Real blot
314     = rounded
315       ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
316       : 0.0;
317
318   return Lookup::round_filled_box (Box (Interval (0, line_thick),
319                                         Interval (h - th, h + th)), blot);
320 }
321
322 Stencil
323 Bar_line::dashed_bar_line (Grob *me, Interval const &extent, Real thick)
324 {
325   Real dash_size
326     = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
327   /*
328     this is a tad complex for what we want to achieve, but with a
329     simple line, the round blotting interferes with staff line
330     connections.
331   */
332   Real ss = Staff_symbol_referencer::staff_space (me);
333   Real const h = extent.length ();
334   int dashes = int (rint (h / ss));
335
336   /*
337     there are two concerns:
338     1. one dash plus one space should be one staff space
339     2. the line should begin and end with half a dash
340
341     both can be satisfied, if the extent is (roughly) an integer
342     multiple of staff space.
343   */
344   if (fabs (h / ss - dashes) < 0.1)
345     {
346       Real blot
347         = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
348
349       Real const half_dash = dash_size / 2;
350       Stencil bar;
351
352       for (int i = 0; i <= dashes; ++i)
353         {
354           Real top_y = extent.at (DOWN)
355                        + (i == dashes ? h : (i + half_dash) * ss);
356           Real bot_y = extent.at (DOWN) + (i ? (i - half_dash) * ss : 0.0);
357
358           bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
359                                                           Interval (bot_y, top_y)),
360                                                      blot));
361         }
362       return bar;
363     }
364   else
365     {
366       /*
367         We have to scale the dashing so it starts and ends with half a
368         dash exactly.
369       */
370       Real total_dash_size = h / dashes;
371       Real factor = (dash_size - thick) / ss;
372
373       SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
374                            scm_from_double (thick),
375                            scm_from_double (factor * total_dash_size),
376                            scm_from_double ((1 - factor) * total_dash_size),
377                            scm_from_double (0),
378                            scm_from_double (h),
379                            scm_from_double (factor * total_dash_size * 0.5),
380                            SCM_UNDEFINED);
381
382       Box box;
383       box.add_point (Offset (0, 0));
384       box.add_point (Offset (0, h));
385
386       Stencil s (box, at);
387       s.translate (Offset (thick / 2, extent.at (DOWN)));
388       return s;
389     }
390   return Stencil ();
391 }
392
393 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
394 SCM
395 Bar_line::calc_anchor (SCM smob)
396 {
397   Grob *me = unsmob_grob (smob);
398   Real kern = robust_scm2double (me->get_property ("kern"), 1);
399   Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
400   string str = robust_scm2string (me->get_property ("glyph-name"), "");
401
402   /* we put the anchor in the center of the barline, unless we are
403      a repeat bar, in which case we put the anchor in the center of
404      the barline without the dots. */
405   Interval ext = me->extent (me, X_AXIS);
406   if (ext.is_empty ())
407     return scm_from_double (0);
408
409   Real anchor = ext.center ();
410
411   Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
412   Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
413   if (str == "|:")
414     anchor -= dot_width / 2;
415   else if (str == ":|")
416     anchor += dot_width / 2;
417
418   return scm_from_double (anchor);
419 }
420
421 ADD_INTERFACE (Bar_line,
422                "Bar line.\n"
423                "\n"
424                "Print a special bar symbol.  It replaces the regular bar"
425                " symbol with a special symbol.  The argument @var{bartype}"
426                " is a string which specifies the kind of bar line to print."
427                "  Options are @code{|}, @code{:|}, @code{|:}, @code{:|:}, @code{:|.|:},"
428                " @code{:|.:}, @code{.}, @code{||}, @code{|.}, @code{.|}, @code{.|.},"
429                " @code{|.|}, @code{:}, @code{dashed}, @code{'} and @code{S}.\n"
430                "\n"
431                "These produce, respectively, a normal bar line, a right repeat, a left repeat,"
432                " a thick double repeat, a thin-thick-thin double repeat,"
433                " a thin-thick double repeat, a thick bar, a double bar, a start bar,"
434                " an end bar, a thick double bar, a thin-thick-thin bar,"
435                " a dotted bar, a dashed bar, a tick as bar line and a segno bar.\n"
436                "\n"
437                "In addition, there is an option"
438                " @code{||:} which is equivalent to @code{|:} except at line"
439                " breaks, where it produces a double bar (@code{||}) at the"
440                " end of the line and a repeat sign (@code{|:}) at the"
441                " beginning of the new line.\n"
442                "\n"
443                "For segno, @code{S} produces a segno sign except at line breaks,"
444                " where it produces a double bar (@code{||}) at the"
445                " end of the line and a segno sign at the beginning of the new line."
446                " @code{|S} is equivalent to @code{S} but produces a simple bar line"
447                " (@code{|}) instead of a double bar line (@code{||}) at line breaks."
448                " @code{S|} produces the segno sign at line breaks and starts the following"
449                " line without special bar lines.\n"
450                "\n"
451                "@code{S|:} and @code{:|S} are used for repeat/segno combinations that are"
452                " separated at line breaks.  Alternatively, @code{.S|:} and @code{:|S.}"
453                " may be used which combine repeat signs and segno at the same line in"
454                " case of a line break.  @code{:|S|:} is a combination of a left repeat"
455                " (@code{:|}), a segno (@code{S}) and a right repeat @code{|:} which"
456                " splits before the segno at line breaks; @code{:|S.|:} splits after"
457                " the segno sign.\n"
458                "\n"
459                "If @var{bartype} is set to @code{empty} then nothing is"
460                " printed, but a line break is allowed at that spot.\n"
461                "\n"
462                "@code{gap} is used for the gaps in dashed bar lines.",
463
464                /* properties */
465                "allow-span-bar "
466                "gap "
467                "kern "
468                "thin-kern "
469                "hair-thickness "
470                "has-span-bar "
471                "thick-thickness "
472                "glyph "
473                "glyph-name "
474                "bar-extent "
475               );