]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
Nitpick run.
[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 #include "pointer-group-interface.hh"
23
24 Stencil
25 System_start_delimiter::staff_bracket (Grob *me, Real height)
26 {
27   Font_metric *fm = Font_interface::get_default_font (me);
28   Drul_array<Stencil> tips (fm->find_by_name ("brackettips.down"),
29                             fm->find_by_name ("brackettips.up"));
30
31   Real thickness = robust_scm2double (me->get_property ("thickness"), 0.25);
32
33   Real overlap = 0.1 * thickness;
34
35   Stencil bracket = Lookup::filled_box (Box (Interval (0, thickness),
36                                              Interval (-1, 1)
37                                              * (height / 2 + overlap)));
38
39   Direction d = DOWN;
40   do
41     bracket.add_at_edge (Y_AXIS, d, tips[d], -overlap, 0.0);
42   while (flip (&d) != DOWN)
43     ;
44
45   return bracket;
46 }
47
48 Stencil
49 System_start_delimiter::simple_bar (Grob *me, Real h)
50 {
51   Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
52   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
53   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)),
54                                    lt);
55 }
56
57 MAKE_SCHEME_CALLBACK (System_start_delimiter, after_line_breaking, 1);
58
59 SCM
60 System_start_delimiter::after_line_breaking (SCM smob)
61 {
62   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
63
64   SCM gl = me->get_property ("glyph");
65   if (ly_is_equal (gl, scm_makfrom0str ("bar-line")))
66     {
67       int count = 0;
68       Paper_column *left_column = me->get_bound (LEFT)->get_column ();
69
70 #if 1 /* slur-script.ly test */
71       /*
72         Get all coordinates, to trigger Hara kiri.
73       */
74       extract_grob_set (me, "elements", elts);
75       Grob *common = common_refpoint_of_array (elts, me, Y_AXIS);
76
77       for (int i = elts.size (); i--;)
78         {
79           Spanner *staff = dynamic_cast<Spanner *> (elts[i]);
80           if (!staff
81               || staff->get_bound (LEFT)->get_column () != left_column)
82             continue;
83
84           Interval v = staff->extent (common, Y_AXIS);
85
86           if (!v.is_empty ())
87             count++;
88         }
89
90       if (count <= 1)
91         me->suicide ();
92 #endif
93     }
94   return SCM_UNSPECIFIED;
95 }
96
97 MAKE_SCHEME_CALLBACK (System_start_delimiter, print, 1);
98 SCM
99 System_start_delimiter::print (SCM smob)
100 {
101   Spanner *me = unsmob_spanner (smob);
102   if (!me)
103     return SCM_EOL;
104
105   SCM s = me->get_property ("glyph");
106   if (!scm_is_string (s))
107     return SCM_EOL;
108   SCM gsym = scm_string_to_symbol (s);
109
110   Real staff_space = Staff_symbol_referencer::staff_space (me);
111
112   extract_grob_set (me, "elements", elts);
113   Grob *common = common_refpoint_of_array (elts, me, Y_AXIS);
114
115   Interval ext;
116
117   for (int i = elts.size (); i--;)
118     {
119       Spanner *sp = dynamic_cast<Spanner *> (elts[i]);
120       if (sp
121           && sp->get_bound (LEFT) == me->get_bound (LEFT))
122         {
123           Interval dims = sp->extent (common, Y_AXIS);
124           if (!dims.is_empty ())
125             ext.unite (dims);
126         }
127     }
128
129   ext -= me->relative_coordinate (common, Y_AXIS);
130
131   Real len = ext.length () / staff_space;
132
133   if (ext.is_empty ()
134       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= len))
135     {
136       me->suicide ();
137       return SCM_EOL;
138     }
139
140   Stencil m;
141
142   if (gsym == ly_symbol2scm ("bracket"))
143     m = staff_bracket (me, len);
144   else if (gsym == ly_symbol2scm ("brace"))
145     m = staff_brace (me, len);
146   else if (gsym == ly_symbol2scm ("bar-line"))
147     m = simple_bar (me, len);
148
149   m.translate_axis (ext.center (), Y_AXIS);
150   return m.smobbed_copy ();
151 }
152
153 Stencil
154 System_start_delimiter::staff_brace (Grob *me, Real y)
155 {
156   Font_metric *fm = 0;
157   /* We go through the style sheet to lookup the font file
158      name.  This is better than using find_font directly,
159      esp. because that triggers mktextfm for non-existent
160      fonts. */
161   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
162                       ly_symbol2scm ("fetaBraces"));
163
164   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
165   fm = select_font (me->get_layout (), scm_list_n (alist, SCM_UNDEFINED));
166
167   int lo = 0;
168   int hi = max (fm->count () - 1, 2);
169   Box b;
170
171   /* do a binary search for each Y, not very efficient, but passable?  */
172   do
173     {
174       int cmp = (lo + hi) / 2;
175       b = fm->get_indexed_char (cmp);
176       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
177         hi = cmp;
178       else
179         lo = cmp;
180     }
181   while (hi - lo > 1);
182
183   Stencil stil (fm->find_by_name ("brace" + to_string (lo)));
184   b = stil.extent_box ();
185   b[X_AXIS] = Interval (0, 0);
186
187   return Stencil (b, stil.expr ());
188 }
189
190 ADD_INTERFACE (System_start_delimiter, "system-start-delimiter-interface",
191                "The brace, bracket or bar in front of the system. "
192                "It is implemented as a spanner.",
193                "collapse-height thickness "
194                "glyph");