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