]> git.donarmstrong.com Git - lilypond.git/blob - lily/ottava-bracket.cc
Issue 4974/1: Add output-attributes grob property
[lilypond.git] / lily / ottava-bracket.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--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 "axis-group-interface.hh"
21 #include "text-interface.hh"
22 #include "spanner.hh"
23 #include "font-interface.hh"
24 #include "dimensions.hh"
25 #include "output-def.hh"
26 #include "warn.hh"
27 #include "paper-column.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "note-column.hh"
30 #include "directional-element-interface.hh"
31 #include "tuplet-bracket.hh"
32 #include "rhythmic-head.hh"
33 #include "pointer-group-interface.hh"
34
35 struct Ottava_bracket
36 {
37   DECLARE_SCHEME_CALLBACK (print, (SCM));
38 };
39
40 /*
41   TODO: the string for ottava shoudl depend on the available space, ie.
42
43   Long: 15ma        Short: 15ma    Empty: 15
44   8va                8va            8
45   8va bassa          8ba            8
46 */
47 MAKE_SCHEME_CALLBACK (Ottava_bracket, print, 1);
48 SCM
49 Ottava_bracket::print (SCM smob)
50 {
51   Spanner *me = unsmob<Spanner> (smob);
52   Interval span_points;
53
54   Grob *common = me->get_bound (LEFT)->common_refpoint (me->get_bound (RIGHT), X_AXIS);
55   Output_def *layout = me->layout ();
56
57   Drul_array<bool> broken;
58   for (LEFT_and_RIGHT (d))
59     {
60       Item *b = me->get_bound (d);
61       broken[d] = (b->break_status_dir () != CENTER);
62
63       if (has_interface<Note_column> (b))
64         {
65           extract_grob_set (b, "note-heads", heads);
66           common = common_refpoint_of_array (heads, common, X_AXIS);
67           for (vsize i = 0; i < heads.size (); i++)
68             {
69               Grob *h = heads[i];
70               Grob *dots = Rhythmic_head::get_dots (h);
71               if (dots)
72                 common = dots->common_refpoint (common, X_AXIS);
73             }
74         }
75     }
76
77   SCM properties = Font_interface::text_font_alist_chain (me);
78   SCM markup = me->get_property ("text");
79   Stencil text;
80   if (Text_interface::is_markup (markup))
81     text = *unsmob<Stencil> (Text_interface::interpret_markup (layout->self_scm (),
82                                                               properties, markup));
83
84   Drul_array<Real> shorten = robust_scm2interval (me->get_property ("shorten-pair"),
85                                                   Interval (0, 0));
86
87   /*
88     TODO: we should check if there are ledgers, and modify length of
89     the spanner to that.
90   */
91   for (LEFT_and_RIGHT (d))
92     {
93       Item *b = me->get_bound (d);
94
95       Interval ext;
96       if (has_interface<Note_column> (b))
97         {
98           extract_grob_set (b, "note-heads", heads);
99           for (vsize i = 0; i < heads.size (); i++)
100             {
101               Grob *h = heads[i];
102               ext.unite (h->extent (common, X_AXIS));
103               Grob *dots = Rhythmic_head::get_dots (h);
104
105               if (dots && d == RIGHT)
106                 ext.unite (dots->extent (common, X_AXIS));
107             }
108         }
109
110       if (ext.is_empty ())
111         ext = robust_relative_extent (b, common, X_AXIS);
112
113       if (broken[d])
114         {
115           span_points[d] = Axis_group_interface::generic_bound_extent (b, common, X_AXIS)[RIGHT];
116           shorten[d] = 0.;
117         }
118
119       else
120         span_points[d] = ext[d];
121
122       span_points[d] -= d * shorten[d];
123     }
124
125   /*
126     0.3 is ~ italic correction.
127   */
128   Real text_size = text.extent (X_AXIS).is_empty ()
129                    ? 0.0 : text.extent (X_AXIS)[RIGHT] + 0.3;
130
131   span_points[LEFT]
132     = min (span_points[LEFT],
133            (span_points[RIGHT] - text_size
134             - robust_scm2double (me->get_property ("minimum-length"), -1.0)));
135
136   Interval bracket_span_points = span_points;
137   bracket_span_points[LEFT] += text_size;
138
139   Drul_array<Real> edge_height = robust_scm2interval (me->get_property ("edge-height"),
140                                                       Interval (1.0, 1.0));
141
142   Drul_array<Real> flare = robust_scm2interval (me->get_property ("bracket-flare"),
143                                                 Interval (0, 0));
144
145   for (LEFT_and_RIGHT (d))
146     {
147       edge_height[d] *= -get_grob_direction (me);
148       if (broken[d])
149         edge_height[d] = 0.0;
150     }
151
152   Stencil b;
153   Interval empty;
154   if (!bracket_span_points.is_empty () && bracket_span_points.length () > 0.001)
155     b = Tuplet_bracket::make_bracket (me,
156                                       Y_AXIS, Offset (bracket_span_points.length (), 0),
157                                       edge_height,
158                                       empty,
159                                       flare, Drul_array<Real> (0, 0));
160
161   /*
162    * The vertical lines should not take space, for the following scenario:
163    *
164    * 8 -----+
165    *     o  |
166    *    |
167    *    |
168    *
169    * Just a small amount, yes.  In tight situations, it is even
170    * possible to center the `8' directly below the note, dropping the
171    * ottava line completely...
172   */
173
174   b = Stencil (Box (b.extent (X_AXIS),
175                     Interval (0.1, 0.1)),
176                b.expr ());
177
178   b.translate_axis (bracket_span_points[LEFT], X_AXIS);
179   text.translate_axis (span_points[LEFT], X_AXIS);
180   text.align_to (Y_AXIS, CENTER);
181   b.add_stencil (text);
182
183   b.translate_axis (- me->relative_coordinate (common, X_AXIS), X_AXIS);
184
185   return b.smobbed_copy ();
186 }
187
188 ADD_INTERFACE (Ottava_bracket,
189                "An ottava bracket.",
190
191                /* properties */
192                "edge-height "
193                "bracket-flare "
194                "shorten-pair "
195                "minimum-length "
196               );
197