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