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