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