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