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