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