]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
Doc-de: fixing linkage
[lilypond.git] / lily / stencil-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "font-metric.hh"
21 #include "libc-extension.hh"
22 #include "lookup.hh"
23 #include "stencil.hh"
24
25 /*
26   TODO: naming add/combine.
27 */
28
29 LY_DEFINE (ly_stencil_translate_axis, "ly:stencil-translate-axis",
30            3, 0, 0, (SCM stil, SCM amount, SCM axis),
31            "Return a copy of @var{stil} but translated by @var{amount}"
32            " in @var{axis} direction.")
33 {
34   Stencil *s = unsmob_stencil (stil);
35   LY_ASSERT_SMOB (Stencil, stil, 1);
36   LY_ASSERT_TYPE (scm_is_number, amount, 2);
37   LY_ASSERT_TYPE (is_axis, axis, 3);
38
39   Real real_amount = scm_to_double (amount);
40
41   SCM new_s = s->smobbed_copy ();
42   Stencil *q = unsmob_stencil (new_s);
43   q->translate_axis (real_amount, Axis (scm_to_int (axis)));
44   return new_s;
45 }
46
47 LY_DEFINE (ly_stencil_translate, "ly:stencil-translate",
48            2, 0, 0, (SCM stil, SCM offset),
49            "Return a @var{stil}, but translated by @var{offset}"
50            " (a pair of numbers).")
51 {
52   Stencil *s = unsmob_stencil (stil);
53   LY_ASSERT_SMOB (Stencil, stil, 1);
54   LY_ASSERT_TYPE (is_number_pair, offset, 2);
55   Offset o = ly_scm2offset (offset);
56
57   SCM new_s = s->smobbed_copy ();
58   Stencil *q = unsmob_stencil (new_s);
59   q->translate (o);
60   return new_s;
61 }
62
63 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
64            1, 0, 0, (SCM stil),
65            "Return the expression of @var{stil}.")
66 {
67   Stencil *s = unsmob_stencil (stil);
68   LY_ASSERT_SMOB (Stencil, stil, 1);
69   return s->expr ();
70 }
71
72 LY_DEFINE (ly_stencil_extent, "ly:stencil-extent",
73            2, 0, 0, (SCM stil, SCM axis),
74            "Return a pair of numbers signifying the extent of @var{stil} in"
75            " @var{axis} direction (@code{0} or @code{1} for x and"
76            " y@tie{}axis, respectively).")
77 {
78   Stencil *s = unsmob_stencil (stil);
79   LY_ASSERT_SMOB (Stencil, stil, 1);
80   LY_ASSERT_TYPE (is_axis, axis, 2);
81
82   return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
83 }
84
85 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
86            1, 0, 0, (SCM stil),
87            "Return whether @var{stil} is empty.")
88 {
89   Stencil *s = unsmob_stencil (stil);
90   LY_ASSERT_SMOB (Stencil, stil, 1);
91   return scm_from_bool (s->is_empty ());
92 }
93
94 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
95            4, 2, 0, (SCM first, SCM axis, SCM direction,
96                      SCM second,
97                      SCM padding,
98                      SCM minimum),
99            "Construct a stencil by putting @var{second} next to @var{first}."
100            "  @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
101            "  @var{direction} can be -1 (left or down) or@tie{}1 (right or"
102            "  up).  The stencils are juxtaposed with @var{padding} as extra"
103            " space.  If this puts the reference points closer than"
104            " @var{minimum}, they are moved by the latter amount."
105            "  @var{first} and @var{second} may also be @code{'()} or"
106            " @code{#f}.")
107 {
108   Stencil *s1 = unsmob_stencil (first);
109   Stencil *s2 = unsmob_stencil (second);
110   Stencil result;
111
112   SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
113                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
114   SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
115                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
116   LY_ASSERT_TYPE (is_axis, axis, 2);
117   LY_ASSERT_TYPE (is_direction, direction, 3);
118
119   Real p = 0.0;
120   if (padding != SCM_UNDEFINED)
121     {
122       LY_ASSERT_TYPE (scm_is_number, padding, 5);
123       p = scm_to_double (padding);
124     }
125   Real m = 0.0;
126   if (minimum != SCM_UNDEFINED)
127     {
128       LY_ASSERT_TYPE (scm_is_number, minimum, 6);
129       m = scm_to_double (minimum);
130     }
131
132   if (s1)
133     result = *s1;
134
135   if (s2)
136     result.add_at_edge (Axis (scm_to_int (axis)),
137                         Direction (scm_to_int (direction)), *s2, p);
138
139   return result.smobbed_copy ();
140 }
141
142 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
143            0, 0, 1, (SCM args),
144            "Combine stencils.  Takes any number of arguments.")
145 {
146 #define FUNC_NAME __FUNCTION__
147   SCM_VALIDATE_REST_ARGUMENT (args);
148
149   SCM expr = SCM_EOL;
150   SCM *tail = &expr;
151   Box extent;
152   extent.set_empty ();
153
154   while (!SCM_NULLP (args))
155     {
156       Stencil *s = unsmob_stencil (scm_car (args));
157       if (!s)
158         SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
159
160       extent.unite (s->extent_box ());
161       *tail = scm_cons (s->expr (), SCM_EOL);
162       tail = SCM_CDRLOC (*tail);
163       args = scm_cdr (args);
164     }
165
166   expr = scm_cons (ly_symbol2scm ("combine-stencil"), expr);
167   return Stencil (extent, expr).smobbed_copy ();
168 }
169
170 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
171            1, 2, 0, (SCM expr, SCM xext, SCM yext),
172            "Stencils are device independent output expressions."
173            "  They carry two pieces of information:\n"
174            "\n"
175            "@enumerate\n"
176            "@item\n"
177            "A specification of how to print this object."
178            "  This specification is processed by the output backends,"
179            " for example @file{scm/output-ps.scm}.\n"
180            "\n"
181            "@item\n"
182            "The vertical and horizontal extents of the object, given as"
183            " pairs.  If an extent is unspecified (or if you use"
184            " @code{(1000 . -1000)} as its value), it is taken to be empty.\n"
185            "@end enumerate\n")
186 {
187   SCM_ASSERT_TYPE (!scm_is_pair (expr)
188                    || is_stencil_head (scm_car (expr)),
189                    expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
190
191   Interval x;
192   if (xext != SCM_UNDEFINED)
193     {
194       LY_ASSERT_TYPE (is_number_pair, xext, 2);
195       x = ly_scm2interval (xext);
196     }
197
198   Interval y;
199   if (yext != SCM_UNDEFINED)
200     {
201       LY_ASSERT_TYPE (is_number_pair, yext, 3);
202       y = ly_scm2interval (yext);
203     }
204
205   Box b (x, y);
206   Stencil s (b, expr);
207   return s.smobbed_copy ();
208 }
209
210 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
211            3, 0, 0, (SCM stil, SCM axis, SCM dir),
212            "Align @var{stil} using its own extents.  @var{dir} is a number."
213            "  @code{-1} and @code{1} are left and right, respectively."
214            "  Other values are interpolated (so @code{0} means the center).")
215 {
216   LY_ASSERT_SMOB (Stencil, stil, 1);
217   LY_ASSERT_TYPE (is_axis, axis, 2);
218   LY_ASSERT_TYPE (scm_is_number, dir, 3);
219
220   Stencil target = *unsmob_stencil (stil);
221
222   target.align_to ((Axis)scm_to_int (axis),
223                    scm_to_double (dir));
224   return target.smobbed_copy ();
225 }
226
227 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
228            1, 0, 0, (SCM s),
229            "Analyze @var{s}, and return a list of fonts used"
230            " in@tie{}@var{s}.")
231 {
232   LY_ASSERT_SMOB (Stencil, s, 1);
233   Stencil *stil = unsmob_stencil (s);
234   return find_expression_fonts (stil->expr ());
235 }
236
237 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
238            4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
239            "Put @var{stc} in a different color.")
240 {
241   LY_ASSERT_SMOB (Stencil, stc, 1);
242   Stencil *stil = unsmob_stencil (stc);
243   return Stencil (stil->extent_box (),
244                   scm_list_3 (ly_symbol2scm ("color"),
245                               scm_list_3 (r, g, b),
246                               stil->expr ())).smobbed_copy ();
247 }
248
249 struct Stencil_interpret_arguments
250 {
251   SCM func;
252   SCM arg1;
253 };
254
255 void stencil_interpret_in_scm (void *p, SCM expr)
256 {
257   Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
258   scm_call_2 (ap->func, ap->arg1, expr);
259 }
260
261 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
262            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
263            "Parse @var{expr}, feed bits to @var{func} with first arg"
264            " @var{arg1} having offset @var{offset}.")
265 {
266   LY_ASSERT_TYPE (ly_is_procedure, func, 2);
267
268   Stencil_interpret_arguments a;
269   a.func = func;
270   a.arg1 = arg1;
271   Offset o = ly_scm2offset (offset);
272
273   interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
274
275   return SCM_UNSPECIFIED;
276 }
277
278 LY_DEFINE (ly_bracket, "ly:bracket",
279            4, 0, 0,
280            (SCM a, SCM iv, SCM t, SCM p),
281            "Make a bracket in direction@tie{}@var{a}.  The extent of the"
282            " bracket is given by @var{iv}.  The wings protrude by an amount"
283            " of@tie{}@var{p}, which may be negative.  The thickness is given"
284            " by@tie{}@var{t}.")
285 {
286   LY_ASSERT_TYPE (is_axis, a, 1);
287   LY_ASSERT_TYPE (is_number_pair, iv, 2);
288   LY_ASSERT_TYPE (scm_is_number, t, 3);
289   LY_ASSERT_TYPE (scm_is_number, p, 4);
290
291   return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
292                           scm_to_double (t),
293                           scm_to_double (p),
294                           0.95 * scm_to_double (t)).smobbed_copy ();
295 }
296
297 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
298            4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
299            "Return a stencil @var{stil} rotated @var{angle} degrees around"
300            " the relative offset (@var{x}, @var{y}). E.g. an offset of"
301            " (-1, 1) will rotate the stencil around the left upper corner.")
302 {
303   Stencil *s = unsmob_stencil (stil);
304   LY_ASSERT_SMOB (Stencil, stil, 1);
305   LY_ASSERT_TYPE (scm_is_number, angle, 2);
306   LY_ASSERT_TYPE (scm_is_number, x, 3);
307   LY_ASSERT_TYPE (scm_is_number, y, 4);
308   Real a = scm_to_double (angle);
309   Real x_off = scm_to_double (x);
310   Real y_off = scm_to_double (y);
311
312   SCM new_s = s->smobbed_copy ();
313   Stencil *q = unsmob_stencil (new_s);
314   q->rotate_degrees (a, Offset (x_off, y_off));
315   return new_s;
316 }
317
318 LY_DEFINE (ly_stencil_rotate_absolute, "ly:stencil-rotate-absolute",
319            4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
320            "Return a stencil @var{stil} rotated @var{angle} degrees around"
321            " point (@var{x}, @var{y}), given in absolute coordinates.")
322 {
323   Stencil *s = unsmob_stencil (stil);
324   LY_ASSERT_SMOB (Stencil, stil, 1);
325   LY_ASSERT_TYPE (scm_is_number, angle, 2);
326   LY_ASSERT_TYPE (scm_is_number, x, 3);
327   LY_ASSERT_TYPE (scm_is_number, y, 4);
328   Real a = scm_to_double (angle);
329   Real x_off = scm_to_double (x);
330   Real y_off = scm_to_double (y);
331
332   SCM new_s = s->smobbed_copy ();
333   Stencil *q = unsmob_stencil (new_s);
334   q->rotate_degrees_absolute (a, Offset (x_off, y_off));
335   return new_s;
336 }
337
338 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
339            3, 0, 0,
340            (SCM xext, SCM yext, SCM blot),
341            "Make a @code{Stencil} object that prints a black box of"
342            " dimensions @var{xext}, @var{yext} and roundness @var{blot}.")
343 {
344   LY_ASSERT_TYPE (is_number_pair, xext, 1);
345   LY_ASSERT_TYPE (is_number_pair, yext, 2);
346   LY_ASSERT_TYPE (scm_is_number, blot, 3);
347
348   return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
349                                    scm_to_double (blot)).smobbed_copy ();
350 }
351
352 LY_DEFINE (ly_round_filled_polygon, "ly:round-filled-polygon",
353            2, 0, 0,
354            (SCM points, SCM blot),
355            "Make a @code{Stencil} object that prints a black polygon with"
356            " corners at the points defined by @var{points} (list of coordinate"
357            " pairs) and roundness @var{blot}.")
358 {
359   SCM_ASSERT_TYPE (scm_ilength (points) > 0, points, SCM_ARG1, __FUNCTION__, "list of coordinate pairs");
360   LY_ASSERT_TYPE (scm_is_number, blot, 2);
361   vector<Offset> pts;
362   for (SCM p = points; scm_is_pair (p); p = scm_cdr (p))
363     {
364       SCM scm_pt = scm_car (p);
365       if (scm_is_pair (scm_pt))
366         {
367           pts.push_back (ly_scm2offset (scm_pt));
368         }
369       else
370         {
371           // TODO: Print out warning
372         }
373     }
374   return Lookup::round_filled_polygon (pts, scm_to_double (blot)).smobbed_copy ();
375 }
376
377 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
378            1, 0, 0,
379            (SCM symbol),
380            "Add @var{symbol} as head of a stencil expression.")
381 {
382   LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
383   register_stencil_head (symbol);
384   return SCM_UNSPECIFIED;
385 }
386
387 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
388            0, 0, 0,
389            (),
390            "Return all symbols recognized as stencil expressions.")
391 {
392   return all_stencil_heads ();
393 }
394
395 LY_DEFINE (ly_stencil_scale, "ly:stencil-scale",
396            3, 0, 0, (SCM stil, SCM x, SCM y),
397            "Scale @var{stil} using the horizontal and vertical scaling"
398            " factors @var{x} and @var{y}.")
399 {
400   Stencil *s = unsmob_stencil (stil);
401   LY_ASSERT_SMOB (Stencil, stil, 1);
402   LY_ASSERT_TYPE (scm_is_number, x, 2);
403   LY_ASSERT_TYPE (scm_is_number, y, 3);
404
405   SCM new_s = s->smobbed_copy ();
406   Stencil *q = unsmob_stencil (new_s);
407
408   q->scale (scm_to_double (x), scm_to_double (y));
409   return new_s;
410 }