]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-item.cc
patch::: 1.3.132.jcn4
[lilypond.git] / lily / local-key-item.cc
1 /*
2   local-key-item.cc -- implement Local_key_item, Pitch
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "local-key-item.hh"
9 #include "molecule.hh"
10 #include "staff-symbol-referencer.hh"
11 #include "font-interface.hh"
12 #include "paper-def.hh"
13 #include "musical-request.hh"
14 #include "rhythmic-head.hh"
15 #include "misc.hh"
16 #include "spanner.hh"
17 #include "tie.hh"
18 #include "lookup.hh"
19
20 static SCM
21 pitch_less  (SCM p1, SCM p2)
22 {
23   return Pitch::less_p (gh_car (p1),  gh_car (p2));
24 }
25
26 static SCM pitch_less_proc;
27
28 void
29 init_pitch_funcs ()
30 {
31   pitch_less_proc = gh_new_procedure2_0 ("pits-less", &pitch_less);
32 }
33
34 ADD_SCM_INIT_FUNC(lkpitch,init_pitch_funcs);
35
36
37 void
38 Local_key_item::add_pitch (Grob*me, Pitch p, bool cautionary, bool natural,
39                            Grob* tie_break_cautionary)
40 {
41   SCM acs = me->get_grob_property ("accidentals");
42   SCM pitch = p.smobbed_copy ();
43   SCM opts = SCM_EOL;
44   if (cautionary)
45     opts = gh_cons (ly_symbol2scm ("cautionary"), opts);
46   if (natural)
47     opts = gh_cons (ly_symbol2scm ("natural"), opts);
48   if (tie_break_cautionary)
49     {
50       /* Ugh, these 'options' can't have a value, faking... */
51       opts = gh_cons (tie_break_cautionary->self_scm (), opts);
52       opts = gh_cons (ly_symbol2scm ("tie-break-cautionary"), opts);
53     }
54
55   pitch = gh_cons (pitch, opts);
56   acs = scm_merge_x (acs, gh_cons (pitch, SCM_EOL), pitch_less_proc);
57
58   me->set_grob_property ("accidentals", acs);
59 }
60
61 Molecule
62 Local_key_item::parenthesize (Grob*me, Molecule m)
63 {
64   Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-("));
65   Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-)"));
66   m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
67   m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
68
69   return m;
70 }
71
72 /*
73   HW says: move to tie.cc
74
75   Ugh: this doesn't work: brew_molecule is called before line-breaking
76  */ 
77 MAKE_SCHEME_CALLBACK (Local_key_item, after_line_breaking, 1);
78 SCM
79 Local_key_item::after_line_breaking (SCM smob)
80 {
81   Grob *me = unsmob_grob (smob);
82
83   SCM accs = me->get_grob_property ("accidentals");
84   for  (SCM s = accs;
85         gh_pair_p (s); s = gh_cdr (s))
86     {
87       SCM opts = gh_cdar (s);
88
89       SCM t = scm_memq (ly_symbol2scm ("tie-break-cautionary"), opts);
90       if (t != SCM_BOOL_F)
91         {
92           Grob *tie = unsmob_grob (gh_cadr (t));
93           Spanner *sp = dynamic_cast<Spanner*> (tie);
94           if (!sp->original_l_)
95             {
96               /* there should be a better way to delete me */
97               scm_set_car_x (s, gh_list (gh_caar (s),
98                                          ly_symbol2scm ("deleted"),
99                                          SCM_UNDEFINED));
100               me->set_grob_property ("molecule", SCM_EOL);
101             }
102         }
103     }
104   
105   return SCM_UNSPECIFIED;
106 }
107
108 /*
109   UGH. clean me, revise placement routine (See Ross & Wanske;
110   accidental placement is more complicated than this.
111  */
112
113 MAKE_SCHEME_CALLBACK(Local_key_item,brew_molecule,1);
114 SCM
115 Local_key_item::brew_molecule (SCM smob)
116 {
117   Grob* me = unsmob_grob (smob);
118   
119   Molecule mol;
120
121   Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
122   Molecule octave_mol;
123   bool oct_b = false;
124   int lastoct = -100;
125
126   SCM accs = me->get_grob_property ("accidentals");
127   for  (SCM s = accs;
128         gh_pair_p (s); s = gh_cdr (s))
129     {
130       Pitch p (*unsmob_pitch (gh_caar (s)));
131       SCM opts = gh_cdar (s);
132       
133       if (scm_memq (ly_symbol2scm ("deleted"), opts) != SCM_BOOL_F)
134         continue;
135       
136       // do one octave
137       if (p.octave_i ()  != lastoct) 
138         {
139           if (oct_b)
140             {
141               Real dy =lastoct*7* note_distance;
142               octave_mol.translate_axis (dy, Y_AXIS);
143               mol.add_molecule (octave_mol);
144               octave_mol = Molecule ();
145             }
146           oct_b = true; 
147         }
148       
149       lastoct = p.octave_i () ;
150
151       SCM c0 =  me->get_grob_property ("c0-position");
152       Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
153         * note_distance;
154       
155       Molecule acc (Font_interface::get_default_font (me)->find_by_name (String ("accidentals-")
156                                                + to_str (p.alteration_i_)));
157       
158       if (scm_memq (ly_symbol2scm ("natural"), opts) != SCM_BOOL_F)
159         {
160           Molecule prefix = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-0"));
161           acc.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
162         }
163
164       if (scm_memq (ly_symbol2scm ("cautionary"), opts) != SCM_BOOL_F)
165         acc = parenthesize (me, acc);
166
167       acc.translate_axis (dy, Y_AXIS);
168       octave_mol.add_at_edge (X_AXIS, RIGHT, acc, 0);
169     }
170
171   if (oct_b)
172     {
173       Real dy =lastoct*7*note_distance;
174       octave_mol.translate_axis (dy, Y_AXIS);
175       mol.add_molecule (octave_mol);
176       octave_mol = Molecule ();
177     }
178   
179  if (gh_pair_p (accs))
180     {
181       Drul_array<SCM> pads;
182
183       /*
184         Use a cons?
185        */
186       pads[RIGHT] = me->get_grob_property ("right-padding");
187       pads[LEFT] = me->get_grob_property ("left-padding");
188
189
190       // unused ?
191       Direction d = LEFT;
192       do {
193         if (!gh_number_p (pads[d]))
194           continue;
195
196         Box b(Interval (0, gh_scm2double (pads[d]) * note_distance),
197               Interval (0,0));
198         Molecule m (Lookup::blank (b));
199         mol.add_at_edge (X_AXIS, d, m, 0);
200       } while ( flip (&d)!= LEFT);
201     }
202
203   return mol.smobbed_copy();
204 }
205
206 bool
207 Local_key_item::has_interface (Grob*m)
208 {
209   return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
210 }
211 void
212 Local_key_item::set_interface (Grob*m)
213 {
214   m->set_interface (ly_symbol2scm ("accidentals-interface"));
215 }