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