]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
Web-ja: update introduction
[lilypond.git] / lily / clef-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Mats Bengtsson <matsb@s3.kth.se>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <cctype>
22 using namespace std;
23
24 #include "item.hh"
25 #include "context.hh"
26 #include "staff-symbol-referencer.hh"
27 #include "engraver.hh"
28 #include "direction.hh"
29 #include "side-position-interface.hh"
30
31 #include "translator.icc"
32 #include "lily-imports.hh"
33
34 class Clef_engraver : public Engraver
35 {
36 public:
37   TRANSLATOR_DECLARATIONS (Clef_engraver);
38
39 protected:
40   void stop_translation_timestep ();
41   void process_music ();
42   void acknowledge_bar_line (Grob_info);
43
44   virtual void derived_mark () const;
45 private:
46   Item *clef_;
47   Item *modifier_;
48
49   SCM prev_glyph_;
50   SCM prev_cpos_;
51   SCM prev_transposition_;
52   void create_clef ();
53   void set_glyph ();
54   void inspect_clef_properties ();
55 };
56
57 void
58 Clef_engraver::derived_mark () const
59 {
60   scm_gc_mark (prev_transposition_);
61   scm_gc_mark (prev_cpos_);
62   scm_gc_mark (prev_glyph_);
63 }
64
65 Clef_engraver::Clef_engraver (Context *c)
66   : Engraver (c)
67 {
68   clef_ = 0;
69   modifier_ = 0;
70
71   /*
72     will trigger a clef at the start since #f != ' ()
73   */
74   prev_transposition_ = prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
75 }
76
77 void
78 Clef_engraver::set_glyph ()
79 {
80   SCM glyph_sym = ly_symbol2scm ("glyph");
81   SCM basic = ly_symbol2scm ("Clef");
82
83   execute_pushpop_property (context (), basic, glyph_sym, SCM_UNDEFINED);
84   execute_pushpop_property (context (), basic, glyph_sym, get_property ("clefGlyph"));
85 }
86
87 /**
88    Generate a clef at the start of a measure. (when you see a Bar,
89    ie. a breakpoint)
90 */
91 void
92 Clef_engraver::acknowledge_bar_line (Grob_info info)
93 {
94   Item *item = info.item ();
95   if (item && scm_is_string (get_property ("clefGlyph")))
96     create_clef ();
97 }
98
99 void
100 Clef_engraver::create_clef ()
101 {
102   if (!clef_)
103     {
104       Item *c = make_item ("Clef", SCM_EOL);
105
106       clef_ = c;
107       SCM cpos = get_property ("clefPosition");
108
109       if (scm_is_number (cpos))
110         clef_->set_property ("staff-position", cpos);
111
112       SCM transp = get_property ("clefTransposition");
113       if (scm_is_number (transp) && scm_to_int (transp))
114         {
115           Item *g = make_item ("ClefModifier", SCM_EOL);
116
117           int abs_transp = scm_to_int (transp);
118           int dir = sign (abs_transp);
119           abs_transp = abs (abs_transp) + 1;
120
121           SCM txt = scm_number_to_string (scm_from_int (abs_transp),
122                                           scm_from_int (10));
123
124           SCM style = get_property ("clefTranspositionStyle");
125
126           SCM formatter = get_property ("clefTranspositionFormatter");
127           if (ly_is_procedure (formatter))
128             g->set_property ("text", scm_call_2 (formatter, txt, style));
129
130           Side_position_interface::add_support (g, clef_);
131
132           g->set_parent (clef_, Y_AXIS);
133           g->set_parent (clef_, X_AXIS);
134           g->set_property ("direction", scm_from_int (dir));
135           modifier_ = g;
136         }
137     }
138 }
139 void
140 Clef_engraver::process_music ()
141 {
142   inspect_clef_properties ();
143 }
144
145 static void apply_on_children (Context *context, SCM fun)
146 {
147   scm_call_1 (fun, context->self_scm ());
148   for (SCM s = context->children_contexts ();
149        scm_is_pair (s); s = scm_cdr (s))
150     apply_on_children (unsmob<Context> (scm_car (s)), fun);
151 }
152
153 void
154 Clef_engraver::inspect_clef_properties ()
155 {
156   SCM glyph = get_property ("clefGlyph");
157   SCM clefpos = get_property ("clefPosition");
158   SCM transposition = get_property ("clefTransposition");
159   SCM force_clef = get_property ("forceClef");
160
161   if (scm_is_null (clefpos)
162       || !ly_is_equal (glyph, prev_glyph_)
163       || !ly_is_equal (clefpos, prev_cpos_)
164       || !ly_is_equal (transposition, prev_transposition_)
165       || to_boolean (force_clef))
166     {
167       apply_on_children (context (), Lily::invalidate_alterations);
168
169       set_glyph ();
170       if (scm_is_true (prev_cpos_) || to_boolean (get_property ("firstClef")))
171         create_clef ();
172
173       if (clef_)
174         clef_->set_property ("non-default", SCM_BOOL_T);
175
176       prev_cpos_ = clefpos;
177       prev_glyph_ = glyph;
178       prev_transposition_ = transposition;
179     }
180
181   if (to_boolean (force_clef))
182     {
183       SCM prev;
184       Context *w = context ()->where_defined (ly_symbol2scm ("forceClef"), &prev);
185       w->set_property ("forceClef", SCM_EOL);
186     }
187 }
188
189 void
190 Clef_engraver::stop_translation_timestep ()
191 {
192   if (clef_)
193     {
194       if (to_boolean (clef_->get_property ("non-default")))
195         {
196           SCM vis = get_property ("explicitClefVisibility");
197
198           if (scm_is_vector (vis))
199             clef_->set_property ("break-visibility", vis);
200         }
201
202       clef_ = 0;
203
204       modifier_ = 0;
205     }
206 }
207
208 void
209 Clef_engraver::boot ()
210 {
211   ADD_ACKNOWLEDGER (Clef_engraver, bar_line);
212 }
213
214 ADD_TRANSLATOR (Clef_engraver,
215                 /* doc */
216                 "Determine and set reference point for pitches.",
217
218                 /* create */
219                 "Clef "
220                 "ClefModifier ",
221
222                 /* read */
223                 "clefGlyph "
224                 "clefTransposition "
225                 "clefTranspositionStyle "
226                 "clefPosition "
227                 "explicitClefVisibility "
228                 "forceClef ",
229
230                 /* write */
231                 ""
232                );