]> git.donarmstrong.com Git - lilypond.git/blob - lily/bar.cc
497af0fa3a222e68343b8ea4386c12abd24be171
[lilypond.git] / lily / bar.cc
1 /*
2   bar.cc -- implement Bar
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 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.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,brew_molecule,1);
25
26 SCM 
27 Bar::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::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 staffspace = me->paper_l ()->get_var ("staffspace")
57     * Staff_symbol_referencer::staff_space (me);
58
59   kern *= staffline;
60   thinkern *= staffline;
61   hair *= staffline;
62   fatline *= staffline;
63   
64   Molecule thin = simple_barline (me, hair, h);
65   Molecule thick = simple_barline (me, fatline, h);
66   Molecule colon;
67   Molecule dot = Font_interface::get_default_font (me)->find_by_name ("dots-dot");
68   Real dist = (2-(Staff_symbol_referencer::line_count (me) & 1))*staffspace;
69   dot.translate_axis(dist/2,Y_AXIS);
70   colon.add_molecule(dot);
71   dot.translate_axis(-dist,Y_AXIS);
72   colon.add_molecule(dot);
73
74   Molecule m;
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);      
87       m.add_at_edge (X_AXIS, LEFT, thin, kern);
88     }
89   else if (str == ".|" || (h == 0 && str == "|:"))
90     {
91       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
92       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
93     }
94   else if (str == ":|")
95     {
96       m.add_at_edge (X_AXIS, LEFT, thick, 0);
97       m.add_at_edge (X_AXIS, LEFT, thin, kern);
98       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
99     }
100   else if (str == "|:")
101     {
102       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
103       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
104       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
105     }
106   else if (str == ":|:")
107     {
108       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
109       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
110       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
111       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
112     }
113   else if (str == ".|.")
114     {
115       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
116       m.add_at_edge (X_AXIS, RIGHT, thick, kern);      
117     }
118   else if (str == "||")
119     {
120       m.add_at_edge (X_AXIS, RIGHT, thin, 0);
121       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);      
122     }
123
124   return m;
125 }
126
127
128 Molecule
129 Bar::simple_barline (Grob*,Real w, Real h) 
130 {
131   return Lookup::filledbox (Box (Interval (0,w), Interval (-h/2, h/2)));
132 }
133
134 MAKE_SCHEME_CALLBACK (Bar,before_line_breaking ,1);
135
136 SCM
137 Bar::before_line_breaking (SCM smob)
138 {
139   Grob*me=unsmob_grob (smob);
140   Item * item = dynamic_cast<Item*> (me);
141   
142   SCM g = me->get_grob_property ("glyph");
143   SCM orig = g;
144   Direction bsd = item->break_status_dir ();
145   if (gh_string_p (g) && bsd)
146     {
147       SCM proc = me->get_grob_property ("break-glyph-function");
148       g = gh_call2 (proc, g, gh_int2scm (bsd));
149     }
150
151   
152   if (!gh_string_p (g))
153     {
154       me->set_grob_property ("molecule-callback", SCM_BOOL_T);
155       me->set_extent_callback (SCM_EOL, X_AXIS);
156       // leave y_extent for spanbar? 
157     }
158
159   if (! gh_equal_p (g, orig))
160     me->set_grob_property ("glyph", g);
161
162   return SCM_UNSPECIFIED;
163 }
164   
165 void
166 Bar::set_interface (Grob*me)
167 {
168   me->set_interface (ly_symbol2scm ("bar-line-interface"));
169 }
170
171 bool
172 Bar::has_interface (Grob*m)
173 {
174   return m && m->has_interface (ly_symbol2scm ("bar-line-interface"));
175 }
176
177
178 MAKE_SCHEME_CALLBACK (Bar,get_staff_bar_size,1);
179 SCM
180 Bar::get_staff_bar_size (SCM smob) 
181 {
182   Grob*me = unsmob_grob (smob);
183   Real ss = Staff_symbol_referencer::staff_space (me);
184   SCM size = me->get_grob_property ("bar-size");
185   if (gh_number_p (size))
186     return gh_double2scm (gh_scm2double (size)*ss);
187   else if (Staff_symbol_referencer::staff_symbol_l (me))
188     {
189       /*
190         If there is no staff-symbol, we get -1 from the next
191         calculation. That's a nonsense value, which would collapse the
192         barline so we return 0.0 in the next alternative.
193       */
194       return gh_double2scm ((Staff_symbol_referencer::line_count (me) -1) * ss);
195     }
196   else
197     return gh_int2scm (0);
198 }