]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
release: 1.3.153
[lilypond.git] / lily / system-start-delimiter.cc
1 /*   
2   system-start-delimiter.cc --  implement System_start_delimiter
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <math.h>
10
11 #include "axis-group-interface.hh"
12 #include "system-start-delimiter.hh"
13 #include "paper-def.hh"
14 #include "molecule.hh"
15 #include "font-interface.hh"
16 #include "all-font-metrics.hh"
17 #include "grob.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "lookup.hh"
20
21 Molecule
22 System_start_delimiter::staff_bracket (Grob*me,Real height)  
23 {
24   Real arc_height = gh_scm2double (me->get_grob_property ("arch-height")) ;
25   
26   SCM at = gh_list (ly_symbol2scm ("bracket"),
27                     me->get_grob_property ("arch-angle"),
28                     me->get_grob_property ("arch-width"),
29                     gh_double2scm (arc_height),
30                     gh_double2scm (height),
31                     me->get_grob_property ("arch-thick"),
32                     me->get_grob_property ("bracket-thick"),
33                     SCM_UNDEFINED);
34
35 /*
36 TODO: sort this out.
37     
38 Another thing:
39 In system-start-delimiter.cc I see the line
40
41   Real h = height + 2 * arc_height;
42
43 But I really think that you mean
44
45  Real h = height + 2 * arc_width;
46
47 (arc_height changes the x-axis-size of arc ; arc_width changes the
48 y-axis-size)
49 Will not fix it since I'm not sure.
50   
51    */
52
53   Real h = height + 2 * arc_height;
54   Box b (Interval (0, 1.5), Interval (-h/2, h/2));
55   Molecule mol (b, at);
56   mol.align_to (X_AXIS, CENTER);
57   return mol;
58 }
59
60 void
61 System_start_delimiter::set_interface (Grob*me)
62 {
63   me->set_interface (ly_symbol2scm ("system-start-delimiter-interface"));
64 }
65
66 bool
67 System_start_delimiter::has_interface (Grob*me)
68 {
69   return  me->has_interface (ly_symbol2scm ("system-start-delimiter-interface"));
70 }
71
72 Molecule
73 System_start_delimiter::simple_bar (Grob*me,Real h) 
74 {
75   Real w = me->paper_l ()->get_var ("stafflinethickness") *
76     gh_scm2double (me->get_grob_property ("thickness"));
77   return Lookup::filledbox (Box (Interval (0,w), Interval (-h/2, h/2)));
78 }
79
80 MAKE_SCHEME_CALLBACK (System_start_delimiter,after_line_breaking,1);
81
82 SCM
83 System_start_delimiter::after_line_breaking (SCM smob)
84 {
85   try_collapse (unsmob_grob (smob));
86   return SCM_UNSPECIFIED;
87 }
88
89 void
90 System_start_delimiter::try_collapse (Grob*me)
91 {
92   SCM   gl = me->get_grob_property ("glyph");
93   
94   if (scm_ilength (me->get_grob_property ("elements")) <=  1 && gl == ly_symbol2scm ("bar-line"))
95     {
96       me->suicide ();
97     }
98   
99 }
100
101
102 MAKE_SCHEME_CALLBACK (System_start_delimiter,brew_molecule,1);
103
104 SCM
105 System_start_delimiter::brew_molecule (SCM smob)
106 {
107   Grob * me = unsmob_grob (smob);
108
109   SCM s = me->get_grob_property ("glyph");
110   if (!gh_symbol_p (s))
111     return SCM_EOL;
112   
113   SCM c = me->get_grob_property ((ly_symbol2string (s) + "-collapse-height")
114                                  .ch_C ());
115   
116   Real staff_space = Staff_symbol_referencer::staff_space (me);
117   Interval ext = ly_scm2interval (Axis_group_interface::group_extent_callback
118  (me->self_scm (), gh_int2scm (Y_AXIS)));
119   Real l = ext.length () / staff_space;
120   
121   if (ext.empty_b ()
122       || (gh_number_p (c) && l <= gh_scm2double (c)))
123     {
124       me->suicide ();
125       return SCM_EOL;
126     }
127
128   Molecule m;
129   if (s == ly_symbol2scm ("bracket"))
130     m = staff_bracket (me,l);
131   else if (s == ly_symbol2scm ("brace"))
132     m =  staff_brace (me,l);
133   else if (s == ly_symbol2scm ("bar-line"))
134     m = simple_bar (me,l);
135   
136   m.translate_axis (ext.center (), Y_AXIS);
137   return m.smobbed_copy ();
138 }
139
140 Molecule
141 System_start_delimiter::staff_brace (Grob*me, Real y)
142 {
143   Font_metric *fm = Font_interface::get_default_font (me);
144   SCM font_defaults = gh_cdr (scm_assoc (ly_symbol2scm ("font-defaults"),
145                                          me->paper_l ()->style_sheet_));
146
147   Box b;
148 #if 0
149   b = fm->get_char (0);
150   int count = fm->count () >? 2;
151 #else 
152   int count = 255;
153 #endif      
154   int lo = 0;
155   int hi = count;
156   int relative_size = 0;
157
158   /* do a binary search for each Y, not very efficient, but passable?  */
159   do
160   {
161     int cmp = (lo + hi) / 2;
162     b = fm->get_char (cmp);
163     if (b[Y_AXIS].empty_b () || b[Y_AXIS].length () > y)
164       hi = cmp;
165     else
166       lo = cmp;
167
168     if (lo == count - 1 && b[Y_AXIS].length () < y && relative_size < 10)
169       {
170         /*
171           ugh: 7
172           We have four fonts: feta-braces0-3.mf
173           
174           In the style-sheet, all paper relative sizes need to start
175           looking at the feta-braces0 font.
176           
177           The smallest paper size, feta11 or -3, has to make 5 steps
178           to get to feta26 or +2.  Only after that, from +3 to +8 are
179           the real bigger sizes, so worst case we need 11 steps to get
180           to the font we need. */
181         fm = Font_interface::get_font
182           (me,
183            gh_list (gh_list (gh_cons (ly_symbol2scm ("font-relative-size"),
184                                       gh_int2scm (++relative_size)),
185                              SCM_UNDEFINED),
186                     me->mutable_property_alist_,
187                     me->immutable_property_alist_,
188                     font_defaults,
189                     SCM_UNDEFINED));
190 #if 0
191         b = fm->get_char (0);
192         count = fm->count () >? 2;
193 #else
194         count = 255;
195 #endif      
196         lo = 0;
197         hi = count;
198       }
199   }
200   while (hi - lo > 1);
201
202   // uRGURGU, why doesn't the height calculation work out??
203   SCM weird = me->get_grob_property ("weird");
204   if (gh_number_p (weird))
205     lo += gh_scm2int (weird);
206   SCM at = gh_list (ly_symbol2scm ("char"), gh_int2scm (lo), SCM_UNDEFINED);
207   at = fontify_atom (fm, at);
208   
209   b = fm->get_char (lo);
210   b[X_AXIS] = Interval (0,0);
211
212   return Molecule (b, at);
213 }
214