]> git.donarmstrong.com Git - lilypond.git/blob - lily/ottava-bracket.cc
release commit
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8  */
9
10 #include "stencil.hh"
11 #include "text-item.hh"
12 #include "line-spanner.hh"
13 #include "spanner.hh"
14 #include "font-interface.hh"
15 #include "dimensions.hh"
16 #include "output-def.hh"
17 #include "warn.hh"
18 #include "paper-column.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "note-column.hh"
21 #include "directional-element-interface.hh"
22 #include "tuplet-bracket.hh"
23 #include "rhythmic-head.hh"
24
25 struct Ottava_bracket
26 {
27   DECLARE_SCHEME_CALLBACK (print, (SCM));
28   static bool has_interface (Grob*);
29 };
30
31
32 /*
33   TODO: the string for ottava shoudl depend on the available space, ie.
34   
35   Long: 15ma        Short: 15ma    Empty: 15
36          8va                8va            8
37          8va bassa          8ba            8
38
39 */
40 MAKE_SCHEME_CALLBACK (Ottava_bracket, print, 1);
41 SCM
42 Ottava_bracket::print (SCM smob)
43 {
44   Spanner*me  = dynamic_cast<Spanner*> (unsmob_grob (smob));
45   Interval span_points;
46   
47   Grob *common = me->get_bound (LEFT)->common_refpoint (me->get_bound (RIGHT), X_AXIS);
48   Output_def * paper = me->get_paper ();
49   
50   Drul_array<bool> broken;
51   Direction d = LEFT;
52   do
53     {
54       Item *b = me->get_bound (d);
55       broken[d] = (b->break_status_dir () != CENTER);
56
57       if (Note_column::has_interface (b))
58         {
59           SCM heads = b->get_property ("note-heads");
60           common = common_refpoint_of_list (heads, common, X_AXIS);
61           for (SCM s = heads; scm_is_pair (s); s =ly_cdr (s))
62             {
63               Grob * h = unsmob_grob (ly_car (s));
64               Grob * dots = Rhythmic_head::get_dots (h);
65               if (dots)
66                 common = dots->common_refpoint (common, X_AXIS);
67             }
68         }
69     }
70   while (flip (&d) != LEFT);
71
72   SCM properties = Font_interface::text_font_alist_chain (me);
73   SCM markup = me->get_property ("text");
74   Stencil text;
75   if (Text_interface::markup_p (markup)) 
76     text = *unsmob_stencil (Text_interface::interpret_markup (paper->self_scm (), properties, markup));
77
78
79   Drul_array<Real> shorten = robust_scm2interval (me->get_property ("shorten-pair"),
80                                                   Interval (0,0));
81
82
83   /*
84     TODO: we should check if there are ledgers, and modify length of
85     the spanner to that.
86    */
87   do
88     {
89       Item *b = me->get_bound (d);
90
91       Interval ext;
92       if (Note_column::has_interface (b))
93         {
94           for (SCM s = b->get_property ("note-heads"); scm_is_pair (s); s =ly_cdr (s))
95             {
96               Grob * h = unsmob_grob (ly_car (s));
97               ext.unite (h->extent (common, X_AXIS));
98               Grob * dots = Rhythmic_head::get_dots (h);
99
100               if (dots && d == RIGHT)
101                 {
102                   ext.unite (dots->extent (common, X_AXIS));  
103                 }
104             }
105         }
106       
107       if (ext.is_empty ())
108         {
109           ext = robust_relative_extent (b, common, X_AXIS);
110         }
111
112       if (broken[d])
113         {
114           span_points[d] = b->extent (common, X_AXIS)[RIGHT];
115           shorten[d] = 0.;
116         }
117             
118       else
119         span_points[d] = ext[d];
120     }
121   while (flip (&d) != LEFT);
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] = span_points[LEFT]
131     <? (span_points[RIGHT] - text_size
132         - robust_scm2double (me->get_property ("minimum-length"), -1.0)); 
133   
134   Interval bracket_span_points = span_points;
135   bracket_span_points[LEFT] += text_size;
136   
137   Drul_array<Real> edge_height = robust_scm2interval (me->get_property ("edge-height"),
138                                                       Interval (1.0, 1.0));
139
140   
141   Drul_array<Real> flare = robust_scm2interval (me->get_property ("bracket-flare"),
142                                                 Interval (0,0));
143
144
145
146   edge_height[LEFT] = 0.0;
147   edge_height[RIGHT] *=  - get_grob_direction (me);
148   if (broken[RIGHT])
149     edge_height[RIGHT] = 0.0;
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
190 ADD_INTERFACE (Ottava_bracket, "ottava-bracket-interface",
191                "An ottava bracket",
192                "edge-height bracket-flare shorten-pair minimum-length");
193