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