]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-heads-engraver.cc
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / lily / note-heads-engraver.cc
1 /*
2   note-heads-engraver.cc -- part of GNU LilyPond
3
4   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 */
6
7 #include "engraver.hh"
8
9 #include <cctype>
10 using namespace std;
11
12 #include "rhythmic-head.hh"
13 #include "output-def.hh"
14 #include "dots.hh"
15 #include "dot-column.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "item.hh"
18 #include "warn.hh"
19 #include "duration.hh"
20
21 class Note_heads_engraver : public Engraver
22 {
23   vector<Item*> notes_;
24   vector<Item*> dots_;
25   vector<Music*> note_evs_;
26
27 public:
28   TRANSLATOR_DECLARATIONS (Note_heads_engraver);
29
30 protected:
31   virtual bool try_music (Music *ev);
32   void process_music ();
33   void stop_translation_timestep ();
34 };
35
36 Note_heads_engraver::Note_heads_engraver ()
37 {
38 }
39
40 bool
41 Note_heads_engraver::try_music (Music *m)
42 {
43   if (m->is_mus_type ("note-event"))
44     {
45       note_evs_.push_back (m);
46       return true;
47     }
48   else if (m->is_mus_type ("busy-playing-event"))
49     return note_evs_.size ();
50
51   return false;
52 }
53
54 void
55 Note_heads_engraver::process_music ()
56 {
57   for (vsize i = 0; i < note_evs_.size (); i++)
58     {
59       Music *ev = note_evs_[i];
60       Item *note = make_item ("NoteHead", ev->self_scm ());
61
62       Duration dur = *unsmob_duration (ev->get_property ("duration"));
63
64       note->set_property ("duration-log", scm_from_int (dur.duration_log ()));
65       if (dur.dot_count ())
66         {
67           Item *d = make_item ("Dots", note->self_scm ());
68           Rhythmic_head::set_dots (note, d);
69
70           if (dur.dot_count ()
71               != robust_scm2int (d->get_property ("dot-count"), 0))
72             d->set_property ("dot-count", scm_from_int (dur.dot_count ()));
73
74           d->set_parent (note, Y_AXIS);
75
76           dots_.push_back (d);
77         }
78
79       Pitch *pit = unsmob_pitch (ev->get_property ("pitch"));
80
81 #if 0 /* TODO: should have a mechanism to switch off these warnings. */
82
83       if (!pit)
84         ev->origin ()->warning (_ ("NoteEvent without pitch"));
85 #endif
86
87       int pos = pit ? pit->steps () : 0;
88       SCM c0 = get_property ("middleCPosition");
89       if (scm_is_number (c0))
90         pos += scm_to_int (c0);
91
92       note->set_property ("staff-position", scm_from_int (pos));
93
94       /*
95         Shape note heads change on step of the scale.
96       */
97       SCM shape_vector = get_property ("shapeNoteStyles");
98       if (scm_is_vector (shape_vector))
99         {
100           SCM scm_tonic = get_property ("tonic");
101           Pitch tonic (0, 0, 0);
102           if (unsmob_pitch (scm_tonic))
103             tonic = *unsmob_pitch (scm_tonic);
104
105           unsigned int delta = (pit->get_notename () - tonic.get_notename () + 7) % 7;
106
107           SCM style = SCM_EOL;
108           if (scm_c_vector_length (shape_vector) > delta
109               && scm_is_symbol (scm_vector_ref (shape_vector, scm_from_int (delta))))
110             style = scm_vector_ref (shape_vector, scm_from_int (delta));
111           if (scm_is_symbol (style))
112             note->set_property ("style", style);
113         }
114
115       notes_.push_back (note);
116     }
117 }
118
119 void
120 Note_heads_engraver::stop_translation_timestep ()
121 {
122   notes_.clear ();
123   dots_.clear ();
124   note_evs_.clear ();
125 }
126
127 #include "translator.icc"
128
129 ADD_TRANSLATOR (Note_heads_engraver,
130                 /* doc */ "Generate noteheads.",
131                 /* create */
132                 "NoteHead "
133                 "Dots",
134                 /* accept */
135                 "note-event "
136                 "busy-playing-event",
137                 /* read */ "middleCPosition",
138                 /* write */ "");