]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar-line.cc
* scm/page-layout.scm (default-page-make-stencil): only add header
[lilypond.git] / lily / bar-line.cc
1 /*
2   bar.cc -- implement Bar
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 <math.h>
10
11 #include "bar-line.hh"
12 #include "lookup.hh"
13 #include "paper-column.hh"
14 #include "output-def.hh"
15 #include "font-interface.hh"
16 #include "all-font-metrics.hh"
17 #include "staff-symbol-referencer.hh"
18
19 MAKE_SCHEME_CALLBACK (Bar_line, print, 1);
20
21 SCM
22 Bar_line::print (SCM smob)
23 {
24   Grob *me = unsmob_grob (smob);
25
26   SCM s = me->get_property ("glyph");
27   SCM barsiz_proc = me->get_property ("bar-size-procedure");
28   if (scm_is_string (s) && ly_c_procedure_p (barsiz_proc))
29     {
30       String str = ly_scm2string (s);
31       SCM siz = scm_call_1 (barsiz_proc, me->self_scm ());
32       Real sz = robust_scm2double (siz, 0);
33       if (sz <= 0)
34         return SCM_EOL;
35
36       return compound_barline (me, str, sz, true).smobbed_copy ();
37     }
38   return SCM_EOL;
39 }
40
41 Stencil
42 Bar_line::compound_barline (Grob *me, String str, Real h,
43                             bool rounded)
44 {
45   Real kern = robust_scm2double (me->get_property ("kern"), 1);
46   Real thinkern = robust_scm2double (me->get_property ("thin-kern"), 1);
47   Real hair = robust_scm2double (me->get_property ("hair-thickness"), 1);
48   Real fatline = robust_scm2double (me->get_property ("thick-thickness"), 1);
49
50   Real staffline = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
51   Real staff_space = Staff_symbol_referencer::staff_space (me);
52
53   kern *= staffline;
54   thinkern *= staffline;
55   hair *= staffline;
56   fatline *= staffline;
57
58   Stencil thin = simple_barline (me, hair, h, rounded);
59   Stencil thick = simple_barline (me, fatline, h, rounded);
60   Stencil dot = Font_interface::get_default_font (me)->find_by_name ("dots.dot");
61
62   int lines = Staff_symbol_referencer::line_count (me);
63   Real dist
64     = ((lines & 1 || lines == 0)
65        ? 1
66        : (staff_space < 2 ? 2 : .5)) * staff_space;
67   Stencil colon (dot);
68   colon.translate_axis (dist, Y_AXIS);
69   colon.add_stencil (dot);
70   colon.translate_axis (-dist / 2, Y_AXIS);
71
72   Stencil m;
73   if (str == "||:")
74     str = "|:";
75
76   if (str == "")
77     {
78       return Lookup::blank (Box (Interval (0, 0), Interval (-h / 2, h / 2)));
79     }
80   else if (str == "|")
81     {
82       return thin;
83     }
84   else if (str == "|." || (h == 0 && str == ":|"))
85     {
86       m.add_at_edge (X_AXIS, LEFT, thick, 0, 0);
87       m.add_at_edge (X_AXIS, LEFT, thin, kern, 0);
88     }
89   else if (str == ".|" || (h == 0 && str == "|:"))
90     {
91       m.add_at_edge (X_AXIS, RIGHT, thick, 0, 0);
92       m.add_at_edge (X_AXIS, RIGHT, thin, kern, 0);
93     }
94   else if (str == ":|")
95     {
96       m.add_at_edge (X_AXIS, LEFT, thick, 0, 0);
97       m.add_at_edge (X_AXIS, LEFT, thin, kern, 0);
98       m.add_at_edge (X_AXIS, LEFT, colon, kern, 0);
99     }
100   else if (str == "|:")
101     {
102       m.add_at_edge (X_AXIS, RIGHT, thick, 0, 0);
103       m.add_at_edge (X_AXIS, RIGHT, thin, kern, 0);
104       m.add_at_edge (X_AXIS, RIGHT, colon, kern, 0);
105     }
106   else if (str == ":|:")
107     {
108       m.add_at_edge (X_AXIS, LEFT, thick, thinkern, 0);
109       m.add_at_edge (X_AXIS, LEFT, colon, kern, 0);
110       m.add_at_edge (X_AXIS, RIGHT, thick, kern, 0);
111       m.add_at_edge (X_AXIS, RIGHT, colon, kern, 0);
112     }
113   else if (str == ".|.")
114     {
115       m.add_at_edge (X_AXIS, LEFT, thick, thinkern, 0);
116       m.add_at_edge (X_AXIS, RIGHT, thick, kern, 0);
117     }
118   else if (str == "||")
119     {
120       /*
121         should align to other side? this never appears
122         on the system-start?
123       */
124       m.add_at_edge (X_AXIS, RIGHT, thin, 0, 0);
125       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern, 0);
126     }
127   else if (str == ":")
128     {
129       int c = (Staff_symbol_referencer::line_count (me));
130
131       for (int i = 0; i < c - 1; i++)
132         {
133           Real y = (- (c - 1.0) / 2 + 0.5 + i * staff_space);
134           Stencil d (dot);
135
136           d.translate_axis (y, Y_AXIS);
137           m.add_stencil (d);
138         }
139     }
140   return m;
141 }
142
143 Stencil
144 Bar_line::simple_barline (Grob *me,
145                           Real w,
146                           Real h,
147                           bool rounded)
148 {
149   Real blot =
150     rounded
151     ? me->get_layout ()->get_dimension (ly_symbol2scm ("blotdiameter"))
152     : 0.0;
153
154   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)), blot);
155 }
156
157 MAKE_SCHEME_CALLBACK (Bar_line, before_line_breaking, 1);
158
159 SCM
160 Bar_line::before_line_breaking (SCM smob)
161 {
162   Grob *me = unsmob_grob (smob);
163   Item *item = dynamic_cast<Item *> (me);
164
165   SCM g = me->get_property ("glyph");
166   SCM orig = g;
167   Direction bsd = item->break_status_dir ();
168   if (scm_is_string (g) && bsd)
169     {
170       SCM proc = me->get_property ("break-glyph-function");
171       g = scm_call_2 (proc, g, scm_int2num (bsd));
172     }
173
174   if (!scm_is_string (g))
175     {
176       me->set_property ("print-function", SCM_EOL);
177       me->set_extent (SCM_EOL, X_AXIS);
178       // leave y_extent for spanbar? 
179     }
180
181   if (! ly_c_equal_p (g, orig))
182     me->set_property ("glyph", g);
183
184   return SCM_UNSPECIFIED;
185 }
186
187 MAKE_SCHEME_CALLBACK (Bar_line, get_staff_bar_size, 1);
188 SCM
189 Bar_line::get_staff_bar_size (SCM smob)
190 {
191   Grob *me = unsmob_grob (smob);
192   Real ss = Staff_symbol_referencer::staff_space (me);
193   SCM size = me->get_property ("bar-size");
194   if (scm_is_number (size))
195     return scm_make_real (scm_to_double (size) * ss);
196   else if (Staff_symbol_referencer::get_staff_symbol (me))
197     {
198       /*
199         If there is no staff-symbol, we get -1 from the next
200         calculation. That's a nonsense value, which would collapse the
201         barline so we return 0.0 in the next alternative.
202       */
203       Real ysize = (Staff_symbol_referencer::line_count (me) -1);
204       ysize = ysize * ss + Staff_symbol_referencer::line_thickness (me);
205       return scm_make_real (ysize);
206     }
207   else
208     return scm_int2num (0);
209 }
210
211 ADD_INTERFACE (Bar_line, "bar-line-interface",
212                "Bar line.\n"
213                "\n"
214                "Print a special bar symbol. It replaces the \n"
215                "regular bar symbol with a special\n"
216                "symbol.  The argument @var{bartype} is a string which specifies the\n"
217                "kind of bar to print.  Options are @code{:|}, \n"
218                "@code{|:}, @code{:|:},\n"
219                "@code{||}, @code{|.},\n"
220                "@code{.|}, and @code{.|.}. \n"
221                "\n"
222                "These produce, respectively, a right repeat, a left repeat, a double\n"
223                "repeat, a double bar, a start bar, an end bar, and a thick double bar.\n"
224                "If @var{bartype} is set to @code{empty} then nothing is printed,\n"
225                "but a line break is allowed at that spot.\n",
226                "bar-size-procedure kern thin-kern hair-thickness thick-thickness glyph bar-size break-glyph-function");