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