]> git.donarmstrong.com Git - lilypond.git/blob - lily/molecule.cc
release: 1.3.105
[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 #include <math.h>
10 #include <libc-extension.hh>
11
12 #include "font-metric.hh" 
13 #include "dimensions.hh"
14 #include "interval.hh"
15 #include "string.hh"
16 #include "molecule.hh"
17 #include "debug.hh"
18 #include "killing-cons.tcc"
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]) > 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                    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_ = 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
107 void
108 Molecule::align_to (Axis a, Direction d)
109 {
110   Interval i (extent (a));
111   Real r =  (d == CENTER) ? i.center () : i[d];
112   translate_axis (-r, a);
113 }
114
115 void
116 Molecule::add_at_edge (Axis a, Direction d, Molecule const &m, Real padding)
117 {
118   Real my_extent= empty_b () ? 0.0 : dim_[a][d];
119   Interval i (m.extent (a));
120   Real his_extent;
121   if (i.empty_b ())
122     {
123       programming_error ("Molecule::add_at_edge: adding empty molecule.");
124       his_extent = 0.0;
125     }
126   else
127     his_extent = i[-d];      
128
129   Real offset = my_extent -  his_extent;
130   Molecule toadd (m);
131   toadd.translate_axis (offset + d * padding, a);
132   add_molecule (toadd);
133 }
134
135 bool
136 Molecule::empty_b () const
137 {
138   return expr_ == SCM_EOL;
139 }
140
141 SCM
142 fontify_atom(Font_metric * met, SCM f)
143 {
144   if (f == SCM_EOL)
145     return f;
146   else
147     return  gh_list (ly_symbol2scm ("fontify"),
148                      ly_quote_scm (met->description_), f, SCM_UNDEFINED);
149 }
150
151 SCM
152 Molecule::get_expr () const
153 {
154   return expr_;
155 }
156
157
158
159 Box
160 Molecule::extent_box () const
161 {
162   return dim_;
163 }
164 IMPLEMENT_SIMPLE_SMOBS(Molecule);
165
166
167 int
168 Molecule::print_smob (SCM s, SCM port, scm_print_state *)
169 {
170   Molecule  *r = (Molecule *) gh_cdr (s);
171      
172   scm_puts ("#<Molecule ", port);
173   /*  String str(r->str());
174   scm_puts ((char *)str.ch_C(), port);*/
175   scm_puts (" >", port);
176   
177   return 1;
178 }
179
180   
181 SCM
182 Molecule::mark_smob (SCM s)
183 {
184   Molecule  *r = (Molecule *) gh_cdr (s);
185   
186   return r->expr_;
187 }
188
189 IMPLEMENT_TYPE_P(Molecule, "molecule?");
190 IMPLEMENT_DEFAULT_EQUAL_P(Molecule);
191 IMPLEMENT_UNSMOB(Molecule, molecule);