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