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