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