]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
* lily/ : rename Request to Event
[lilypond.git] / lily / script-engraver.cc
1 /*
2   script-engraver.cc -- implement Script_engraver
3
4   (c)  1997--2002 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
15 class Script_engraver : public Engraver
16 {
17   Link_array<Grob> scripts_;
18   Link_array<Music> script_reqs_;
19
20 public:
21   TRANSLATOR_DECLARATIONS(Script_engraver);
22 protected:
23   virtual bool try_music (Music*);
24   virtual void initialize ();
25   virtual void stop_translation_timestep ();
26   virtual void start_translation_timestep ();
27   virtual void process_music ();
28   virtual void acknowledge_grob (Grob_info);
29 };
30
31 void
32 Script_engraver::initialize ()
33 {
34   script_reqs_.clear ();
35 }
36
37 bool
38 Script_engraver::try_music (Music *r)
39 {
40   if (r->is_mus_type ("articulation-event"))
41     {
42       script_reqs_.push (r);
43       return true;
44     }
45   return false;
46 }
47
48 void
49 Script_engraver::process_music ()
50 {
51   for (int i=0; i < script_reqs_.size (); i++)
52     {
53       Music* l=script_reqs_[i];
54
55       SCM alist = get_property ("scriptDefinitions");
56       SCM art = scm_assoc (l->get_mus_property ("articulation-type"), alist);
57
58       if (art == SCM_BOOL_F)
59         {
60           String a = ly_scm2string (l->get_mus_property ("articulation-type"));
61           l->origin ()->warning (_f ("Don't know how to interpret articulation `%s'", a.to_str0 ()));
62                         
63           continue;
64         }
65       // todo -> use result of articulation-to-scriptdef directly as basic prop list.
66       Grob *p =new Item (get_property ("Script"));
67       art = ly_cdr (art);
68       p->set_grob_property ("script-molecule", ly_car (art));
69
70       art = ly_cdr (art);
71       bool follow_staff = gh_scm2bool (ly_car (art));
72       art = ly_cdr (art);
73       SCM relative_stem_dir = ly_car (art);
74       art = ly_cdr (art);
75
76       SCM force_dir = l->get_mus_property ("direction");
77       if (ly_dir_p (force_dir) && !to_dir (force_dir))
78         force_dir = ly_car (art);
79       
80       art = ly_cdr (art);
81       int priority = gh_scm2int (ly_car (art));
82
83       SCM s = p->get_grob_property ("script-priority");
84       if (gh_number_p (s))
85         priority = gh_scm2int (s);
86
87       /* Make sure they're in order of user input by adding index i.
88       Don't use the direction in this priority. Smaller means closer
89       to the head.
90       */
91       priority += i;
92
93       if (ly_dir_p (force_dir) && to_dir (force_dir))
94         p->set_grob_property ("direction", force_dir);
95       else if (to_dir (relative_stem_dir))
96         p->set_grob_property ("side-relative-direction", relative_stem_dir);
97
98
99       /*
100         FIXME: should figure this out in relation with basic props! 
101        */
102       SCM axisprop = get_property ("scriptHorizontal");
103       bool xaxis = to_boolean (axisprop);
104       Side_position_interface::set_axis (p, xaxis ? X_AXIS : Y_AXIS);
105
106       if (!follow_staff && ! xaxis)
107         p->set_grob_property ("staff-support", SCM_BOOL_T);
108
109       if (!xaxis && follow_staff)
110         p->add_offset_callback (Side_position_interface::quantised_position_proc, Y_AXIS);
111       
112       
113       p->set_grob_property ("script-priority", gh_int2scm (priority));
114   
115       scripts_.push (p);
116       
117       announce_grob (p, l->self_scm());
118     }
119 }
120
121 void
122 Script_engraver::acknowledge_grob (Grob_info inf)
123 {
124   if (Stem::has_interface (inf.grob_))
125     {
126       for (int i=0; i < scripts_.size (); i++)
127         {
128           Grob*e = scripts_[i];
129
130           e->set_grob_property ("direction-source", inf.grob_->self_scm ());
131           e->add_dependency (inf.grob_);
132           Side_position_interface::add_support (e, inf.grob_);
133         }
134     }
135   else if (Rhythmic_head::has_interface (inf.grob_))
136     {
137       for (int i=0; i < scripts_.size (); i++)
138         {
139           Grob *e = scripts_[i];
140           
141           if (Side_position_interface::get_axis (e) == X_AXIS
142               && !e->get_parent (Y_AXIS))
143             {
144               e->set_parent (inf.grob_, Y_AXIS);
145               e->add_dependency (inf.grob_); // ??
146             }
147           Side_position_interface::add_support (e,inf.grob_);
148         }
149     }
150   else if (Note_column::has_interface (inf.grob_))
151     {
152
153       /*
154         We make note column the parent of the script. That's not
155         correct, but due to seconds in a chord, noteheads may be
156         swapped around horizontally. We don't know which note head to
157         put it on, so we postpone this decision to
158         Script_interface::before_line_breaking ().
159  
160        */
161       for (int i=0; i < scripts_.size (); i++)
162         {
163           Grob *e = scripts_[i];
164           
165           if (!e->get_parent (X_AXIS) &&
166               Side_position_interface::get_axis (e) == Y_AXIS)
167             {
168               e->set_parent (inf.grob_, X_AXIS);
169             }
170         }
171     }
172 }
173
174 void
175 Script_engraver::stop_translation_timestep ()
176 {
177   for (int i=0; i < scripts_.size (); i++) 
178     {
179
180       /*
181         TODO: junk staff-support.
182        */
183       Grob * sc = scripts_[i];
184       if (to_boolean (sc->get_grob_property ("staff-support")))
185         {
186           Side_position_interface::add_staff_support (sc);
187         }
188       typeset_grob (sc);
189     }
190   scripts_.clear ();
191 }
192
193 void
194 Script_engraver::start_translation_timestep ()
195 {
196   script_reqs_.clear ();
197 }
198
199
200
201 Script_engraver::Script_engraver(){}
202
203 ENTER_DESCRIPTION(Script_engraver,
204 /* descr */       "    Handles note ornaments generated by @code{\\script}.  
205 ",
206 /* creats*/       "Script",
207 /* accepts */     "script-event articulation-event",
208 /* acks  */      "stem-interface rhythmic-head-interface note-column-interface",
209 /* reads */       "scriptDefinitions scriptHorizontal",
210 /* write */       "");