]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
release: 1.5.11
[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 ("thickness"),
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   Grob * me = unsmob_grob (smob);
86   SCM   gl = me->get_grob_property ("glyph");
87   
88   if (scm_ilength (me->get_grob_property ("elements")) <=  1 && gl == ly_symbol2scm ("bar-line"))
89     {
90       me->suicide ();
91     }
92
93   return SCM_UNSPECIFIED;
94 }
95
96
97 MAKE_SCHEME_CALLBACK (System_start_delimiter,brew_molecule,1);
98 SCM
99 System_start_delimiter::brew_molecule (SCM smob)
100 {
101   Grob * me = unsmob_grob (smob);
102
103   SCM s = me->get_grob_property ("glyph");
104   if (!gh_symbol_p (s))
105     return SCM_EOL;
106   
107   SCM c = me->get_grob_property ("collapse-height");
108   
109   Real staff_space = Staff_symbol_referencer::staff_space (me);
110   Interval ext = ly_scm2interval (Axis_group_interface::group_extent_callback
111  (me->self_scm (), gh_int2scm (Y_AXIS)));
112   Real l = ext.length () / staff_space;
113   
114   if (ext.empty_b ()
115       || (gh_number_p (c) && l <= gh_scm2double (c)))
116     {
117       me->suicide ();
118       return SCM_EOL;
119     }
120
121   Molecule m;
122   if (s == ly_symbol2scm ("bracket"))
123     m = staff_bracket (me,l);
124   else if (s == ly_symbol2scm ("brace"))
125     m =  staff_brace (me,l);
126   else if (s == ly_symbol2scm ("bar-line"))
127     m = simple_bar (me,l);
128   
129   m.translate_axis (ext.center (), Y_AXIS);
130   return m.smobbed_copy ();
131 }
132
133 Molecule
134 System_start_delimiter::staff_brace (Grob*me, Real y)
135 {
136   Font_metric *fm = 0;
137   
138   for (int i = 0; ; i++)
139     {
140       if (!fm || y > fm->get_char (fm->count ()-1)[Y_AXIS].length ())
141         {
142           /* We go through the style sheet to lookup the font file
143              name.  This is better than using find_font directly,
144              esp. because that triggers mktextfm for non-existent
145              fonts. */
146           SCM alist = gh_list (gh_cons (ly_symbol2scm ("font-family"),
147                                         ly_symbol2scm ("braces")),
148                                gh_cons (ly_symbol2scm ("font-relative-size"),
149                                         gh_int2scm (i)),
150                                SCM_UNDEFINED);
151           fm = Font_interface::get_font (me, gh_list (alist, SCM_UNDEFINED));
152           /* Hmm, if lookup fails, we get cmr10 anyway */
153           if (ly_scm2string (gh_car (fm->description_)) == "cmr10")
154             break;
155         }
156       else
157         break;
158     }
159
160   int lo = 0;
161
162   int hi = (fm->count () - 1) >? 2;
163   Box b;
164
165   /* do a binary search for each Y, not very efficient, but passable?  */
166   do
167     {
168       int cmp = (lo + hi) / 2;
169       b = fm->get_char (cmp);
170       if (b[Y_AXIS].empty_b () || b[Y_AXIS].length () > y)
171         hi = cmp;
172       else
173         lo = cmp;
174     }
175   while (hi - lo > 1);
176       
177   SCM at = gh_list (ly_symbol2scm ("char"), gh_int2scm (lo), SCM_UNDEFINED);
178   at = fontify_atom (fm, at);
179   
180   b = fm->get_char (lo);
181   b[X_AXIS] = Interval (0,0);
182
183   return Molecule (b, at);
184 }
185