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