]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-item.cc
1b37fb22a3d8c80e36f57f0b42b43d6b32249e93
[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 "font-interface.hh"
12 #include "paper-def.hh"
13 #include "musical-request.hh"
14 #include "rhythmic-head.hh"
15 #include "misc.hh"
16 #include "lookup.hh"
17
18 SCM
19 pitch_less  (SCM p1, SCM p2)
20 {
21   for (int i = 3; i--; p1 = gh_cdr (p1), p2 = gh_cdr (p2))
22     {
23       if (scm_less_p (gh_car (p1), gh_car (p2)))
24         return SCM_BOOL_T;
25       if (gh_car (p1) != gh_car (p2))
26         return SCM_BOOL_F;
27     }
28   return SCM_BOOL_T;
29 }
30
31 SCM pitch_less_proc;
32
33
34 void
35 init_pitch_funcs ()
36 {
37   pitch_less_proc = gh_new_procedure2_0 ("pitch-less", &pitch_less);
38 }
39
40 ADD_SCM_INIT_FUNC(pitch,init_pitch_funcs);
41
42
43 void
44 Local_key_item::add_pitch (Score_element*me, Musical_pitch p, bool cautionary, bool natural)
45 {
46   SCM acs = me->get_elt_property ("accidentals");
47   SCM pitch = p.to_scm ();
48   SCM opts = SCM_EOL;
49   if (cautionary)
50     opts = gh_cons (ly_symbol2scm ("cautionary"), opts);
51   if (natural)
52     opts = gh_cons (ly_symbol2scm ("natural"), opts);
53
54   pitch = gh_append2 (pitch, opts);
55   acs = scm_merge_x (acs, gh_cons (pitch, SCM_EOL), pitch_less_proc);
56
57   me->set_elt_property ("accidentals", acs);
58 }
59
60 Molecule
61 Local_key_item::parenthesize (Score_element*me, Molecule m)
62 {
63   Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-("));
64   Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-)"));
65   m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
66   m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
67
68   return m;
69 }
70
71 /*
72   UGH. clean me, revise placement routine (See Ross & Wanske;
73   accidental placement is more complicated than this.
74  */
75
76 MAKE_SCHEME_CALLBACK(Local_key_item,brew_molecule,1);
77 SCM
78 Local_key_item::brew_molecule (SCM smob)
79 {
80   Score_element* me = unsmob_element (smob);
81   
82   Molecule mol;
83
84   Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
85   Molecule octave_mol;
86   bool oct_b = false;
87   int lastoct = -100;
88
89   SCM accs = me->get_elt_property ("accidentals");
90   for  (SCM s = accs;
91         gh_pair_p (s); s = gh_cdr (s))
92     {
93       Musical_pitch p (gh_car (s));
94       
95       // do one octave
96       if (p.octave_i_ != lastoct) 
97         {
98           if (oct_b)
99             {
100               Real dy =lastoct*7* note_distance;
101               octave_mol.translate_axis (dy, Y_AXIS);
102               mol.add_molecule (octave_mol);
103               octave_mol = Molecule ();
104             }
105           oct_b = true; 
106         }
107       
108       lastoct = p.octave_i_;
109
110       SCM c0 =  me->get_elt_property ("c0-position");
111       Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
112         * note_distance;
113       
114       Molecule acc (Font_interface::get_default_font (me)->find_by_name (String ("accidentals-")
115                                                + to_str (p.accidental_i_)));
116       
117       if (scm_memq (ly_symbol2scm ("natural"), gh_car (s)) != SCM_BOOL_F)
118         {
119           Molecule prefix = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-0"));
120           acc.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
121         }
122
123       if (scm_memq (ly_symbol2scm ("cautionary"), gh_car (s)) != SCM_BOOL_F)
124         acc = parenthesize (me, acc);
125
126       acc.translate_axis (dy, Y_AXIS);
127       octave_mol.add_at_edge (X_AXIS, RIGHT, acc, 0);
128     }
129
130   if (oct_b)
131     {
132       Real dy =lastoct*7*note_distance;
133       octave_mol.translate_axis (dy, Y_AXIS);
134       mol.add_molecule (octave_mol);
135       octave_mol = Molecule ();
136     }
137   
138  if (gh_pair_p (accs))
139     {
140       Drul_array<SCM> pads;
141
142       /*
143         Use a cons?
144        */
145       pads[RIGHT] = me->get_elt_property ("right-padding");
146       pads[LEFT] = me->get_elt_property ("left-padding");
147
148
149       // unused ?
150       Direction d = LEFT;
151       do {
152         if (!gh_number_p (pads[d]))
153           continue;
154
155         Box b(Interval (0, gh_scm2double (pads[d]) * note_distance),
156               Interval (0,0));
157         Molecule m (Lookup::blank (b));
158         mol.add_at_edge (X_AXIS, d, m, 0);
159       } while ( flip (&d)!= LEFT);
160     }
161
162   return mol.smobbed_copy();
163 }
164
165 bool
166 Local_key_item::has_interface (Score_element*m)
167 {
168   return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
169 }
170 void
171 Local_key_item::set_interface (Score_element*m)
172 {
173   m->set_interface (ly_symbol2scm ("accidentals-interface"));
174 }