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