]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-spanner.cc
complete rewrite of multiplicity. This fixes 16th
[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 "warn.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   /* Ugh, must be same as Hairpin::brew_molecule.  */
49   Real padding = gh_scm2double (me->get_grob_property ("if-text-padding"));
50   Real broken_left =  spanner->get_broken_left_end_align ();
51   Real width = spanner->spanner_length ();
52   width -= broken_left;
53
54   Drul_array<bool> broken;
55   Drul_array<Real> extra_off;
56   Direction d = LEFT;
57   do
58     {
59       extra_off [d]=0;
60       Item *b = spanner->get_bound (d);
61       broken[d] = b->break_status_dir () != CENTER;
62
63       if (!broken [d])
64         {
65
66           Interval e = b->extent (b, X_AXIS);
67           Real r = 0.0;
68           if (!e.empty_b ())
69             r = e[-d] + padding;
70
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             {
75               width -= d * r;
76             }
77           else
78             {
79               width += d * r;
80               extra_off[d] = r;
81             }
82         }
83     }
84   while (flip (&d) != LEFT);
85
86   // FIXME: ecs tells us -- only for (de)cresc. spanners
87   width += gh_scm2double (me->get_grob_property ("width-correct"));
88   /* /Ugh */
89
90   // who is ecs? --hwn
91
92   SCM properties = Font_interface::font_alist_chain (me);
93
94   SCM edge_text = me->get_grob_property ("edge-text");
95   Drul_array<Molecule> edge;
96   if (gh_pair_p (edge_text))
97     {
98       Direction d = LEFT;
99       do
100         {
101           /*  Don't repeat edge text for broken end */
102           if (!broken[d])
103             {
104               SCM text = index_get_cell (edge_text, d);
105               edge[d] = Text_item::text2molecule (me, text, properties);
106               if (!edge[d].empty_b ())
107                 edge[d].align_to (Y_AXIS, CENTER);
108             }
109         }
110       while (flip (&d) != LEFT);
111     }
112   width -= edge[LEFT].extent (X_AXIS).length ()
113     + edge[RIGHT].extent (X_AXIS).length ();
114
115   Drul_array<Real> shorten;
116   shorten[LEFT] = 0;
117   shorten[RIGHT] = 0;
118
119   SCM s = me->get_grob_property ("shorten-pair");
120   if (gh_pair_p (s))
121     {
122       shorten[LEFT] = gh_scm2double (ly_car (s));
123       shorten[RIGHT] = gh_scm2double (ly_cdr (s));
124     }
125
126   width -= shorten[LEFT] + shorten[RIGHT];
127   
128   if (width < 0)
129     {
130       me->warning (_ ("Text_spanner too small"));
131       width = 0;
132     }
133
134   /* ugh */
135   
136   Real thick = me->paper_l ()->get_var ("linethickness");  
137   SCM st = me->get_grob_property ("thickness");
138   if (gh_number_p (st))
139     {
140       thick *=  gh_scm2double (st);
141
142     }
143   Molecule line = Line_spanner::line_molecule (me, thick, width, 0);
144   
145   Drul_array<Molecule> edge_line;
146   s = me->get_grob_property ("edge-height");
147   SCM ew = me->get_grob_property ("edge-widen");
148   if (gh_pair_p (s))
149     {
150       Direction d = LEFT;
151       int dir = to_dir (me->get_grob_property ("direction"));
152       do
153         {
154           Real dx = ( gh_pair_p (ew)  ? 
155                       gh_scm2double (index_get_cell (ew, d)) * d :  
156                       0 );
157           Real dy = gh_scm2double (index_get_cell (s, d)) * - dir;
158           if (dy)
159             {
160               edge_line[d] = Line_spanner::line_molecule (me, thick, dx, dy);
161             }
162         }
163       while (flip (&d) != LEFT);
164     }
165   
166   Molecule m;
167   if (!edge[LEFT].empty_b ())
168     m = edge[LEFT];
169
170   if (!edge_line[LEFT].empty_b ())
171     m.add_at_edge (X_AXIS, RIGHT, edge_line[LEFT], 0);
172   if (!line.empty_b ())
173     m.add_at_edge (X_AXIS, RIGHT, line,
174                    edge_line[LEFT].empty_b () ? 0 : -thick/2);
175   if (!edge_line[RIGHT].empty_b ())
176     m.add_at_edge (X_AXIS, RIGHT, edge_line[RIGHT], -thick/2);
177   if (!edge[RIGHT].empty_b ())
178     m.add_at_edge (X_AXIS, RIGHT, edge[RIGHT], 0);
179   m.translate_axis (broken_left + extra_off[LEFT] + shorten[LEFT], X_AXIS);
180
181   return m.smobbed_copy ();
182 }
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-widen");
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     {
222       Item *b = me->get_bound (d);
223
224       e = b->extent (b, X_AXIS);
225       if (!e.empty_b ())
226         r[d] = d * (e[-d] + padding);
227
228       broken[d] = b->break_status_dir () != CENTER;
229       width[d]  =  0;
230       height[d] =  0;
231       shorten[d] = 0;
232       if ( ly_number_pair_p (ew) )
233         width[d] +=  gh_scm2double (index_get_cell (ew, d));
234       if ( !broken[d] && (ly_number_pair_p (eh) ) )
235         height[d] += gh_scm2double (index_get_cell (eh, d));
236       if ( ly_number_pair_p (sp) )
237         shorten[d] +=  gh_scm2double (index_get_cell (sp, d));
238     }
239   while (flip (&d) != LEFT);
240   
241   Real extra_short = 0;
242   // For 'Mixed' style pedals, i.e.  a bracket preceded by text:  Ped._____|
243   // need to shorten by the extent of the text grob
244   if ( to_boolean (me->get_grob_property ("text-start")) )
245     {
246       height[LEFT] = 0;
247       extra_short = padding;
248       if (Grob *textbit = unsmob_grob (me->get_grob_property("pedal-text")))
249         {
250           if (textbit->internal_has_interface(ly_symbol2scm("text-interface")))
251             // for plain text, e.g., Sost. Ped.
252             {
253               SCM text  =  textbit->get_grob_property("text"); 
254               if (gh_string_p (text)) {
255                 SCM properties = Font_interface::font_alist_chain (me);
256                 Molecule mol = Text_item::text2molecule (me, text, properties);
257                 extra_short += mol.extent(X_AXIS).length() / 2;
258               }
259             }
260         }
261       shorten[RIGHT] -= thick;
262     }
263
264   shorten[LEFT] += extra_short ;
265   
266   if (broken[LEFT])
267     {
268       shorten[LEFT]  -=  me->get_broken_left_end_align () ;
269       shorten[RIGHT]  +=  abs(width[RIGHT])  +  thick  -  r[RIGHT];
270     }
271
272   else
273     {
274       // Shorten a ____/ on the right so that it will touch an adjoining \___
275       shorten[RIGHT]  +=  abs(width[LEFT])  +  abs(width[RIGHT])  +  thick;
276       // Also shorten so that it ends just before the spanned note.
277       shorten[RIGHT]  -=  (r[LEFT]  +  r[RIGHT]);
278     }
279
280   me->set_grob_property ("edge-height", ly_interval2scm (height));
281   me->set_grob_property ("edge-widen",  ly_interval2scm(width));
282   me->set_grob_property ("shorten-pair", ly_interval2scm (shorten));
283 }
284
285
286 struct Pianopedal
287 {
288   static bool has_interface (Grob*);
289 };
290 ADD_INTERFACE (Pianopedal,"piano-pedal-interface",
291                "",
292                "pedal-type edge-widen edge-height shorten-pair text-start left-widen right-widen pedal-text");
293
294 ADD_INTERFACE (Text_spanner,"text-spanner-interface",
295                "generic text spanner",
296                "dash-period if-text-padding dash-length edge-height edge-widen edge-text shorten-pair type thickness outer width-correct");
297