]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar-line.cc
Merge commit '76de7e1'
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "bar-line.hh"
10
11 #include "all-font-metrics.hh"
12 #include "font-interface.hh"
13 #include "lookup.hh"
14 #include "output-def.hh"
15 #include "paper-column.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "line-interface.hh"
18
19 /* Get the extent of just the line part of the bar (ie. excluding any
20    repeat dots) */
21 Interval
22 Bar_line::bar_y_extent (Grob *me, Grob *refpoint)
23 {
24   SCM size = me->get_property ("bar-size");
25   if (!scm_is_number (size))
26     return Interval ();
27
28   Real h = scm_to_double (size);
29   Interval iv (-h/2, h/2);
30
31   iv.translate (me->relative_coordinate (refpoint, Y_AXIS));
32   return iv;
33 }
34
35 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
36 SCM
37 Bar_line::print (SCM smob)
38 {
39   Grob *me = unsmob_grob (smob);
40
41   SCM s = me->get_property ("glyph-name");
42   SCM barsize = me->get_property ("bar-size");
43   
44   if (scm_is_string (s) && scm_is_number (barsize))
45     {
46       string str = ly_scm2string (s);
47       Real sz = robust_scm2double (barsize, 0);
48       if (sz <= 0)
49         return SCM_EOL;
50
51       return compound_barline (me, str, sz, false).smobbed_copy ();
52     }
53   return SCM_EOL;
54 }
55
56 Stencil
57 Bar_line::compound_barline (Grob *me, string str, Real h,
58                             bool rounded)
59 {
60   Real kern = robust_scm2double (me->get_property ("kern"), 1);
61   Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
62   Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
63   Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
64
65   Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
66   Real staff_space = Staff_symbol_referencer::staff_space (me);
67
68   kern *= staffline;
69   thinkern *= staffline;
70   hair *= staffline;
71   fatline *= staffline;
72
73   Stencil thin = simple_barline (me, hair, h, rounded);
74   Stencil thick = simple_barline (me, fatline, h, rounded);
75   Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
76
77   int lines = Staff_symbol_referencer::line_count (me);
78   Real dist
79     = ((lines & 1 || lines == 0)
80        ? 1
81        : (staff_space < 2 ? 2 : .5)) * staff_space;
82   Stencil colon (dot);
83   colon.translate_axis (dist, Y_AXIS);
84   colon.add_stencil (dot);
85   colon.translate_axis (-dist / 2, Y_AXIS);
86
87   Stencil m;
88   if (str == "||:")
89     str = "|:";
90
91   if (str == "")
92     return Lookup::blank (Box (Interval (0, 0), Interval (-h / 2, h / 2)));
93   else if (str == "|")
94     return thin;
95   else if (str == "|." || (h == 0 && str == ":|"))
96     {
97       m.add_at_edge (X_AXIS, LEFT, thick, 0);
98       m.add_at_edge (X_AXIS, LEFT, thin, kern);
99     }
100   else if (str == ".|" || (h == 0 && str == "|:"))
101     {
102       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
103       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
104     }
105   else if (str == ":|")
106     {
107       m.add_at_edge (X_AXIS, LEFT, thick, 0);
108       m.add_at_edge (X_AXIS, LEFT, thin, kern);
109       m.add_at_edge (X_AXIS, LEFT, colon, kern);
110     }
111   else if (str == "|:")
112     {
113       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
114       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
115       m.add_at_edge (X_AXIS, RIGHT, colon, kern);
116     }
117   else if (str == ":|:")
118     {
119       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
120       m.add_at_edge (X_AXIS, LEFT, colon, kern);
121       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
122       m.add_at_edge (X_AXIS, RIGHT, colon, kern);
123     }
124   else if (str == ".|.")
125     {
126       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
127       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
128     }
129   else if (str == "||")
130     {
131       /*
132         should align to other side? this never appears
133         on the system-start?
134       */
135       m.add_at_edge (X_AXIS, RIGHT, thin, 0);
136       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
137     }
138   else if (str == ":")
139     {
140       int c = (Staff_symbol_referencer::line_count (me));
141
142       for (int i = 0; i < c - 1; i++)
143         {
144           Real y = (- (c - 1.0) / 2 + 0.5 + i) * staff_space;
145           Stencil d (dot);
146
147           d.translate_axis (y, Y_AXIS);
148           m.add_stencil (d);
149         }
150     }
151   else if (str == "dashed")
152     {
153       m = dashed_bar_line (me, h, hair);
154     }
155   else if (str == ".")
156     {
157       m = dot;
158     }
159   return m;
160 }
161
162 Stencil
163 Bar_line::simple_barline (Grob *me,
164                           Real w,
165                           Real h,
166                           bool rounded)
167 {
168   Real blot
169     = rounded
170     ? me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"))
171     : 0.0;
172
173   return Lookup::round_filled_box (Box (Interval (0, w),
174                                         Interval (-h / 2, h / 2)), blot);
175 }
176
177 MAKE_SCHEME_CALLBACK (Bar_line, calc_bar_size, 1);
178 SCM
179 Bar_line::calc_bar_size (SCM smob)
180 {
181   Grob *me = unsmob_grob (smob);
182   if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
183     {
184       Interval staff_y = staff->extent (staff, Y_AXIS);
185       return scm_from_double (staff_y.is_empty () ? 0.0 : staff_y.length ());
186     }
187   return scm_from_int (0);
188 }
189
190
191 Stencil
192 Bar_line::dashed_bar_line (Grob *me, Real h, Real thick)
193 {
194   Real dash_size
195     = 1.0 - robust_scm2double (me->get_property ("gap"), 0.3);
196   /*
197     this is a tad complex for what we want to achieve, but with a
198     simple line, the round blotting interferes with staff line
199     connections.
200       */
201   Real ss = Staff_symbol_referencer::staff_space (me);
202   int count = Staff_symbol_referencer::line_count (me);
203       Real line_thick = Staff_symbol_referencer::line_thickness (me);
204
205   if (fabs (line_thick + (count -1) * ss - h) <   0.1) // ugh.
206     {
207       Real blot = 
208         me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
209
210       Real half_space = ss/2;
211       Stencil bar;
212   
213       for (int i = (count-1); i >= -(count-1); i -= 2)
214         {
215           Real top_y = min ((i + dash_size) * half_space,
216                             (count-1) * half_space +  line_thick / 2);
217           Real bot_y = max ((i - dash_size) * half_space,
218                             -(count-1) * half_space - line_thick/2);
219
220           bar.add_stencil (Lookup::round_filled_box (Box (Interval (0, thick),
221                                                           Interval (bot_y, top_y)),
222                                                      blot));
223         }
224       return bar;
225     }
226   else
227     {
228       /*
229         We have to scale the dashing so it starts and ends with half a
230         dash exactly.
231        */
232       int dashes = int (rint (h / ss));
233       Real total_dash_size = h / dashes;
234       Real factor = (dash_size - thick) / ss;
235       
236       SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
237                            scm_from_double (thick),
238                            scm_from_double (factor * total_dash_size),
239                            scm_from_double ((1-factor) * total_dash_size),
240                            scm_from_double (0),
241                            scm_from_double (h),
242                            scm_from_double (factor * total_dash_size * 0.5),
243                            SCM_UNDEFINED);
244
245       Box box;
246       box.add_point (Offset (0, 0));
247       box.add_point (Offset (0, h));
248
249       Stencil s (box, at);
250       s.translate (Offset (thick/2, -h/2));
251       return s;
252     }
253   return Stencil ();
254 }
255
256 MAKE_SCHEME_CALLBACK (Bar_line, calc_anchor, 1)
257 SCM
258 Bar_line::calc_anchor (SCM smob)
259 {
260   Grob *me = unsmob_grob (smob);
261   Real kern = robust_scm2double (me->get_property ("kern"), 1);
262   Real staffline = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
263   string str = robust_scm2string (me->get_property ("glyph-name"), "");
264
265   /* we put the anchor in the center of the barline, unless we are
266      a repeat bar, in which case we put the anchor in the center of
267      the barline without the dots. */
268   Interval ext = me->extent (me, X_AXIS);
269   if (ext.is_empty ())
270     return scm_from_double (0);
271
272   Real anchor = ext.center ();
273
274   Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
275   Real dot_width = dot.extent (X_AXIS).length () + kern * staffline;
276   if (str == "|:")
277     anchor -= dot_width / 2;
278   else if (str == ":|")
279     anchor += dot_width / 2;
280
281   return scm_from_double (anchor);
282 }
283
284 ADD_INTERFACE (Bar_line,
285
286                "Bar line.\n"
287                "\n"
288                "Print a special bar symbol. It replaces the \n"
289                "regular bar symbol with a special\n"
290                "symbol.  The argument @var{bartype} is a string which specifies the\n"
291                "kind of bar to print.  Options are @code{:|}, \n"
292                "@code{|:}, @code{:|:},\n"
293                "@code{||}, @code{|.},\n"
294                "@code{.|}, and @code{.|.}. \n"
295                "\n"
296                "These produce, respectively, a right repeat, a left repeat, a double\n"
297                "repeat, a double bar, a start bar, an end bar, and a thick double bar.\n"
298                "In addition, there is an option @code{||:} which is equivalent to\n"
299                "@code{|:} except at line breaks, where it produces a double bar (@code{||})\n"
300                "at the end of the line and a repeat sign (@code{|:}) at the beginning\n"
301                "of the new line."
302                "If @var{bartype} is set to @code{empty} then nothing is printed,\n"
303                "but a line break is allowed at that spot.\n"
304                "\n\n"
305                "@code{gap} is used for the gaps in dashed barlines."
306
307                ,
308
309
310                /* properties */
311                "allow-span-bar "
312                "gap "
313                "kern "
314                "thin-kern "
315                "hair-thickness "
316                "thick-thickness "
317                "glyph "
318                "glyph-name "
319                "bar-size "
320                );
321
322