]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
Web-ja: update introduction
[lilypond.git] / lily / stencil-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 "offset.hh"
24 #include "stencil.hh"
25
26 /*
27  * A few general helpers in degrees
28  */
29
30 LY_DEFINE (ly_angle, "ly:angle",
31            1, 1, 0, (SCM x, SCM y),
32            "Calculates angle in degrees of given vector.  With one argument,"
33            " @var{x} is a number pair indicating the vector.  With two"
34            " arguments, @var{x} and @var{y} specify the respective coordinates.")
35 {
36   Offset off;
37   if (SCM_UNBNDP (y)) {
38     LY_ASSERT_TYPE (is_number_pair, x, 1);
39     off = ly_scm2offset (x);
40   } else {
41     LY_ASSERT_TYPE (scm_is_number, x, 1);
42     LY_ASSERT_TYPE (scm_is_number, y, 2);
43     off = Offset (scm_to_double (x), scm_to_double (y));
44   }
45   return scm_from_double (off.angle_degrees ());
46 }
47
48 LY_DEFINE (ly_length, "ly:length",
49            1, 1, 0, (SCM x, SCM y),
50            "Calculates magnitude of given vector.  With one argument,"
51            " @var{x} is a number pair indicating the vector.  With two"
52            " arguments, @var{x} and @var{y} specify the respective coordinates.")
53 {
54   Offset off;
55   if (SCM_UNBNDP (y)) {
56     LY_ASSERT_TYPE (is_number_pair, x, 1);
57     off = ly_scm2offset (x);
58   } else {
59     LY_ASSERT_TYPE (scm_is_number, x, 1);
60     LY_ASSERT_TYPE (scm_is_number, y, 2);
61     off = Offset (scm_to_double (x), scm_to_double (y));
62   }
63   return scm_from_double (off.length ());
64 }
65
66 LY_DEFINE (ly_directed, "ly:directed",
67            1, 1, 0, (SCM direction, SCM magnitude),
68            "Calculates an @code{(x . y)} pair with optional @var{magnitude}"
69            " (defaulting to @code{1.0}) and @var{direction} specified either"
70            " as an angle in degrees or a coordinate pair giving the direction. "
71            " If @var{magnitude} is a pair, the respective coordinates are"
72            " scaled independently, useful for ellipse drawings.")
73 {
74   Offset res;
75   if (scm_is_pair (direction))
76     {
77       LY_ASSERT_TYPE (is_number_pair, direction, 1);
78       res = ly_scm2offset (direction).direction ();
79     }
80   else
81     {
82       LY_ASSERT_TYPE (scm_is_number, direction, 1);
83       res = offset_directed (scm_to_double (direction));
84     }
85   if (SCM_UNBNDP (magnitude))
86     return ly_offset2scm (res);
87   if (scm_is_pair (magnitude))
88     {
89       LY_ASSERT_TYPE (is_number_pair, magnitude, 2);
90       return ly_offset2scm (res.scale (ly_scm2offset (magnitude)));
91     }
92   LY_ASSERT_TYPE (scm_is_number, magnitude, 2);
93   return ly_offset2scm (scm_to_double (magnitude) * res);
94 }
95
96
97 /*
98   TODO: naming add/combine.
99 */
100
101 LY_DEFINE (ly_stencil_translate_axis, "ly:stencil-translate-axis",
102            3, 0, 0, (SCM stil, SCM amount, SCM axis),
103            "Return a copy of @var{stil} but translated by @var{amount}"
104            " in @var{axis} direction.")
105 {
106   Stencil *s = unsmob<Stencil> (stil);
107   LY_ASSERT_SMOB (Stencil, stil, 1);
108   LY_ASSERT_TYPE (scm_is_number, amount, 2);
109
110   LY_ASSERT_TYPE (is_axis, axis, 3);
111
112   Real real_amount = scm_to_double (amount);
113
114   SCM new_s = s->smobbed_copy ();
115   scm_remember_upto_here_1 (stil);
116
117   Stencil *q = unsmob<Stencil> (new_s);
118   q->translate_axis (real_amount, Axis (scm_to_int (axis)));
119   return new_s;
120 }
121
122 LY_DEFINE (ly_stencil_translate, "ly:stencil-translate",
123            2, 0, 0, (SCM stil, SCM offset),
124            "Return a @var{stil}, but translated by @var{offset}"
125            " (a pair of numbers).")
126 {
127   Stencil *s = unsmob<Stencil> (stil);
128   LY_ASSERT_SMOB (Stencil, stil, 1);
129   LY_ASSERT_TYPE (is_number_pair, offset, 2);
130   Offset o = ly_scm2offset (offset);
131
132   SCM new_s = s->smobbed_copy ();
133   scm_remember_upto_here_1 (stil);
134
135   Stencil *q = unsmob<Stencil> (new_s);
136   q->translate (o);
137   return new_s;
138 }
139
140 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
141            1, 0, 0, (SCM stil),
142            "Return the expression of @var{stil}.")
143 {
144   Stencil *s = unsmob<Stencil> (stil);
145   LY_ASSERT_SMOB (Stencil, stil, 1);
146   return s->expr ();
147 }
148
149 LY_DEFINE (ly_stencil_extent, "ly:stencil-extent",
150            2, 0, 0, (SCM stil, SCM axis),
151            "Return a pair of numbers signifying the extent of @var{stil} in"
152            " @var{axis} direction (@code{0} or @code{1} for x and"
153            " y@tie{}axis, respectively).")
154 {
155   Stencil *s = unsmob<Stencil> (stil);
156   LY_ASSERT_SMOB (Stencil, stil, 1);
157   LY_ASSERT_TYPE (is_axis, axis, 2);
158
159   return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
160 }
161
162 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
163            1, 1, 0, (SCM stil, SCM axis),
164            "Return whether @var{stil} is empty.  If an optional"
165            " @var{axis} is supplied, the emptiness check is"
166            " restricted to that axis.")
167 {
168   Stencil *s = unsmob<Stencil> (stil);
169   LY_ASSERT_SMOB (Stencil, stil, 1);
170   if (SCM_UNBNDP (axis))
171     return scm_from_bool (s->is_empty ());
172   LY_ASSERT_TYPE (is_axis, axis, 2);
173   return scm_from_bool (s->is_empty (Axis (scm_to_int (axis))));
174 }
175
176 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
177            4, 1, 0, (SCM first, SCM axis, SCM direction,
178                      SCM second,
179                      SCM padding),
180            "Construct a stencil by putting @var{second} next to @var{first}."
181            "  @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
182            "  @var{direction} can be -1 (left or down) or@tie{}1 (right or"
183            " up).  The stencils are juxtaposed with @var{padding} as extra"
184            " space.  @var{first} and @var{second} may also be @code{'()} or"
185            " @code{#f}.")
186 {
187   Stencil *s1 = unsmob<Stencil> (first);
188   Stencil *s2 = unsmob<Stencil> (second);
189   Stencil result;
190
191   SCM_ASSERT_TYPE (s1 || scm_is_false (first) || scm_is_null (first),
192                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
193   SCM_ASSERT_TYPE (s2 || scm_is_false (second) || scm_is_null (second),
194                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
195   LY_ASSERT_TYPE (is_axis, axis, 2);
196   LY_ASSERT_TYPE (is_direction, direction, 3);
197
198   Real p = 0.0;
199   if (!SCM_UNBNDP (padding))
200     {
201       LY_ASSERT_TYPE (scm_is_number, padding, 5);
202       p = scm_to_double (padding);
203     }
204
205   if (s1)
206     result = *s1;
207
208   if (s2)
209     result.add_at_edge (Axis (scm_to_int (axis)),
210                         Direction (scm_to_int (direction)), *s2, p);
211
212   scm_remember_upto_here_2 (first, second);
213
214   return result.smobbed_copy ();
215 }
216
217 LY_DEFINE (ly_stencil_stack, "ly:stencil-stack",
218            4, 2, 0, (SCM first, SCM axis, SCM direction,
219                      SCM second,
220                      SCM padding,
221                      SCM mindist),
222            "Construct a stencil by stacking @var{second} next to @var{first}."
223            "  @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
224            "  @var{direction} can be -1 (left or down) or@tie{}1 (right or"
225            " up).  The stencils are juxtaposed with @var{padding} as extra"
226            " space.  @var{first} and @var{second} may also be @code{'()} or"
227            " @code{#f}.  As opposed to @code{ly:stencil-combine-at-edge},"
228            " metrics are suited for successively accumulating lines of"
229            " stencils.  Also, @var{second} stencil is drawn last.\n\n"
230            "If @var{mindist} is specified, reference points are placed"
231            " apart at least by this distance.  If either of the stencils"
232            " is spacing, @var{padding} and @var{mindist} do not apply.")
233 {
234   Stencil *s1 = unsmob<Stencil> (first);
235   Stencil *s2 = unsmob<Stencil> (second);
236   Stencil result;
237
238   SCM_ASSERT_TYPE (s1 || scm_is_false (first) || scm_is_null (first),
239                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
240   SCM_ASSERT_TYPE (s2 || scm_is_false (second) || scm_is_null (second),
241                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
242   LY_ASSERT_TYPE (is_axis, axis, 2);
243   LY_ASSERT_TYPE (is_direction, direction, 3);
244
245   Real p = 0.0;
246   if (!SCM_UNBNDP (padding))
247     {
248       LY_ASSERT_TYPE (scm_is_number, padding, 5);
249       p = scm_to_double (padding);
250     }
251   Real d = -infinity_f;
252   if (!SCM_UNBNDP (mindist))
253     {
254       LY_ASSERT_TYPE (scm_is_number, mindist, 6);
255       d = scm_to_double (mindist);
256     }
257
258   if (s1)
259     result = *s1;
260
261   if (s2)
262     result.stack (Axis (scm_to_int (axis)),
263                   Direction (scm_to_int (direction)), *s2, p, d);
264
265   scm_remember_upto_here_2 (first, second);
266
267   return result.smobbed_copy ();
268 }
269
270 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
271            0, 0, 1, (SCM args),
272            "Combine stencils.  Takes any number of arguments.")
273 {
274 #define FUNC_NAME __FUNCTION__
275   SCM_VALIDATE_REST_ARGUMENT (args);
276
277   SCM expr = SCM_EOL;
278   SCM cs = ly_symbol2scm ("combine-stencil");
279
280   Box extent;
281   extent.set_empty ();
282
283   while (!SCM_NULLP (args))
284     {
285       Stencil *s = unsmob<Stencil> (scm_car (args));
286       if (!s)
287         SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
288
289       extent.unite (s->extent_box ());
290       if (scm_is_pair (s->expr ()) && scm_is_eq (cs, s->expr ()))
291         {
292           expr = scm_reverse_x (scm_list_copy (scm_cdr (s->expr ())),
293                                 expr);
294         }
295       else
296         expr = scm_cons (s->expr (), expr);
297
298       args = scm_cdr (args);
299     }
300
301   expr = scm_cons (cs, scm_reverse_x (expr, SCM_EOL));
302   return Stencil (extent, expr).smobbed_copy ();
303 }
304
305 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
306            1, 2, 0, (SCM expr, SCM xext, SCM yext),
307            "Stencils are device independent output expressions."
308            "  They carry two pieces of information:\n"
309            "\n"
310            "@enumerate\n"
311            "@item\n"
312            "A specification of how to print this object."
313            "  This specification is processed by the output backends,"
314            " for example @file{scm/output-ps.scm}.\n"
315            "\n"
316            "@item\n"
317            "The vertical and horizontal extents of the object, given as"
318            " pairs.  If an extent is unspecified (or if you use"
319            " @code{empty-interval} as its value), it is taken to be empty.\n"
320            "@end enumerate\n")
321 {
322   SCM_ASSERT_TYPE (!scm_is_pair (expr)
323                    || is_stencil_head (scm_car (expr)),
324                    expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
325
326   Interval x;
327   if (!SCM_UNBNDP (xext))
328     {
329       LY_ASSERT_TYPE (is_number_pair, xext, 2);
330       x = ly_scm2interval (xext);
331     }
332
333   Interval y;
334   if (!SCM_UNBNDP (yext))
335     {
336       LY_ASSERT_TYPE (is_number_pair, yext, 3);
337       y = ly_scm2interval (yext);
338     }
339
340   Box b (x, y);
341   Stencil s (b, expr);
342   return s.smobbed_copy ();
343 }
344
345 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
346            3, 0, 0, (SCM stil, SCM axis, SCM dir),
347            "Align @var{stil} using its own extents.  @var{dir} is a number."
348            "  @w{@code{-1}} and @code{1} are left and right, respectively."
349            "  Other values are interpolated (so @code{0} means the center).")
350 {
351   LY_ASSERT_SMOB (Stencil, stil, 1);
352   LY_ASSERT_TYPE (is_axis, axis, 2);
353   LY_ASSERT_TYPE (scm_is_number, dir, 3);
354
355   Stencil target = *unsmob<Stencil> (stil);
356
357   target.align_to ((Axis)scm_to_int (axis),
358                    scm_to_double (dir));
359   return target.smobbed_copy ();
360 }
361
362 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
363            1, 0, 0, (SCM s),
364            "Analyze @var{s}, and return a list of fonts used"
365            " in@tie{}@var{s}.")
366 {
367   LY_ASSERT_SMOB (Stencil, s, 1);
368   Stencil *stil = unsmob<Stencil> (s);
369   return find_expression_fonts (stil->expr ());
370 }
371
372 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
373            4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
374            "Put @var{stc} in a different color.")
375 {
376   LY_ASSERT_SMOB (Stencil, stc, 1);
377   Stencil *stil = unsmob<Stencil> (stc);
378   return Stencil (stil->extent_box (),
379                   scm_list_3 (ly_symbol2scm ("color"),
380                               scm_list_3 (r, g, b),
381                               stil->expr ())).smobbed_copy ();
382 }
383
384 struct Stencil_interpret_arguments
385 {
386   SCM func;
387   SCM arg1;
388 };
389
390 SCM stencil_interpret_in_scm (void *p, SCM expr)
391 {
392   Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
393   return scm_call_2 (ap->func, ap->arg1, expr);
394 }
395
396 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
397            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
398            "Parse @var{expr}, feed bits to @var{func} with first arg"
399            " @var{arg1} having offset @var{offset}.")
400 {
401   LY_ASSERT_TYPE (ly_is_procedure, func, 2);
402
403   Stencil_interpret_arguments a;
404   a.func = func;
405   a.arg1 = arg1;
406   Offset o = ly_scm2offset (offset);
407
408   interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
409
410   return SCM_UNSPECIFIED;
411 }
412
413 LY_DEFINE (ly_bracket, "ly:bracket",
414            4, 0, 0,
415            (SCM a, SCM iv, SCM t, SCM p),
416            "Make a bracket in direction@tie{}@var{a}.  The extent of the"
417            " bracket is given by @var{iv}.  The wings protrude by an amount"
418            " of@tie{}@var{p}, which may be negative.  The thickness is given"
419            " by@tie{}@var{t}.")
420 {
421   LY_ASSERT_TYPE (is_axis, a, 1);
422   LY_ASSERT_TYPE (is_number_pair, iv, 2);
423   LY_ASSERT_TYPE (scm_is_number, t, 3);
424   LY_ASSERT_TYPE (scm_is_number, p, 4);
425
426   return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
427                           scm_to_double (t),
428                           scm_to_double (p),
429                           0.95 * scm_to_double (t)).smobbed_copy ();
430 }
431
432 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
433            4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
434            "Return a stencil @var{stil} rotated @var{angle} degrees around"
435            " the relative offset (@var{x}, @var{y}).  E.g., an offset of"
436            " (-1, 1) will rotate the stencil around the left upper corner.")
437 {
438   Stencil *s = unsmob<Stencil> (stil);
439   LY_ASSERT_SMOB (Stencil, stil, 1);
440   LY_ASSERT_TYPE (scm_is_number, angle, 2);
441   LY_ASSERT_TYPE (scm_is_number, x, 3);
442   LY_ASSERT_TYPE (scm_is_number, y, 4);
443   Real a = scm_to_double (angle);
444   Real x_off = scm_to_double (x);
445   Real y_off = scm_to_double (y);
446
447   SCM new_s = s->smobbed_copy ();
448   Stencil *q = unsmob<Stencil> (new_s);
449   q->rotate_degrees (a, Offset (x_off, y_off));
450   return new_s;
451 }
452
453 LY_DEFINE (ly_stencil_rotate_absolute, "ly:stencil-rotate-absolute",
454            4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
455            "Return a stencil @var{stil} rotated @var{angle} degrees around"
456            " point (@var{x}, @var{y}), given in absolute coordinates.")
457 {
458   Stencil *s = unsmob<Stencil> (stil);
459   LY_ASSERT_SMOB (Stencil, stil, 1);
460   LY_ASSERT_TYPE (scm_is_number, angle, 2);
461   LY_ASSERT_TYPE (scm_is_number, x, 3);
462   LY_ASSERT_TYPE (scm_is_number, y, 4);
463   Real a = scm_to_double (angle);
464   Real x_off = scm_to_double (x);
465   Real y_off = scm_to_double (y);
466
467   SCM new_s = s->smobbed_copy ();
468   Stencil *q = unsmob<Stencil> (new_s);
469   q->rotate_degrees_absolute (a, Offset (x_off, y_off));
470   return new_s;
471 }
472
473 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
474            3, 0, 0,
475            (SCM xext, SCM yext, SCM blot),
476            "Make a @code{Stencil} object that prints a black box of"
477            " dimensions @var{xext}, @var{yext} and roundness @var{blot}.")
478 {
479   LY_ASSERT_TYPE (is_number_pair, xext, 1);
480   LY_ASSERT_TYPE (is_number_pair, yext, 2);
481   LY_ASSERT_TYPE (scm_is_number, blot, 3);
482
483   return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
484                                    scm_to_double (blot)).smobbed_copy ();
485 }
486
487 LY_DEFINE (ly_round_filled_polygon, "ly:round-filled-polygon",
488            2, 1, 0,
489            (SCM points, SCM blot, SCM extroversion),
490            "Make a @code{Stencil} object that prints a black polygon with"
491            " corners at the points defined by @var{points} (list of coordinate"
492            " pairs) and roundness @var{blot}.  Optional"
493            "@var{extroversion} shifts the outline outward, with the"
494            "default of@tie{}@code{-1.0} keeping the outer boundary of"
495            "the outline just inside of the polygon.")
496 {
497   SCM_ASSERT_TYPE (scm_ilength (points) > 0, points, SCM_ARG1, __FUNCTION__, "list of coordinate pairs");
498   LY_ASSERT_TYPE (scm_is_number, blot, 2);
499   Real ext = -1;
500   if (!SCM_UNBNDP (extroversion))
501     {
502       LY_ASSERT_TYPE (scm_is_number, extroversion, 3);
503       ext = scm_to_double (extroversion);
504     }
505   vector<Offset> pts;
506   for (SCM p = points; scm_is_pair (p); p = scm_cdr (p))
507     {
508       SCM scm_pt = scm_car (p);
509       if (scm_is_pair (scm_pt))
510         {
511           pts.push_back (ly_scm2offset (scm_pt));
512         }
513       else
514         {
515           // TODO: Print out warning
516         }
517     }
518   return Lookup::round_filled_polygon (pts, scm_to_double (blot), ext)
519     .smobbed_copy ();
520 }
521
522 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
523            1, 0, 0,
524            (SCM symbol),
525            "Add @var{symbol} as head of a stencil expression.")
526 {
527   LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
528   register_stencil_head (symbol);
529   return SCM_UNSPECIFIED;
530 }
531
532 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
533            0, 0, 0,
534            (),
535            "Return all symbols recognized as stencil expressions.")
536 {
537   return all_stencil_heads ();
538 }
539
540 LY_DEFINE (ly_stencil_scale, "ly:stencil-scale",
541            3, 0, 0, (SCM stil, SCM x, SCM y),
542            "Scale stencil @var{stil} using the horizontal and vertical"
543            " scaling factors @var{x} and @var{y}.  Negative values will"
544            " flip or mirror @var{stil} without changing its origin;"
545            " this may result in collisions unless it is repositioned.")
546 {
547   Stencil *s = unsmob<Stencil> (stil);
548   LY_ASSERT_SMOB (Stencil, stil, 1);
549   LY_ASSERT_TYPE (scm_is_number, x, 2);
550   LY_ASSERT_TYPE (scm_is_number, y, 3);
551
552   SCM new_s = s->smobbed_copy ();
553   Stencil *q = unsmob<Stencil> (new_s);
554
555   q->scale (scm_to_double (x), scm_to_double (y));
556   return new_s;
557 }
558
559 LY_DEFINE (ly_stencil_outline, "ly:stencil-outline",
560            2, 0, 0, (SCM stil, SCM outline),
561            "Return a stencil with the stencil expression (inking)"
562            " of stencil @var{stil} but with outline and dimensions"
563            " from stencil @var{outline}.")
564 {
565   Stencil s = *LY_ASSERT_SMOB (Stencil, stil, 1);
566   Stencil o = *LY_ASSERT_SMOB (Stencil, outline, 2);
567   return s.with_outline (o).smobbed_copy ();
568 }