1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 1998--2020 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "directional-element-interface.hh"
21 #include "engraver.hh"
22 #include "item.hh"
23 #include "note-column.hh"
24 #include "pointer-group-interface.hh"
25 #include "side-position-interface.hh"
26 #include "stream-event.hh"
27 #include "text-interface.hh"
28
29 #include "translator.icc"
30
31 using std::vector;
32
33 /**
34 typeset directions that are plain text.
35 */
36
37 class Text_engraver : public Engraver
38 {
39 vector<Stream_event *> evs_;
40 vector<Grob *> scripts_;
41 public:
42 TRANSLATOR_DECLARATIONS (Text_engraver);
43 protected:
44 void stop_translation_timestep ();
45 void process_music ();
46
47 void acknowledge_note_column (Grob_info);
48 void listen_text_script (Stream_event *);
49 };
50
51 void
listen_text_script(Stream_event * ev)52 Text_engraver::listen_text_script (Stream_event *ev)
53 {
54 evs_.push_back (ev);
55 }
56
57 void
process_music()58 Text_engraver::process_music ()
59 {
60 for (vsize i = 0; i < evs_.size (); i++)
61 {
62 Stream_event *ev = evs_[i];
63
64 Item *script = make_item ("TextScript", ev->self_scm ());
65 scripts_.push_back (script);
66
67 /* see script-engraver.cc */
68 SCM priority = get_property (script, "script-priority");
69 if (!scm_is_number (priority))
70 priority = to_scm (200); // TODO: Explain magic.
71 priority = scm_sum (priority, to_scm (i));
72 set_property (script, "script-priority", priority);
73
74 Direction dir = from_scm<Direction> (get_property (ev, "direction"));
75 if (dir)
76 set_grob_direction (script, dir);
77
78 SCM mark = get_property (ev, "text");
79
80 set_property (script, "text", mark);
81 }
82 }
83
84 void
acknowledge_note_column(Grob_info info)85 Text_engraver::acknowledge_note_column (Grob_info info)
86 {
87 // Make note column (or rest, if there are no heads) the parent of the script.
88 extract_grob_set (info.grob (), "note-heads", heads);
89 Grob *x_parent = (heads.size ()
90 ? info.grob ()
91 : unsmob<Grob> (get_object (info.grob (), "rest")));
92
93 for (vsize i = 0; i < scripts_.size (); i++)
94 {
95 Grob *el = scripts_[i];
96
97 if (el && !el->get_x_parent () && x_parent)
98 el->set_x_parent (x_parent);
99 }
100 }
101
102 void
stop_translation_timestep()103 Text_engraver::stop_translation_timestep ()
104 {
105 evs_.clear ();
106 scripts_.clear ();
107 }
108
Text_engraver(Context * c)109 Text_engraver::Text_engraver (Context *c)
110 : Engraver (c)
111 {
112 }
113
114 void
boot()115 Text_engraver::boot ()
116 {
117 ADD_LISTENER (Text_engraver, text_script);
118 ADD_ACKNOWLEDGER (Text_engraver, note_column);
119 }
120
121 ADD_TRANSLATOR (Text_engraver,
122 /* doc */
123 "Create text scripts.",
124
125 /* create */
126 "TextScript ",
127
128 /* read */
129 "",
130
131 /* write */
132 ""
133 );
134