]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-spanner.cc
6457d433c30aa346a8240bfed2d6d95f0f990b8f
[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-spanner
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->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 ("stafflinethickness");  
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 ("stafflinethickness");  
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> w, 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   SCM wl = me->get_grob_property ("left-widen");
213   SCM wr = me->get_grob_property ("right-widen");
214
215   // Pedal has an angled left edge \__  or an angled right edge __/ 
216   w[LEFT] = w[RIGHT] = false;
217   if (gh_boolean_p (wl) )  
218     w[LEFT]   = to_boolean (wl);
219   if (gh_boolean_p (wr) )  
220     w[RIGHT]  = to_boolean (wr);
221   
222   Direction d = LEFT;
223   Interval e;
224   Real padding = 0;
225
226   if (gh_number_p (pa) )
227     padding = gh_scm2double (pa);
228
229   do {
230     Item *b = me->get_bound (d);
231
232     e = b->extent (b, X_AXIS);
233     if (!e.empty_b ())
234       r[d] = d * (e[-d] + padding);
235
236     broken[d] = b->break_status_dir () != CENTER;
237     width[d]  =  0;
238     height[d] =  0;
239     shorten[d] = 0;
240     if ( w[d] && gh_pair_p (ew) )
241       width[d] +=  gh_scm2double (index_cell (ew, d)) * d;
242     if ( !broken[d] && (gh_pair_p (eh) ) )
243       height[d] = gh_scm2double (index_cell (eh, d));
244     if ( gh_pair_p (sp) )
245       shorten[d] =  gh_scm2double (index_cell (sp, d));
246   }
247   while (flip (&d) != LEFT);
248   
249   Real extra_short = 0;
250   // For 'Mixed' style pedals, i.e.  a bracket preceded by text:  Ped._____|
251   // need to shorten by the extent of the text grob
252   if ( to_boolean (me->get_grob_property ("text-start")) )
253     {
254       height[LEFT] = 0;
255       Grob * textbit = me->get_parent(Y_AXIS);
256       extra_short = padding;
257       if (textbit->has_interface(ly_symbol2scm("piano-pedal-interface")))
258         // for pretty Ped. scripts. 
259         {
260           e = textbit->extent(textbit, Y_AXIS);
261           extra_short += e.length();
262         }
263       if (textbit->has_interface(ly_symbol2scm("text-interface"))) 
264         // for plain text, e.g., Sost. Ped.
265         {
266           SCM text  =  textbit->get_grob_property("text"); 
267           if (gh_string_p (text)) {
268             SCM properties = Font_interface::font_alist_chain (me);
269             Molecule mol = Text_item::text2molecule (me, text, properties);
270             extra_short += mol.extent(X_AXIS).length() / 2;
271           }
272         }
273       shorten[RIGHT] -= thick;
274     }
275
276   // Shorten a \____ on the left so that it will touch an adjoining ___/ 
277   shorten[LEFT] += abs(width[LEFT]) * 2   +  extra_short ;
278   
279   if (broken[LEFT]) {
280     shorten[LEFT] -= me->get_broken_left_end_align () ;
281     shorten[RIGHT] -= r[RIGHT];
282   }
283   else 
284     // Shorten bracket on the right so it ends just before the spanned note.
285     shorten[RIGHT]  +=  thick  -  (r[LEFT]  +  r[RIGHT]);
286
287   me->set_grob_property ("edge-height", gh_cons ( gh_double2scm ( height[LEFT] ) , 
288                                                   gh_double2scm ( height[RIGHT]) ) );
289   me->set_grob_property ("edge-width",  gh_cons ( gh_double2scm ( width[LEFT]  ), 
290                                                   gh_double2scm ( width[RIGHT] ) ));
291   me->set_grob_property ("shorten-pair", gh_cons ( gh_double2scm ( shorten[LEFT] ), 
292                                                    gh_double2scm ( shorten[RIGHT] ) ));
293 }