]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
Merge branch 'issue3330'
[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, 1, 0, (SCM stil, SCM axis),
88            "Return whether @var{stil} is empty.  If an optional"
89            " @var{axis} is supplied, the emptiness check is"
90            " restricted to that axis.")
91 {
92   Stencil *s = unsmob_stencil (stil);
93   LY_ASSERT_SMOB (Stencil, stil, 1);
94   if (SCM_UNBNDP (axis))
95     return scm_from_bool (s->is_empty ());
96   LY_ASSERT_TYPE (is_axis, axis, 2);
97   return scm_from_bool (s->is_empty (Axis (scm_to_int (axis))));
98 }
99
100 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
101            4, 1, 0, (SCM first, SCM axis, SCM direction,
102                      SCM second,
103                      SCM padding),
104            "Construct a stencil by putting @var{second} next to @var{first}."
105            "  @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
106            "  @var{direction} can be -1 (left or down) or@tie{}1 (right or"
107            " up).  The stencils are juxtaposed with @var{padding} as extra"
108            " space.  @var{first} and @var{second} may also be @code{'()} or"
109            " @code{#f}.")
110 {
111   Stencil *s1 = unsmob_stencil (first);
112   Stencil *s2 = unsmob_stencil (second);
113   Stencil result;
114
115   SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
116                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
117   SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
118                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
119   LY_ASSERT_TYPE (is_axis, axis, 2);
120   LY_ASSERT_TYPE (is_direction, direction, 3);
121
122   Real p = 0.0;
123   if (padding != SCM_UNDEFINED)
124     {
125       LY_ASSERT_TYPE (scm_is_number, padding, 5);
126       p = scm_to_double (padding);
127     }
128
129   if (s1)
130     result = *s1;
131
132   if (s2)
133     result.add_at_edge (Axis (scm_to_int (axis)),
134                         Direction (scm_to_int (direction)), *s2, p);
135
136   return result.smobbed_copy ();
137 }
138
139 LY_DEFINE (ly_stencil_stack, "ly:stencil-stack",
140            4, 2, 0, (SCM first, SCM axis, SCM direction,
141                      SCM second,
142                      SCM padding,
143                      SCM mindist),
144            "Construct a stencil by stacking @var{second} next to @var{first}."
145            "  @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
146            "  @var{direction} can be -1 (left or down) or@tie{}1 (right or"
147            " up).  The stencils are juxtaposed with @var{padding} as extra"
148            " space.  @var{first} and @var{second} may also be @code{'()} or"
149            " @code{#f}.  As opposed to @code{ly:stencil-combine-at-edge},"
150            " metrics are suited for successively accumulating lines of"
151            " stencils.  Also, @var{second} stencil is drawn last.\n\n"
152            "If @var{mindist} is specified, reference points are placed"
153            " apart at least by this distance.  If either of the stencils"
154            " is spacing, @var{padding} and @var{mindist} do not apply.")
155 {
156   Stencil *s1 = unsmob_stencil (first);
157   Stencil *s2 = unsmob_stencil (second);
158   Stencil result;
159
160   SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
161                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
162   SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
163                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
164   LY_ASSERT_TYPE (is_axis, axis, 2);
165   LY_ASSERT_TYPE (is_direction, direction, 3);
166
167   Real p = 0.0;
168   if (padding != SCM_UNDEFINED)
169     {
170       LY_ASSERT_TYPE (scm_is_number, padding, 5);
171       p = scm_to_double (padding);
172     }
173   Real d = -infinity_f;
174   if (!SCM_UNBNDP (mindist))
175     {
176       LY_ASSERT_TYPE (scm_is_number, mindist, 6);
177       d = scm_to_double (mindist);
178     }
179
180   if (s1)
181     result = *s1;
182
183   if (s2)
184     result.stack (Axis (scm_to_int (axis)),
185                   Direction (scm_to_int (direction)), *s2, p, d);
186
187   return result.smobbed_copy ();
188 }
189
190 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
191            0, 0, 1, (SCM args),
192            "Combine stencils.  Takes any number of arguments.")
193 {
194 #define FUNC_NAME __FUNCTION__
195   SCM_VALIDATE_REST_ARGUMENT (args);
196
197   SCM expr = SCM_EOL;
198   SCM cs = ly_symbol2scm ("combine-stencil");
199
200   Box extent;
201   extent.set_empty ();
202
203   while (!SCM_NULLP (args))
204     {
205       Stencil *s = unsmob_stencil (scm_car (args));
206       if (!s)
207         SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
208
209       extent.unite (s->extent_box ());
210       if (scm_is_pair (s->expr ()) && scm_is_eq (cs, s->expr ()))
211         {
212           expr = scm_reverse_x (scm_list_copy (scm_cdr (s->expr ())),
213                                 expr);
214         }
215       else
216         expr = scm_cons (s->expr (), expr);
217
218       args = scm_cdr (args);
219     }
220
221   expr = scm_cons (cs, scm_reverse_x (expr, SCM_EOL));
222   return Stencil (extent, expr).smobbed_copy ();
223 }
224
225 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
226            1, 2, 0, (SCM expr, SCM xext, SCM yext),
227            "Stencils are device independent output expressions."
228            "  They carry two pieces of information:\n"
229            "\n"
230            "@enumerate\n"
231            "@item\n"
232            "A specification of how to print this object."
233            "  This specification is processed by the output backends,"
234            " for example @file{scm/output-ps.scm}.\n"
235            "\n"
236            "@item\n"
237            "The vertical and horizontal extents of the object, given as"
238            " pairs.  If an extent is unspecified (or if you use"
239            " @code{empty-interval} as its value), it is taken to be empty.\n"
240            "@end enumerate\n")
241 {
242   SCM_ASSERT_TYPE (!scm_is_pair (expr)
243                    || is_stencil_head (scm_car (expr)),
244                    expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
245
246   Interval x;
247   if (xext != SCM_UNDEFINED)
248     {
249       LY_ASSERT_TYPE (is_number_pair, xext, 2);
250       x = ly_scm2interval (xext);
251     }
252
253   Interval y;
254   if (yext != SCM_UNDEFINED)
255     {
256       LY_ASSERT_TYPE (is_number_pair, yext, 3);
257       y = ly_scm2interval (yext);
258     }
259
260   Box b (x, y);
261   Stencil s (b, expr);
262   return s.smobbed_copy ();
263 }
264
265 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
266            3, 0, 0, (SCM stil, SCM axis, SCM dir),
267            "Align @var{stil} using its own extents.  @var{dir} is a number."
268            "  @w{@code{-1}} and @code{1} are left and right, respectively."
269            "  Other values are interpolated (so @code{0} means the center).")
270 {
271   LY_ASSERT_SMOB (Stencil, stil, 1);
272   LY_ASSERT_TYPE (is_axis, axis, 2);
273   LY_ASSERT_TYPE (scm_is_number, dir, 3);
274
275   Stencil target = *unsmob_stencil (stil);
276
277   target.align_to ((Axis)scm_to_int (axis),
278                    scm_to_double (dir));
279   return target.smobbed_copy ();
280 }
281
282 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
283            1, 0, 0, (SCM s),
284            "Analyze @var{s}, and return a list of fonts used"
285            " in@tie{}@var{s}.")
286 {
287   LY_ASSERT_SMOB (Stencil, s, 1);
288   Stencil *stil = unsmob_stencil (s);
289   return find_expression_fonts (stil->expr ());
290 }
291
292 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
293            4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
294            "Put @var{stc} in a different color.")
295 {
296   LY_ASSERT_SMOB (Stencil, stc, 1);
297   Stencil *stil = unsmob_stencil (stc);
298   return Stencil (stil->extent_box (),
299                   scm_list_3 (ly_symbol2scm ("color"),
300                               scm_list_3 (r, g, b),
301                               stil->expr ())).smobbed_copy ();
302 }
303
304 struct Stencil_interpret_arguments
305 {
306   SCM func;
307   SCM arg1;
308 };
309
310 void stencil_interpret_in_scm (void *p, SCM expr)
311 {
312   Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
313   scm_call_2 (ap->func, ap->arg1, expr);
314 }
315
316 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
317            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
318            "Parse @var{expr}, feed bits to @var{func} with first arg"
319            " @var{arg1} having offset @var{offset}.")
320 {
321   LY_ASSERT_TYPE (ly_is_procedure, func, 2);
322
323   Stencil_interpret_arguments a;
324   a.func = func;
325   a.arg1 = arg1;
326   Offset o = ly_scm2offset (offset);
327
328   interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
329
330   return SCM_UNSPECIFIED;
331 }
332
333 LY_DEFINE (ly_bracket, "ly:bracket",
334            4, 0, 0,
335            (SCM a, SCM iv, SCM t, SCM p),
336            "Make a bracket in direction@tie{}@var{a}.  The extent of the"
337            " bracket is given by @var{iv}.  The wings protrude by an amount"
338            " of@tie{}@var{p}, which may be negative.  The thickness is given"
339            " by@tie{}@var{t}.")
340 {
341   LY_ASSERT_TYPE (is_axis, a, 1);
342   LY_ASSERT_TYPE (is_number_pair, iv, 2);
343   LY_ASSERT_TYPE (scm_is_number, t, 3);
344   LY_ASSERT_TYPE (scm_is_number, p, 4);
345
346   return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
347                           scm_to_double (t),
348                           scm_to_double (p),
349                           0.95 * scm_to_double (t)).smobbed_copy ();
350 }
351
352 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
353            4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
354            "Return a stencil @var{stil} rotated @var{angle} degrees around"
355            " the relative offset (@var{x}, @var{y}).  E.g., an offset of"
356            " (-1, 1) will rotate the stencil around the left upper corner.")
357 {
358   Stencil *s = unsmob_stencil (stil);
359   LY_ASSERT_SMOB (Stencil, stil, 1);
360   LY_ASSERT_TYPE (scm_is_number, angle, 2);
361   LY_ASSERT_TYPE (scm_is_number, x, 3);
362   LY_ASSERT_TYPE (scm_is_number, y, 4);
363   Real a = scm_to_double (angle);
364   Real x_off = scm_to_double (x);
365   Real y_off = scm_to_double (y);
366
367   SCM new_s = s->smobbed_copy ();
368   Stencil *q = unsmob_stencil (new_s);
369   q->rotate_degrees (a, Offset (x_off, y_off));
370   return new_s;
371 }
372
373 LY_DEFINE (ly_stencil_rotate_absolute, "ly:stencil-rotate-absolute",
374            4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
375            "Return a stencil @var{stil} rotated @var{angle} degrees around"
376            " point (@var{x}, @var{y}), given in absolute coordinates.")
377 {
378   Stencil *s = unsmob_stencil (stil);
379   LY_ASSERT_SMOB (Stencil, stil, 1);
380   LY_ASSERT_TYPE (scm_is_number, angle, 2);
381   LY_ASSERT_TYPE (scm_is_number, x, 3);
382   LY_ASSERT_TYPE (scm_is_number, y, 4);
383   Real a = scm_to_double (angle);
384   Real x_off = scm_to_double (x);
385   Real y_off = scm_to_double (y);
386
387   SCM new_s = s->smobbed_copy ();
388   Stencil *q = unsmob_stencil (new_s);
389   q->rotate_degrees_absolute (a, Offset (x_off, y_off));
390   return new_s;
391 }
392
393 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
394            3, 0, 0,
395            (SCM xext, SCM yext, SCM blot),
396            "Make a @code{Stencil} object that prints a black box of"
397            " dimensions @var{xext}, @var{yext} and roundness @var{blot}.")
398 {
399   LY_ASSERT_TYPE (is_number_pair, xext, 1);
400   LY_ASSERT_TYPE (is_number_pair, yext, 2);
401   LY_ASSERT_TYPE (scm_is_number, blot, 3);
402
403   return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
404                                    scm_to_double (blot)).smobbed_copy ();
405 }
406
407 LY_DEFINE (ly_round_filled_polygon, "ly:round-filled-polygon",
408            2, 0, 0,
409            (SCM points, SCM blot),
410            "Make a @code{Stencil} object that prints a black polygon with"
411            " corners at the points defined by @var{points} (list of coordinate"
412            " pairs) and roundness @var{blot}.")
413 {
414   SCM_ASSERT_TYPE (scm_ilength (points) > 0, points, SCM_ARG1, __FUNCTION__, "list of coordinate pairs");
415   LY_ASSERT_TYPE (scm_is_number, blot, 2);
416   vector<Offset> pts;
417   for (SCM p = points; scm_is_pair (p); p = scm_cdr (p))
418     {
419       SCM scm_pt = scm_car (p);
420       if (scm_is_pair (scm_pt))
421         {
422           pts.push_back (ly_scm2offset (scm_pt));
423         }
424       else
425         {
426           // TODO: Print out warning
427         }
428     }
429   return Lookup::round_filled_polygon (pts, scm_to_double (blot)).smobbed_copy ();
430 }
431
432 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
433            1, 0, 0,
434            (SCM symbol),
435            "Add @var{symbol} as head of a stencil expression.")
436 {
437   LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
438   register_stencil_head (symbol);
439   return SCM_UNSPECIFIED;
440 }
441
442 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
443            0, 0, 0,
444            (),
445            "Return all symbols recognized as stencil expressions.")
446 {
447   return all_stencil_heads ();
448 }
449
450 LY_DEFINE (ly_stencil_scale, "ly:stencil-scale",
451            3, 0, 0, (SCM stil, SCM x, SCM y),
452            "Scale @var{stil} using the horizontal and vertical scaling"
453            " factors @var{x} and @var{y}.")
454 {
455   Stencil *s = unsmob_stencil (stil);
456   LY_ASSERT_SMOB (Stencil, stil, 1);
457   LY_ASSERT_TYPE (scm_is_number, x, 2);
458   LY_ASSERT_TYPE (scm_is_number, y, 3);
459
460   SCM new_s = s->smobbed_copy ();
461   Stencil *q = unsmob_stencil (new_s);
462
463   q->scale (scm_to_double (x), scm_to_double (y));
464   return new_s;
465 }