]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
release commit
[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 "input-smob.hh"
13 #include "font-metric.hh" 
14 #include "dimensions.hh"
15 #include "interval.hh"
16 #include "string.hh"
17 #include "stencil.hh"
18 #include "warn.hh"
19
20 #include "ly-smobs.icc"
21
22 Stencil::Stencil ()
23 {
24   expr_ = SCM_EOL;
25   set_empty (true);
26 }
27
28 Stencil::Stencil (Box b, SCM func)
29 {
30   expr_ = func;
31   dim_ = b;
32 }
33
34 int
35 Stencil::print_smob (SCM, SCM port, scm_print_state *)
36 {
37   scm_puts ("#<Stencil ", port);
38   scm_puts (" >", port);
39   return 1;
40 }
41
42 SCM
43 Stencil::mark_smob (SCM smob)
44 {
45   Stencil *s = (Stencil*) ly_cdr (smob);
46   return s->expr_;
47 }
48
49 IMPLEMENT_SIMPLE_SMOBS (Stencil);
50 IMPLEMENT_TYPE_P (Stencil, "ly:stencil?");
51 IMPLEMENT_DEFAULT_EQUAL_P (Stencil);
52
53 Interval
54 Stencil::extent (Axis a) const
55 {
56   return dim_[a];
57 }
58
59 /* Hmm... maybe this is not such a good idea ; stuff can be empty,
60    while expr_ == '()  */
61 bool
62 Stencil::is_empty () const
63 {
64   return expr_ == SCM_EOL;
65 }
66
67 SCM
68 Stencil::expr () const
69 {
70   return expr_;
71 }
72
73 Box
74 Stencil::extent_box () const
75 {
76   return dim_;
77 }
78 Offset
79 Stencil::origin () const
80 {
81   return origin_;
82 }
83
84 void
85 Stencil::translate (Offset o)
86 {
87   Axis a = X_AXIS;
88   while (a < NO_AXES)
89     {
90       if (abs (o[a]) > 100 CM
91           || isinf (o[a]) || isnan (o[a]))
92         {
93           programming_error ("Improbable offset for translation: setting to zero");
94           o[a] =  0.0;
95         }
96       incr (a);
97     }
98
99   expr_ = scm_list_n (ly_symbol2scm ("translate-stencil"),
100                       ly_offset2scm (o),
101                       expr_, SCM_UNDEFINED);
102   if (!is_empty ())
103     dim_.translate (o);
104   origin_ += o;
105 }
106   
107 void
108 Stencil::translate_axis (Real x, Axis a)
109 {
110   Offset o (0,0);
111   o[a] = x;
112   translate (o);
113 }
114
115 void
116 Stencil::add_stencil (Stencil const &s)
117 {
118   expr_ = scm_list_3 (ly_symbol2scm ("combine-stencil"), s.expr_, expr_);
119   dim_.unite (s.dim_);
120 }
121
122
123
124 void
125 Stencil::set_empty (bool e)
126 {
127   if (e)
128     {
129       dim_[X_AXIS].set_empty ();
130       dim_[Y_AXIS].set_empty ();
131     }
132   else
133     {
134       dim_[X_AXIS] = Interval (0,0);
135       dim_[Y_AXIS] = Interval (0,0);
136     }
137 }
138
139 void
140 Stencil::align_to (Axis a, Real x)
141 {
142   if (is_empty ())
143     return;
144
145   Interval i (extent (a));
146   translate_axis (-i.linear_combination (x), a);
147 }
148
149 /* FIXME: unintuitive naming, you would expect *this to be moved.
150    Kept (keeping?) API for compat with add_at_edge ().
151
152    What is PADDING, what is MINIMUM, exactly?  */
153 Stencil
154 Stencil::moved_to_edge (Axis a, Direction d, Stencil const &s,
155                         Real padding, Real minimum) const
156 {
157   Interval my_extent = dim_[a];
158   Interval i (s.extent (a));
159   Real his_extent;
160   if (i.is_empty ())
161     {
162       programming_error ("Stencil::moved_to_edge: adding empty stencil.");
163       his_extent = 0.0;
164     }
165   else
166     his_extent = i[-d];
167
168   Real offset = (my_extent.is_empty () ? 0.0 : my_extent[d] - his_extent)
169     + d * padding;
170
171   Stencil toadd (s);
172   toadd.translate_axis (offset,a);
173
174   if (minimum > 0 && d * (-origin ()[a] + toadd.origin ()[a]) < minimum)
175     toadd.translate_axis ( -toadd.origin ()[a]
176                            + origin ()[a] + d * minimum, a);
177
178   return toadd;
179 }
180
181 /*  See scheme Function.  */
182 void
183 Stencil::add_at_edge (Axis a, Direction d, Stencil const &s, Real padding,
184                       Real minimum)
185 {
186   add_stencil (moved_to_edge (a, d, s, padding, minimum));
187 }
188
189
190 /****************************************************************/
191
192
193 void
194 interpret_stencil_expression (SCM expr,
195                               void (*func) (void*, SCM),
196                               void *func_arg,
197                               Offset o)
198 {
199   while (1)
200     {
201       if (!scm_is_pair (expr))
202         return;
203
204       SCM head = ly_car (expr);
205      
206       if (head == ly_symbol2scm ("translate-stencil"))
207         {
208           o += ly_scm2offset (ly_cadr (expr));
209           expr = ly_caddr (expr);
210         }
211       else if (head == ly_symbol2scm ("combine-stencil"))
212         {
213           for (SCM x = ly_cdr (expr); scm_is_pair (x); x = ly_cdr (x))
214             interpret_stencil_expression (ly_car (x), func, func_arg, o);
215           return;
216         }
217       else if (head == ly_symbol2scm ("grob-cause"))
218         {
219           SCM grob = ly_cadr (expr);
220           
221           (*func) (func_arg, scm_list_2 (head, grob));
222           interpret_stencil_expression (ly_caddr (expr), func, func_arg, o);
223           (*func) (func_arg, scm_list_1 (ly_symbol2scm ("no-origin")));
224           
225           return ; 
226         }
227       else
228         {
229           (*func) (func_arg, 
230                    scm_list_4 (ly_symbol2scm ("placebox"),
231                                scm_make_real (o[X_AXIS]),
232                                scm_make_real (o[Y_AXIS]),
233                                expr));
234            return;
235         }
236     }
237 }
238
239
240 struct Font_list
241 {
242   SCM fonts_;
243 };
244
245 static void
246 find_font_function (void *fs, SCM x)
247 {
248   Font_list *me = (Font_list*) fs;
249
250   if (ly_car (x) == ly_symbol2scm ("placebox"))
251     {
252       SCM args = ly_cdr (x); 
253       SCM what = ly_caddr (args);
254
255       if (scm_is_pair (what))
256         {
257           SCM head = ly_car (what);
258           if (ly_symbol2scm ("text") == head)
259             me->fonts_ = scm_cons (ly_cadr (what), me->fonts_);
260           else if (head == ly_symbol2scm ("char"))
261             me->fonts_ = scm_cons (ly_cadr (what), me->fonts_);
262         }
263     }
264 }
265
266 SCM
267 find_expression_fonts (SCM expr)
268 {
269   Font_list fl;
270   
271   fl.fonts_ = SCM_EOL;
272   
273   interpret_stencil_expression (expr, &find_font_function, 
274                                 (void*) &fl, Offset (0,0));
275
276   return fl.fonts_;
277 }
278
279
280 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
281            1, 0, 0, (SCM s),
282           " Analyse @var{s}, and return a list of fonts used in @var{s}.")
283 {
284   Stencil *stil = unsmob_stencil (s);
285   SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
286   return find_expression_fonts (stil->expr ());
287 }
288
289 struct Stencil_interpret_arguments
290 {
291   SCM func;
292   SCM arg1;
293 };
294
295 void stencil_interpret_in_scm (void *p, SCM expr)
296 {
297   Stencil_interpret_arguments *ap = (Stencil_interpret_arguments*) p;
298   scm_call_2 (ap->func, ap->arg1, expr);
299 }
300
301
302
303 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
304            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
305            "Parse EXPR, feed bits to FUNC with first arg ARG1")
306 {
307   SCM_ASSERT_TYPE (ly_c_procedure_p(func), func, SCM_ARG1, __FUNCTION__,
308                    "procedure");
309
310   Stencil_interpret_arguments a;
311   a.func = func;
312   a.arg1 = arg1;
313   Offset o = ly_scm2offset (offset);
314
315   interpret_stencil_expression (expr, stencil_interpret_in_scm, (void*) &a, o);
316
317   return SCM_UNSPECIFIED;
318 }