1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 1997--2020 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "bracket.hh"
22
23 #include "axis-group-interface.hh"
24 #include "directional-element-interface.hh"
25 #include "grob.hh"
26 #include "lookup.hh"
27 #include "output-def.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "spanner.hh"
30 #include "item.hh"
31 #include "line-interface.hh"
32
33 using std::vector;
34
35 /*
36 should move to lookup?
37
38 TODO: this will fail for very short (shorter than the flare)
39 brackets.
40 */
41 Stencil
make_bracket(Grob * me,Axis protrusion_axis,Offset dz,Drul_array<Real> height,Interval gap,Drul_array<Real> flare,Drul_array<Real> shorten)42 Bracket::make_bracket (Grob *me, // for line properties.
43 Axis protrusion_axis, Offset dz,
44 Drul_array<Real> height, Interval gap,
45 Drul_array<Real> flare, Drul_array<Real> shorten)
46 {
47 Drul_array<Offset> corners (Offset (0, 0), dz);
48
49 Real length = dz.length ();
50 Drul_array<Offset> gap_corners;
51
52 Axis bracket_axis = other_axis (protrusion_axis);
53
54 Drul_array<Offset> straight_corners = corners;
55
56 for (LEFT_and_RIGHT (d))
57 straight_corners[d] += -d * shorten[d] / length * dz;
58
59 if (!gap.is_empty ())
60 {
61 for (LEFT_and_RIGHT (d))
62 gap_corners[d] = (dz * 0.5) + gap[d] / length * dz;
63 }
64
65 Drul_array<Offset> flare_corners = straight_corners;
66 for (LEFT_and_RIGHT (d))
67 {
68 flare_corners[d][bracket_axis] = straight_corners[d][bracket_axis];
69 flare_corners[d][protrusion_axis] += height[d];
70 straight_corners[d][bracket_axis] += -d * flare[d];
71 }
72
73 Stencil m;
74 if (!gap.is_empty ())
75 for (LEFT_and_RIGHT (d))
76 m.add_stencil (Line_interface::line (me, straight_corners[d],
77 gap_corners[d]));
78 else
79 m.add_stencil (Line_interface::line (me, straight_corners[LEFT],
80 straight_corners[RIGHT]));
81
82 if (scm_is_eq (get_property (me, "style"), ly_symbol2scm ("dashed-line"))
83 && !from_scm<bool> (get_property (me, "dashed-edge")))
84 set_property (me, "style", ly_symbol2scm ("line"));
85 for (LEFT_and_RIGHT (d))
86 m.add_stencil (Line_interface::line (me, straight_corners[d],
87 flare_corners[d]));
88 return m;
89 }
90
91 /*
92 Return a bracket oriented along either the X- or Y-axis. Passing
93 Interval () for gap creates an unbroken bracket.
94 */
95 Stencil
make_axis_constrained_bracket(Grob * me,Real length,Axis a,Direction dir,Interval gap)96 Bracket::make_axis_constrained_bracket (Grob *me, Real length, Axis a,
97 Direction dir, Interval gap)
98 {
99 Drul_array<Real> edge_height = from_scm (get_property (me, "edge-height"),
100 Drul_array<Real> (1.0, 1.0));
101 Drul_array<Real> flare = from_scm (get_property (me, "bracket-flare"),
102 Drul_array<Real> (0.0, 0.0));
103 Drul_array<Real> shorten = from_scm (get_property (me, "shorten-pair"),
104 Drul_array<Real> (0.0, 0.0));
105
106 // Make sure that it points in the correct direction:
107 scale_drul (&edge_height, static_cast<Real> (-dir));
108
109 Offset start;
110 start[a] = length;
111
112 Drul_array<bool> connect_to_other
113 = from_scm (get_property (me, "connect-to-neighbor"),
114 Drul_array<bool> (false, false));
115
116 for (LEFT_and_RIGHT (d))
117 {
118 if (connect_to_other[d])
119 {
120 edge_height[d] = 0.0;
121 flare[d] = 0.0;
122 shorten[d] = 0.0;
123 }
124 }
125
126 return make_bracket (me, other_axis (a), start, edge_height,
127 gap, flare, shorten);
128 }
129
130 /*
131 Return an axis-constrained, ungapped bracket which encloses a group of
132 grobs. Used for analysis brackets (HorizontalBracket) and
133 figured bass (BassFigureBracket).
134 */
135 Stencil
make_enclosing_bracket(Grob * me,Grob * refpoint,vector<Grob * > grobs,Axis a,Direction dir)136 Bracket::make_enclosing_bracket (Grob *me, Grob *refpoint,
137 vector<Grob *> grobs, Axis a,
138 Direction dir)
139 {
140 Grob *common = common_refpoint_of_array (grobs, refpoint, a);
141 Interval ext = Axis_group_interface::relative_group_extent (grobs, common, a);
142
143 if (ext.is_empty ())
144 {
145 me->programming_error ("Can't enclose empty extents with bracket");
146 return Stencil ();
147 }
148 else
149 {
150 Stencil b = make_axis_constrained_bracket (me, ext.length (), a, dir, Interval ());
151 b.translate_axis (ext[LEFT] - refpoint->relative_coordinate (common, a), a);
152 return b;
153 }
154 }
155