]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil.cc
(output_stencil): don't use
[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 (!ly_c_pair_p (expr))
202         return;
203   
204       SCM head =ly_car (expr);
205       if (unsmob_input (head))
206         {
207           Input *ip = unsmob_input (head);
208           (*func)(func_arg,
209                   scm_list_4 (ly_symbol2scm ("define-origin"),
210                               scm_makfrom0str (ip->file_string ()
211                                                .to_str0 ()),
212                               scm_int2num (ip->line_number ()),
213                               scm_int2num (ip->column_number ())));
214           
215           expr = ly_cadr (expr);
216         }
217       else  if (head ==  ly_symbol2scm ("no-origin"))
218         {
219           (*func) (func_arg, scm_list_1 (head));
220           expr = ly_cadr (expr);
221         }
222       else if (head == ly_symbol2scm ("translate-stencil"))
223         {
224           o += ly_scm2offset (ly_cadr (expr));
225           expr = ly_caddr (expr);
226         }
227       else if (head == ly_symbol2scm ("combine-stencil"))
228         {
229           for (SCM x = ly_cdr (expr); ly_c_pair_p (x); x = ly_cdr (x))
230             {
231               interpret_stencil_expression (ly_car (x), func, func_arg, o);
232             }
233           return ;
234         }
235       else
236         {
237           (*func) (func_arg, 
238                    scm_list_4 (ly_symbol2scm ("placebox"),
239                                scm_make_real (o[X_AXIS]),
240                                scm_make_real (o[Y_AXIS]),
241                                expr));
242           return;
243         }
244     }
245 }
246
247
248 struct Font_list
249 {
250   SCM fonts_;
251 };
252
253 static void
254 find_font_function (void * fs, SCM x)
255 {
256   Font_list * me = (Font_list*)fs;
257   
258   if (ly_car (x) == ly_symbol2scm ("placebox"))
259     {
260       SCM args = ly_cdr (x); 
261       SCM what = ly_caddr (args);
262
263       if (ly_c_pair_p (what))
264         {
265           SCM head = ly_car (what);
266           if (ly_symbol2scm ("text")  == head)
267             me->fonts_ = scm_cons (ly_cadr (what), me->fonts_);
268           else  if (head == ly_symbol2scm ("char"))
269             me->fonts_ = scm_cons (ly_cadr (what), me->fonts_);
270         }
271     }
272 }
273
274 SCM
275 find_expression_fonts (SCM expr)
276 {
277   Font_list fl;
278   
279   fl.fonts_ = SCM_EOL;
280   
281   interpret_stencil_expression (expr, &find_font_function, 
282                           (void*) &fl, Offset (0,0));
283
284   return fl.fonts_;
285 }
286
287
288 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
289            1, 0, 0, (SCM s),
290           " Analyse @var{s}, and return a list of fonts used in @var{s}.")
291 {
292   Stencil *stil = unsmob_stencil (s);
293   SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
294   return find_expression_fonts (stil->expr ());
295 }
296
297 struct Stencil_interpret_arguments {
298   SCM func;
299   SCM arg1;
300 };
301
302 void stencil_interpret_in_scm (void* p, SCM expr)
303 {
304   Stencil_interpret_arguments * ap = (Stencil_interpret_arguments*) p;
305   scm_call_2 (ap->func, ap->arg1, expr);
306 }
307
308
309
310 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
311            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
312            "Parse EXPR, feed bits to FUNC with first arg ARG1")
313 {
314   SCM_ASSERT_TYPE(ly_c_procedure_p(func), func, SCM_ARG1, __FUNCTION__,
315                   "procedure");
316   
317   Stencil_interpret_arguments a ;
318   a.func = func;
319   a.arg1 = arg1;
320   Offset o = ly_scm2offset (offset);
321
322   interpret_stencil_expression (expr, stencil_interpret_in_scm,
323                                 (void*) &a, o);
324
325   return SCM_UNSPECIFIED;
326 }