]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-element.cc
7bbf22594dcc22889d9bfe2a1cb727698277beab
[lilypond.git] / lily / score-element.cc
1 /*
2   score-elem.cc -- implement Score_element
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <string.h>
11
12 #include "group-interface.hh"
13 #include "misc.hh"
14 #include "paper-score.hh"
15 #include "paper-def.hh"
16 #include "lookup.hh"
17 #include "molecule.hh"
18 #include "score-element.hh"
19 #include "debug.hh"
20 #include "spanner.hh"
21 #include "line-of-score.hh"
22 #include "item.hh"
23 #include "paper-column.hh"
24 #include "molecule.hh"
25 #include "misc.hh"
26 #include "paper-outputter.hh"
27 #include "dimension-cache.hh"
28 #include "side-position-interface.hh"
29 #include "item.hh"
30
31 Score_element::Score_element()
32 {
33   output_p_ =0;
34   dim_cache_[X_AXIS] = new Dimension_cache;
35   dim_cache_[Y_AXIS] = new Dimension_cache;
36   dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
37   
38   used_b_ = false;
39
40   dim_cache_[X_AXIS]->set_callback (molecule_extent);
41   dim_cache_[Y_AXIS]->set_callback (molecule_extent); 
42   used_b_ = false;
43   pscore_l_=0;
44   lookup_l_ =0;
45   status_i_ = 0;
46   self_scm_ = SCM_EOL;
47   original_l_ = 0;
48   element_property_alist_ = SCM_EOL;
49
50   smobify_self ();
51
52
53   set_elt_property ("dependencies", SCM_EOL);
54 }
55
56 SCM ly_deep_copy (SCM);
57
58 SCM
59 ly_deep_copy (SCM l)
60 {
61   if (gh_pair_p (l))
62     {
63       return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
64     }
65   else
66     return l;
67 }
68
69
70 Score_element::Score_element (Score_element const&s)
71 {
72   dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
73   dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
74   dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
75   
76   self_scm_ = SCM_EOL;
77   used_b_ = true;
78   original_l_ =(Score_element*) &s;
79
80   /*
81     should protect because smobify_self () might trigger GC.
82    */
83   element_property_alist_ = scm_protect_object (ly_deep_copy (s.element_property_alist_));
84
85   output_p_ =0;
86   status_i_ = s.status_i_;
87   lookup_l_ = s.lookup_l_;
88   pscore_l_ = s.pscore_l_;
89
90   smobify_self ();
91 }
92
93 Score_element::~Score_element()
94 {
95   assert (!output_p_);
96   assert (status_i_ >=0);
97   status_i_  = -1;
98
99   delete dim_cache_[X_AXIS];
100   delete dim_cache_[Y_AXIS];  
101 }
102
103
104 Real
105 Score_element::get_real (String s) const
106 {
107   return gh_scm2double (get_elt_property (s));
108 }
109
110 void
111 Score_element::set_real (String s, Real r)
112 {
113   set_elt_property (s, gh_double2scm (r));
114 }
115
116 // should also have one that takes SCM arg. 
117 SCM
118 Score_element::get_elt_property (String nm) const
119 {
120   SCM sym =  ly_symbol2scm (nm.ch_C());
121   SCM s = scm_assq(sym, element_property_alist_);
122
123   if (s != SCM_BOOL_F)
124     return gh_cdr (s); 
125   
126   if (pscore_l_)
127     {
128       SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
129       SCM val;
130       
131       // should probably check for Type::sym as well.
132       Paper_def * p= pscore_l_->paper_l_;
133       if (p->default_properties_.try_retrieve (sym2, &val))
134         return val;
135       else if (p->default_properties_.try_retrieve (sym, &val))
136         return val;
137     }
138   
139   return SCM_UNDEFINED;
140 }
141
142 SCM
143 Score_element::remove_elt_property (String key)
144 {
145   SCM s = get_elt_property (key); 
146   SCM sym = ly_symbol2scm (key.ch_C());
147   element_property_alist_ =  scm_assq_remove_x (element_property_alist_, sym);
148   return s;
149 }
150
151 /*
152   UGH. assoc vs. assq
153  */
154 void
155 Score_element::set_elt_property (String k, SCM v)
156 {
157   SCM s = ly_symbol2scm (k.ch_C( ));
158   element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
159 }
160
161 Interval
162 Score_element::molecule_extent(Dimension_cache const *c)
163 {
164   Score_element *s = dynamic_cast<Score_element*>(c->element_l());
165   Molecule*m = s->do_brew_molecule_p();
166   return  m->extent()[c->axis ()];
167 }
168
169
170 void
171 Score_element::print() const
172 {
173 #ifndef NPRINT
174   DEBUG_OUT << classname(this) << "{\n";
175     
176   if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
177     ly_display_scm (element_property_alist_);
178
179   if (original_l_)
180     DEBUG_OUT << "Copy ";
181   do_print();
182   
183   DEBUG_OUT <<  "}\n";
184 #endif
185 }
186
187 Paper_def*
188 Score_element::paper_l ()  const
189 {
190  return pscore_l_ ? pscore_l_->paper_l_ : 0;
191 }
192
193 Lookup const *
194 Score_element::lookup_l () const
195 {
196   if (!lookup_l_)
197     {
198       Score_element * urg = (Score_element*)this;
199       SCM sz = urg->remove_elt_property ("fontsize");
200       int i = (gh_number_p (sz))
201         ? gh_scm2int  (sz)
202         : 0;
203
204       urg->lookup_l_ =  (Lookup*)pscore_l_->paper_l_->lookup_l (i);
205     }
206   return lookup_l_;
207 }
208
209 void
210 Score_element::add_processing()
211 {
212   assert (status_i_ >=0);
213   if (status_i_)
214     return;
215   status_i_ ++;
216
217 #if 0
218     /*
219     UGH. UGH. UGH.
220    */
221   if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
222       && !dim_cache_[X_AXIS]->off_callback_l_)
223     {
224       dim_cache_[X_AXIS]->off_callbacks_.push (Side_position_interface::self_alignment);
225     }
226   
227   if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
228       && !dim_cache_[X_AXIS]->off_callback_l_)
229       
230     {
231       dim_cache_[Y_AXIS]->set_offset_callback (Side_position_interface::self_alignment);
232     }
233 #endif
234   
235   do_add_processing();
236 }
237
238 void
239 Score_element::calculate_dependencies (int final, int busy,
240                                        Score_element_method_pointer funcptr)
241 {
242   assert (status_i_ >=0);
243
244   if (status_i_ >= final)
245     return;
246
247   assert (status_i_!= busy);
248   status_i_= busy;
249
250   Link_array<Score_element> dependency_arr =
251     Group_interface__extract_elements (this, (Score_element*)0, "dependencies");
252   
253   for (int i=0; i < dependency_arr.size(); i++)
254     dependency_arr[i]->calculate_dependencies (final, busy, funcptr);
255
256   Link_array<Score_element> extra (get_extra_dependencies());
257   for (int i=0; i < extra.size(); i++)
258     extra[i]->calculate_dependencies (final, busy, funcptr);
259   
260   (this->*funcptr)();
261   status_i_= final;
262 }
263
264 void
265 Score_element::output_processing () 
266 {
267   if (to_boolean  (get_elt_property ("transparent")))
268     return;
269
270   // we're being silly here. 
271   if (output_p_)
272     delete output_p_;
273   
274   output_p_ = do_brew_molecule_p ();
275   Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
276
277   SCM s = get_elt_property ("extra-offset");
278   if (gh_pair_p (s))
279     {
280       Real il = paper_l ()->get_var ("interline");
281       o[X_AXIS] += il * gh_scm2double (gh_car (s));
282       o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));      
283     }
284   
285   pscore_l_->outputter_l_->output_molecule (output_p_,
286                                             o,
287                                             classname(this));
288
289   delete output_p_;
290   output_p_ =0;
291 }
292
293 /*
294   
295   VIRTUAL STUBS
296
297  */
298 void
299 Score_element::do_break_processing()
300 {
301 }
302
303 void
304 Score_element::do_post_processing()
305 {
306 }
307
308 void
309 Score_element::do_breakable_col_processing()
310 {
311   handle_prebroken_dependencies();
312 }
313
314 void
315 Score_element::do_pre_processing()
316 {
317 }
318
319 void
320 Score_element::do_space_processing ()
321 {
322 }
323
324 void
325 Score_element::do_add_processing()
326 {
327 }
328
329
330
331 Molecule*
332 Score_element::do_brew_molecule_p() const
333 {
334   SCM glyph = get_elt_property ("glyph");
335   if (gh_string_p (glyph))
336     {
337       Molecule*output = new Molecule (lookup_l ()->afm_find (String (ly_scm2string (glyph))));
338       
339       return output;
340     }
341   else
342     {
343       Interval emp;
344       emp.set_empty ();
345       Molecule a (lookup_l ()->fill (Box (emp,emp)));
346       return new Molecule (a);
347     }
348 }
349
350
351 Line_of_score *
352 Score_element::line_l() const
353 {
354   return 0;
355 }
356
357 void
358 Score_element::add_dependency (Score_element*e)
359 {
360   if (e)
361     {
362       Group_interface gi (this, "dependencies");
363       gi.add_element (e);
364     }
365   else
366     programming_error ("Null dependency added");
367 }
368
369
370
371
372 /**
373       Do break substitution in S, using CRITERION. Return new value.
374          CRITERION is either a SMOB pointer to the desired line, or a number
375          representing the break direction.  */
376 SCM
377 Score_element::handle_broken_smobs (SCM s, SCM criterion)
378 {
379  again:
380
381   
382   Score_element *sc = unsmob_element ( s);
383   if (sc)
384     {
385       if (criterion == SCM_UNDEFINED)
386         return SCM_UNDEFINED;
387       else if (gh_number_p (criterion))
388         {
389           Item * i = dynamic_cast<Item*> (sc);
390           Direction d = to_dir (criterion);
391           if (i && i->break_status_dir () != d)
392             {
393               Item *br = i->find_broken_piece (d);
394               return  (br) ? br->self_scm_ : SCM_UNDEFINED;
395             }
396         }
397       else
398         {
399           Score_element * ln = unsmob_element ( criterion);
400           Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
401           Score_element * br =0;
402           Line_of_score * dep_line = sc->line_l ();
403           if (dep_line != line)
404             {
405               br = sc->find_broken_piece (line);
406               return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
407             }
408           if (!dep_line)
409             return SCM_UNDEFINED;
410         }
411     }
412   else if (gh_pair_p (s))
413     {
414       /*
415         UGH! breaks on circular lists.
416       */
417       SCM car = handle_broken_smobs (gh_car (s), criterion);
418       SCM cdr = gh_cdr (s);
419       
420       if (car == SCM_UNDEFINED
421           && (gh_pair_p (cdr) || cdr == SCM_EOL))
422         {
423           /*
424             This is tail-recursion, ie. 
425             
426             return handle_broken_smobs (cdr, criterion);
427
428             We don't want to rely on the compiler to do this.  */
429           s =  cdr;     
430           goto again;
431         }
432
433       gh_set_car_x (s, car);
434       gh_set_cdr_x (s, handle_broken_smobs (cdr, criterion));
435       return s;
436     }
437   return s;
438 }
439
440 void
441 Score_element::handle_broken_dependencies()
442 {
443   Line_of_score *line  = line_l();
444   element_property_alist_ = handle_broken_smobs (element_property_alist_,
445                                                  line ? line->self_scm_ : SCM_UNDEFINED);
446
447   if (!line)
448     return;
449 }
450
451
452 /*
453   TODO: cleanify.
454  */
455 void
456 Score_element::handle_prebroken_dependencies()
457 {
458   if (Item*i =dynamic_cast<Item*> (this))
459     {
460       element_property_alist_
461         = handle_broken_smobs (element_property_alist_,
462                                gh_int2scm (i->break_status_dir ()));
463     }
464 }
465
466
467
468
469
470 Link_array<Score_element>
471 Score_element::get_extra_dependencies() const
472 {
473   Link_array<Score_element> empty;
474   return empty;
475 }
476
477 bool
478 Score_element::linked_b() const
479 {
480   return used_b_;
481 }
482
483 void
484 Score_element::do_print () const
485 {
486 }
487
488 Score_element*
489 Score_element::find_broken_piece (Line_of_score*) const
490 {
491   return 0;
492 }
493
494
495
496 void
497 Score_element::translate_axis (Real y, Axis a)
498 {
499   dim_cache_[a]->translate (y);
500 }  
501
502 Real
503 Score_element::relative_coordinate (Score_element const*e, Axis a) const
504 {
505   return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
506 }
507
508 Score_element * 
509 Score_element::common_refpoint (Score_element const* s, Axis a) const
510 {
511   Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
512   return  dim ? dim->element_l () : 0;
513 }
514
515 void
516 Score_element::set_empty (Axis a)
517 {
518   dim_cache_[a]->callback_l_ =0;
519 }
520
521 bool
522 Score_element::empty_b (Axis a)const
523 {
524   return !dim_cache_[a]->callback_l_;
525 }
526
527 Interval
528 Score_element::extent (Axis a) const
529 {
530   Dimension_cache const * d = dim_cache_[a];
531
532   return d->get_dim ();
533 }
534
535
536 Score_element*
537 Score_element::parent_l (Axis a) const
538 {
539   Dimension_cache*d= dim_cache_[a]->parent_l_;
540   return d ? d->elt_l_ : 0;
541 }
542
543 Score_element *
544 Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
545 {
546   Dimension_cache * common = dim_cache_[a];
547   for (int i=0; i < gs.size (); i++)
548     {
549       common = common->common_refpoint (gs[i]->dim_cache_[a]);
550     }
551
552   return common->element_l ();
553 }
554
555 char const *
556 Score_element::name () const
557 {
558   return classname (this);
559 }
560
561
562 void
563 Score_element::set_parent (Score_element *g, Axis a)
564 {
565   dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
566 }
567
568 void
569 Score_element::fixup_refpoint ()
570 {
571   for (int a = X_AXIS; a < NO_AXES; a ++)
572     {
573       Axis ax = (Axis)a;
574       Score_element * par = parent_l (ax);
575
576       if (!par)
577         continue;
578       
579       if (par->line_l () != line_l ())
580         {
581           Score_element * newpar = par->find_broken_piece (line_l ());
582           set_parent (newpar, ax);
583         }
584
585       if (Item * i  = dynamic_cast<Item*> (this))
586         {
587           Item *pari = dynamic_cast<Item*> (par);
588
589           if (pari && i)
590             {
591               Direction  my_dir = i->break_status_dir () ;
592               if (my_dir!= pari->break_status_dir())
593                 {
594                   Item *newpar =  pari->find_broken_piece (my_dir);
595                   set_parent (newpar, ax);
596                 }
597             }
598         }
599     }
600 }
601
602
603
604 /****************************************************
605   SMOB funcs
606  ****************************************************/
607
608
609 #include "ly-smobs.icc"
610
611 IMPLEMENT_SMOBS(Score_element);
612 IMPLEMENT_UNSMOB(Score_element, element);
613 SCM
614 Score_element::mark_smob (SCM ses)
615 {
616   Score_element * s = SMOB_TO_TYPE (Score_element, ses);
617   if (s->self_scm_ != ses)
618     {
619       programming_error ("SMOB marking gone awry");
620       return SCM_EOL;
621     }
622   return s->element_property_alist_;
623 }
624
625 int
626 Score_element::print_smob (SCM s, SCM port, scm_print_state *)
627 {
628   Score_element *sc = (Score_element *) gh_cdr (s);
629      
630   scm_puts ("#<Score_element ", port);
631   scm_puts ((char *)sc->name (), port);
632
633   // scm_puts (" properties = ", port);
634   // scm_display (sc->element_property_alist_, port);
635   scm_puts (" >", port);
636   return 1;
637 }
638
639 void
640 Score_element::do_smobify_self ()
641 {
642   scm_unprotect_object (element_property_alist_); // ugh
643 }
644
645 SCM
646 Score_element::equal_p (SCM a, SCM b)
647 {
648   return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F;
649 }