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