]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-spanner.cc
2e4acbfa9ac51de05083e10ced2316719edc4ab8
[lilypond.git] / lily / text-spanner.cc
1 /*
2
3 text-spanner.cc -- implement Text_spanner
4
5 source file of the GNU LilyPond music typesetter
6
7 (c) 2000--2002 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "molecule.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 "paper-def.hh"
18 #include "debug.hh"
19 #include "paper-column.hh"
20 #include "staff-symbol-referencer.hh"
21
22 /*
23   TODO:
24   - vertical start / vertical end (fixme-name) |
25   - contination types (vert. star, vert. end)  |-> eat volta-bracket
26   - more styles
27   - more texts/positions
28 */
29
30 MAKE_SCHEME_CALLBACK (Text_spanner, brew_molecule, 1);
31
32 /*
33   TODO: this function is too long, too hairy.
34
35   TODO: document this. What the heck is happening here?
36 */
37 SCM
38 Text_spanner::brew_molecule (SCM smob) 
39 {
40   Grob *me= unsmob_grob (smob);
41   Spanner *spanner = dynamic_cast<Spanner*> (me);
42
43   if (spanner->internal_has_interface (ly_symbol2scm ("piano-pedal-interface")) ) 
44     {
45       setup_pedal_bracket(spanner);
46     }
47
48
49   /* Ugh, must be same as Hairpin::brew_molecule.  */
50   Real padding = gh_scm2double (me->get_grob_property ("if-text-padding"));
51   Real broken_left =  spanner->get_broken_left_end_align ();
52   Real width = spanner->spanner_length ();
53   width -= broken_left;
54
55   Drul_array<bool> broken;
56   Drul_array<Real> extra_off;
57   Direction d = LEFT;
58   do
59     {
60       extra_off [d]=0;
61       Item *b = spanner->get_bound (d);
62       broken[d] = b->break_status_dir () != CENTER;
63
64       if (!broken [d])
65         {
66
67           Interval e = b->extent (b, X_AXIS);
68           Real r = 0.0;
69           if (!e.empty_b ())
70             r = e[-d] + padding;
71           /* Text spanners such as ottava, should span from outer limits of
72              noteheads, iso (de)cresc. spanners that span the inner space */
73           if (me->get_grob_property ("outer") != SCM_EOL)
74             // r *= -1; // huh?
75             {
76               width -= d * r;
77             }
78           else
79             {
80               width += d * r;
81               extra_off[d] = r;
82             }
83         }
84     }
85   while (flip (&d) != LEFT);
86
87   // FIXME: ecs tells us -- only for (de)cresc. spanners
88   width += gh_scm2double (me->get_grob_property ("width-correct"));
89   /* /Ugh */
90
91   // who is ecs? --hwn
92
93   SCM properties = Font_interface::font_alist_chain (me);
94
95   SCM edge_text = me->get_grob_property ("edge-text");
96   Drul_array<Molecule> edge;
97   if (gh_pair_p (edge_text))
98     {
99       Direction d = LEFT;
100       do
101         {
102           /*  Don't repeat edge text for broken end */
103           if (!broken[d])
104             {
105               SCM text = index_cell (edge_text, d);
106               edge[d] = Text_item::text2molecule (me, text, properties);
107               if (!edge[d].empty_b ())
108                 edge[d].align_to (Y_AXIS, CENTER);
109             }
110         }
111       while (flip (&d) != LEFT);
112     }
113   width -= edge[LEFT].extent (X_AXIS).length ()
114     + edge[RIGHT].extent (X_AXIS).length ();
115
116   Drul_array<Real> shorten;
117   shorten[LEFT] = 0;
118   shorten[RIGHT] = 0;
119
120   SCM s = me->get_grob_property ("shorten-pair");
121   if (gh_pair_p (s))
122     {
123       shorten[LEFT] = gh_scm2double (ly_car (s));
124       shorten[RIGHT] = gh_scm2double (ly_cdr (s));
125     }
126
127   width -= shorten[LEFT] + shorten[RIGHT];
128   
129   if (width < 0)
130     {
131       me->warning (_ ("Text_spanner too small"));
132       width = 0;
133     }
134
135   /* ugh */
136   
137   Real thick = me->paper_l ()->get_var ("linethickness");  
138   SCM st = me->get_grob_property ("thickness");
139   if (gh_number_p (st))
140     {
141       thick *=  gh_scm2double (st);
142
143     }
144   Molecule line = Line_spanner::line_molecule (me, thick, width, 0);
145   
146   Drul_array<Molecule> edge_line;
147   s = me->get_grob_property ("edge-height");
148   SCM ew = me->get_grob_property ("edge-width");
149   if (gh_pair_p (s))
150     {
151       Direction d = LEFT;
152       int dir = to_dir (me->get_grob_property ("direction"));
153       do
154         {
155           Real dx = ( gh_pair_p (ew)  ? 
156                       gh_scm2double (index_cell (ew, d)) * - dir  :  
157                       0 );
158           Real dy = gh_scm2double (index_cell (s, d)) * - dir;
159           if (dy)
160             {
161               edge_line[d] = Line_spanner::line_molecule (me, thick, dx, dy);
162             }
163         }
164       while (flip (&d) != LEFT);
165     }
166   
167   Molecule m;
168   if (!edge[LEFT].empty_b ())
169     m = edge[LEFT];
170
171   if (!edge_line[LEFT].empty_b ())
172     m.add_at_edge (X_AXIS, RIGHT, edge_line[LEFT], 0);
173   if (!line.empty_b ())
174     m.add_at_edge (X_AXIS, RIGHT, line,
175                    edge_line[LEFT].empty_b () ? 0 : -thick/2);
176   if (!edge_line[RIGHT].empty_b ())
177     m.add_at_edge (X_AXIS, RIGHT, edge_line[RIGHT], -thick/2);
178   if (!edge[RIGHT].empty_b ())
179     m.add_at_edge (X_AXIS, RIGHT, edge[RIGHT], 0);
180   m.translate_axis (broken_left + extra_off[LEFT] + shorten[LEFT], X_AXIS);
181
182   return m.smobbed_copy ();
183 }
184
185
186
187 /* 
188    Piano pedal brackets are a special case of a text spanner.
189    Pedal up-down (restart) indicated by the angled right and left edges 
190    of consecutive pedals touching exactly to form an __/\__
191    Chris Jackson <chris@fluffhouse.org.uk>
192 */
193
194 void 
195 Text_spanner::setup_pedal_bracket(Spanner *me)
196 {
197
198   Real thick = me->paper_l ()->get_var ("linethickness");  
199   SCM st = me->get_grob_property ("thickness");
200   if (gh_number_p (st))
201     {
202       thick *=  gh_scm2double (st);
203     }  
204
205   Drul_array<bool> broken;
206   Drul_array<Real> height, width, shorten, r;
207
208   SCM pa = me->get_grob_property ("if-text-padding");
209   SCM ew = me->get_grob_property ("edge-width");
210   SCM eh = me->get_grob_property ("edge-height");
211   SCM sp = me->get_grob_property ("shorten-pair");
212   
213   Direction d = LEFT;
214   Interval e;
215   Real padding = 0;
216
217   if (gh_number_p (pa) )
218     padding = gh_scm2double (pa);
219
220   do {
221     Item *b = me->get_bound (d);
222
223     e = b->extent (b, X_AXIS);
224     if (!e.empty_b ())
225       r[d] = d * (e[-d] + padding);
226
227     broken[d] = b->break_status_dir () != CENTER;
228     width[d]  =  0;
229     height[d] =  0;
230     shorten[d] = 0;
231     if ( gh_pair_p (ew) )
232       width[d] +=  gh_scm2double (index_cell (ew, d)) * d;
233     if ( !broken[d] && (gh_pair_p (eh) ) )
234       height[d] += gh_scm2double (index_cell (eh, d));
235     if ( gh_pair_p (sp) )
236       shorten[d] +=  gh_scm2double (index_cell (sp, d));
237   }
238   while (flip (&d) != LEFT);
239   
240   Real extra_short = 0;
241   // For 'Mixed' style pedals, i.e.  a bracket preceded by text:  Ped._____|
242   // need to shorten by the extent of the text grob
243   if ( to_boolean (me->get_grob_property ("text-start")) )
244     {
245       height[LEFT] = 0;
246       Grob * textbit = me->get_parent(Y_AXIS);
247       extra_short = padding;
248       if (textbit->internal_has_interface(ly_symbol2scm("text-interface"))) 
249         // for plain text, e.g., Sost. Ped.
250         {
251           SCM text  =  textbit->get_grob_property("text"); 
252           if (gh_string_p (text)) {
253             SCM properties = Font_interface::font_alist_chain (me);
254             Molecule mol = Text_item::text2molecule (me, text, properties);
255             extra_short += mol.extent(X_AXIS).length() / 2;
256           }
257         }
258       shorten[RIGHT] -= thick;
259     }
260
261   shorten[LEFT] += extra_short ;
262   
263   if (broken[LEFT]) {
264     shorten[LEFT]  -=  me->get_broken_left_end_align () ;
265     shorten[RIGHT]  +=  abs(width[RIGHT])  +  thick  -  r[RIGHT];
266   }
267
268   else {
269     // Shorten a ____/ on the right so that it will touch an adjoining \___
270     shorten[RIGHT]  +=  abs(width[LEFT])  +  abs(width[RIGHT])  +  thick;
271     // Also shorten so that it ends just before the spanned note.
272     shorten[RIGHT]  -=  (r[LEFT]  +  r[RIGHT]);
273   }
274
275   me->set_grob_property ("edge-height", gh_cons ( gh_double2scm ( height[LEFT] ) , 
276                                                   gh_double2scm ( height[RIGHT]) ) );
277   me->set_grob_property ("edge-width",  gh_cons ( gh_double2scm ( width[LEFT]  ), 
278                                                   gh_double2scm ( width[RIGHT] ) ));
279   me->set_grob_property ("shorten-pair", gh_cons ( gh_double2scm ( shorten[LEFT] ), 
280                                                    gh_double2scm ( shorten[RIGHT] ) ));
281 }
282
283
284 struct Pianopedal
285 {
286     static bool has_interface (Grob*);
287 };
288 ADD_INTERFACE (Pianopedal,"piano-pedal-interface",
289   "",
290   "pedal-type edge-width edge-height shorten-pair text-start left-widen right-widen");
291
292 ADD_INTERFACE (Text_spanner,"text-spanner-interface",
293   "generic text spanner",
294   "dash-period if-text-padding dash-length edge-height edge-width edge-text shorten-pair type");
295