]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
* lily/include/lily-guile.hh: compatibility glue for 1.6
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <math.h>
10
11 #include "spanner.hh"
12 #include "axis-group-interface.hh"
13 #include "system-start-delimiter.hh"
14 #include "output-def.hh"
15 #include "stencil.hh"
16 #include "font-interface.hh"
17 #include "all-font-metrics.hh"
18 #include "grob.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "lookup.hh"
21
22 Stencil
23 System_start_delimiter::staff_bracket (Grob*me,Real height)  
24 {
25   Real arc_height = ly_scm2double (me->get_property ("arch-height")) ;
26   
27   SCM at = scm_list_n (ly_symbol2scm ("bracket"),
28                     me->get_property ("arch-angle"),
29                     me->get_property ("arch-width"),
30                     scm_make_real (arc_height),
31                     scm_make_real (height),
32                     me->get_property ("arch-thick"),
33                     me->get_property ("thickness"),
34                     SCM_UNDEFINED);
35
36 /*
37 TODO: sort this out.
38     
39 Another thing:
40 In system-start-delimiter.cc I see the line
41
42   Real h = height + 2 * arc_height;
43
44 But I really think that you mean
45
46  Real h = height + 2 * arc_width;
47
48 (arc_height changes the x-axis-size of arc ; arc_width changes the
49 y-axis-size)
50 Will not fix it since I'm not sure.
51   
52    */
53
54   Real h = height + 2 * arc_height;
55   Box b (Interval (0, 1.5), Interval (-h/2, h/2));
56   Stencil mol (b, at);
57   mol.align_to (X_AXIS, CENTER);
58   return mol;
59 }
60
61
62
63 Stencil
64 System_start_delimiter::simple_bar (Grob*me,Real h) 
65 {
66   Real lt =me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness")) ;
67   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
68   return Lookup::round_filled_box (Box (Interval (0,w), Interval (-h/2, h/2)),
69                                    lt);
70 }
71
72 MAKE_SCHEME_CALLBACK (System_start_delimiter,after_line_breaking,1);
73
74 SCM
75 System_start_delimiter::after_line_breaking (SCM smob)
76 {
77   Grob * me = unsmob_grob (smob);
78   SCM   gl = me->get_property ("glyph");
79   if (ly_c_equal_p (gl,scm_makfrom0str ("bar-line")))
80     {
81       int count = 0;
82
83       /*
84         Get all coordinates, to trigger Hara kiri. 
85       */
86       SCM elts = me->get_property ("elements");
87       Grob *common = common_refpoint_of_list (elts, me, Y_AXIS);
88       for (SCM s = elts; ly_c_pair_p (s); s = ly_cdr (s))
89         {
90           Interval v = unsmob_grob (ly_car (s))->extent (common, Y_AXIS);
91
92           if (!v.is_empty ())
93             count ++;
94         }
95   
96   
97       if (count <=  1)
98         {
99           me->suicide ();
100         }
101     }
102   return SCM_UNSPECIFIED;
103 }
104
105
106 MAKE_SCHEME_CALLBACK (System_start_delimiter,print,1);
107 SCM
108 System_start_delimiter::print (SCM smob)
109 {
110   Spanner * me = unsmob_spanner (smob);
111   if (!me)
112     return SCM_EOL;
113   
114   SCM s = me->get_property ("glyph");
115   if (!scm_is_string (s))
116     return SCM_EOL;
117   SCM gsym = scm_string_to_symbol (s) ;
118   
119   Real staff_space = Staff_symbol_referencer::staff_space (me);
120
121   SCM elts = me->get_property ("elements");
122   Grob * common = common_refpoint_of_list (elts, me, Y_AXIS);
123
124   Interval ext;
125   for (SCM s = elts; ly_c_pair_p (s); s = ly_cdr (s))
126     {
127       Spanner * sp = unsmob_spanner (ly_car (s));
128       if (sp &&
129           sp->get_bound (LEFT) == me->get_bound (LEFT))
130         {
131           Interval dims = sp->extent (common, Y_AXIS);
132           if (!dims.is_empty ())
133             ext.unite (dims);
134         }
135     }
136
137   ext -= me->relative_coordinate (common, Y_AXIS);
138   
139   Real l = ext.length () / staff_space;
140   
141   if (ext.is_empty ()
142       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= l))
143     {
144       me->suicide ();
145       return SCM_EOL;
146     }
147
148   Stencil m;
149
150   if (gsym== ly_symbol2scm ("bracket"))
151     m = staff_bracket (me,l);
152   else if (gsym == ly_symbol2scm ("brace"))
153     m =  staff_brace (me,l);
154   else if (gsym == ly_symbol2scm ("bar-line"))
155     m = simple_bar (me,l);
156   
157   m.translate_axis (ext.center (), Y_AXIS);
158   return m.smobbed_copy ();
159 }
160
161 Stencil
162 System_start_delimiter::staff_brace (Grob*me, Real y)
163 {
164   Font_metric *fm = 0;
165   
166   /* We go through the style sheet to lookup the font file
167      name.  This is better than using find_font directly,
168      esp. because that triggers mktextfm for non-existent
169      fonts. */
170   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"), ly_symbol2scm ("fetaBraces"));
171   
172   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
173   fm = select_font (me->get_paper (), scm_list_n (alist, SCM_UNDEFINED));
174   
175
176   int lo = 0;
177
178   int hi = (fm->count () - 1) >? 2;
179   Box b;
180
181   /* do a binary search for each Y, not very efficient, but passable?  */
182   do
183     {
184       int cmp = (lo + hi) / 2;
185       b = fm->get_indexed_char (cmp);
186       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
187         hi = cmp;
188       else
189         lo = cmp;
190     }
191   while (hi - lo > 1);
192
193   /* FIXME: ascii? */
194   Stencil stil (fm->get_indexed_char_stencil (lo));
195   b = stil.extent_box ();
196   b[X_AXIS] = Interval (0, 0);
197
198   return Stencil (b, stil.expr ());
199 }
200   
201
202
203
204 ADD_INTERFACE (System_start_delimiter,"system-start-delimiter-interface",
205                "The brace, bracket or bar in front of the system. "
206                "It is implemented as a spanner."
207                ,
208                "collapse-height thickness "
209                "arch-height arch-angle arch-thick arch-width bracket-thick glyph");