]> git.donarmstrong.com Git - lilypond.git/blob - lily/molecule.cc
release: 1.3.28
[lilypond.git] / lily / molecule.cc
1 /*
2   molecule.cc -- implement Molecule
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 /*
10   ugh. Rewrite not finished yet. Still must copy atom lists.
11  */
12
13 #include <math.h>
14
15 #include "interval.hh"
16 #include "string.hh"
17 #include "molecule.hh"
18 #include "atom.hh"
19 #include "debug.hh"
20 #include "killing-cons.tcc"
21
22
23 Box
24 Molecule::extent() const
25 {
26   return dim_;
27 }
28
29 Interval
30 Molecule::extent(Axis a) const
31 {
32   return dim_[a];
33 }
34
35 void
36 Molecule::translate (Offset o)
37 {
38   if (isinf (o.length ()))
39     {
40       programming_error ("Translating infinitely. Ignore.");
41       return;
42     }
43     
44   for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
45     {
46       gh_set_car_x (ptr, translate_atom (o, gh_car (ptr)));
47     }
48   if (!empty_b ())
49     dim_.translate (o);
50 }
51
52 void
53 Molecule::translate_axis (Real x,Axis a)
54 {
55   if (isinf (x))
56     {
57       programming_error ("Translating infinitely. Ignore.");
58       return;
59     }
60   for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
61     {
62       gh_set_car_x (ptr, translate_atom_axis (x, a, gh_car (ptr)));
63     }
64
65   if (!dim_[a].empty_b ())
66     dim_[a] += x;
67 }
68
69 void
70 Molecule::add_molecule (Molecule const &m)
71 {
72   for (SCM ptr = gh_cdr (m.atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
73     {
74       add_atom (gh_car (ptr));
75     }
76   dim_.unite (m.dim_);
77 }
78
79 void
80 Molecule::add_atom (SCM atomsmob)
81 {
82   gh_set_cdr_x (atom_list_,
83                 gh_cons  (atomsmob, gh_cdr (atom_list_)));
84 }
85
86 void
87 Molecule::operator=(Molecule const & src)
88 {
89   if (&src == this)
90     return;
91
92   atom_list_ = gh_cons (SCM_EOL,scm_list_copy (gh_cdr (src.atom_list_)));
93   dim_= src.dim_;
94 }
95
96 void
97 Molecule::set_empty (bool e)
98 {
99   if (e)
100     {
101       dim_[X_AXIS].set_empty ();
102       dim_[Y_AXIS].set_empty ();
103     }
104   else
105     {
106       dim_[X_AXIS] = Interval(0,0);
107       dim_[Y_AXIS] = Interval (0,0);
108     }
109 }
110
111 void
112 Molecule::print () const
113 {
114 #ifndef NPRINT
115   for (SCM ptr = gh_cdr (atom_list_);  ptr != SCM_EOL; ptr = gh_cdr(ptr))
116     gh_display (gh_car (ptr));
117 #endif
118 }
119
120 Molecule::Molecule (Molecule const &s)
121 {
122   atom_list_ = gh_cons (SCM_EOL, scm_list_copy (gh_cdr (s.atom_list_)));
123   dim_ = s.dim_;
124 }
125
126 Molecule::~Molecule ()
127 {
128 }
129
130
131 void
132 Molecule::align_to (Axis a, Direction d)
133 {
134   if (d == CENTER)
135     {
136       Interval i (extent (a));
137       translate_axis (-i.center (), a);
138     }
139   else
140     {
141       translate_axis (-extent (a)[d], a);
142     }
143 }
144
145 Molecule::Molecule ()
146 {
147   dim_[X_AXIS].set_empty ();
148   dim_[Y_AXIS].set_empty ();
149   atom_list_ = gh_cons (SCM_EOL, SCM_EOL);
150 }
151
152
153 void
154 Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
155 {
156   Real my_extent= empty_b () ? 0.0 : dim_[a][d];
157   Interval i (m.extent ()[a]);
158   if (i.empty_b ())
159     programming_error ("Molecule::add_at_edge: adding empty molecule.");
160   
161   Real his_extent = i[-d];
162   Real offset = my_extent -  his_extent;
163   Molecule toadd (m);
164   toadd.translate_axis (offset + d * padding, a);
165   add_molecule (toadd);
166 }
167
168 bool
169 Molecule::empty_b () const
170 {
171   return gh_cdr (atom_list_) == SCM_EOL;
172 }