]> git.donarmstrong.com Git - lilypond.git/blob - lily/ottava-bracket.cc
* input/regression/markup-arrows.ly: new file.
[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--2005 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   static bool has_interface (Grob *);
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->get_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 (int 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 (int 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                 {
98                   ext.unite (dots->extent (common, X_AXIS));
99                 }
100             }
101         }
102
103       if (ext.is_empty ())
104         {
105           ext = robust_relative_extent (b, common, X_AXIS);
106         }
107
108       if (broken[d])
109         {
110           span_points[d] = b->extent (common, X_AXIS)[RIGHT];
111           shorten[d] = 0.;
112         }
113
114       else
115         span_points[d] = ext[d];
116     }
117   while (flip (&d) != LEFT);
118
119   /*
120     0.3 is ~ italic correction.
121   */
122   Real text_size = text.extent (X_AXIS).is_empty ()
123     ? 0.0 : text.extent (X_AXIS)[RIGHT] + 0.3;
124
125   span_points[LEFT] =
126     min (span_points[LEFT],
127          (span_points[RIGHT] - text_size
128           - robust_scm2double (me->get_property ("minimum-length"), -1.0)));
129
130   Interval bracket_span_points = span_points;
131   bracket_span_points[LEFT] += text_size;
132
133   Drul_array<Real> edge_height = robust_scm2interval (me->get_property ("edge-height"),
134                                                       Interval (1.0, 1.0));
135
136   Drul_array<Real> flare = robust_scm2interval (me->get_property ("bracket-flare"),
137                                                 Interval (0, 0));
138
139   edge_height[LEFT] = 0.0;
140   edge_height[RIGHT] *= -get_grob_direction (me);
141   if (broken[RIGHT])
142     edge_height[RIGHT] = 0.0;
143
144   Stencil b;
145   Interval empty;
146   if (!bracket_span_points.is_empty () && bracket_span_points.length () > 0.001)
147     b = Tuplet_bracket::make_bracket (me,
148                                       Y_AXIS, Offset (bracket_span_points.length (), 0),
149                                       edge_height,
150                                       empty,
151                                       flare, shorten);
152
153   /*
154     The vertical lines should not take space, for the following scenario:
155
156     8 -----+
157     o  |
158     |
159     |
160
161
162     Just a small amount, yes.  In tight situations, it is even
163     possible to center the `8' directly below the note, dropping the
164     ottava line completely...
165
166   */
167
168   b = Stencil (Box (b.extent (X_AXIS),
169                     Interval (0.1, 0.1)),
170                b.expr ());
171
172   b.translate_axis (bracket_span_points[LEFT], X_AXIS);
173   text.translate_axis (span_points[LEFT], X_AXIS);
174   text.align_to (Y_AXIS, CENTER);
175   b.add_stencil (text);
176
177   b.translate_axis (- me->relative_coordinate (common, X_AXIS), X_AXIS);
178
179   return b.smobbed_copy ();
180 }
181
182 ADD_INTERFACE (Ottava_bracket, "ottava-bracket-interface",
183                "An ottava bracket",
184                "edge-height bracket-flare shorten-pair minimum-length");
185