]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-item.cc
patch::: 1.3.132.jcn3
[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->broken_into_l_arr_.size ())
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             }
101         }
102     }
103   
104   return SCM_UNSPECIFIED;
105 }
106
107 /*
108   UGH. clean me, revise placement routine (See Ross & Wanske;
109   accidental placement is more complicated than this.
110  */
111
112 MAKE_SCHEME_CALLBACK(Local_key_item,brew_molecule,1);
113 SCM
114 Local_key_item::brew_molecule (SCM smob)
115 {
116   Grob* me = unsmob_grob (smob);
117   
118   Molecule mol;
119
120   Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
121   Molecule octave_mol;
122   bool oct_b = false;
123   int lastoct = -100;
124
125   SCM accs = me->get_grob_property ("accidentals");
126   for  (SCM s = accs;
127         gh_pair_p (s); s = gh_cdr (s))
128     {
129       Pitch p (*unsmob_pitch (gh_caar (s)));
130       SCM opts = gh_cdar (s);
131       
132       if (scm_memq (ly_symbol2scm ("deleted"), opts) != SCM_BOOL_F)
133         continue;
134       
135       // do one octave
136       if (p.octave_i ()  != lastoct) 
137         {
138           if (oct_b)
139             {
140               Real dy =lastoct*7* note_distance;
141               octave_mol.translate_axis (dy, Y_AXIS);
142               mol.add_molecule (octave_mol);
143               octave_mol = Molecule ();
144             }
145           oct_b = true; 
146         }
147       
148       lastoct = p.octave_i () ;
149
150       SCM c0 =  me->get_grob_property ("c0-position");
151       Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
152         * note_distance;
153       
154       Molecule acc (Font_interface::get_default_font (me)->find_by_name (String ("accidentals-")
155                                                + to_str (p.alteration_i_)));
156       
157       if (scm_memq (ly_symbol2scm ("natural"), opts) != SCM_BOOL_F)
158         {
159           Molecule prefix = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-0"));
160           acc.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
161         }
162
163       if (scm_memq (ly_symbol2scm ("cautionary"), opts) != SCM_BOOL_F)
164         acc = parenthesize (me, acc);
165
166       acc.translate_axis (dy, Y_AXIS);
167       octave_mol.add_at_edge (X_AXIS, RIGHT, acc, 0);
168     }
169
170   if (oct_b)
171     {
172       Real dy =lastoct*7*note_distance;
173       octave_mol.translate_axis (dy, Y_AXIS);
174       mol.add_molecule (octave_mol);
175       octave_mol = Molecule ();
176     }
177   
178  if (gh_pair_p (accs))
179     {
180       Drul_array<SCM> pads;
181
182       /*
183         Use a cons?
184        */
185       pads[RIGHT] = me->get_grob_property ("right-padding");
186       pads[LEFT] = me->get_grob_property ("left-padding");
187
188
189       // unused ?
190       Direction d = LEFT;
191       do {
192         if (!gh_number_p (pads[d]))
193           continue;
194
195         Box b(Interval (0, gh_scm2double (pads[d]) * note_distance),
196               Interval (0,0));
197         Molecule m (Lookup::blank (b));
198         mol.add_at_edge (X_AXIS, d, m, 0);
199       } while ( flip (&d)!= LEFT);
200     }
201
202   return mol.smobbed_copy();
203 }
204
205 bool
206 Local_key_item::has_interface (Grob*m)
207 {
208   return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
209 }
210 void
211 Local_key_item::set_interface (Grob*m)
212 {
213   m->set_interface (ly_symbol2scm ("accidentals-interface"));
214 }