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