]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
* scm/page-layout.scm (default-page-make-stencil): only add header
[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   Stencil result;
196
197   while (!SCM_NULLP (args))
198     {
199       Stencil *s = unsmob_stencil (scm_car (args));
200       if (!s)
201         SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
202
203       result.add_stencil (*s);
204       args = scm_cdr (args);
205     }
206
207   return result.smobbed_copy ();
208 }
209
210 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
211            3, 0, 0, (SCM expr, SCM xext, SCM yext),
212            " \n"
213            "Stencils are a device independent output expressions."
214            "They carry two pieces of information: \n\n"
215            "1: a specification of how to print this object. "
216            "This specification is processed by the output backends, "
217            " for example @file{scm/output-tex.scm}.\n\n"
218            "2: the vertical and horizontal extents of the object.\n\n")
219 {
220   SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
221   SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
222
223   Box b (ly_scm2interval (xext), ly_scm2interval (yext));
224   Stencil s (b, expr);
225   return s.smobbed_copy ();
226 }
227
228 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
229            3, 0, 0, (SCM stil, SCM axis, SCM dir),
230            "Align @var{stil} using its own extents. "
231            "@var{dir} is a number -1, 1 are left and right respectively. "
232            "Other values are interpolated (so 0 means the center).")
233 {
234   SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
235   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
236   SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
237
238   Stencil target = *unsmob_stencil (stil);
239
240   target.align_to ((Axis)scm_to_int (axis),
241                    scm_to_double (dir));
242   return target.smobbed_copy ();
243 }
244
245 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
246            1, 0, 0, (SCM s),
247            " Analyse @var{s}, and return a list of fonts used in @var{s}.")
248 {
249   Stencil *stil = unsmob_stencil (s);
250   SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
251   return find_expression_fonts (stil->expr ());
252 }
253
254 struct Stencil_interpret_arguments
255 {
256   SCM func;
257   SCM arg1;
258 };
259
260 void stencil_interpret_in_scm (void *p, SCM expr)
261 {
262   Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
263   scm_call_2 (ap->func, ap->arg1, expr);
264 }
265
266 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
267            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
268            "Parse EXPR, feed bits to FUNC with first arg ARG1")
269 {
270   SCM_ASSERT_TYPE (ly_c_procedure_p (func), func, SCM_ARG1, __FUNCTION__,
271                    "procedure");
272
273   Stencil_interpret_arguments a;
274   a.func = func;
275   a.arg1 = arg1;
276   Offset o = ly_scm2offset (offset);
277
278   interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
279
280   return SCM_UNSPECIFIED;
281 }
282
283 LY_DEFINE (ly_bracket, "ly:bracket",
284            4, 0, 0,
285            (SCM a, SCM iv, SCM t, SCM p),
286            "Make a bracket in direction @var{a}. The extent of the bracket is "
287            "given by @var{iv}. The wings protude by an amount of @var{p}, which "
288            "may be negative. The thickness is given by @var{t}.")
289 {
290   SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis");
291   SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair");
292   SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number");
293   SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number");
294
295   return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
296                           scm_to_double (t),
297                           scm_to_double (p),
298                           0.95 * scm_to_double (t)).smobbed_copy ();
299 }
300
301 LY_DEFINE (ly_filled_box, "ly:round-filled-box",
302            3, 0, 0,
303            (SCM xext, SCM yext, SCM blot),
304            "Make a @code{Stencil} "
305            "that prints a black box of dimensions @var{xext}, "
306            "@var{yext} and roundness @var{blot}.")
307 {
308   SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair");
309   SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair");
310   SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number");
311
312   return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
313                                    scm_to_double (blot)).smobbed_copy ();
314 }
315