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