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