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