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