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