]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar-line.cc
Nitpick run.
[lilypond.git] / lily / bar-line.cc
1 /*
2   bar-line.cc -- implement Bar_line
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "bar-line.hh"
10
11 #include <math.h>
12
13 #include "all-font-metrics.hh"
14 #include "font-interface.hh"
15 #include "lookup.hh"
16 #include "output-def.hh"
17 #include "paper-column.hh"
18 #include "staff-symbol-referencer.hh"
19
20 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
21
22 SCM
23 Bar_line::print (SCM smob)
24 {
25   Grob *me = unsmob_grob (smob);
26
27   SCM s = me->get_property ("glyph");
28   SCM barsiz_proc = me->get_property ("bar-size-procedure");
29   if (scm_is_string (s) && ly_is_procedure (barsiz_proc))
30     {
31       String str = ly_scm2string (s);
32       SCM siz = scm_call_1 (barsiz_proc, me->self_scm ());
33       Real sz = robust_scm2double (siz, 0);
34       if (sz <= 0)
35         return SCM_EOL;
36
37       return compound_barline (me, str, sz, false).smobbed_copy ();
38     }
39   return SCM_EOL;
40 }
41
42 Stencil
43 Bar_line::compound_barline (Grob *me, String str, Real h,
44                             bool rounded)
45 {
46   Real kern = robust_scm2double (me->get_property ("kern"), 1);
47   Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
48   Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
49   Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
50
51   Real staffline = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
52   Real staff_space = Staff_symbol_referencer::staff_space (me);
53
54   kern *= staffline;
55   thinkern *= staffline;
56   hair *= staffline;
57   fatline *= staffline;
58
59   Stencil thin = simple_barline (me, hair, h, rounded);
60   Stencil thick = simple_barline (me, fatline, h, rounded);
61   Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
62
63   int lines = Staff_symbol_referencer::line_count (me);
64   Real dist
65     = ((lines & 1 || lines == 0)
66        ? 1
67        : (staff_space < 2 ? 2 : .5)) * staff_space;
68   Stencil colon (dot);
69   colon.translate_axis (dist, Y_AXIS);
70   colon.add_stencil (dot);
71   colon.translate_axis (-dist / 2, Y_AXIS);
72
73   Stencil m;
74   if (str == "||:")
75     str = "|:";
76
77   if (str == "")
78     return Lookup::blank (Box (Interval (0, 0), Interval (-h / 2, h / 2)));
79   else if (str == "|")
80     return thin;
81   else if (str == "|." || (h == 0 && str == ":|"))
82     {
83       m.add_at_edge (X_AXIS, LEFT, thick, 0, 0);
84       m.add_at_edge (X_AXIS, LEFT, thin, kern, 0);
85     }
86   else if (str == ".|" || (h == 0 && str == "|:"))
87     {
88       m.add_at_edge (X_AXIS, RIGHT, thick, 0, 0);
89       m.add_at_edge (X_AXIS, RIGHT, thin, kern, 0);
90     }
91   else if (str == ":|")
92     {
93       m.add_at_edge (X_AXIS, LEFT, thick, 0, 0);
94       m.add_at_edge (X_AXIS, LEFT, thin, kern, 0);
95       m.add_at_edge (X_AXIS, LEFT, colon, kern, 0);
96     }
97   else if (str == "|:")
98     {
99       m.add_at_edge (X_AXIS, RIGHT, thick, 0, 0);
100       m.add_at_edge (X_AXIS, RIGHT, thin, kern, 0);
101       m.add_at_edge (X_AXIS, RIGHT, colon, kern, 0);
102     }
103   else if (str == ":|:")
104     {
105       m.add_at_edge (X_AXIS, LEFT, thick, thinkern, 0);
106       m.add_at_edge (X_AXIS, LEFT, colon, kern, 0);
107       m.add_at_edge (X_AXIS, RIGHT, thick, kern, 0);
108       m.add_at_edge (X_AXIS, RIGHT, colon, kern, 0);
109     }
110   else if (str == ".|.")
111     {
112       m.add_at_edge (X_AXIS, LEFT, thick, thinkern, 0);
113       m.add_at_edge (X_AXIS, RIGHT, thick, kern, 0);
114     }
115   else if (str == "||")
116     {
117       /*
118         should align to other side? this never appears
119         on the system-start?
120       */
121       m.add_at_edge (X_AXIS, RIGHT, thin, 0, 0);
122       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern, 0);
123     }
124   else if (str == ":")
125     {
126       int c = (Staff_symbol_referencer::line_count (me));
127
128       for (int i = 0; i < c - 1; i++)
129         {
130           Real y = (- (c - 1.0) / 2 + 0.5 + i * staff_space);
131           Stencil d (dot);
132
133           d.translate_axis (y, Y_AXIS);
134           m.add_stencil (d);
135         }
136     }
137   return m;
138 }
139
140 Stencil
141 Bar_line::simple_barline (Grob *me,
142                           Real w,
143                           Real h,
144                           bool rounded)
145 {
146   Real blot
147     = rounded
148     ? me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"))
149     : 0.0;
150
151   return Lookup::round_filled_box (Box (Interval (0, w),
152                                         Interval (-h / 2, h / 2)), blot);
153 }
154
155 MAKE_SCHEME_CALLBACK (Bar_line, before_line_breaking, 1);
156
157 SCM
158 Bar_line::before_line_breaking (SCM smob)
159 {
160   Grob *me = unsmob_grob (smob);
161   Item *item = dynamic_cast<Item *> (me);
162
163   SCM g = me->get_property ("glyph");
164   SCM orig = g;
165   Direction bsd = item->break_status_dir ();
166   if (scm_is_string (g) && bsd)
167     {
168       SCM proc = me->get_property ("break-glyph-function");
169       g = scm_call_2 (proc, g, scm_from_int (bsd));
170     }
171
172   if (!scm_is_string (g))
173     {
174       me->set_property ("print-function", SCM_EOL);
175       me->set_extent (SCM_EOL, X_AXIS);
176       // leave y_extent for spanbar? 
177     }
178
179   if (! ly_is_equal (g, orig))
180     me->set_property ("glyph", g);
181
182   return SCM_UNSPECIFIED;
183 }
184
185 MAKE_SCHEME_CALLBACK (Bar_line, get_staff_bar_size, 1);
186 SCM
187 Bar_line::get_staff_bar_size (SCM smob)
188 {
189   Grob *me = unsmob_grob (smob);
190   Real ss = Staff_symbol_referencer::staff_space (me);
191   SCM size = me->get_property ("bar-size");
192   if (scm_is_number (size))
193     return scm_from_double (scm_to_double (size) * ss);
194   else if (Staff_symbol_referencer::get_staff_symbol (me))
195     {
196       /*
197         If there is no staff-symbol, we get -1 from the next
198         calculation. That's a nonsense value, which would collapse the
199         barline so we return 0.0 in the next alternative.
200       */
201       Real ysize = (Staff_symbol_referencer::line_count (me) -1);
202       ysize = ysize * ss + Staff_symbol_referencer::line_thickness (me);
203       return scm_from_double (ysize);
204     }
205   return scm_from_int (0);
206 }
207
208 ADD_INTERFACE (Bar_line, "bar-line-interface",
209                "Bar line.\n"
210                "\n"
211                "Print a special bar symbol. It replaces the \n"
212                "regular bar symbol with a special\n"
213                "symbol.  The argument @var{bartype} is a string which specifies the\n"
214                "kind of bar to print.  Options are @code{:|}, \n"
215                "@code{|:}, @code{:|:},\n"
216                "@code{||}, @code{|.},\n"
217                "@code{.|}, and @code{.|.}. \n"
218                "\n"
219                "These produce, respectively, a right repeat, a left repeat, a double\n"
220                "repeat, a double bar, a start bar, an end bar, and a thick double bar.\n"
221                "If @var{bartype} is set to @code{empty} then nothing is printed,\n"
222                "but a line break is allowed at that spot.\n",
223                "bar-size-procedure kern thin-kern hair-thickness thick-thickness glyph bar-size break-glyph-function");