]> git.donarmstrong.com Git - lilypond.git/blob - lily/engraver.cc
Emit not-quite-cross-staff beams in the right context.
[lilypond.git] / lily / engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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 "engraver.hh"
21
22 #include "context.hh"
23 #include "international.hh"
24 #include "music.hh"
25 #include "paper-column.hh"
26 #include "score-engraver.hh"
27 #include "spanner.hh"
28 #include "stream-event.hh"
29 #include "warn.hh"
30
31 Engraver_group *
32 Engraver::get_daddy_engraver () const
33 {
34   return dynamic_cast<Engraver_group *> (get_daddy_translator ());
35 }
36
37 void
38 Engraver::announce_grob (Grob_info inf)
39 {
40   get_daddy_engraver ()->announce_grob (inf);
41 }
42
43 void
44 Engraver::announce_end_grob (Grob_info inf)
45 {
46   inf.start_end_ = STOP;
47   get_daddy_engraver ()->announce_grob (inf);
48 }
49
50 Grob_info
51 Engraver::make_grob_info(Grob *e, SCM cause)
52 {
53   /* TODO: Remove Music code when it's no longer needed */
54   if (Music *m = unsmob_music (cause))
55     {
56       cause = m->to_event ()->unprotect ();
57     }
58   if (e->get_property ("cause") == SCM_EOL
59       && (unsmob_stream_event (cause) || unsmob_grob (cause)))
60     e->set_property ("cause", cause);
61
62   return Grob_info (this, e);
63 }
64
65 /*
66   CAUSE is the object (typically a Stream_event object)  that
67   was the reason for making E.
68 */
69 void
70 Engraver::announce_grob (Grob *e, SCM cause)
71 {
72   announce_grob (make_grob_info (e, cause));
73 }
74
75
76 /*
77   CAUSE is the object (typically a grob or stream-event object) that
78   was the reason for ending E.  */
79 void
80 Engraver::announce_end_grob (Grob *e, SCM cause)
81 {
82   announce_end_grob (make_grob_info (e, cause));
83 }
84
85
86 Engraver::Engraver ()
87 {
88 }
89
90 #ifndef NDEBUG
91 static SCM creation_callback = SCM_EOL;
92 LY_DEFINE (ly_set_grob_creation_callback, "ly:set-grob-creation-callback",
93            1, 0, 0, (SCM cb),
94            "Specify a procedure that will be called every time a new grob"
95            " is created.  The callback will receive as arguments the grob"
96            " that was created, the name of the C++ source file that caused"
97            " the grob to be created, and the corresponding line number in"
98            " the C++ source file.")
99 {
100   LY_ASSERT_TYPE (ly_is_procedure, cb, 1);
101
102   creation_callback = cb;
103
104   return SCM_UNSPECIFIED;
105 }
106 #endif
107
108 Grob *
109 Engraver::internal_make_grob (SCM symbol,
110                               SCM cause,
111                               char const * /* name */,
112                               char const *file,
113                               int line,
114                               char const *fun)
115 {
116 #ifdef NDEBUG
117   (void)file;
118   (void)line;
119   (void)fun;
120 #endif
121
122   SCM props = updated_grob_properties (context (), symbol);
123
124   Grob *grob = 0;
125
126   SCM handle = scm_sloppy_assq (ly_symbol2scm ("meta"), props);
127   SCM klass = scm_cdr (scm_sloppy_assq (ly_symbol2scm ("class"), scm_cdr (handle)));
128
129   if (klass == ly_symbol2scm ("Item"))
130     grob = new Item (props);
131   else if (klass == ly_symbol2scm ("Spanner"))
132     grob = new Spanner (props);
133   else if (klass == ly_symbol2scm ("Paper_column"))
134     grob = new Paper_column (props);
135
136   assert (grob);
137   announce_grob (grob, cause);
138
139 #ifndef NDEBUG
140   if (ly_is_procedure (creation_callback))
141     scm_apply_0 (creation_callback,
142                  scm_list_n (grob->self_scm (), scm_from_locale_string (file),
143                              scm_from_int (line), scm_from_locale_string (fun), SCM_UNDEFINED));
144 #endif
145
146   return grob;
147 }
148
149 Item *
150 Engraver::internal_make_item (SCM x, SCM cause,
151                               char const *name,
152                               char const *file, int line, char const *fun)
153 {
154   Item *it = dynamic_cast<Item *> (internal_make_grob (x, cause, name, file, line, fun));
155   assert (it);
156   return it;
157 }
158
159 Paper_column *
160 Engraver::internal_make_column (SCM x, char const *name,
161                                 char const *file, int line, char const *fun)
162 {
163   return dynamic_cast<Paper_column *> (internal_make_grob (x, SCM_EOL, name, file, line, fun));
164 }
165
166 Spanner *
167 Engraver::internal_make_spanner (SCM x, SCM cause, char const *name,
168                                  char const *file, int line, char const *fun)
169 {
170   Spanner *sp = dynamic_cast<Spanner *> (internal_make_grob (x, cause, name, file, line, fun));
171   assert (sp);
172   return sp;
173 }
174
175 Engraver*
176 unsmob_engraver (SCM eng)
177 {
178   return dynamic_cast<Engraver*> (unsmob_translator (eng));
179 }
180
181 bool
182 ly_is_grob_cause (SCM obj)
183 {
184   return unsmob_grob (obj) || unsmob_stream_event (obj) || (obj == SCM_EOL);
185 }
186
187 #include "translator.icc"
188
189 ADD_TRANSLATOR (Engraver,
190                 /* doc */
191                 "Base class for engravers.  Does nothing, so it is not used.",
192
193                 /* create */
194                 "",
195
196                 /* read */
197                 "",
198
199                 /* write */
200                 ""
201                 );
202
203