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