]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar-line.cc
*** empty log message ***
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.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
18 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
19
20 SCM
21 Bar_line::print (SCM smob)
22 {
23   Grob *me = unsmob_grob (smob);
24
25   SCM s = me->get_property ("glyph");
26   SCM barsiz_proc = me->get_property ("bar-size-procedure");
27   if (scm_is_string (s) && ly_is_procedure (barsiz_proc))
28     {
29       String str = ly_scm2string (s);
30       SCM siz = scm_call_1 (barsiz_proc, me->self_scm ());
31       Real sz = robust_scm2double (siz, 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->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
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   return m;
136 }
137
138 Stencil
139 Bar_line::simple_barline (Grob *me,
140                           Real w,
141                           Real h,
142                           bool rounded)
143 {
144   Real blot
145     = rounded
146     ? me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"))
147     : 0.0;
148
149   return Lookup::round_filled_box (Box (Interval (0, w),
150                                         Interval (-h / 2, h / 2)), blot);
151 }
152
153 MAKE_SCHEME_CALLBACK (Bar_line, before_line_breaking, 1);
154
155 SCM
156 Bar_line::before_line_breaking (SCM smob)
157 {
158   Grob *me = unsmob_grob (smob);
159   Item *item = dynamic_cast<Item *> (me);
160
161   SCM g = me->get_property ("glyph");
162   SCM orig = g;
163   Direction bsd = item->break_status_dir ();
164   if (scm_is_string (g) && bsd)
165     {
166       SCM proc = me->get_property ("break-glyph-function");
167       g = scm_call_2 (proc, g, scm_from_int (bsd));
168     }
169
170   if (!scm_is_string (g))
171     {
172       me->set_property ("print-function", SCM_EOL);
173       me->set_extent (SCM_EOL, X_AXIS);
174       // leave y_extent for spanbar? 
175     }
176
177   if (! ly_is_equal (g, orig))
178     me->set_property ("glyph", g);
179
180   return SCM_UNSPECIFIED;
181 }
182
183 MAKE_SCHEME_CALLBACK (Bar_line, get_staff_bar_size, 1);
184 SCM
185 Bar_line::get_staff_bar_size (SCM smob)
186 {
187   Grob *me = unsmob_grob (smob);
188   Real ss = Staff_symbol_referencer::staff_space (me);
189   SCM size = me->get_property ("bar-size");
190   if (scm_is_number (size))
191     return scm_from_double (scm_to_double (size) * ss);
192   else if (Staff_symbol_referencer::get_staff_symbol (me))
193     {
194       /*
195         If there is no staff-symbol, we get -1 from the next
196         calculation. That's a nonsense value, which would collapse the
197         barline so we return 0.0 in the next alternative.
198       */
199       Real ysize = (Staff_symbol_referencer::line_count (me) -1);
200       ysize = ysize * ss + Staff_symbol_referencer::line_thickness (me);
201       return scm_from_double (ysize);
202     }
203   return scm_from_int (0);
204 }
205
206 ADD_INTERFACE (Bar_line, "bar-line-interface",
207                "Bar line.\n"
208                "\n"
209                "Print a special bar symbol. It replaces the \n"
210                "regular bar symbol with a special\n"
211                "symbol.  The argument @var{bartype} is a string which specifies the\n"
212                "kind of bar to print.  Options are @code{:|}, \n"
213                "@code{|:}, @code{:|:},\n"
214                "@code{||}, @code{|.},\n"
215                "@code{.|}, and @code{.|.}. \n"
216                "\n"
217                "These produce, respectively, a right repeat, a left repeat, a double\n"
218                "repeat, a double bar, a start bar, an end bar, and a thick double bar.\n"
219                "If @var{bartype} is set to @code{empty} then nothing is printed,\n"
220                "but a line break is allowed at that spot.\n",
221                "bar-size-procedure kern thin-kern hair-thickness thick-thickness glyph bar-size break-glyph-function");