]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
* lily/system-start-delimiter.cc (line_bracket): don't shorten height.
[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   Real thick
60     = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"))
61     * robust_scm2double (me->get_property ("thickness"), 1);
62   Real w = 0.8;
63   
64   Stencil tip1 = Line_interface::make_line (thick,
65                                            Offset (0, -height/2),
66                                            Offset (w, -height/2));
67   Stencil tip2 = Line_interface::make_line (thick,
68                                             Offset (0, height/2),
69                                             Offset (w, height/2));
70   Stencil vline = Line_interface::make_line (thick,
71                                              Offset (0, -height/2),
72                                              Offset (0, height/2));
73
74   vline.add_stencil (tip1);
75   vline.add_stencil (tip2);
76   vline.translate_axis (-w, X_AXIS);
77   return vline;
78 }
79
80 Stencil
81 System_start_delimiter::simple_bar (Grob *me, Real h)
82 {
83   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
84   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
85   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)),
86                                    lt);
87 }
88
89 MAKE_SCHEME_CALLBACK (System_start_delimiter, print, 1);
90 SCM
91 System_start_delimiter::print (SCM smob)
92 {
93   Spanner *me = unsmob_spanner (smob);
94   extract_grob_set (me, "elements", elts);
95   Grob *common = common_refpoint_of_array (elts, me, Y_AXIS);
96
97   Interval ext;
98   int non_empty_count = 0;
99   for (int i = elts.size (); i--;)
100     {
101       Spanner *sp = dynamic_cast<Spanner *> (elts[i]);
102
103       if (sp
104           && sp->get_bound (LEFT) == me->get_bound (LEFT))
105         {
106           Interval dims = sp->extent (common, Y_AXIS);
107           if (!dims.is_empty ())
108             {
109               non_empty_count ++;
110               ext.unite (dims);
111             }
112         }
113     }
114
115   SCM glyph_sym = me->get_property ("style");
116   Real len = ext.length ();
117   if (ext.is_empty ()
118       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= ext.length ())
119       || (glyph_sym == ly_symbol2scm ("bar-line")
120           && non_empty_count <= 1))
121     {
122       me->suicide ();
123       return SCM_UNSPECIFIED;
124     }
125
126   Stencil m;
127   if (glyph_sym == ly_symbol2scm ("bracket"))
128     m = staff_bracket (me, len);
129   else if (glyph_sym == ly_symbol2scm ("brace"))
130     m = staff_brace (me, len);
131   else if (glyph_sym == ly_symbol2scm ("bar-line"))
132     m = simple_bar (me, len);
133
134   m.translate_axis (ext.center (), Y_AXIS);
135   return m.smobbed_copy ();
136 }
137
138 Stencil
139 System_start_delimiter::staff_brace (Grob *me, Real y)
140 {
141   Font_metric *fm = 0;
142   /* We go through the style sheet to lookup the font file
143      name.  This is better than using find_font directly,
144      esp. because that triggers mktextfm for non-existent
145 >     fonts. */
146   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
147                       ly_symbol2scm ("fetaBraces"));
148
149   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
150   fm = select_font (me->layout (), scm_list_n (alist, SCM_UNDEFINED));
151
152   int lo = 0;
153   int hi = max (fm->count () - 1, 2);
154
155   /* do a binary search for each Y, not very efficient, but passable?  */
156   Box b;
157   do
158     {
159       int cmp = (lo + hi) / 2;
160       b = fm->get_indexed_char (cmp);
161       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
162         hi = cmp;
163       else
164         lo = cmp;
165     }
166   while (hi - lo > 1);
167
168   Stencil stil (fm->find_by_name ("brace" + to_string (lo)));
169   stil.translate_axis (-b[X_AXIS].length()/2, X_AXIS);
170
171   stil.translate_axis (-0.2, X_AXIS);
172   
173   return stil;
174 }
175
176 ADD_INTERFACE (System_start_delimiter, "system-start-delimiter-interface",
177                "The brace, bracket or bar in front of the system. "
178                ,
179
180                /* properties */
181                "collapse-height "
182                "style "
183                "staff-hierarchy "
184                "thickness "
185                );