]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / lily / system-start-delimiter.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "system-start-delimiter.hh"
21 #include "text-interface.hh"
22 #include "all-font-metrics.hh"
23 #include "axis-group-interface.hh"
24 #include "font-interface.hh"
25 #include "item.hh"
26 #include "line-interface.hh"
27 #include "lookup.hh"
28 #include "output-def.hh"
29 #include "pointer-group-interface.hh"
30 #include "spanner.hh"
31 #include "staff-symbol-referencer.hh"
32
33 Stencil
34 System_start_delimiter::staff_bracket (Grob *me, Real height)
35 {
36   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
37                       ly_symbol2scm ("fetaMusic"));
38
39   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
40   Font_metric *fm = select_font (me->layout (), scm_list_n (alist, SCM_UNDEFINED));
41
42   Drul_array<Stencil> tips (fm->find_by_name ("brackettips.down"),
43                             fm->find_by_name ("brackettips.up"));
44
45   Real thickness = robust_scm2double (me->get_property ("thickness"), 0.25);
46
47   Real overlap = 0.1 * thickness;
48
49   Box box (Interval (0, thickness),
50            Interval (-1, 1)
51            * (height / 2 + overlap));
52   
53   Stencil bracket = Lookup::filled_box (box);
54   Direction d = DOWN;
55   do
56     bracket.add_at_edge (Y_AXIS, d, tips[d], -overlap);
57   while (flip (&d) != DOWN);
58   bracket = Stencil (box, bracket.expr ());
59
60   bracket.translate_axis (-0.8, X_AXIS);
61   
62   return bracket;
63 }
64
65 Stencil
66 System_start_delimiter::line_bracket (Grob *me, Real height)
67 {
68   Real thick
69     = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"))
70     * robust_scm2double (me->get_property ("thickness"), 1);
71   Real w = 0.8;
72   
73   Stencil tip1 = Line_interface::make_line (thick,
74                                            Offset (0, -height/2),
75                                            Offset (w, -height/2));
76   Stencil tip2 = Line_interface::make_line (thick,
77                                             Offset (0, height/2),
78                                             Offset (w, height/2));
79   Stencil vline = Line_interface::make_line (thick,
80                                              Offset (0, -height/2),
81                                              Offset (0, height/2));
82
83   vline.add_stencil (tip1);
84   vline.add_stencil (tip2);
85   vline.translate_axis (-w, X_AXIS);
86   return vline;
87 }
88
89 Stencil
90 System_start_delimiter::simple_bar (Grob *me, Real h)
91 {
92   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
93   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
94   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)),
95                                    lt);
96 }
97
98 MAKE_SCHEME_CALLBACK (System_start_delimiter, print, 1);
99 SCM
100 System_start_delimiter::print (SCM smob)
101 {
102   Spanner *me = unsmob_spanner (smob);
103   extract_grob_set (me, "elements", elts);
104   Grob *common = common_refpoint_of_array (elts, me, Y_AXIS);
105
106   Interval ext;
107   int non_empty_count = 0;
108   for (vsize i = elts.size (); i--;)
109     {
110       Spanner *sp = dynamic_cast<Spanner *> (elts[i]);
111
112       if (sp
113           && sp->get_bound (LEFT) == me->get_bound (LEFT))
114         {
115           Interval dims = sp->extent (common, Y_AXIS);
116           if (!dims.is_empty ())
117             {
118               non_empty_count ++;
119               ext.unite (dims);
120             }
121         }
122     }
123
124   SCM glyph_sym = me->get_property ("style");
125   Real len = ext.length ();
126   if (ext.is_empty ()
127       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= ext.length ()))
128     {
129       me->suicide ();
130       return SCM_UNSPECIFIED;
131     }
132
133   Stencil m;
134   if (glyph_sym == ly_symbol2scm ("bracket"))
135     m = staff_bracket (me, len);
136   else if (glyph_sym == ly_symbol2scm ("brace"))
137     m = staff_brace (me, len);
138   else if (glyph_sym == ly_symbol2scm ("bar-line"))
139     m = simple_bar (me, len);
140   else if (glyph_sym == ly_symbol2scm ("line-bracket"))
141     m = line_bracket (me, len);
142
143   m.translate_axis (ext.center (), Y_AXIS);
144   return m.smobbed_copy ();
145 }
146
147 Stencil
148 System_start_delimiter::staff_brace (Grob *me, Real y)
149 {
150   Font_metric *fm = 0;
151
152   /*
153     Find the default brace font if the user overrides it.
154   */
155   fm = Font_interface::get_default_font (me);
156
157   int
158     lo = 0;
159   int hi = max ((int) fm->count () - 1, 2);
160
161   /* do a binary search for each Y, not very efficient, but passable?  */
162   Box b;
163   do
164     {
165       int cmp = (lo + hi) / 2;
166       b = fm->get_indexed_char (cmp);
167       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
168         hi = cmp;
169       else
170         lo = cmp;
171     }
172   while (hi - lo > 1);
173
174   Stencil stil (fm->find_by_name ("brace" + to_string (lo)));
175   stil.translate_axis (-b[X_AXIS].length ()/2, X_AXIS);
176
177   stil.translate_axis (-0.2, X_AXIS);
178   
179   return stil;
180 }
181
182 ADD_INTERFACE (System_start_delimiter,
183                "The brace, bracket or bar in front of the system.  The"
184                " following values for @code{style} are recognized:\n"
185                "\n"
186                "@table @code\n"
187                "@item bracket\n"
188                "A thick bracket, normally used to group similar"
189                " instruments in a score.  Default for @code{StaffGroup}."
190                "  @code{SystemStartBracket} uses this style.\n"
191                "@item brace\n"
192                "A @q{piano style} brace normally used for an instrument"
193                " that uses two staves.  The default style for"
194                " @code{GrandStaff}.  @code{SystemStartBrace} uses this"
195                " style.\n"
196                "@item bar-line\n"
197                "A simple line between the staves in a score.  Default"
198                " for staves enclosed in @code{<<} and @code{>>}."
199                "  @code{SystemStartBar} uses this style.\n"
200                "@item line-bracket\n"
201                "A simple square, normally used for subgrouping"
202                " instruments in a score.  @code{SystemStartSquare} uses"
203                " this style.\n"
204                "@end table\n"
205                "\n"
206                "See also @file{input/regression/system-start-nesting.ly}.",
207
208                /* properties */
209                "collapse-height "
210                "style "
211                "thickness "
212                );