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