]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar-line.cc
release: 1.5.47
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <math.h>
9
10 #include "lookup.hh"
11 #include "paper-column.hh"
12 #include "main.hh"
13 #include "grob.hh"
14 #include "bar-line.hh"
15 #include "string.hh"
16 #include "molecule.hh"
17 #include "paper-def.hh"
18 #include "font-interface.hh"
19 #include "debug.hh"
20 #include "all-font-metrics.hh"
21 #include "item.hh"
22 #include "staff-symbol-referencer.hh"
23
24 MAKE_SCHEME_CALLBACK (Bar_line,brew_molecule,1);
25
26 SCM 
27 Bar_line::brew_molecule (SCM smob) 
28 {
29   Grob * me = unsmob_grob (smob);
30
31   SCM s = me->get_grob_property ("glyph");
32   SCM barsiz_proc = me->get_grob_property ("bar-size-procedure");
33   if (gh_string_p (s) && gh_procedure_p (barsiz_proc))
34     {
35       String str  =ly_scm2string (s);
36       SCM siz = gh_call1 (barsiz_proc, me->self_scm ());
37       Real sz =  gh_scm2double (siz);
38       if (sz < 0)
39         return SCM_EOL;
40       
41       return compound_barline (me, str, sz).smobbed_copy ();
42     }
43   return SCM_EOL;
44 }
45
46
47 Molecule
48 Bar_line::compound_barline (Grob*me, String str, Real h)
49 {
50   Real kern = gh_scm2double (me->get_grob_property ("kern"));
51   Real thinkern = gh_scm2double (me->get_grob_property ("thin-kern"));
52   Real hair = gh_scm2double (me->get_grob_property ("hair-thickness"));
53   Real fatline = gh_scm2double (me->get_grob_property ("thick-thickness"));
54
55   Real staffline = me->paper_l ()->get_var ("stafflinethickness");
56   Real staff_space = Staff_symbol_referencer::staff_space (me);
57   Real staffspace = me->paper_l ()->get_var ("staffspace")
58     * staff_space;
59
60   kern *= staffline;
61   thinkern *= staffline;
62   hair *= staffline;
63   fatline *= staffline;
64   
65   Molecule thin = simple_barline (me, hair, h);
66   Molecule thick = simple_barline (me, fatline, h);
67   Molecule colon;
68   Molecule dot = Font_interface::get_default_font (me)->find_by_name ("dots-dot");
69   Real dist = ( Staff_symbol_referencer::line_count (me) & 1 ? 1 :
70                 staff_space<2 ? 2 : .5 ) * staffspace;
71   dot.translate_axis(dist/2,Y_AXIS);
72   colon.add_molecule(dot);
73   dot.translate_axis(-dist,Y_AXIS);
74   colon.add_molecule(dot);
75
76   Molecule m;
77   
78   if (str == "")
79     {
80       return Lookup::blank (Box (Interval (0, 0), Interval (-h/2, h/2)));
81     }
82   else if (str == "|")
83     {
84       return thin;
85     }
86   else if (str == "|." || (h == 0 && str == ":|"))
87     {
88       m.add_at_edge (X_AXIS, LEFT, thick, 0);      
89       m.add_at_edge (X_AXIS, LEFT, thin, kern);
90     }
91   else if (str == ".|" || (h == 0 && str == "|:"))
92     {
93       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
94       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
95     }
96   else if (str == ":|")
97     {
98       m.add_at_edge (X_AXIS, LEFT, thick, 0);
99       m.add_at_edge (X_AXIS, LEFT, thin, kern);
100       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
101     }
102   else if (str == "|:")
103     {
104       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
105       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
106       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
107     }
108   else if (str == ":|:")
109     {
110       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
111       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
112       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
113       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
114     }
115   else if (str == ".|.")
116     {
117       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
118       m.add_at_edge (X_AXIS, RIGHT, thick, kern);      
119     }
120   else if (str == "||")
121     {
122       m.add_at_edge (X_AXIS, RIGHT, thin, 0);
123       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);      
124     }
125
126   return m;
127 }
128
129
130 Molecule
131 Bar_line::simple_barline (Grob*,Real w, Real h) 
132 {
133   return Lookup::filledbox (Box (Interval (0,w), Interval (-h/2, h/2)));
134 }
135
136 MAKE_SCHEME_CALLBACK (Bar_line,before_line_breaking ,1);
137
138 SCM
139 Bar_line::before_line_breaking (SCM smob)
140 {
141   Grob*me=unsmob_grob (smob);
142   Item * item = dynamic_cast<Item*> (me);
143   
144   SCM g = me->get_grob_property ("glyph");
145   SCM orig = g;
146   Direction bsd = item->break_status_dir ();
147   if (gh_string_p (g) && bsd)
148     {
149       SCM proc = me->get_grob_property ("break-glyph-function");
150       g = gh_call2 (proc, g, gh_int2scm (bsd));
151     }
152
153   
154   if (!gh_string_p (g))
155     {
156       me->set_grob_property ("molecule-callback", SCM_EOL);
157       me->set_extent_callback (SCM_EOL, X_AXIS);
158       // leave y_extent for spanbar? 
159     }
160
161   if (! gh_equal_p (g, orig))
162     me->set_grob_property ("glyph", g);
163
164   return SCM_UNSPECIFIED;
165 }
166   
167
168 bool
169 Bar_line::has_interface (Grob*m)
170 {
171   return m && m->has_interface (ly_symbol2scm ("bar-line-interface"));
172 }
173
174
175 MAKE_SCHEME_CALLBACK (Bar_line,get_staff_bar_size,1);
176 SCM
177 Bar_line::get_staff_bar_size (SCM smob) 
178 {
179   Grob*me = unsmob_grob (smob);
180   Real ss = Staff_symbol_referencer::staff_space (me);
181   SCM size = me->get_grob_property ("bar-size");
182   if (gh_number_p (size))
183     return gh_double2scm (gh_scm2double (size)*ss);
184   else if (Staff_symbol_referencer::staff_symbol_l (me))
185     {
186       /*
187         If there is no staff-symbol, we get -1 from the next
188         calculation. That's a nonsense value, which would collapse the
189         barline so we return 0.0 in the next alternative.
190       */
191       return gh_double2scm ((Staff_symbol_referencer::line_count (me) -1) * ss);
192     }
193   else
194     return gh_int2scm (0);
195 }
196
197
198
199 ADD_INTERFACE (Bar_line, "bar-line-interface",
200   "Bar line.
201
202 This is a request to print a special bar symbol. It replaces the 
203 regular bar symbol with a special
204 symbol.  The argument @var{bartype} is a string which specifies the
205 kind of bar to print.  Options are @code{:|}
206 @cindex |A@@@code{:|}
207 ,
208 @code{|:}
209 @cindex |B@@@code{|:}
210 , @code{:|:}
211 @cindex |C@@@code{:|:}
212 ,
213 @code{||}
214 @cindex |D@@@code{||}
215 , @code{|.}
216 @cindex |E@@@code{|.}
217 ,
218 @code{.|}
219 @cindex |F@@@code{.|}
220 , and @code{.|.}
221 @cindex |G@@@code{.|.}
222
223
224 These produce, respectively, a right repeat, a left repeat, a double
225 repeat, a double bar, a start bar, an end bar, and a thick double bar.
226 If @var{bartype} is set to @code{empty} then nothing is printed,
227 but a line break is allowed at that spot.
228 "
229 ,
230   "bar-size-procedure kern thin-kern hair-thickness thick-thickness glyph bar-size break-glyph-function");