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