]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
* lily/book.cc (to_stencil): New method.
[lilypond.git] / lily / stencil.cc
1 /*
2   stencil.cc -- implement Stencil
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 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 "stencil.hh"
17 #include "warn.hh"
18
19 #include "ly-smobs.icc"
20
21 Stencil::Stencil ()
22 {
23   expr_ = SCM_EOL;
24   set_empty (true);
25 }
26
27 Stencil::Stencil (Box b, SCM func)
28 {
29   expr_ = func;
30   dim_ = b;
31 }
32
33 int
34 Stencil::print_smob (SCM, SCM port, scm_print_state *)
35 {
36   scm_puts ("#<Stencil ", port);
37   scm_puts (" >", port);
38   return 1;
39 }
40
41 SCM
42 Stencil::mark_smob (SCM smob)
43 {
44   Stencil *s = (Stencil*) ly_cdr (smob);
45   return s->expr_;
46 }
47
48 IMPLEMENT_SIMPLE_SMOBS (Stencil);
49 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
50 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
51
52 Interval
53 Stencil::extent (Axis a) const
54 {
55   return dim_[a];
56 }
57
58 /* Hmm... maybe this is not such a good idea ; stuff can be empty,
59    while expr_ == '()  */
60 bool
61 Stencil::is_empty () const
62 {
63   return expr_ == SCM_EOL;
64 }
65
66 SCM
67 Stencil::expr () const
68 {
69   return expr_;
70 }
71
72 Box
73 Stencil::extent_box () const
74 {
75   return dim_;
76 }
77 Offset
78 Stencil::origin () const
79 {
80   return origin_;
81 }
82
83 void
84 Stencil::translate (Offset o)
85 {
86   Axis a = X_AXIS;
87   while (a < NO_AXES)
88     {
89       if (abs (o[a]) > 100 CM
90           || isinf (o[a]) || isnan (o[a]))
91         {
92           programming_error ("Improbable offset for translation: setting to zero");
93           o[a] =  0.0;
94         }
95       incr (a);
96     }
97
98   expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
99                       ly_offset2scm (o),
100                       expr_, SCM_UNDEFINED);
101   if (!is_empty ())
102     dim_.translate (o);
103   origin_ += o;
104 }
105   
106 void
107 Stencil::translate_axis (Real x, Axis a)
108 {
109   Offset o (0,0);
110   o[a] = x;
111   translate (o);
112 }
113
114 void
115 Stencil::add_stencil (Stencil const &s)
116 {
117   expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
118   dim_.unite (s.dim_);
119 }
120
121 void
122 Stencil::set_empty (bool e)
123 {
124   if (e)
125     {
126       dim_[X_AXIS].set_empty ();
127       dim_[Y_AXIS].set_empty ();
128     }
129   else
130     {
131       dim_[X_AXIS] = Interval (0,0);
132       dim_[Y_AXIS] = Interval (0,0);
133     }
134 }
135
136 void
137 Stencil::align_to (Axis a, Real x)
138 {
139   if (is_empty ())
140     return ;
141
142   Interval i (extent (a));
143   translate_axis (-i.linear_combination (x), a);
144 }
145
146 /* FIXME: unintuitive naming, you would expect *this to be moved.
147    Kept (keeping?) API for compat with add_at_edge ().
148
149    What is PADDING, what is MINIMUM, exactly?  */
150 Stencil
151 Stencil::moved_to_edge (Axis a, Direction d, Stencil const &s,
152                         Real padding, Real minimum) const
153 {
154   Interval my_extent = dim_[a];
155   Interval i (s.extent (a));
156   Real his_extent;
157   if (i.is_empty ())
158     {
159       programming_error ("Stencil::moved_to_edge: adding empty stencil.");
160       his_extent = 0.0;
161     }
162   else
163     his_extent = i[-d];
164
165   Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
166     + d * padding;
167
168   Stencil toadd (s);
169   toadd.translate_axis (offset,a);
170
171   if (minimum > 0 && d * (-origin ()[a] + toadd.origin ()[a]) < minimum)
172     toadd.translate_axis ( -toadd.origin ()[a]
173                            + origin ()[a] + d * minimum, a);
174
175   return toadd;
176 }
177
178 /*  See scheme Function.  */
179 void
180 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding,
181                       Real minimum)
182 {
183   add_stencil (moved_to_edge (a, d, s, padding, minimum));
184 }