2 key-item.cc -- implement Key_item
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2002 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.
30 - TODO: put this in Scheme
32 - lots of values trivially shared (key doesn't change very
33 often). Compute those once, and use that as cache for the rest.
37 alteration_pos (SCM what, int alter, int c0p)
40 return gh_scm2int (ly_car (what)) * 7 + gh_scm2int (ly_cdr (what)) + c0p;
42 int p = gh_scm2int (what);
44 // Find the c in the range -4 through 2
45 int from_bottom_pos = c0p + 4;
46 from_bottom_pos = from_bottom_pos%7;
47 from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
48 int c0 = from_bottom_pos - 4;
51 if ((alter <0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1))
53 (alter >0 && ((p>SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2)))
55 p -= 7; /* Typeset below c_position */
57 /* Provide for the four cases in which there's a glitch
58 it's a hack, but probably not worth
59 the effort of finding a nicer solution.
61 if (c0==2 && alter >0 && p==3)
63 if (c0==-3 && alter>0 && p==-1)
65 if (c0==-4 && alter<0 && p==-1)
67 if (c0==-2 && alter<0 && p==-3)
75 - space the `natural' signs wider
77 MAKE_SCHEME_CALLBACK (Key_item,brew_molecule,1);
79 Key_item::brew_molecule (SCM smob)
81 Grob*me =unsmob_grob (smob);
83 Real inter = Staff_symbol_referencer::staff_space (me)/2.0;
85 SCM scm_style = me->get_grob_property ("style");
87 if (gh_symbol_p (scm_style))
89 style = ly_scm2string (scm_symbol_to_string (scm_style));
96 SCM newas = me->get_grob_property ("new-accidentals");
99 SCM c0s = me->get_grob_property ("c0-position");
101 if (gh_number_p (c0s))
102 c0p = gh_scm2int (c0s);
105 SCM lists are stacks, so we work from right to left, ending with
106 the cancellation signature.
109 for (SCM s = newas; gh_pair_p (s); s = ly_cdr (s))
111 SCM what = ly_caar (s);
112 int alter = gh_scm2int (ly_cdar (s));
113 int pos = alteration_pos (what, alter, c0p);
115 Molecule m = Font_interface::get_default_font (me)->
116 find_by_name (String ("accidentals-") + style + to_str (alter));
117 m.translate_axis (pos * inter, Y_AXIS);
118 mol.add_at_edge (X_AXIS, LEFT, m, 0);
121 Item *it = dynamic_cast<Item*> (me) ;
122 if (it->break_status_dir () != RIGHT)
124 SCM old = me->get_grob_property ("old-accidentals");
127 Add half a space between cancellation and key sig.
129 As suggested by [Ross], p.148.
131 Interval x (0, inter);
134 mol.add_at_edge (X_AXIS, LEFT, Lookup::blank (Box (x,y)),0);
138 natural=Font_interface::get_default_font (me)->
139 find_by_name (String ("accidentals-") + style + String ("0"));
141 for (; gh_pair_p (old); old = ly_cdr (old))
143 SCM found = scm_assoc (ly_caar (old), newas);
144 if (found == SCM_BOOL_F
145 || ly_cdr (found) != ly_cdar (old))
147 SCM what = ly_caar (old);
149 int pos = alteration_pos (what, alter, c0p);
151 Molecule m = natural;
152 m.translate_axis (pos* inter, Y_AXIS);
154 mol.add_at_edge (X_AXIS, LEFT, m, 0);
159 return mol.smobbed_copy ();
163 Key_item::has_interface (Grob*m)
165 return m && m->has_interface (ly_symbol2scm ("key-signature-interface"));
169 Key_item::set_interface (Grob*m)
171 m->set_interface (ly_symbol2scm ("key-signature-interface"));