]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-engraver.cc
a448d15ef820b7972cd97a1efae7e250183746fd
[lilypond.git] / lily / text-engraver.cc
1 /*   
2   text-engraver.cc --  implement Text_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10
11 #include "engraver.hh"
12 #include "side-position-interface.hh"
13 #include "item.hh"
14 #include "musical-request.hh"
15 #include "stem.hh"
16 #include "rhythmic-head.hh"
17
18
19 /**
20    typeset directions that are  plain text.
21  */
22 class Text_engraver : public Engraver
23 {
24   Link_array<Text_script_req> reqs_;
25   Link_array<Item> texts_;
26 public:
27   TRANSLATOR_DECLARATIONS(Text_engraver);
28 protected:
29   virtual bool try_music (Music* m);
30   virtual void stop_translation_timestep ();
31   virtual void start_translation_timestep ();
32   virtual void process_acknowledged_grobs ();
33   virtual void acknowledge_grob (Grob_info);
34 };
35
36 bool
37 Text_engraver::try_music (Music *m)
38 {
39   if (dynamic_cast<Text_script_req*> (m)
40       && m->get_mus_property ("text-type") != ly_symbol2scm ("finger")
41       && m->get_mus_property ("text-type") != ly_symbol2scm ("dynamic"))
42     {
43       reqs_.push (dynamic_cast<Text_script_req*> (m));
44       return true;
45     }
46   return false;
47 }
48
49 void
50 Text_engraver::acknowledge_grob (Grob_info inf)
51 {
52   if (Rhythmic_head::has_interface (inf.grob_))
53     {
54       for (int i=0; i < texts_.size (); i++)
55         {
56           Grob*t = texts_[i];
57           Side_position_interface::add_support (t,inf.grob_);
58
59           /*
60             ugh.
61            */
62           if (Side_position_interface::get_axis (t) == X_AXIS
63               && !t->get_parent (Y_AXIS))
64             t->set_parent (inf.grob_, Y_AXIS);
65           else if (Side_position_interface::get_axis (t) == Y_AXIS
66               && !t->get_parent (X_AXIS))
67             t->set_parent (inf.grob_, X_AXIS);
68         }
69     }
70   
71   if (Stem::has_interface (inf.grob_))
72     {
73       for (int i=0; i < texts_.size (); i++)
74         {
75           Side_position_interface::add_support (texts_[i],inf.grob_);
76         }
77     }
78 }
79
80 void
81 Text_engraver::process_acknowledged_grobs ()
82 {
83   if (texts_.size ())
84     return;
85   for (int i=0; i < reqs_.size (); i++)
86     {
87       Text_script_req * r = reqs_[i];
88       
89       // URG: Text vs TextScript
90       String basic = "TextScript";
91
92       Item *text = new Item (get_property (basic.to_str0 ()));
93
94       /*
95         FIXME -> need to use basic props.
96        */
97       SCM axisprop = get_property ("scriptHorizontal");
98       
99       Axis ax = to_boolean (axisprop) ? X_AXIS : Y_AXIS;
100       Side_position_interface::set_axis (text, ax);
101
102       // Hmm
103       int priority = 200;
104       SCM s = text->get_grob_property ("script-priority");
105       if (gh_number_p (s))
106         priority = gh_scm2int (s);
107       
108       /* Make sure they're in order of user input by adding index i. */
109       priority += i * (r->get_direction () ? r->get_direction () : 1);
110       
111       text->set_grob_property ("script-priority", gh_int2scm (priority));
112
113       if (r->get_direction ())
114         Side_position_interface::set_direction (text, r->get_direction ());
115       
116       text->set_grob_property ("text", r->get_mus_property ("text"));
117       announce_grob (text, r->self_scm ());
118       texts_.push (text);
119     }
120 }
121
122 void
123 Text_engraver::stop_translation_timestep ()
124 {
125   for (int i=0; i < texts_.size (); i++)
126     {
127       Item *ti = texts_[i];
128       if (!to_boolean (get_property ("scriptHorizontal")))
129         Side_position_interface::add_staff_support (ti);
130       typeset_grob (ti);
131     }
132   texts_.clear ();
133 }
134
135 void
136 Text_engraver::start_translation_timestep ()
137 {
138   reqs_.clear ();
139 }
140
141
142 Text_engraver::Text_engraver(){}
143
144 ENTER_DESCRIPTION(Text_engraver,
145 /* descr */       "Create text-scripts",
146 /* creats*/       "TextScript",
147 /* acks  */       "rhythmic-head-interface stem-interface",
148 /* reads */       "scriptHorizontal",
149 /* write */       "");