]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
* scm/define-grobs.scm (all-grob-descriptions): new grob
[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@xs4all.nl>
7 */
8
9 #include "system-start-delimiter.hh"
10
11
12 #include "spanner.hh"
13 #include "axis-group-interface.hh"
14 #include "output-def.hh"
15 #include "font-interface.hh"
16 #include "all-font-metrics.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "lookup.hh"
19 #include "item.hh"
20 #include "line-interface.hh"
21
22 #include "pointer-group-interface.hh"
23
24 Stencil
25 System_start_delimiter::staff_bracket (Grob *me, Real height)
26 {
27   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
28                       ly_symbol2scm ("fetaMusic"));
29
30   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
31   Font_metric *fm = select_font (me->layout (), scm_list_n (alist, SCM_UNDEFINED));
32
33   Drul_array<Stencil> tips (fm->find_by_name ("brackettips.down"),
34                             fm->find_by_name ("brackettips.up"));
35
36   Real thickness = robust_scm2double (me->get_property ("thickness"), 0.25);
37
38   Real overlap = 0.1 * thickness;
39
40   Box box (Interval (0, thickness),
41            Interval (-1, 1)
42            * (height / 2 + overlap));
43   
44   Stencil bracket = Lookup::filled_box (box);
45   Direction d = DOWN;
46   do
47     bracket.add_at_edge (Y_AXIS, d, tips[d], -overlap, 0.0);
48   while (flip (&d) != DOWN);
49   bracket = Stencil (box, bracket.expr ());
50
51   bracket.translate_axis (-0.8, X_AXIS);
52   
53   return bracket;
54 }
55
56 Stencil
57 System_start_delimiter::line_bracket (Grob *me, Real height)
58 {
59   height -= 1.0;
60
61   Real thick
62     = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"))
63     * robust_scm2double (me->get_property ("thickness"), 1);
64   Real w = 0.8;
65   
66   Stencil tip1 = Line_interface::make_line (thick,
67                                            Offset (0, -height/2),
68                                            Offset (w, -height/2));
69   Stencil tip2 = Line_interface::make_line (thick,
70                                             Offset (0, height/2),
71                                             Offset (w, height/2));
72   Stencil vline = Line_interface::make_line (thick,
73                                              Offset (0, -height/2),
74                                              Offset (0, height/2));
75
76   vline.add_stencil (tip1);
77   vline.add_stencil (tip2);
78   vline.translate_axis (-w, X_AXIS);
79   return vline;
80 }
81
82 Stencil
83 System_start_delimiter::simple_bar (Grob *me, Real h)
84 {
85   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
86   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
87   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)),
88                                    lt);
89 }
90
91 MAKE_SCHEME_CALLBACK (System_start_delimiter, print, 1);
92 SCM
93 System_start_delimiter::print (SCM smob)
94 {
95   Spanner *me = unsmob_spanner (smob);
96   extract_grob_set (me, "elements", elts);
97   Grob *common = common_refpoint_of_array (elts, me, Y_AXIS);
98
99   Interval ext;
100   int non_empty_count = 0;
101   for (int i = elts.size (); i--;)
102     {
103       Spanner *sp = dynamic_cast<Spanner *> (elts[i]);
104
105       if (sp
106           && sp->get_bound (LEFT) == me->get_bound (LEFT))
107         {
108           Interval dims = sp->extent (common, Y_AXIS);
109           if (!dims.is_empty ())
110             {
111               non_empty_count ++;
112               ext.unite (dims);
113             }
114         }
115     }
116
117   SCM glyph_sym = me->get_property ("style");
118   Real len = ext.length ();
119   if (ext.is_empty ()
120       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= ext.length ())
121       || (glyph_sym == ly_symbol2scm ("bar-line")
122           && non_empty_count <= 1))
123     {
124       me->suicide ();
125       return SCM_UNSPECIFIED;
126     }
127
128   Stencil m;
129   if (glyph_sym == ly_symbol2scm ("bracket"))
130     m = staff_bracket (me, len);
131   else if (glyph_sym == ly_symbol2scm ("brace"))
132     m = staff_brace (me, len);
133   else if (glyph_sym == ly_symbol2scm ("bar-line"))
134     m = simple_bar (me, len);
135
136   m.translate_axis (ext.center (), Y_AXIS);
137   return m.smobbed_copy ();
138 }
139
140 Stencil
141 System_start_delimiter::staff_brace (Grob *me, Real y)
142 {
143   Font_metric *fm = 0;
144   /* We go through the style sheet to lookup the font file
145      name.  This is better than using find_font directly,
146      esp. because that triggers mktextfm for non-existent
147 >     fonts. */
148   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
149                       ly_symbol2scm ("fetaBraces"));
150
151   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
152   fm = select_font (me->layout (), scm_list_n (alist, SCM_UNDEFINED));
153
154   int lo = 0;
155   int hi = max (fm->count () - 1, 2);
156
157   /* do a binary search for each Y, not very efficient, but passable?  */
158   Box b;
159   do
160     {
161       int cmp = (lo + hi) / 2;
162       b = fm->get_indexed_char (cmp);
163       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
164         hi = cmp;
165       else
166         lo = cmp;
167     }
168   while (hi - lo > 1);
169
170   Stencil stil (fm->find_by_name ("brace" + to_string (lo)));
171   stil.translate_axis (-b[X_AXIS].length()/2, X_AXIS);
172
173   stil.translate_axis (-0.2, X_AXIS);
174   
175   return stil;
176 }
177
178 ADD_INTERFACE (System_start_delimiter, "system-start-delimiter-interface",
179                "The brace, bracket or bar in front of the system. "
180                ,
181
182                /* properties */
183                "collapse-height "
184                "styles "
185                "staff-hierarchy "
186                "thickness "
187                );