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