]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-item.cc
release: 1.3.72
[lilypond.git] / lily / local-key-item.cc
1 /*
2   local-key-item.cc -- implement Local_key_item, Musical_pitch
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "local-key-item.hh"
9 #include "molecule.hh"
10 #include "staff-symbol-referencer.hh"
11 #include "lookup.hh"
12 #include "paper-def.hh"
13 #include "musical-request.hh"
14 #include "rhythmic-head.hh"
15 #include "misc.hh"
16
17 void
18 Local_key_item::add_pitch (Musical_pitch p, bool cautionary, bool natural)
19 {
20   for (int i=0; i< accidental_arr_.size(); i++)
21     if (!Musical_pitch::compare (p, accidental_arr_[i].pitch_))
22       return;
23           /* maybe natural (and cautionary) should be modif. nonetheless? */
24
25   Local_key_cautionary_tuple t;
26   t.pitch_ = p;
27   t.cautionary_b_ = cautionary;
28   t.natural_b_ = natural;
29   accidental_arr_.push (t);
30 }
31
32 MAKE_SCHEME_CALLBACK(Local_key_item,before_line_breaking);
33
34 SCM
35 Local_key_item::before_line_breaking (SCM smob)
36 {
37   Local_key_item* me = dynamic_cast<Local_key_item*>(unsmob_element (smob));
38   me->accidental_arr_.sort (Local_key_cautionary_tuple::compare);
39   return SCM_UNSPECIFIED;
40 }
41
42 Molecule
43 Local_key_item::accidental (Score_element*me, int j, bool cautionary, bool natural) 
44 {
45   Molecule m (me->lookup_l ()->afm_find (String ("accidentals-") + to_str (j)));
46   if (natural)
47     {
48       Molecule prefix = me->lookup_l ()->afm_find (String ("accidentals-0"));
49       m.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
50     }
51   if (cautionary) 
52     {
53       Molecule open = me->lookup_l ()->afm_find (String ("accidentals-("));
54       Molecule close = me->lookup_l ()->afm_find (String ("accidentals-)"));
55       m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
56       m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
57     }
58   
59   return m;
60 }
61
62 /*
63   UGH. clean me up
64  */
65
66 MAKE_SCHEME_CALLBACK(Local_key_item,brew_molecule);
67 SCM
68 Local_key_item::brew_molecule (SCM smob)
69 {
70   Local_key_item* lki = dynamic_cast<Local_key_item*>(unsmob_element (smob));
71   Score_element* me = lki;
72   
73   Molecule mol;
74
75   Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
76   Molecule octave_mol;
77   bool oct_b = false;
78   int lastoct = -100;
79   
80   for  (int i = 0; i < lki->accidental_arr_.size(); i++) 
81     {
82       Musical_pitch p (lki->accidental_arr_[i].pitch_);
83       // do one octave
84       if (p.octave_i_ != lastoct) 
85         {
86           if (oct_b)
87             {
88               Real dy =lastoct*7* note_distance;
89               octave_mol.translate_axis (dy, Y_AXIS);
90               mol.add_molecule (octave_mol);
91               octave_mol = Molecule ();
92             }
93           oct_b = true; 
94         }
95       
96       lastoct = p.octave_i_;
97
98       SCM c0 =  me->get_elt_property ("c0-position");
99       Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
100         * note_distance;
101       
102       Molecule m (accidental (me,p.accidental_i_,
103                               lki->accidental_arr_[i].cautionary_b_,
104                               lki->accidental_arr_[i].natural_b_));
105
106       m.translate_axis (dy, Y_AXIS);
107       octave_mol.add_at_edge (X_AXIS, RIGHT, m, 0);
108     }
109
110   if (oct_b)
111     {
112       Real dy =lastoct*7*note_distance;
113       octave_mol.translate_axis (dy, Y_AXIS);
114       mol.add_molecule (octave_mol);
115       octave_mol = Molecule ();
116     }
117   
118  if (lki->accidental_arr_.size()) 
119     {
120       Drul_array<SCM> pads;
121
122       /*
123         Use a cons?
124        */
125       pads[RIGHT] = me->get_elt_property ("right-padding");
126       pads[LEFT] = me->get_elt_property ("left-padding");
127
128
129       // unused ?
130       Direction d = LEFT;
131       do {
132         if (!gh_number_p (pads[d]))
133           continue;
134
135         Box b(Interval (0, gh_scm2double (pads[d]) * note_distance),
136               Interval (0,0));
137         Molecule m (me->lookup_l ()->blank (b));
138         mol.add_at_edge (X_AXIS, d, m, 0);
139       } while ( flip (&d)!= LEFT);
140     }
141
142   return mol.create_scheme();
143 }
144
145 Local_key_item::Local_key_item (SCM s)
146   : Item (s)
147 {
148   
149 }
150 bool
151 Local_key_item::has_interface (Score_element*m)
152 {
153   return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
154 }