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