]> git.donarmstrong.com Git - lilypond.git/blob - lily/ottava-bracket.cc
* scm/lily.scm (postscript->png): use ~a iso. ~s
[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           Real x = b->relative_coordinate (common, X_AXIS);
102           ext = Interval (x,x);
103         }
104       span_points[d] =  (broken [d]) ? b->extent (common, X_AXIS)[-d] : ext[d];
105
106       if (broken[d])
107         shorten [d] = 0.0; 
108     }
109   while (flip (&d) != LEFT);
110
111
112   /*
113     0.3 is ~ italic correction. 
114    */
115   Real text_size =  text.extent (X_AXIS).is_empty ()
116     ? 0.0 : text.extent (X_AXIS)[RIGHT] +  0.3;
117   
118   span_points[LEFT] = span_points[LEFT]
119     <? (span_points[RIGHT] - text_size
120         - robust_scm2double (me->get_property ("minimum-length"), -1.0)); 
121   
122   Interval bracket_span_points = span_points;
123   bracket_span_points[LEFT] += text_size;
124   
125   Drul_array<Real> edge_height = robust_scm2interval (me->get_property ("edge-height"),
126                                                       Interval (1.0, 1.0));
127
128   
129   Drul_array<Real> flare = robust_scm2interval (me->get_property ("bracket-flare"),
130                                                 Interval (0,0));
131
132
133
134   edge_height[LEFT] = 0.0;
135   edge_height[RIGHT] *=  - get_grob_direction (me);
136   if (broken[RIGHT])
137     edge_height[RIGHT] = 0.0;
138   
139   Stencil b;
140   Interval empty;
141   if (!bracket_span_points.is_empty () && bracket_span_points.length () > 0.001)
142     b = Tuplet_bracket::make_bracket (me,
143                                       Y_AXIS, Offset (bracket_span_points.length (), 0),
144                                        edge_height,
145                                       empty,
146                                       flare, shorten);
147
148   /*
149     The vertical lines should not take space, for the following scenario:
150
151     8 -----+
152         o  |
153        |
154        |
155        
156
157     Just a small amount, yes.  In tight situations, it is even
158     possible to center the `8' directly below the note, dropping the
159     ottava line completely...
160
161   */
162   
163   b = Stencil (Box (b.extent (X_AXIS),
164                      Interval (0.1,0.1)),
165                 b.expr ());
166   
167   b.translate_axis (bracket_span_points[LEFT], X_AXIS);
168   text.translate_axis (span_points[LEFT], X_AXIS);
169   text.align_to (Y_AXIS, CENTER);
170   b.add_stencil (text);
171   
172   b.translate_axis (- me->relative_coordinate (common, X_AXIS), X_AXIS);
173   
174   return b.smobbed_copy ();  
175 }
176
177
178 ADD_INTERFACE (Ottava_bracket, "ottava-bracket-interface",
179                "An ottava bracket",
180                "edge-height bracket-flare shorten-pair minimum-length");
181