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