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