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