]> git.donarmstrong.com Git - lilypond.git/blob - lily/molecule.cc
string() -> to_string()
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10 #include <libc-extension.hh>    // isinf
11
12 #include "font-metric.hh" 
13 #include "dimensions.hh"
14 #include "interval.hh"
15 #include "string.hh"
16 #include "molecule.hh"
17 #include "warn.hh"
18
19
20 #include "ly-smobs.icc"
21
22
23 SCM
24 Molecule::smobbed_copy () const
25 {
26   Molecule * m = new Molecule (*this);
27
28   return m->smobbed_self ();
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]) > 100 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_ = scm_list_n (ly_symbol2scm ("translate-molecule"),
65                    ly_offset2scm (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_ = scm_list_n (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
107 void
108 Molecule::align_to (Axis a, Direction d)
109 {
110   if (empty_b())
111     return ;
112
113   Interval i (extent (a));
114   Real r = (d == CENTER) ? i.center () : i[d];
115   translate_axis (-r, a);
116 }
117
118 /*
119   See scheme Function.
120  */
121 void
122 Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding,
123                        Real minimum)
124 {
125   Real my_extent= empty_b () ? 0.0 : dim_[a][d];
126   Interval i (m.extent (a));
127   Real his_extent;
128   if (i.empty_b ())
129     {
130       programming_error ("Molecule::add_at_edge: adding empty molecule.");
131       his_extent = 0.0;
132     }
133   else
134     his_extent = i[-d];      
135
136   Real offset = (my_extent -  his_extent)  + d*padding;
137   if (minimum > 0  && fabs (offset) <  minimum)
138     offset = sign (offset) * minimum; 
139   
140   Molecule toadd (m);
141   toadd.translate_axis (offset, a);
142   add_molecule (toadd);
143 }
144
145
146
147 /*
148   Hmm... maybe this is not such a good idea ; stuff can be empty,
149   while expr_ == '()
150  */
151 bool
152 Molecule::empty_b () const
153 {
154   return expr_ == SCM_EOL;
155 }
156
157 SCM
158 Molecule::get_expr () const
159 {
160   return expr_;
161 }
162
163
164
165 Box
166 Molecule::extent_box () const
167 {
168   return dim_;
169 }
170 IMPLEMENT_SIMPLE_SMOBS (Molecule);
171
172
173 int
174 Molecule::print_smob (SCM , SCM port, scm_print_state *)
175 {
176   scm_puts ("#<Molecule ", port);
177 #if 0
178   Molecule  *r = (Molecule *) ly_cdr (s);
179   String string (r->to_string ());
180   scm_puts ((char *)str.to_str0 (), port);
181 #endif
182   scm_puts (" >", port);
183   
184   return 1;
185 }
186
187   
188 SCM
189 Molecule::mark_smob (SCM s)
190 {
191   Molecule  *r = (Molecule *) ly_cdr (s);
192   
193   return r->expr_;
194 }
195
196 IMPLEMENT_TYPE_P (Molecule, "ly:molecule?");
197 IMPLEMENT_DEFAULT_EQUAL_P (Molecule);
198