]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
*** empty log message ***
[lilypond.git] / lily / stencil-scheme.cc
1 /*
2   stencil-scheme.cc -- implement Stencil scheme accessors
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
11 #include <libc-extension.hh>    // isinf
12
13 #include "font-metric.hh"
14 #include "stencil.hh"
15 #include "lookup.hh"
16
17 /*
18   TODO: naming add/combine.
19  */
20 /*
21   UGH. Junk all mutators.
22  */
23 LY_DEFINE (ly_stencil_set_extent_x, "ly:stencil-set-extent!",
24            3, 0, 0, (SCM stil, SCM axis, SCM np),
25            "Set the extent of @var{stil} "
26            "(@var{extent} must be a pair of numbers) "
27            "in @var{axis} direction (0 or 1 for x- and y-axis respectively).")
28 {
29   Stencil *s = unsmob_stencil (stil);
30   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
31   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
32   SCM_ASSERT_TYPE (is_number_pair (np), np, SCM_ARG3, __FUNCTION__,
33                    "number pair");
34
35   Interval iv = ly_scm2interval (np);
36   s->dim_[Axis (scm_to_int (axis))] = iv;
37
38   return SCM_UNSPECIFIED;
39 }
40
41 LY_DEFINE (ly_translate_stencil_axis, "ly:stencil-translate-axis",
42            3, 0, 0, (SCM stil, SCM amount, SCM axis),
43            "Return a copy of @var{stil} but translated by @var{amount} in @var{axis} direction.")
44 {
45   Stencil *s = unsmob_stencil (stil);
46   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
47   SCM_ASSERT_TYPE (scm_is_number (amount), amount, SCM_ARG2, __FUNCTION__, "number");
48
49   Real real_amount = scm_to_double (amount);
50   SCM_ASSERT_TYPE (!isinf (real_amount) && !isnan (real_amount),
51                    amount, SCM_ARG2, __FUNCTION__, "finite number");
52   
53   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
54
55   SCM new_s = s->smobbed_copy ();
56   Stencil *q = unsmob_stencil (new_s);
57   q->translate_axis (real_amount, Axis (scm_to_int (axis)));
58   return new_s;
59
60 }
61
62 LY_DEFINE (ly_translate_stencil, "ly:stencil-translate",
63            2, 0, 0, (SCM stil, SCM offset),
64            "Return a @var{stil}, "
65            "but translated by @var{offset} (a pair of numbers).")
66 {
67   Stencil *s = unsmob_stencil (stil);
68   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
69   SCM_ASSERT_TYPE (is_number_pair (offset), offset, SCM_ARG2, __FUNCTION__, "number pair");
70   Offset o = ly_scm2offset (offset);
71
72   SCM new_s = s->smobbed_copy ();
73   Stencil *q = unsmob_stencil (new_s);
74   q->translate (o);
75   return new_s;
76 }
77
78 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
79            1, 0, 0, (SCM stil),
80            "Return the expression of @var{stil}.")
81 {
82   Stencil *s = unsmob_stencil (stil);
83   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
84   return s->expr ();
85 }
86
87 LY_DEFINE (ly_stencil_get_extent, "ly:stencil-extent",
88            2, 0, 0, (SCM stil, SCM axis),
89            "Return a pair of numbers signifying the extent of @var{stil} in "
90            "@var{axis} direction (0 or 1 for x and y axis respectively).")
91 {
92   Stencil *s = unsmob_stencil (stil);
93   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
94   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
95
96   return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
97 }
98
99
100 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
101            1, 0, 0, (SCM stil),
102            "Return whether @var{stil} is empty ")
103 {
104   Stencil *s = unsmob_stencil (stil);
105   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
106   return scm_from_bool (s->is_empty ());
107 }
108
109
110 LY_DEFINE (ly_stencil_origin, "ly:stencil-origin",
111            2, 0, 0, (SCM stil, SCM axis),
112            "Return a pair of numbers signifying the origin @var{stil} in "
113            "@var{axis} direction (0 or 1 for x and y axis respectively).")
114 {
115   Stencil *s = unsmob_stencil (stil);
116   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
117   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
118
119   return scm_from_double (s->origin()[Axis (scm_to_int (axis))]);
120 }
121
122
123 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
124            4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
125                      SCM padding, SCM minimum),
126            "Similar to @code{ly:stencil-combine-edge}, but returns "
127            "@var{second} positioned to be next to @var{first}. ")
128 {
129   /*
130     C&P from combine-at-edge.
131    */
132   Stencil *s1 = unsmob_stencil (first);
133   Stencil *s2 = unsmob_stencil (second);
134   Stencil first_stencil;
135
136   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
137   SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
138
139   Real p = 0.0;
140   if (padding != SCM_UNDEFINED)
141     {
142       SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
143       p = scm_to_double (padding);
144     }
145   Real m = 0.0;
146   if (minimum != SCM_UNDEFINED)
147     {
148       SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
149       m = scm_to_double (minimum);
150     }
151
152   if (s1)
153     first_stencil = *s1;
154
155   if (s2)
156     return first_stencil.moved_to_edge (Axis (scm_to_int (axis)),
157                                         Direction (scm_to_int (direction)),
158                                         *s2, p, m).smobbed_copy ();
159   else
160     return Stencil().smobbed_copy ();
161 }
162
163
164
165 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
166            4, 2, 0,  (SCM first, SCM axis, SCM direction,
167                       SCM second,
168                       SCM padding,
169                       SCM minimum),
170            "Construct a stencil by putting @var{second} next to @var{first}. "
171            "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
172            "@var{direction} can be -1 (left or down) or 1 (right or up). "
173            "The stencils are juxtaposed with  @var{padding} as extra space. "
174            "If this puts the reference points closer than @var{minimum}, "
175            "they are moved by the latter amount."
176            "@var{first} and @var{second} may also be '() or #f.")
177 {
178   Stencil *s1 = unsmob_stencil (first);
179   Stencil *s2 = unsmob_stencil (second);
180   Stencil result;
181
182   SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first  == SCM_EOL,
183                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
184   SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second  == SCM_EOL,
185                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
186   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
187   SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
188
189   Real p = 0.0;
190   if (padding != SCM_UNDEFINED)
191     {
192       SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
193       p = scm_to_double (padding);
194     }
195   Real m = 0.0;
196   if (minimum != SCM_UNDEFINED)
197     {
198       SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
199       m = scm_to_double (minimum);
200     }
201
202   if (s1)
203     result = *s1;
204   
205   if (s2)
206     result.add_at_edge (Axis (scm_to_int (axis)),
207                         Direction (scm_to_int (direction)), *s2, p, m);
208
209   return result.smobbed_copy ();
210 }
211
212 LY_DEFINE (ly_stencil_add , "ly:stencil-add",
213            0, 0, 1, (SCM args),
214            "Combine stencils. Takes any number of arguments.")
215 {
216 #define FUNC_NAME __FUNCTION__
217   SCM_VALIDATE_REST_ARGUMENT (args);
218
219   Stencil result;
220
221   while (!SCM_NULLP (args))
222     {
223       Stencil *s = unsmob_stencil (scm_car (args));
224       if (!s)
225         SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
226
227       result.add_stencil (*s);
228       args = scm_cdr (args);
229     }
230
231   return result.smobbed_copy ();
232 }
233
234 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
235            3, 0, 0,  (SCM expr, SCM xext, SCM yext),
236            " \n"
237            "Stencils are a device independent output expressions."
238            "They carry two pieces of information: \n\n"
239            "1: a specification of how to print this object. "
240            "This specification is processed by the output backends, "
241            " for example @file{scm/output-tex.scm}.\n\n"
242            "2: the vertical and horizontal extents of the object.\n\n")
243 {
244   SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
245   SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
246
247   Box b (ly_scm2interval (xext), ly_scm2interval (yext));
248   Stencil s (b, expr);
249   return s.smobbed_copy ();
250 }
251
252
253 LY_DEFINE (ly_stencil_align_to_x, "ly:stencil-align-to!",
254            3, 0, 0, (SCM stil, SCM axis, SCM dir),
255            "Align @var{stil} using its own extents. "
256            "@var{dir} is a number -1, 1 are left and right respectively. "
257            "Other values are interpolated (so 0 means the center. ")
258 {
259   SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
260   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
261   SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
262
263   unsmob_stencil (stil)->align_to ((Axis)scm_to_int (axis),
264                                    scm_to_double (dir));
265   return stil;
266 }
267
268
269 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
270            1, 0, 0, (SCM s),
271           " Analyse @var{s}, and return a list of fonts used in @var{s}.")
272 {
273   Stencil *stil = unsmob_stencil (s);
274   SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
275   return find_expression_fonts (stil->expr ());
276 }
277
278
279 struct Stencil_interpret_arguments
280 {
281   SCM func;
282   SCM arg1;
283 };
284
285 void stencil_interpret_in_scm (void *p, SCM expr)
286 {
287   Stencil_interpret_arguments *ap = (Stencil_interpret_arguments*) p;
288   scm_call_2 (ap->func, ap->arg1, expr);
289 }
290
291 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
292            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
293            "Parse EXPR, feed bits to FUNC with first arg ARG1")
294 {
295   SCM_ASSERT_TYPE (ly_c_procedure_p(func), func, SCM_ARG1, __FUNCTION__,
296                    "procedure");
297
298   Stencil_interpret_arguments a;
299   a.func = func;
300   a.arg1 = arg1;
301   Offset o = ly_scm2offset (offset);
302
303   interpret_stencil_expression (expr, stencil_interpret_in_scm, (void*) &a, o);
304
305   return SCM_UNSPECIFIED;
306 }
307
308
309
310 LY_DEFINE (ly_bracket ,"ly:bracket",
311           4, 0, 0,
312           (SCM a, SCM iv, SCM t, SCM p),
313           "Make a bracket in direction @var{a}. The extent of the bracket is " 
314           "given by @var{iv}. The wings protude by an amount of @var{p}, which "
315           "may be negative. The thickness is given by @var{t}.")
316 {
317   SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
318   SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
319   SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number") ;
320   SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number") ;
321
322
323   return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
324                           scm_to_double (t),
325                           scm_to_double (p),
326                           0.95 * scm_to_double (t)).smobbed_copy ();
327 }
328
329
330
331 LY_DEFINE (ly_filled_box ,"ly:round-filled-box",
332           3, 0, 0,
333           (SCM xext, SCM yext, SCM blot),
334           "Make a @code{Stencil} "
335           "that prints a black box of dimensions @var{xext}, "
336           "@var{yext} and roundness @var{blot}."
337           )
338 {
339   SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair") ;
340   SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair") ;
341   SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number") ;
342
343   return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
344                                    scm_to_double (blot)).smobbed_copy ();
345 }
346