]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-item.cc
release: 1.3.110
[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--2000 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 "lookup.hh"
17
18 static SCM
19 pitch_less  (SCM p1, SCM p2)
20 {
21   return Pitch::less_p (gh_car (p1),  gh_car (p2));
22 }
23
24 static SCM pitch_less_proc;
25
26 void
27 init_pitch_funcs ()
28 {
29   pitch_less_proc = gh_new_procedure2_0 ("pits-less", &pitch_less);
30 }
31
32 ADD_SCM_INIT_FUNC(lkpitch,init_pitch_funcs);
33
34
35 void
36 Local_key_item::add_pitch (Grob*me, Pitch p, bool cautionary, bool natural)
37 {
38   SCM acs = me->get_grob_property ("accidentals");
39   SCM pitch = p.smobbed_copy ();
40   SCM opts = SCM_EOL;
41   if (cautionary)
42     opts = gh_cons (ly_symbol2scm ("cautionary"), opts);
43   if (natural)
44     opts = gh_cons (ly_symbol2scm ("natural"), opts);
45
46   pitch = gh_cons (pitch, opts);
47   acs = scm_merge_x (acs, gh_cons (pitch, SCM_EOL), pitch_less_proc);
48
49   me->set_grob_property ("accidentals", acs);
50 }
51
52 Molecule
53 Local_key_item::parenthesize (Grob*me, Molecule m)
54 {
55   Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-("));
56   Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-)"));
57   m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
58   m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
59
60   return m;
61 }
62
63 /*
64   UGH. clean me, revise placement routine (See Ross & Wanske;
65   accidental placement is more complicated than this.
66  */
67
68 MAKE_SCHEME_CALLBACK(Local_key_item,brew_molecule,1);
69 SCM
70 Local_key_item::brew_molecule (SCM smob)
71 {
72   Grob* me = unsmob_grob (smob);
73   
74   Molecule mol;
75
76   Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
77   Molecule octave_mol;
78   bool oct_b = false;
79   int lastoct = -100;
80
81   SCM accs = me->get_grob_property ("accidentals");
82   for  (SCM s = accs;
83         gh_pair_p (s); s = gh_cdr (s))
84     {
85       Pitch p (*unsmob_pitch (gh_caar (s)));
86       SCM opts = gh_cdar (s);
87       
88       // do one octave
89       if (p.octave_i ()  != lastoct) 
90         {
91           if (oct_b)
92             {
93               Real dy =lastoct*7* note_distance;
94               octave_mol.translate_axis (dy, Y_AXIS);
95               mol.add_molecule (octave_mol);
96               octave_mol = Molecule ();
97             }
98           oct_b = true; 
99         }
100       
101       lastoct = p.octave_i () ;
102
103       SCM c0 =  me->get_grob_property ("c0-position");
104       Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
105         * note_distance;
106       
107       Molecule acc (Font_interface::get_default_font (me)->find_by_name (String ("accidentals-")
108                                                + to_str (p.alteration_i_)));
109       
110       if (scm_memq (ly_symbol2scm ("natural"), opts) != SCM_BOOL_F)
111         {
112           Molecule prefix = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-0"));
113           acc.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
114         }
115
116       if (scm_memq (ly_symbol2scm ("cautionary"), opts) != SCM_BOOL_F)
117         acc = parenthesize (me, acc);
118
119       acc.translate_axis (dy, Y_AXIS);
120       octave_mol.add_at_edge (X_AXIS, RIGHT, acc, 0);
121     }
122
123   if (oct_b)
124     {
125       Real dy =lastoct*7*note_distance;
126       octave_mol.translate_axis (dy, Y_AXIS);
127       mol.add_molecule (octave_mol);
128       octave_mol = Molecule ();
129     }
130   
131  if (gh_pair_p (accs))
132     {
133       Drul_array<SCM> pads;
134
135       /*
136         Use a cons?
137        */
138       pads[RIGHT] = me->get_grob_property ("right-padding");
139       pads[LEFT] = me->get_grob_property ("left-padding");
140
141
142       // unused ?
143       Direction d = LEFT;
144       do {
145         if (!gh_number_p (pads[d]))
146           continue;
147
148         Box b(Interval (0, gh_scm2double (pads[d]) * note_distance),
149               Interval (0,0));
150         Molecule m (Lookup::blank (b));
151         mol.add_at_edge (X_AXIS, d, m, 0);
152       } while ( flip (&d)!= LEFT);
153     }
154
155   return mol.smobbed_copy();
156 }
157
158 bool
159 Local_key_item::has_interface (Grob*m)
160 {
161   return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
162 }
163 void
164 Local_key_item::set_interface (Grob*m)
165 {
166   m->set_interface (ly_symbol2scm ("accidentals-interface"));
167 }