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