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