]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
* mf/GNUmakefile: always trace pfa fonts.
[lilypond.git] / lily / script-engraver.cc
1 /*
2   script-engraver.cc -- implement Script_engraver
3
4   (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
6
7 #include "script.hh"
8 #include "side-position-interface.hh"
9 #include "event.hh"
10 #include "stem.hh"
11 #include "rhythmic-head.hh"
12 #include "engraver.hh"
13 #include "note-column.hh"
14 #include "translator-group.hh"
15
16 struct Script_tuple
17 {
18   Music *event_;
19   Grob * script_;
20   SCM description_;
21   Script_tuple ()
22   {
23     event_ = 0;
24     script_ = 0;
25     description_ = SCM_EOL;
26   }
27 };
28
29 class Script_engraver : public Engraver
30 {
31   Array<Script_tuple> scripts_;
32 public:
33   TRANSLATOR_DECLARATIONS(Script_engraver);
34 protected:
35   virtual bool try_music (Music*);
36   virtual void initialize ();
37   virtual void stop_translation_timestep ();
38   virtual void start_translation_timestep ();
39   virtual void process_music ();
40   virtual void acknowledge_grob (Grob_info);
41 };
42
43 void
44 Script_engraver::initialize ()
45 {
46   scripts_.clear ();
47 }
48
49 bool
50 Script_engraver::try_music (Music *r)
51 {
52   if (r->is_mus_type ("articulation-event"))
53     {
54       Script_tuple t;
55       t.event_ =r;
56       scripts_.push (t);
57       return true;
58     }
59   return false;
60 }
61
62 void
63 copy_property (Grob * g , SCM sym, SCM alist)
64 {
65   if (g->internal_get_grob_property (sym) == SCM_EOL)
66     {
67       SCM entry = scm_assoc (sym,alist);
68       if (gh_pair_p (entry))
69         {
70           g->internal_set_grob_property (sym, gh_cdr (entry));
71         }
72     }
73 }
74
75
76 /*
77   We add the properties, one by one for each Script. We could save a
78   little space by tacking the props onto the Script grob (i.e. make
79   ScriptStaccato , ScriptMarcato, etc. )
80  */
81 Grob *make_script_from_event (SCM * descr, Translator_group*tg, Music * event,
82                               int index)
83 {
84   SCM alist = tg->get_property ("scriptDefinitions");
85   SCM art = scm_assoc (event->get_mus_property ("articulation-type"), alist);
86
87   if (art == SCM_BOOL_F)
88     {
89       String a = ly_scm2string (event->get_mus_property ("articulation-type"));
90       event->origin ()->warning (_f ("Don't know how to interpret articulation `%s'", a.to_str0 ()));
91       return 0 ;
92     }
93
94   art = gh_cdr (art);
95     
96   Grob *p =new Item (tg->get_property ("Script"));
97   *descr = art;  
98
99   SCM force_dir = event->get_mus_property ("direction");
100   if (is_direction (force_dir) && to_dir (force_dir))
101     p->set_grob_property ("direction", force_dir);
102
103   copy_property (p, ly_symbol2scm ("script-molecule"), art);
104   copy_property (p, ly_symbol2scm ("direction"), art);
105   copy_property (p, ly_symbol2scm ("side-relative-direction"), art);
106
107   int prio =0;
108   SCM sprio = scm_assoc (ly_symbol2scm ("script-priority"), art);
109   if (gh_pair_p (sprio))
110     prio = gh_scm2int (gh_cdr (sprio));
111
112
113   /*
114     Make sure they're in order of user input by adding index i.
115     Don't use the direction in this priority. Smaller means closer
116     to the head.
117   */
118   prio += index;
119
120   Side_position_interface::set_axis (p, Y_AXIS);
121   p->set_grob_property ("script-priority", gh_int2scm (prio));
122   return p;
123 }
124
125 void
126 Script_engraver::process_music ()
127 {
128   for (int i=0; i < scripts_.size (); i++)
129     {
130       Music* l=scripts_[i].event_;
131
132       Grob * p = make_script_from_event (&scripts_[i].description_, daddy_trans_, l, i);
133
134       scripts_[i].script_ = p;
135       if (p)
136         announce_grob (p, l->self_scm());
137     }
138 }
139
140 void
141 Script_engraver::acknowledge_grob (Grob_info inf)
142 {
143   if (Stem::has_interface (inf.grob_))
144     {
145       for (int i=0; i < scripts_.size (); i++)
146         {
147           Grob*e = scripts_[i].script_;
148
149           if (to_dir (e->get_grob_property ("side-relative-direction")))
150             e->set_grob_property ("direction-source", inf.grob_->self_scm ());
151
152           /*
153             add dep ? 
154            */
155           e->add_dependency (inf.grob_);
156           Side_position_interface::add_support (e, inf.grob_);
157         }
158     }
159   else if (Rhythmic_head::has_interface (inf.grob_))
160     {
161       for (int i=0; i < scripts_.size (); i++)
162         {
163           Grob *e = scripts_[i].script_;
164           
165           if (Side_position_interface::get_axis (e) == X_AXIS
166               && !e->get_parent (Y_AXIS))
167             {
168               e->set_parent (inf.grob_, Y_AXIS);
169               e->add_dependency (inf.grob_); // ??
170             }
171           Side_position_interface::add_support (e,inf.grob_);
172         }
173     }
174   else if (Note_column::has_interface (inf.grob_))
175     {
176
177       /*
178         We make note column the parent of the script. That's not
179         correct, but due to seconds in a chord, noteheads may be
180         swapped around horizontally. We don't know which note head to
181         put it on, so we postpone this decision to
182         Script_interface::before_line_breaking ().
183  
184        */
185       for (int i=0; i < scripts_.size (); i++)
186         {
187           Grob *e = scripts_[i].script_;
188           
189           if (!e->get_parent (X_AXIS) &&
190               Side_position_interface::get_axis (e) == Y_AXIS)
191             {
192               e->set_parent (inf.grob_, X_AXIS);
193             }
194         }
195     }
196 }
197
198 void
199 Script_engraver::stop_translation_timestep ()
200 {
201   for (int i=0; i < scripts_.size (); i++) 
202     {
203       if (!scripts_[i].script_)
204         continue;
205       
206       Grob * sc = scripts_[i].script_;
207
208       SCM follow = scm_assoc (ly_symbol2scm ("follow-into-staff"), scripts_[i].description_);
209       if (gh_pair_p (follow) && to_boolean (gh_cdr (follow)))
210         sc->add_offset_callback (Side_position_interface::quantised_position_proc, Y_AXIS);
211       else
212         Side_position_interface::add_staff_support (sc);
213       typeset_grob (sc);
214     }
215   scripts_.clear ();
216 }
217
218 void
219 Script_engraver::start_translation_timestep ()
220 {
221   scripts_.clear ();
222 }
223
224
225
226 Script_engraver::Script_engraver(){}
227
228 ENTER_DESCRIPTION(Script_engraver,
229 /* descr */       "Handles note scripted articulations.",
230 /* creats*/       "Script",
231 /* accepts */     "script-event articulation-event",
232 /* acks  */      "stem-interface rhythmic-head-interface note-column-interface",
233 /* reads */       "scriptDefinitions",
234 /* write */       "");