]> git.donarmstrong.com Git - lilypond.git/blob - lily/property-engraver.cc
c0f2887008f22d93a14e5b5652cd9422f048e5be
[lilypond.git] / lily / property-engraver.cc
1 /*   
2   property-engraver.cc --  implement Property engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "lily-guile.hh"
11 #include "engraver.hh"
12 #include "protected-scm.hh"
13 #include "dictionary.hh"
14 #include "score-element.hh"
15
16 class Property_engraver : public Engraver
17 {
18   Dictionary<Protected_scm> prop_dict_;
19   void apply_properties (SCM, Score_element*);
20
21 protected:
22   virtual void acknowledge_element (Score_element_info ei);
23   virtual void do_creation_processing ();
24
25   VIRTUAL_COPY_CONS(Translator);
26 };
27
28 void
29 Property_engraver::do_creation_processing ()
30 {
31   SCM plist = get_property ("Generic_property_list");
32   for (; SCM_NIMP (plist); plist = gh_cdr (plist))
33     {
34       SCM elt_props = gh_car (plist);
35       prop_dict_[ly_scm2string (gh_car (elt_props))] = gh_cdr (elt_props);
36     }
37 }
38
39 void
40 Property_engraver::acknowledge_element (Score_element_info i)
41 {
42   if (prop_dict_.elem_b (i.elem_l_->name()))
43     {
44       SCM p = prop_dict_[i.elem_l_->name()];
45       apply_properties (p,i.elem_l_);
46     }
47   if (prop_dict_.elem_b ("all"))
48     {
49       apply_properties (prop_dict_["all"], i.elem_l_);
50     }
51 }
52
53
54 void
55 Property_engraver::apply_properties (SCM p, Score_element *e)
56 {  
57   for (; gh_pair_p (p); p = gh_cdr (p))
58     {
59       /*
60         Try each property in order; earlier descriptions take
61         precedence over later ones, and we don't touch elt-properties if
62         they're already set.
63       */
64       
65       SCM entry = gh_car (p);
66       SCM prop_sym = gh_car (entry);
67       SCM type_p   = gh_cadr (entry);
68       SCM elt_prop_sym = gh_caddr (entry);
69
70       SCM preset = scm_assq(elt_prop_sym, e->element_property_alist_);
71       if (preset != SCM_BOOL_F)
72         continue;
73   
74       SCM val = get_property (prop_sym);
75
76      
77       if (val == SCM_UNDEFINED)
78         ;                       // Not defined in context.
79       else if (gh_apply (type_p, scm_listify (val, SCM_UNDEFINED))
80                == SCM_BOOL_T)   // defined and  right type: do it
81         e->set_elt_property (ly_symbol2string (elt_prop_sym), val);
82       else
83  /*
84             we don't print a warning if VAL == #f, because we would
85             get lots of warnings when we restore stuff to default, eg.
86
87             slurDash = #1 [...] slurDash = ##f
88
89             should not cause "type error: slurDash expects number not
90             boolean"
91
92           */
93         if (val != SCM_BOOL_F)
94         {                       // not the right type: error message.
95           SCM errport = scm_current_error_port ();
96           warning (_("Wrong type for property"));
97           scm_display (prop_sym, errport);
98           scm_puts (", type predicate: ", errport);
99           scm_display (type_p, errport);
100           scm_puts (", value found: ", errport);
101           scm_display (val, errport);
102           scm_puts (" type: ", errport);
103           scm_display (ly_type (val), errport);
104           scm_puts ("\n", errport);
105         }
106     }
107 }
108
109 ADD_THIS_TRANSLATOR(Property_engraver);