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