]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
Web-ja: update introduction
[lilypond.git] / lily / system-start-delimiter.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2015 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   Font_metric *fm = Font_interface::get_default_font (me);
37
38   Drul_array<Stencil> tips (fm->find_by_name ("brackettips.down"),
39                             fm->find_by_name ("brackettips.up"));
40
41   Real thickness = robust_scm2double (me->get_property ("thickness"), 0.25);
42
43   Real overlap = 0.1 * thickness;
44
45   Box box (Interval (0, thickness),
46            Interval (-1, 1)
47            * (height / 2 + overlap));
48
49   Stencil bracket = Lookup::filled_box (box);
50   for (DOWN_and_UP (d))
51     bracket.add_at_edge (Y_AXIS, d, tips[d], -overlap);
52   bracket = Stencil (box, bracket.expr ());
53
54   bracket.translate_axis (-0.8, X_AXIS);
55
56   return bracket;
57 }
58
59 Stencil
60 System_start_delimiter::line_bracket (Grob *me, Real height)
61 {
62   Real thick
63     = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"))
64       * robust_scm2double (me->get_property ("thickness"), 1);
65   Real w = 0.8;
66
67   Stencil tip1 = Line_interface::make_line (thick,
68                                             Offset (0, -height / 2),
69                                             Offset (w, -height / 2));
70   Stencil tip2 = Line_interface::make_line (thick,
71                                             Offset (0, height / 2),
72                                             Offset (w, height / 2));
73   Stencil vline = Line_interface::make_line (thick,
74                                              Offset (0, -height / 2),
75                                              Offset (0, height / 2));
76
77   vline.add_stencil (tip1);
78   vline.add_stencil (tip2);
79   vline.translate_axis (-w, X_AXIS);
80   return vline;
81 }
82
83 Stencil
84 System_start_delimiter::simple_bar (Grob *me, Real h)
85 {
86   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
87   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
88   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)),
89                                    lt);
90 }
91
92 MAKE_SCHEME_CALLBACK (System_start_delimiter, print, 1);
93 SCM
94 System_start_delimiter::print (SCM smob)
95 {
96   Spanner *me = unsmob<Spanner> (smob);
97   extract_grob_set (me, "elements", elts);
98   Grob *common = common_refpoint_of_array (elts, me, Y_AXIS);
99
100   Interval ext;
101   Real staffspace = 1.0;
102   int non_empty_count = 0;
103   for (vsize i = elts.size (); i--;)
104     {
105       Spanner *sp = dynamic_cast<Spanner *> (elts[i]);
106
107       if (sp
108           && sp->get_bound (LEFT) == me->get_bound (LEFT))
109         {
110           Interval dims = sp->extent (common, Y_AXIS);
111           if (!dims.is_empty ())
112             {
113               non_empty_count++;
114               ext.unite (dims);
115               staffspace = Staff_symbol_referencer::staff_space (sp);
116             }
117         }
118     }
119
120   SCM glyph_sym = me->get_property ("style");
121   Real len = ext.length ();
122
123   // Use collapse-height in multiples of the staff-space
124   if (ext.is_empty ()
125       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= (len / staffspace)))
126     {
127       me->suicide ();
128       return SCM_UNSPECIFIED;
129     }
130
131   Stencil m;
132   if (scm_is_eq (glyph_sym, ly_symbol2scm ("bracket")))
133     m = staff_bracket (me, len);
134   else if (scm_is_eq (glyph_sym, ly_symbol2scm ("brace")))
135     m = staff_brace (me, len);
136   else if (scm_is_eq (glyph_sym, ly_symbol2scm ("bar-line")))
137     m = simple_bar (me, len);
138   else if (scm_is_eq (glyph_sym, ly_symbol2scm ("line-bracket")))
139     m = line_bracket (me, len);
140
141   m.translate_axis (ext.center (), Y_AXIS);
142   return m.smobbed_copy ();
143 }
144
145 Stencil
146 System_start_delimiter::staff_brace (Grob *me, Real y)
147 {
148   Font_metric *fm = 0;
149
150   /*
151     Find the default brace font if the user overrides it.
152   */
153   fm = Font_interface::get_default_font (me);
154
155   int
156   lo = 0;
157   int hi = max ((int) fm->count () - 1, 2);
158
159   /* do a binary search for each Y, not very efficient, but passable?  */
160   Box b;
161   do
162     {
163       int cmp = (lo + hi) / 2;
164       b = fm->get_indexed_char_dimensions (cmp);
165       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
166         hi = cmp;
167       else
168         lo = cmp;
169     }
170   while (hi - lo > 1);
171
172   Stencil stil (fm->find_by_name ("brace" + ::to_string (lo)));
173   stil.translate_axis (-b[X_AXIS].length () / 2, X_AXIS);
174
175   stil.translate_axis (-0.2, X_AXIS);
176
177   return stil;
178 }
179
180 ADD_INTERFACE (System_start_delimiter,
181                "The brace, bracket or bar in front of the system.  The"
182                " following values for @code{style} are recognized:\n"
183                "\n"
184                "@table @code\n"
185                "@item bracket\n"
186                "A thick bracket, normally used to group similar"
187                " instruments in a score.  Default for @code{StaffGroup}."
188                "  @code{SystemStartBracket} uses this style.\n"
189                "@item brace\n"
190                "A @q{piano style} brace normally used for an instrument"
191                " that uses two staves.  The default style for"
192                " @code{GrandStaff}.  @code{SystemStartBrace} uses this"
193                " style.\n"
194                "@item bar-line\n"
195                "A simple line between the staves in a score.  Default"
196                " for staves enclosed in @code{<<} and @code{>>}."
197                "  @code{SystemStartBar} uses this style.\n"
198                "@item line-bracket\n"
199                "A simple square, normally used for subgrouping"
200                " instruments in a score.  @code{SystemStartSquare} uses"
201                " this style.\n"
202                "@end table\n"
203                "\n"
204                "See also @file{input/regression/system-start-nesting.ly}.",
205
206                /* properties */
207                "collapse-height "
208                "style "
209                "thickness "
210               );