2 key-item.cc -- implement Key_item
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 keyplacement by Mats Bengtsson
12 #include "key-item.hh"
13 #include "molecule.hh"
14 #include "paper-def.hh"
16 #include "staff-symbol-referencer.hh"
19 FIXME: too much hardcoding here.
21 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
22 const int SHARP_TOP_PITCH=4; /* ais and bis typeset in lower octave */
26 FIXME: key-item should just get a list of (position, acc), and leave
27 the thinking to other parties.
30 Key_item::calculate_position(Score_element *ki, SCM pair)
32 int p = gh_scm2int (gh_car (pair));
33 int a = gh_scm2int (gh_cdr (pair));
34 int c0p = gh_scm2int (ki->get_elt_property ("c0-position"));
35 if (to_boolean (ki->get_elt_property ("multi-octave")))
40 // Find the c in the range -4 through 2
41 int from_bottom_pos = c0p + 4;
42 from_bottom_pos = from_bottom_pos%7;
43 from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
44 int c0 = from_bottom_pos - 4;
47 if ((a<0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1))
49 (a>0 && ((p>SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2)))
51 p -= 7; /* Typeset below c_position */
53 /* Provide for the four cases in which there's a glitch
54 it's a hack, but probably not worth
55 the effort of finding a nicer solution.
57 if (c0==2 && a>0 && p==3)
59 if (c0==-3 && a>0 && p==-1)
61 if (c0==-4 && a<0 && p==-1)
63 if (c0==-2 && a<0 && p==-3)
72 - space the `natural' signs wider
74 MAKE_SCHEME_CALLBACK(Key_item,brew_molecule);
76 Key_item::brew_molecule (SCM smob)
78 Score_element*me =unsmob_element (smob);
81 Real inter = Staff_symbol_referencer::staff_space (me)/2.0;
83 SCM newas = me->get_elt_property ("new-accidentals");
86 SCM lists are stacks, so we work from right to left, ending with
87 the cancellation signature.
89 for (SCM s = newas; gh_pair_p (s); s = gh_cdr (s))
91 int a = gh_scm2int (gh_cdar (s));
92 Molecule m = me->lookup_l ()->afm_find ("accidentals-" + to_str (a));
93 m.translate_axis (calculate_position(me, gh_car (s)) * inter, Y_AXIS);
94 mol.add_at_edge (X_AXIS, LEFT, m, 0);
97 Item *it = dynamic_cast<Item*> (me) ;
98 if (it->break_status_dir () != RIGHT)
100 SCM old = me->get_elt_property ("old-accidentals");
102 Add half a space between cancellation and key sig.
104 As suggested by [Ross], p.148.
106 Interval x(0, inter);
109 mol.add_at_edge (X_AXIS, LEFT, me->lookup_l()->blank (Box(x,y)),0);
111 for (; gh_pair_p (old); old = gh_cdr (old))
116 find correspondences in pitches
118 for (SCM s = newas; gh_pair_p (s); s = gh_cdr (s))
119 if (gh_caar(s) == gh_caar (old))
122 if (found == SCM_EOL || gh_cdr (found) != gh_cdar (old))
124 Molecule m =me->lookup_l ()->afm_find ("accidentals-0");
126 m.translate_axis (calculate_position (me, gh_car (old)) * inter, Y_AXIS);
127 mol.add_at_edge (X_AXIS, LEFT, m,0);
132 return mol.create_scheme();
140 Key_item::has_interface (Score_element*m)
142 return m && m->has_interface (ly_symbol2scm ("key-signature-interface"));
146 Key_item::set_interface (Score_element*m)
148 m->set_interface (ly_symbol2scm ("key-signature-interface"));