]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
* lily/system-start-delimiter.cc (staff_bracket): use glyphs.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "system-start-delimiter.hh"
10
11 #include <math.h>
12
13 #include "spanner.hh"
14 #include "axis-group-interface.hh"
15 #include "output-def.hh"
16 #include "font-interface.hh"
17 #include "all-font-metrics.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "lookup.hh"
20 #include "item.hh"
21
22 Stencil
23 System_start_delimiter::old_staff_bracket (Grob *me, Real height)
24 {
25   Real arc_height = scm_to_double (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::staff_bracket (Grob *me, Real height)
65 {
66   Font_metric *fm = Font_interface::get_default_font (me);
67   Drul_array<Stencil> tips (fm->find_by_name ("brackettips.down"),
68                             fm->find_by_name ("brackettips.up"));
69
70   Real thickness = robust_scm2double (me->get_property ("thickness"), 0.25);
71
72   
73   Stencil bracket = Lookup::filled_box (Box (Interval (0, thickness),
74                                              Interval (-height/2, height/2)));
75
76   Direction d = DOWN;
77   do
78     {
79       bracket.add_at_edge (Y_AXIS, d, tips[d], 0.0, 0.0);
80     }
81   while (flip (&d) != DOWN); 
82
83   bracket.translate_axis (-1.0, X_AXIS); // ugh.
84   return bracket;
85 }
86
87
88 Stencil
89 System_start_delimiter::simple_bar (Grob *me, Real h)
90 {
91   Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
92   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
93   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)),
94                                    lt);
95 }
96
97 MAKE_SCHEME_CALLBACK (System_start_delimiter, after_line_breaking, 1);
98
99 SCM
100 System_start_delimiter::after_line_breaking (SCM smob)
101 {
102   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
103   
104   SCM gl = me->get_property ("glyph");
105   if (ly_c_equal_p (gl, scm_makfrom0str ("bar-line")))
106     {
107       int count = 0;
108       Paper_column *left_column = me->get_bound (LEFT)->get_column ();  
109
110       /*
111         Get all coordinates, to trigger Hara kiri.
112       */
113       SCM elts = me->get_property ("elements");
114       Grob *common = common_refpoint_of_list (elts, me, Y_AXIS);
115       for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
116         {
117           Spanner *staff = dynamic_cast<Spanner*> (unsmob_grob (scm_car (s)));
118           if (!staff || 
119               staff->get_bound (LEFT)->get_column () != left_column)
120             continue;
121           
122           Interval v = staff->extent (common, Y_AXIS);
123           
124           if (!v.is_empty ())
125             count++;
126         }
127
128       if (count <= 1)
129         {
130           me->suicide ();
131         }
132     }
133   return SCM_UNSPECIFIED;
134 }
135
136 MAKE_SCHEME_CALLBACK (System_start_delimiter, print, 1);
137 SCM
138 System_start_delimiter::print (SCM smob)
139 {
140   Spanner *me = unsmob_spanner (smob);
141   if (!me)
142     return SCM_EOL;
143
144   SCM s = me->get_property ("glyph");
145   if (!scm_is_string (s))
146     return SCM_EOL;
147   SCM gsym = scm_string_to_symbol (s);
148
149   Real staff_space = Staff_symbol_referencer::staff_space (me);
150
151   SCM elts = me->get_property ("elements");
152   Grob *common = common_refpoint_of_list (elts, me, Y_AXIS);
153
154   Interval ext;
155   for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
156     {
157       Spanner *sp = unsmob_spanner (scm_car (s));
158       if (sp
159           && sp->get_bound (LEFT) == me->get_bound (LEFT))
160         {
161           Interval dims = sp->extent (common, Y_AXIS);
162           if (!dims.is_empty ())
163             ext.unite (dims);
164         }
165     }
166
167   ext -= me->relative_coordinate (common, Y_AXIS);
168
169   Real len = ext.length () / staff_space;
170
171   if (ext.is_empty ()
172       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= len))
173     {
174       me->suicide ();
175       return SCM_EOL;
176     }
177
178   Stencil m;
179
180   if (gsym == ly_symbol2scm ("bracket"))
181     m = staff_bracket (me, len);
182   else if (gsym == ly_symbol2scm ("brace"))
183     m = staff_brace (me, len);
184   else if (gsym == ly_symbol2scm ("bar-line"))
185     m = simple_bar (me, len);
186
187   m.translate_axis (ext.center (), Y_AXIS);
188   return m.smobbed_copy ();
189 }
190
191 Stencil
192 System_start_delimiter::staff_brace (Grob *me, Real y)
193 {
194   Font_metric *fm = 0;
195   /* We go through the style sheet to lookup the font file
196      name.  This is better than using find_font directly,
197      esp. because that triggers mktextfm for non-existent
198      fonts. */
199   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
200                       ly_symbol2scm ("fetaBraces"));
201
202   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
203   fm = select_font (me->get_layout (), scm_list_n (alist, SCM_UNDEFINED));
204
205   int lo = 0;
206   int hi = max (fm->count () - 1,2);
207   Box b;
208
209   /* do a binary search for each Y, not very efficient, but passable?  */
210   do
211     {
212       int cmp = (lo + hi) / 2;
213       b = fm->get_indexed_char (cmp);
214       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
215         hi = cmp;
216       else
217         lo = cmp;
218     }
219   while (hi - lo > 1);
220
221   Stencil stil (fm->find_by_name ("brace" + to_string (lo)));
222   b = stil.extent_box ();
223   b[X_AXIS] = Interval (0, 0);
224
225   return Stencil (b, stil.expr ());
226 }
227
228 ADD_INTERFACE (System_start_delimiter, "system-start-delimiter-interface",
229                "The brace, bracket or bar in front of the system. "
230                "It is implemented as a spanner.",
231                "collapse-height thickness "
232                "arch-height arch-angle arch-thick arch-width bracket-thick glyph");