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