]> git.donarmstrong.com Git - lilypond.git/blob - lily/ottava-bracket.cc
(print): always start from right edge 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 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           common = common_refpoint_of_list (b->get_property ("heads"), common, X_AXIS);
60         }
61     }
62   while (flip (&d) != LEFT);
63
64   SCM properties = Font_interface::text_font_alist_chain (me);
65   SCM markup = me->get_property ("text");
66   Stencil text;
67   if (Text_item::markup_p (markup)) 
68     text = *unsmob_stencil (Text_item::interpret_markup (paper->self_scm (), properties, markup));
69
70
71   Drul_array<Real> shorten = robust_scm2interval (me->get_property ("shorten-pair"),
72                                                   Interval (0,0));
73
74
75   /*
76     TODO: we should check if there are ledgers, and modify length of
77     the spanner to that.
78    */
79   do
80     {
81       Item *b = me->get_bound (d);
82
83       Interval ext;
84       if (Note_column::has_interface (b))
85         {
86           for (SCM s = b->get_property ("note-heads"); ly_c_pair_p (s); s =ly_cdr (s))
87             {
88               Grob * h = unsmob_grob (ly_car (s));
89               ext.unite (h->extent (common, X_AXIS));
90               Grob * dots = Rhythmic_head::get_dots (h);
91
92               if (dots && d == RIGHT)
93                 {
94                   ext.unite (dots->extent (common, X_AXIS));  
95                 }
96             }
97         }
98       
99       if (ext.is_empty ())
100         {
101           ext = robust_relative_extent (b, common, X_AXIS);
102         }
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   /*
117     0.3 is ~ italic correction. 
118    */
119   Real text_size =  text.extent (X_AXIS).is_empty ()
120     ? 0.0 : text.extent (X_AXIS)[RIGHT] +  0.3;
121   
122   span_points[LEFT] = 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   
133   Drul_array<Real> flare = robust_scm2interval (me->get_property ("bracket-flare"),
134                                                 Interval (0,0));
135
136
137
138   edge_height[LEFT] = 0.0;
139   edge_height[RIGHT] *=  - get_grob_direction (me);
140   if (broken[RIGHT])
141     edge_height[RIGHT] = 0.0;
142   
143   Stencil b;
144   Interval empty;
145   if (!bracket_span_points.is_empty () && bracket_span_points.length () > 0.001)
146     b = Tuplet_bracket::make_bracket (me,
147                                       Y_AXIS, Offset (bracket_span_points.length (), 0),
148                                        edge_height,
149                                       empty,
150                                       flare, shorten);
151
152   /*
153     The vertical lines should not take space, for the following scenario:
154
155     8 -----+
156         o  |
157        |
158        |
159        
160
161     Just a small amount, yes.  In tight situations, it is even
162     possible to center the `8' directly below the note, dropping the
163     ottava line completely...
164
165   */
166   
167   b = Stencil (Box (b.extent (X_AXIS),
168                      Interval (0.1,0.1)),
169                 b.expr ());
170   
171   b.translate_axis (bracket_span_points[LEFT], X_AXIS);
172   text.translate_axis (span_points[LEFT], X_AXIS);
173   text.align_to (Y_AXIS, CENTER);
174   b.add_stencil (text);
175   
176   b.translate_axis (- me->relative_coordinate (common, X_AXIS), X_AXIS);
177   
178   return b.smobbed_copy ();  
179 }
180
181
182 ADD_INTERFACE (Ottava_bracket, "ottava-bracket-interface",
183                "An ottava bracket",
184                "edge-height bracket-flare shorten-pair minimum-length");
185