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