]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-signature-interface.cc
make implementation for Class::has_interface automatically. Junk all
[lilypond.git] / lily / key-signature-interface.cc
1
2 /*
3   key-item.cc -- implement Key_signature_interface
4
5   source file of the GNU LilyPond music typesetter
6
7   (c) 1996--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9   keyplacement by Mats Bengtsson
10 */
11
12 #include "item.hh"
13
14 #include "molecule.hh"
15 #include "paper-def.hh"
16 #include "font-interface.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "lookup.hh"
19 #include "lily-guile.hh"
20 #include "lily-proto.hh"
21
22
23 struct Key_signature_interface
24 {
25   DECLARE_SCHEME_CALLBACK (brew_molecule, (SCM ));
26
27   static bool has_interface (Grob*);
28 };
29
30
31 /*
32   FIXME: too much hardcoding here.
33  */
34 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
35 const int SHARP_TOP_PITCH=4; /*  ais and bis typeset in lower octave */
36
37
38 /*
39   FIXME: key-item should just get a list of (position, acc), and leave
40   the thinking to other parties.
41
42   - TODO: put this in Scheme
43   
44   - lots of values trivially shared (key doesn't change very
45   often). Compute those once, and use that as cache for the rest.
46
47 */
48 int
49 alteration_pos  (SCM what, int alter, int c0p)
50 {
51   if (gh_pair_p (what))
52     return gh_scm2int (ly_car (what)) * 7 + gh_scm2int (ly_cdr (what)) + c0p;
53
54   int p = gh_scm2int (what);
55
56   // Find the c in the range -4 through 2
57   int from_bottom_pos = c0p + 4;
58   from_bottom_pos = from_bottom_pos%7;
59   from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
60   int c0 = from_bottom_pos - 4;
61
62     
63   if ((alter <0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1)) 
64       ||
65       (alter >0 && ((p>SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2))) 
66     {
67       p -= 7; /* Typeset below c_position */
68     }
69   /* Provide for the four cases in which there's a glitch 
70        it's a hack, but probably not worth  
71        the effort of finding a nicer solution.
72        --dl. */
73   if (c0==2 && alter >0 && p==3)
74     p -= 7;
75   if (c0==-3 && alter>0 && p==-1)
76     p += 7;
77   if (c0==-4 && alter<0 && p==-1)
78     p += 7;
79   if (c0==-2 && alter<0 && p==-3)
80     p += 7;
81     
82   return p + c0;
83 }
84
85 /*
86   TODO
87   - space the `natural' signs wider
88  */
89 MAKE_SCHEME_CALLBACK (Key_signature_interface,brew_molecule,1);
90 SCM
91 Key_signature_interface::brew_molecule (SCM smob)
92 {
93   Grob*me =unsmob_grob (smob);
94
95   Real inter = Staff_symbol_referencer::staff_space (me)/2.0;
96   
97   SCM scm_style = me->get_grob_property ("style");
98   String style;
99   if (gh_symbol_p (scm_style))
100     {
101       style = ly_scm2string (scm_symbol_to_string (scm_style));
102     }
103   else
104     {
105       style = "";
106     }
107
108   SCM newas = me->get_grob_property ("new-accidentals");  
109   Molecule mol;
110
111   SCM c0s = me->get_grob_property ("c0-position");
112   int c0p=0;
113   if (gh_number_p (c0s))
114      c0p = gh_scm2int (c0s);
115
116   /*
117     SCM lists are stacks, so we work from right to left, ending with
118     the cancellation signature.
119   */
120
121   for (SCM s = newas; gh_pair_p (s); s = ly_cdr (s))
122     {
123       SCM what = ly_caar (s);
124       int alter = gh_scm2int (ly_cdar (s));
125       int pos = alteration_pos (what, alter, c0p);
126       
127       Molecule m = Font_interface::get_default_font (me)->
128           find_by_name (String ("accidentals-") + style + to_str (alter));
129       m.translate_axis (pos * inter, Y_AXIS);
130       mol.add_at_edge (X_AXIS, LEFT, m, 0);
131     }
132
133   Item *it = dynamic_cast<Item*> (me) ;
134   if (it->break_status_dir () != RIGHT)
135     {
136       SCM old = me->get_grob_property ("old-accidentals");
137       
138       /*
139         Add half a space between  cancellation and key sig.
140
141         As suggested by [Ross], p.148.
142        */
143       Interval x (0, inter);
144       Interval y (0,0);
145
146       mol.add_at_edge (X_AXIS, LEFT, Lookup::blank (Box (x,y)),0);
147
148       Molecule natural;
149       if (gh_pair_p (old))
150         natural=Font_interface::get_default_font (me)->
151             find_by_name (String ("accidentals-") + style + String ("0"));
152       
153       for (; gh_pair_p (old); old = ly_cdr (old))
154         {
155           SCM found = scm_assoc (ly_caar (old), newas);
156           if (found == SCM_BOOL_F
157               || ly_cdr (found) != ly_cdar (old))
158             {
159               SCM what = ly_caar (old);
160               int alter = 0;
161               int pos = alteration_pos (what, alter, c0p);
162
163               Molecule m = natural;
164               m.translate_axis (pos* inter, Y_AXIS);
165
166               mol.add_at_edge (X_AXIS, LEFT, m, 0);
167             }
168         }
169     }
170
171   mol.align_to (X_AXIS, LEFT);
172   
173   return mol.smobbed_copy ();
174 }
175
176 ADD_INTERFACE (Key_signature_interface, "key-signature-interface",
177   "A group of  accidentals.",
178   "c0-position old-accidentals new-accidentals");