]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-spanner.cc
* scm/define-grobs.scm: uniform naming for definitions and output
[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--2003 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 = 0.0;
50   SCM itp= me->get_grob_property ("if-text-padding");
51   if (gh_number_p (itp))
52     padding = gh_scm2double (itp);
53   
54   Real broken_left =  spanner->get_broken_left_end_align ();
55   Real width = spanner->spanner_length ();
56   width -= broken_left;
57
58   Drul_array<bool> broken;
59   Drul_array<Real> extra_off;
60   Direction d = LEFT;
61   do
62     {
63       extra_off [d]=0;
64       Item *b = spanner->get_bound (d);
65       broken[d] = b->break_status_dir () != CENTER;
66
67       if (!broken [d])
68         {
69
70           Interval e = b->extent (b, X_AXIS);
71           Real r = 0.0;
72           if (!e.empty_b ())
73             r = e[-d] + padding;
74
75           /* Text spanners such as ottava, should span from outer limits of
76              noteheads, iso (de)cresc. spanners that span the inner space */
77           if (me->get_grob_property ("enclose-bounds") != SCM_EOL)
78             {
79               width -= d * r;
80             }
81           else
82             {
83               width += d * r;
84               extra_off[d] = r;
85             }
86         }
87     }
88   while (flip (&d) != LEFT);
89
90   // FIXME: ecs tells us -- only for (de)cresc. spanners
91   width += gh_scm2double (me->get_grob_property ("width-correct"));
92   /* /Ugh */
93
94   // who is ecs? --hwn
95
96   SCM properties = Font_interface::font_alist_chain (me);
97
98   SCM edge_text = me->get_grob_property ("edge-text");
99   Drul_array<Molecule> edge;
100   if (gh_pair_p (edge_text))
101     {
102       Direction d = LEFT;
103       do
104         {
105           /*  Don't repeat edge text for broken end */
106           if (!broken[d])
107             {
108               SCM text = index_get_cell (edge_text, d);
109               edge[d] = Text_item::text2molecule (me, text, properties);
110               if (!edge[d].empty_b ())
111                 edge[d].align_to (Y_AXIS, CENTER);
112             }
113         }
114       while (flip (&d) != LEFT);
115     }
116   width -= edge[LEFT].extent (X_AXIS).length ()
117     + edge[RIGHT].extent (X_AXIS).length ();
118
119   Drul_array<Real> shorten;
120   shorten[LEFT] = 0;
121   shorten[RIGHT] = 0;
122
123   SCM s = me->get_grob_property ("shorten-pair");
124   if (gh_pair_p (s))
125     {
126       shorten[LEFT] = gh_scm2double (ly_car (s));
127       shorten[RIGHT] = gh_scm2double (ly_cdr (s));
128     }
129
130   width -= shorten[LEFT] + shorten[RIGHT];
131   
132   if (width < 0)
133     {
134       me->warning (_ ("Text_spanner too small"));
135       width = 0;
136     }
137
138   /* ugh */
139   
140   Real thick = me->get_paper ()->get_var ("linethickness");  
141   SCM st = me->get_grob_property ("thickness");
142   if (gh_number_p (st))
143     {
144       thick *=  gh_scm2double (st);
145
146     }
147   Molecule line = Line_spanner::line_molecule (me, thick, width, 0);
148   
149   Drul_array<Molecule> edge_line;
150   s = me->get_grob_property ("edge-height");
151   SCM ew = me->get_grob_property ("edge-widen");
152   if (gh_pair_p (s))
153     {
154       Direction d = LEFT;
155       int dir = to_dir (me->get_grob_property ("direction"));
156       do
157         {
158           Real dx = ( gh_pair_p (ew)  ? 
159                       gh_scm2double (index_get_cell (ew, d)) * d :  
160                       0 );
161           Real dy = gh_scm2double (index_get_cell (s, d)) * - dir;
162           if (dy)
163             {
164               edge_line[d] = Line_spanner::line_molecule (me, thick, dx, dy);
165             }
166         }
167       while (flip (&d) != LEFT);
168     }
169   
170   Molecule m;
171   if (!edge[LEFT].empty_b ())
172     m = edge[LEFT];
173
174   if (!edge_line[LEFT].empty_b ())
175     m.add_at_edge (X_AXIS, RIGHT, edge_line[LEFT], 0,0);
176   if (!line.empty_b ())
177     m.add_at_edge (X_AXIS, RIGHT, line,
178                    edge_line[LEFT].empty_b () ? 0 : -thick/2, 0);
179   if (!edge_line[RIGHT].empty_b ())
180     m.add_at_edge (X_AXIS, RIGHT, edge_line[RIGHT], -thick/2, 0);
181   if (!edge[RIGHT].empty_b ())
182     m.add_at_edge (X_AXIS, RIGHT, edge[RIGHT], 0, 0);
183   m.translate_axis (broken_left + extra_off[LEFT] + shorten[LEFT], X_AXIS);
184
185   return m.smobbed_copy ();
186 }
187
188
189
190
191 /* 
192    Piano pedal brackets are a special case of a text spanner.
193    Pedal up-down (restart) indicated by the angled right and left edges 
194    of consecutive pedals touching exactly to form an __/\__
195    Chris Jackson <chris@fluffhouse.org.uk>
196 */
197
198 void 
199 Text_spanner::setup_pedal_bracket(Spanner *me)
200 {
201
202   Real thick = me->get_paper ()->get_var ("linethickness");  
203   SCM st = me->get_grob_property ("thickness");
204   if (gh_number_p (st))
205     {
206       thick *=  gh_scm2double (st);
207     }  
208
209   Drul_array<bool> broken;
210   Drul_array<Real> height, width, shorten, r;
211
212   SCM pa = me->get_grob_property ("if-text-padding");
213   SCM ew = me->get_grob_property ("edge-widen");
214   SCM eh = me->get_grob_property ("edge-height");
215   SCM sp = me->get_grob_property ("shorten-pair");
216   
217   Direction d = LEFT;
218   Interval e;
219   Real padding = 0;
220
221   if (gh_number_p (pa) )
222     padding = gh_scm2double (pa);
223
224   do
225     {
226       Item *b = me->get_bound (d);
227
228       e = b->extent (b, X_AXIS);
229       if (!e.empty_b ())
230         r[d] = d * (e[-d] + padding);
231
232       broken[d] = b->break_status_dir () != CENTER;
233       width[d]  =  0;
234       height[d] =  0;
235       shorten[d] = 0;
236       if ( ly_number_pair_p (ew) )
237         width[d] +=  gh_scm2double (index_get_cell (ew, d));
238       if ( !broken[d] && (ly_number_pair_p (eh) ) )
239         height[d] += gh_scm2double (index_get_cell (eh, d));
240       if ( ly_number_pair_p (sp) )
241         shorten[d] +=  gh_scm2double (index_get_cell (sp, d));
242     }
243   while (flip (&d) != LEFT);
244   
245   Real extra_short = 0;
246   // For 'Mixed' style pedals, i.e.  a bracket preceded by text:  Ped._____|
247   // need to shorten by the extent of the text grob
248   if ( to_boolean (me->get_grob_property ("text-start")) )
249     {
250       height[LEFT] = 0;
251       extra_short = padding;
252       if (Grob *textbit = unsmob_grob (me->get_grob_property("pedal-text")))
253         {
254           if (textbit->internal_has_interface(ly_symbol2scm("text-interface")))
255             // for plain text, e.g., Sost. Ped.
256             {
257               SCM text  =  textbit->get_grob_property("text"); 
258               if (gh_string_p (text)) {
259                 SCM properties = Font_interface::font_alist_chain (me);
260                 Molecule mol = Text_item::text2molecule (me, text, properties);
261                 extra_short += mol.extent(X_AXIS).length() / 2;
262               }
263             }
264         }
265       shorten[RIGHT] -= thick;
266     }
267
268   shorten[LEFT] += extra_short ;
269   
270   if (broken[LEFT])
271     {
272       shorten[LEFT]  -=  me->get_broken_left_end_align () ;
273       shorten[RIGHT]  +=  abs(width[RIGHT])  +  thick  -  r[RIGHT];
274     }
275
276   else
277     {
278       // Shorten a ____/ on the right so that it will touch an adjoining \___
279       shorten[RIGHT]  +=  abs(width[LEFT])  +  abs(width[RIGHT])  +  thick;
280       // Also shorten so that it ends just before the spanned note.
281       shorten[RIGHT]  -=  (r[LEFT]  +  r[RIGHT]);
282     }
283
284   me->set_grob_property ("edge-height", ly_interval2scm (height));
285   me->set_grob_property ("edge-widen",  ly_interval2scm(width));
286   me->set_grob_property ("shorten-pair", ly_interval2scm (shorten));
287 }
288
289
290 struct Pianopedal
291 {
292   static bool has_interface (Grob*);
293 };
294 ADD_INTERFACE (Pianopedal,"piano-pedal-interface",
295                "",
296                "pedal-type edge-widen edge-height shorten-pair text-start left-widen right-widen pedal-text");
297
298 ADD_INTERFACE (Text_spanner,"text-spanner-interface",
299                "generic text spanner",
300                "dash-period if-text-padding dash-length edge-height edge-widen edge-text shorten-pair type thickness enclose-bounds width-correct");
301