]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
new file, move from
[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 "axis-group-interface.hh"
12 #include "system-start-delimiter.hh"
13 #include "output-def.hh"
14 #include "stencil.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 Stencil
22 System_start_delimiter::staff_bracket (Grob*me,Real height)  
23 {
24   Real arc_height = ly_scm2double (me->get_property ("arch-height")) ;
25   
26   SCM at = scm_list_n (ly_symbol2scm ("bracket"),
27                     me->get_property ("arch-angle"),
28                     me->get_property ("arch-width"),
29                     scm_make_real (arc_height),
30                     scm_make_real (height),
31                     me->get_property ("arch-thick"),
32                     me->get_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   Stencil mol (b, at);
56   mol.align_to (X_AXIS, CENTER);
57   return mol;
58 }
59
60
61
62 Stencil
63 System_start_delimiter::simple_bar (Grob*me,Real h) 
64 {
65   Real lt =me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness")) ;
66   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
67   return Lookup::round_filled_box (Box (Interval (0,w), Interval (-h/2, h/2)),
68                                    lt);
69 }
70
71 MAKE_SCHEME_CALLBACK (System_start_delimiter,after_line_breaking,1);
72
73 SCM
74 System_start_delimiter::after_line_breaking (SCM smob)
75 {
76   Grob * me = unsmob_grob (smob);
77   SCM   gl = me->get_property ("glyph");
78   if (ly_c_equal_p (gl,scm_makfrom0str ("bar-line")))
79     {
80       int count = 0;
81
82       /*
83         Get all coordinates, to trigger Hara kiri. 
84       */
85       SCM elts = me->get_property ("elements");
86       Grob *common = common_refpoint_of_list (elts, me, Y_AXIS);
87       for (SCM s = elts; ly_c_pair_p (s); s = ly_cdr (s))
88         {
89           Interval v = unsmob_grob (ly_car (s))->extent (common, Y_AXIS);
90
91           if (!v.is_empty ())
92             count ++;
93         }
94   
95   
96       if (count <=  1)
97         {
98           me->suicide ();
99         }
100     }
101   return SCM_UNSPECIFIED;
102 }
103
104
105 MAKE_SCHEME_CALLBACK (System_start_delimiter,print,1);
106 SCM
107 System_start_delimiter::print (SCM smob)
108 {
109   Grob * me = unsmob_grob (smob);
110
111   SCM s = me->get_property ("glyph");
112   if (!ly_c_string_p (s))
113     return SCM_EOL;
114   SCM gsym = scm_string_to_symbol (s) ;
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 (), scm_int2num (Y_AXIS)));
119   Real l = ext.length () / staff_space;
120   
121   if (ext.is_empty ()
122       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= l))
123     {
124       me->suicide ();
125       return SCM_EOL;
126     }
127
128   Stencil m;
129
130   if (gsym== ly_symbol2scm ("bracket"))
131     m = staff_bracket (me,l);
132   else if (gsym == ly_symbol2scm ("brace"))
133     m =  staff_brace (me,l);
134   else if (gsym == ly_symbol2scm ("bar-line"))
135     m = simple_bar (me,l);
136   
137   m.translate_axis (ext.center (), Y_AXIS);
138   return m.smobbed_copy ();
139 }
140
141 Stencil
142 System_start_delimiter::staff_brace (Grob*me, Real y)
143 {
144   Font_metric *fm = 0;
145   
146   /* We go through the style sheet to lookup the font file
147      name.  This is better than using find_font directly,
148      esp. because that triggers mktextfm for non-existent
149      fonts. */
150   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"), ly_symbol2scm ("fetaBraces"));
151   
152   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
153   fm = select_font (me->get_paper (), scm_list_n (alist, SCM_UNDEFINED));
154   
155
156   int lo = 0;
157
158   int hi = (fm->count () - 1) >? 2;
159   Box b;
160
161   /* do a binary search for each Y, not very efficient, but passable?  */
162   do
163     {
164       int cmp = (lo + hi) / 2;
165       b = fm->get_indexed_char (cmp);
166       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
167         hi = cmp;
168       else
169         lo = cmp;
170     }
171   while (hi - lo > 1);
172
173   /* FIXME: ascii? */
174   Stencil stil (fm->get_indexed_char_stencil (lo));
175   b = stil.extent_box ();
176   b[X_AXIS] = Interval (0, 0);
177
178   return Stencil (b, stil.expr ());
179 }
180   
181
182
183
184 ADD_INTERFACE (System_start_delimiter,"system-start-delimiter-interface",
185                "The brace, bracket or bar in front of the system. "
186                "It is implemented as a spanner."
187                ,
188                "collapse-height thickness "
189                "arch-height arch-angle arch-thick arch-width bracket-thick glyph");