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"
15 #include "font-interface.hh"
16 #include "staff-symbol-referencer.hh"
20 FIXME: too much hardcoding here.
22 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
23 const int SHARP_TOP_PITCH=4; /* ais and bis typeset in lower octave */
27 FIXME: key-item should just get a list of (position, acc), and leave
28 the thinking to other parties.
31 Key_item::calculate_position(Grob *ki, SCM pair)
33 int p = gh_scm2int (gh_car (pair));
34 int a = gh_scm2int (gh_cdr (pair));
35 int c0p = gh_scm2int (ki->get_grob_property ("c0-position"));
36 if (to_boolean (ki->get_grob_property ("multi-octave")))
41 // Find the c in the range -4 through 2
42 int from_bottom_pos = c0p + 4;
43 from_bottom_pos = from_bottom_pos%7;
44 from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
45 int c0 = from_bottom_pos - 4;
48 if ((a<0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1))
50 (a>0 && ((p>SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2)))
52 p -= 7; /* Typeset below c_position */
54 /* Provide for the four cases in which there's a glitch
55 it's a hack, but probably not worth
56 the effort of finding a nicer solution.
58 if (c0==2 && a>0 && p==3)
60 if (c0==-3 && a>0 && p==-1)
62 if (c0==-4 && a<0 && p==-1)
64 if (c0==-2 && a<0 && p==-3)
73 - space the `natural' signs wider
75 MAKE_SCHEME_CALLBACK(Key_item,brew_molecule,1);
77 Key_item::brew_molecule (SCM smob)
79 Grob*me =unsmob_grob (smob);
82 Real inter = Staff_symbol_referencer::staff_space (me)/2.0;
84 SCM newas = me->get_grob_property ("new-accidentals");
87 SCM lists are stacks, so we work from right to left, ending with
88 the cancellation signature.
90 for (SCM s = newas; gh_pair_p (s); s = gh_cdr (s))
92 int a = gh_scm2int (gh_cdar (s));
93 Molecule m = Font_interface::get_default_font (me)->find_by_name ("accidentals-" + to_str (a));
94 m.translate_axis (calculate_position(me, gh_car (s)) * inter, Y_AXIS);
95 mol.add_at_edge (X_AXIS, LEFT, m, 0);
98 Item *it = dynamic_cast<Item*> (me) ;
99 if (it->break_status_dir () != RIGHT)
101 SCM old = me->get_grob_property ("old-accidentals");
103 Add half a space between cancellation and key sig.
105 As suggested by [Ross], p.148.
107 Interval x(0, inter);
110 mol.add_at_edge (X_AXIS, LEFT, Lookup::blank (Box(x,y)),0);
112 for (; gh_pair_p (old); old = gh_cdr (old))
117 find correspondences in pitches
119 for (SCM s = newas; gh_pair_p (s); s = gh_cdr (s))
120 if (gh_caar(s) == gh_caar (old))
123 if (found == SCM_EOL || gh_cdr (found) != gh_cdar (old))
125 Molecule m =Font_interface::get_default_font (me)->find_by_name ("accidentals-0");
127 m.translate_axis (calculate_position (me, gh_car (old)) * inter, Y_AXIS);
128 mol.add_at_edge (X_AXIS, LEFT, m,0);
133 return mol.smobbed_copy();
141 Key_item::has_interface (Grob*m)
143 return m && m->has_interface (ly_symbol2scm ("key-signature-interface"));
147 Key_item::set_interface (Grob*m)
149 m->set_interface (ly_symbol2scm ("key-signature-interface"));