]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar-line.cc
6bf140efec83e154dae024351a80341ec7b99271
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10
11 #include "lookup.hh"
12 #include "paper-column.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
20 #include "all-font-metrics.hh"
21 #include "item.hh"
22 #include "staff-symbol-referencer.hh"
23
24 MAKE_SCHEME_CALLBACK (Bar_line,print,1);
25
26 SCM 
27 Bar_line::print (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 = robust_scm2double (siz, 0);
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 = robust_scm2double (me->get_grob_property ("kern"), 1);
51   Real thinkern = robust_scm2double (me->get_grob_property ("thin-kern"), 1);
52   Real hair = robust_scm2double (me->get_grob_property ("hair-thickness"), 1);
53   Real fatline = robust_scm2double (me->get_grob_property ("thick-thickness"), 1);
54
55   Real staffline = me->get_paper ()->get_realvar (ly_symbol2scm ("linethickness"));
56   Real staff_space = Staff_symbol_referencer::staff_space (me);
57
58   kern *= staffline;
59   thinkern *= staffline;
60   hair *= staffline;
61   fatline *= staffline;
62   
63   Molecule thin = simple_barline (me, hair, h);
64   Molecule thick = simple_barline (me, fatline, h);
65   Molecule dot = Font_interface::get_default_font (me)->find_by_name ("dots-dot");
66   Real dist = ( Staff_symbol_referencer::line_count (me) & 1 ? 1 :
67                 (staff_space<2 ? 2 : .5) ) * staff_space;
68   Molecule colon (dot);
69   colon.translate_axis(dist,Y_AXIS);
70   colon.add_molecule(dot);
71   colon.translate_axis(-dist/2,Y_AXIS);
72
73   Molecule m;
74   
75   if (str == "")
76     {
77       return Lookup::blank (Box (Interval (0, 0), Interval (-h/2, h/2)));
78     }
79   else if (str == "|")
80     {
81       return thin;
82     }
83   else if (str == "|." || (h == 0 && str == ":|"))
84     {
85       m.add_at_edge (X_AXIS, LEFT, thick, 0, 0);      
86       m.add_at_edge (X_AXIS, LEFT, thin, kern,0 );
87     }
88   else if (str == ".|" || (h == 0 && str == "|:"))
89     {
90       m.add_at_edge (X_AXIS, RIGHT, thick, 0, 0);
91       m.add_at_edge (X_AXIS, RIGHT, thin, kern, 0);
92     }
93   else if (str == ":|")
94     {
95       m.add_at_edge (X_AXIS, LEFT, thick, 0, 0);
96       m.add_at_edge (X_AXIS, LEFT, thin, kern, 0);
97       m.add_at_edge (X_AXIS, LEFT, colon, kern, 0);      
98     }
99   else if (str == "|:")
100     {
101       m.add_at_edge (X_AXIS, RIGHT, thick, 0, 0);
102       m.add_at_edge (X_AXIS, RIGHT, thin, kern, 0);
103       m.add_at_edge (X_AXIS, RIGHT, colon, kern, 0);      
104     }
105   else if (str == ":|:")
106     {
107       m.add_at_edge (X_AXIS, LEFT, thick, thinkern, 0);
108       m.add_at_edge (X_AXIS, LEFT, colon, kern, 0);
109       m.add_at_edge (X_AXIS, RIGHT, thick, kern, 0);
110       m.add_at_edge (X_AXIS, RIGHT, colon, kern, 0);      
111     }
112   else if (str == ".|.")
113     {
114       m.add_at_edge (X_AXIS, LEFT, thick, thinkern, 0);
115       m.add_at_edge (X_AXIS, RIGHT, thick, kern, 0);
116     }
117   else if (str == "||")
118     {
119       /*
120         should align to other side? this never appears
121         on the system-start?
122       */
123       m.add_at_edge (X_AXIS, RIGHT, thin, 0, 0);
124       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern, 0);
125     }
126   else if (str == ":")
127     {
128       int c = (Staff_symbol_referencer::line_count (me));
129       
130       for (int i = 0 ; i < c - 1; i++)
131         {
132           Real y = (- (c-1.0) / 2 + 0.5  +   i * staff_space);
133           Molecule d (dot);
134
135           d. translate_axis (y,Y_AXIS);
136           m.add_molecule (d);
137         }
138     }
139   return m;
140 }
141
142 Molecule
143 Bar_line::simple_barline (Grob *me,Real w, Real h) 
144 {
145   Real blot = me->get_paper ()->get_realvar (ly_symbol2scm ("blotdiameter"));
146   return Lookup::round_filled_box (Box (Interval (0,w), Interval (-h/2, h/2)), blot);
147 }
148
149 MAKE_SCHEME_CALLBACK (Bar_line,before_line_breaking ,1);
150
151 SCM
152 Bar_line::before_line_breaking (SCM smob)
153 {
154   Grob*me=unsmob_grob (smob);
155   Item * item = dynamic_cast<Item*> (me);
156   
157   SCM g = me->get_grob_property ("glyph");
158   SCM orig = g;
159   Direction bsd = item->break_status_dir ();
160   if (gh_string_p (g) && bsd)
161     {
162       SCM proc = me->get_grob_property ("break-glyph-function");
163       g = gh_call2 (proc, g, scm_int2num (bsd));
164     }
165   
166   if (!gh_string_p (g))
167     {
168       me->set_grob_property ("print-function", SCM_EOL);
169       me->set_extent (SCM_EOL, X_AXIS);
170       // leave y_extent for spanbar? 
171     }
172
173   if (! gh_equal_p (g, orig))
174     me->set_grob_property ("glyph", g);
175
176   return SCM_UNSPECIFIED;
177 }
178   
179
180
181
182 MAKE_SCHEME_CALLBACK (Bar_line,get_staff_bar_size,1);
183 SCM
184 Bar_line::get_staff_bar_size (SCM smob) 
185 {
186   Grob*me = unsmob_grob (smob);
187   Real ss = Staff_symbol_referencer::staff_space (me);
188   SCM size = me->get_grob_property ("bar-size");
189   if (gh_number_p (size))
190     return gh_double2scm (gh_scm2double (size)*ss);
191   else if (Staff_symbol_referencer::get_staff_symbol (me))
192     {
193       /*
194         If there is no staff-symbol, we get -1 from the next
195         calculation. That's a nonsense value, which would collapse the
196         barline so we return 0.0 in the next alternative.
197       */
198       return gh_double2scm ((Staff_symbol_referencer::line_count (me) -1) * ss);
199     }
200   else
201     return scm_int2num (0);
202 }
203
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");