]> git.donarmstrong.com Git - lilypond.git/blob - lily/molecule.cc
release: 1.3.29
[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
14 #include <math.h>
15
16 #include "dimensions.hh"
17 #include "interval.hh"
18 #include "string.hh"
19 #include "molecule.hh"
20 #include "atom.hh"
21 #include "debug.hh"
22 #include "killing-cons.tcc"
23
24
25 Box
26 Molecule::extent() const
27 {
28   return dim_;
29 }
30
31 Interval
32 Molecule::extent(Axis a) const
33 {
34   return dim_[a];
35 }
36
37 Molecule::Molecule (Box b, SCM func)
38 {
39   expr_ = func;
40   dim_ = b ;
41 }
42
43 Molecule::Molecule()
44 {
45   expr_ = SCM_EOL;
46   set_empty (true);
47 }
48
49 void
50 Molecule::translate (Offset o)
51 {
52   Axis a = X_AXIS;
53   while (a < NO_AXES)
54     {
55       if (abs(o[a]) > 30 CM
56           || isinf (o[a]) || isnan (o[a]))
57         {
58           programming_error ("Improbable offset for translation: setting to zero");
59           o[a] =  0.0;
60         }
61       incr (a);
62     }
63
64   expr_ = gh_list (ly_symbol2scm ("translate-molecule"),
65                    to_scm (o),
66                    expr_, SCM_UNDEFINED);
67   if (!empty_b ())
68     dim_.translate (o);
69 }
70   
71
72 void
73 Molecule::translate_axis (Real x,Axis a)
74 {
75   Offset o(0,0);
76   o[a] = x;
77   translate (o);
78 }  
79
80
81
82 void
83 Molecule::add_molecule (Molecule const &m)
84 {
85   expr_ = gh_list (ly_symbol2scm ("combine-molecule"),
86                    m.expr_,
87                    expr_, SCM_UNDEFINED);
88   dim_.unite (m.dim_);
89 }
90
91 void
92 Molecule::set_empty (bool e)
93 {
94   if (e)
95     {
96       dim_[X_AXIS].set_empty ();
97       dim_[Y_AXIS].set_empty ();
98     }
99   else
100     {
101       dim_[X_AXIS] = Interval(0,0);
102       dim_[Y_AXIS] = Interval (0,0);
103     }
104 }
105
106 void
107 Molecule::print () const
108 {
109 #ifndef NPRINT
110   gh_display (expr_);
111 #endif
112 }
113
114 void
115 Molecule::align_to (Axis a, Direction d)
116 {
117   Interval i (extent (a));
118   Real r =  (d == CENTER) ? i.center () : i[d];
119   translate_axis (-r, a);
120 }
121
122
123 void
124 Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
125 {
126   Real my_extent= empty_b () ? 0.0 : dim_[a][d];
127   Interval i (m.extent ()[a]);
128   if (i.empty_b ())
129     programming_error ("Molecule::add_at_edge: adding empty molecule.");
130   
131   Real his_extent = i[-d];
132   Real offset = my_extent -  his_extent;
133   Molecule toadd (m);
134   toadd.translate_axis (offset + d * padding, a);
135   add_molecule (toadd);
136 }
137
138 bool
139 Molecule::empty_b () const
140 {
141   return expr_ == SCM_EOL;
142 }