]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-item.cc
release: 0.1.61
[lilypond.git] / lily / local-key-item.cc
1 /*
2   local-key-item.cc -- implement Local_key_item, Local_acc
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1998 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8 #include "dimen.hh"
9 #include "local-key-item.hh"
10 #include "molecule.hh"
11 #include "scalar.hh"
12 #include "lookup.hh"
13 #include "paper-def.hh"
14 #include "musical-request.hh"
15 #include "note-head.hh"
16 #include "misc.hh"
17
18
19
20 Local_key_item::Local_key_item (int i)
21 {
22   c0_position  = i;
23 }
24
25 void
26 Local_key_item::add_support (Item*head_l)
27 {
28   if (support_items_.find_l(head_l))
29     return ;
30   support_items_.push (head_l);
31   add_dependency (head_l);
32 }
33
34 void
35 Local_key_item::add (Melodic_req*m_l)
36 {
37   add (m_l->octave_i_, m_l->notename_i_, m_l->accidental_i_);
38 }
39
40 void
41 Local_key_item::add (int o, int p , int a)
42 {
43   Local_acc l;
44   l.octave_i_ = o;
45   l.name_i_ = p;
46   l.accidental_i_ = a;
47   for (int i=0; i< accs.size(); i++)
48     if (!Local_acc::compare (l, accs[i]))
49       return;
50   
51   accs.push (l);
52 }
53
54 void
55 Local_key_item::do_pre_processing()
56 {
57   accs.sort (Local_acc::compare);
58 }
59
60 Molecule*
61 Local_key_item::brew_molecule_p() const
62 {
63   Molecule*output = new Molecule;
64
65   Molecule *octave_mol_p = 0;
66   int lastoct = -100;
67   for  (int i = 0; i <  accs.size(); i++) 
68     {
69       // do one octave
70       if (accs[i].octave_i_ != lastoct) 
71         {
72           if (octave_mol_p)
73             {
74               Real dy =lastoct*7*paper()->internote_f ();
75               octave_mol_p->translate_axis (dy, Y_AXIS);
76               output->add (*octave_mol_p);
77               delete octave_mol_p;
78             }
79           octave_mol_p= new Molecule;
80         }
81       lastoct = accs[i].octave_i_;
82       Real dy = (accs[i].name_i_ + c0_position) * paper()->internote_f ();
83       Atom a (paper()->lookup_l ()->accidental (accs[i].accidental_i_));
84
85       a.translate_axis (dy, Y_AXIS);
86       Molecule m(a);
87       octave_mol_p->add_at_edge (X_AXIS, RIGHT, m);
88     }
89
90   if (octave_mol_p)
91     {
92       Real dy =lastoct*7*paper()->internote_f ();
93       octave_mol_p->translate_axis (dy, Y_AXIS);
94       output->add (*octave_mol_p);
95       delete octave_mol_p;
96     }
97   
98  if (accs.size()) 
99     {
100       Box b(Interval (0, paper()->internote_f ()), Interval (0,0));
101       Molecule m (paper()->lookup_l ()->fill (b));
102       output->add_at_edge (X_AXIS, RIGHT, m);
103     }
104
105   Interval x_int;
106   for (int i=0; i < support_items_.size(); i++) 
107     {
108       Axis_group_element *common = 
109         common_group (support_items_[i], X_AXIS);
110
111       Real x = support_items_[i]->relative_coordinate (common, X_AXIS)  
112         -relative_coordinate (common, X_AXIS);
113
114       x_int.unite (x + support_items_[i]->width());
115     }
116   if (x_int.empty_b ())
117     x_int = Interval(0,0);
118   
119   output->translate_axis (-output->extent()[X_AXIS][RIGHT] + x_int[LEFT], X_AXIS);
120   
121   return output;
122 }
123
124 int
125 Local_acc::compare (Local_acc&a, Local_acc&b)
126 {
127   if (a.octave_i_ - b.octave_i_)
128     return a.octave_i_ - b.octave_i_;
129   if (a.name_i_ - b.name_i_)
130     return a.name_i_ - b.name_i_;
131   
132   return a.accidental_i_ - b.accidental_i_;
133 };
134
135 IMPLEMENT_IS_TYPE_B1(Local_key_item,Item);
136
137 void
138 Local_key_item::do_substitute_dependency (Score_elem*o,Score_elem*n)
139 {
140   Item* o_l = o->item();
141   Item* n_l = n?n->item():0;
142
143   support_items_.substitute (o_l, n_l);
144 }