]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
uniformize C++ <-> scheme name mappings.
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9
10 #include "font-metric.hh"
11 #include "libc-extension.hh"
12 #include "lookup.hh"
13 #include "stencil.hh"
14
15 /*
16   TODO: naming add/combine.
17 */
18
19 LY_DEFINE (ly_stencil_translate_axis, "ly:stencil-translate-axis",
20            3, 0, 0, (SCM stil, SCM amount, SCM axis),
21            "Return a copy of @var{stil} but translated by @var{amount} in @var{axis} direction.")
22 {
23   Stencil *s = unsmob_stencil (stil);
24   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
25   SCM_ASSERT_TYPE (scm_is_number (amount), amount, SCM_ARG2, __FUNCTION__, "number");
26
27   Real real_amount = scm_to_double (amount);
28
29 #if 0
30   SCM_ASSERT_TYPE (!isinf (real_amount) && !isnan (real_amount),
31                    amount, SCM_ARG2, __FUNCTION__, "finite number");
32 #endif
33
34   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
35
36   SCM new_s = s->smobbed_copy ();
37   Stencil *q = unsmob_stencil (new_s);
38   q->translate_axis (real_amount, Axis (scm_to_int (axis)));
39   return new_s;
40 }
41
42 LY_DEFINE (ly_stencil_translate, "ly:stencil-translate",
43            2, 0, 0, (SCM stil, SCM offset),
44            "Return a @var{stil}, "
45            "but translated by @var{offset} (a pair of numbers).")
46 {
47   Stencil *s = unsmob_stencil (stil);
48   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
49   SCM_ASSERT_TYPE (is_number_pair (offset), offset, SCM_ARG2, __FUNCTION__, "number pair");
50   Offset o = ly_scm2offset (offset);
51
52   SCM new_s = s->smobbed_copy ();
53   Stencil *q = unsmob_stencil (new_s);
54   q->translate (o);
55   return new_s;
56 }
57
58 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
59            1, 0, 0, (SCM stil),
60            "Return the expression of @var{stil}.")
61 {
62   Stencil *s = unsmob_stencil (stil);
63   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
64   return s->expr ();
65 }
66
67 LY_DEFINE (ly_stencil_extent, "ly:stencil-extent",
68            2, 0, 0, (SCM stil, SCM axis),
69            "Return a pair of numbers signifying the extent of @var{stil} in "
70            "@var{axis} direction (0 or 1 for x and y axis respectively).")
71 {
72   Stencil *s = unsmob_stencil (stil);
73   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
74   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
75
76   return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
77 }
78
79 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
80            1, 0, 0, (SCM stil),
81            "Return whether @var{stil} is empty ")
82 {
83   Stencil *s = unsmob_stencil (stil);
84   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
85   return scm_from_bool (s->is_empty ());
86 }
87
88 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
89            4, 2, 0, (SCM first, SCM axis, SCM direction,
90                      SCM second,
91                      SCM padding,
92                      SCM minimum),
93            "Construct a stencil by putting @var{second} next to @var{first}. "
94            "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
95            "@var{direction} can be -1 (left or down) or 1 (right or up). "
96            "The stencils are juxtaposed with  @var{padding} as extra space. "
97            "If this puts the reference points closer than @var{minimum}, "
98            "they are moved by the latter amount."
99            "@var{first} and @var{second} may also be '() or #f.")
100 {
101   Stencil *s1 = unsmob_stencil (first);
102   Stencil *s2 = unsmob_stencil (second);
103   Stencil result;
104
105   SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
106                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
107   SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
108                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
109   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
110   SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
111
112   Real p = 0.0;
113   if (padding != SCM_UNDEFINED)
114     {
115       SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
116       p = scm_to_double (padding);
117     }
118   Real m = 0.0;
119   if (minimum != SCM_UNDEFINED)
120     {
121       SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
122       m = scm_to_double (minimum);
123     }
124
125   if (s1)
126     result = *s1;
127
128   if (s2)
129     result.add_at_edge (Axis (scm_to_int (axis)),
130                         Direction (scm_to_int (direction)), *s2, p);
131
132   return result.smobbed_copy ();
133 }
134
135 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
136            0, 0, 1, (SCM args),
137            "Combine stencils. Takes any number of arguments.")
138 {
139 #define FUNC_NAME __FUNCTION__
140   SCM_VALIDATE_REST_ARGUMENT (args);
141
142   SCM expr = SCM_EOL;
143   SCM *tail = &expr;
144   Box extent;
145   extent.set_empty ();
146
147   while (!SCM_NULLP (args))
148     {
149       Stencil *s = unsmob_stencil (scm_car (args));
150       if (!s)
151         SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
152
153       extent.unite (s->extent_box ());
154       *tail = scm_cons (s->expr (), SCM_EOL);
155       tail = SCM_CDRLOC (*tail);
156       args = scm_cdr (args);
157     }
158
159   expr = scm_cons (ly_symbol2scm ("combine-stencil"), expr);
160   return Stencil (extent, expr).smobbed_copy ();
161 }
162
163 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
164            1, 2, 0, (SCM expr, SCM xext, SCM yext),
165            " \n"
166            "Stencils are a device independent output expressions."
167            "They carry two pieces of information: \n\n"
168            "1: a specification of how to print this object. "
169            "This specification is processed by the output backends, "
170            " for example @file{scm/output-ps.scm}.\n\n"
171            "2: the vertical and horizontal extents of the object.\n\n"
172            "If the extents are unspecified, they are taken  to be empty."
173            )
174 {
175   SCM_ASSERT_TYPE (!scm_is_pair (expr)
176                    || is_stencil_head (scm_car (expr)),
177                    expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
178
179
180   Interval x; 
181   if (xext != SCM_UNDEFINED)
182     {
183       SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
184       x = ly_scm2interval (xext);
185     }
186
187   Interval y; 
188   if (yext != SCM_UNDEFINED)
189     {
190       SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
191       y = ly_scm2interval (yext);
192     }
193
194   Box b (x, y);
195   Stencil s (b, expr);
196   return s.smobbed_copy ();
197 }
198
199 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
200            3, 0, 0, (SCM stil, SCM axis, SCM dir),
201            "Align @var{stil} using its own extents. "
202            "@var{dir} is a number -1, 1 are left and right respectively. "
203            "Other values are interpolated (so 0 means the center).")
204 {
205   SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
206   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
207   SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
208
209   Stencil target = *unsmob_stencil (stil);
210
211   target.align_to ((Axis)scm_to_int (axis),
212                    scm_to_double (dir));
213   return target.smobbed_copy ();
214 }
215
216 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
217            1, 0, 0, (SCM s),
218            " Analyse @var{s}, and return a list of fonts used in @var{s}.")
219 {
220   Stencil *stil = unsmob_stencil (s);
221   SCM_ASSERT_TYPE (stil, s, SCM_ARG1, __FUNCTION__, "Stencil");
222   return find_expression_fonts (stil->expr ());
223 }
224
225 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
226            4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
227            "Put @var{stc} in a different color.")
228 {
229   Stencil *stil = unsmob_stencil (stc);
230   SCM_ASSERT_TYPE (stil, stc, SCM_ARG1, __FUNCTION__, "Stencil");
231   return Stencil (stil->extent_box (),
232                   scm_list_3 (ly_symbol2scm ("color"),
233                               scm_list_3 (r, g, b),
234                               stil->expr ())).smobbed_copy ();
235 }
236
237 struct Stencil_interpret_arguments
238 {
239   SCM func;
240   SCM arg1;
241 };
242
243 void stencil_interpret_in_scm (void *p, SCM expr)
244 {
245   Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
246   scm_call_2 (ap->func, ap->arg1, expr);
247 }
248
249 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
250            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
251            "Parse EXPR, feed bits to FUNC with first arg ARG1")
252 {
253   SCM_ASSERT_TYPE (ly_is_procedure (func), func, SCM_ARG1, __FUNCTION__,
254                    "procedure");
255
256   Stencil_interpret_arguments a;
257   a.func = func;
258   a.arg1 = arg1;
259   Offset o = ly_scm2offset (offset);
260
261   interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
262
263   return SCM_UNSPECIFIED;
264 }
265
266 LY_DEFINE (ly_bracket, "ly:bracket",
267            4, 0, 0,
268            (SCM a, SCM iv, SCM t, SCM p),
269            "Make a bracket in direction @var{a}. The extent of the bracket is "
270            "given by @var{iv}. The wings protude by an amount of @var{p}, which "
271            "may be negative. The thickness is given by @var{t}.")
272 {
273   SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis");
274   SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair");
275   SCM_ASSERT_TYPE (scm_is_number (t), a, SCM_ARG3, __FUNCTION__, "number");
276   SCM_ASSERT_TYPE (scm_is_number (p), a, SCM_ARG4, __FUNCTION__, "number");
277
278   return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
279                           scm_to_double (t),
280                           scm_to_double (p),
281                           0.95 * scm_to_double (t)).smobbed_copy ();
282 }
283
284 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
285            4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
286            "Return a @var{stil} rotated @var{angle} degrees around point (@var{x}, @var{y}).")
287 {
288   Stencil *s = unsmob_stencil (stil);
289   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
290   SCM_ASSERT_TYPE (scm_is_number (angle), angle, SCM_ARG2, __FUNCTION__, "number");
291   SCM_ASSERT_TYPE (scm_is_number (x), x, SCM_ARG3, __FUNCTION__, "number");
292   SCM_ASSERT_TYPE (scm_is_number (y), y, SCM_ARG4, __FUNCTION__, "number");
293   Real a = scm_to_double (angle);
294   Real x_off = scm_to_double (x);
295   Real y_off = scm_to_double (y);
296
297   SCM new_s = s->smobbed_copy ();
298   Stencil *q = unsmob_stencil (new_s);
299   q->rotate (a, Offset (x_off, y_off));
300   return new_s;
301 }
302
303 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
304            3, 0, 0,
305            (SCM xext, SCM yext, SCM blot),
306            "Make a @code{Stencil} "
307            "that prints a black box of dimensions @var{xext}, "
308            "@var{yext} and roundness @var{blot}.")
309 {
310   SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair");
311   SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair");
312   SCM_ASSERT_TYPE (scm_is_number (blot), blot, SCM_ARG3, __FUNCTION__, "number");
313
314   return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
315                                    scm_to_double (blot)).smobbed_copy ();
316 }
317
318 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
319            1, 0, 0,
320            (SCM symbol),
321            "Add @var{symbol} as head of a stencil expression")
322 {
323   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol,
324                    SCM_ARG1, __FUNCTION__, "Symbol");
325   register_stencil_head (symbol);
326   return SCM_UNSPECIFIED;
327 }
328
329 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
330            0, 0, 0,
331            (),
332            "Return all symbols recognized as stencil expressions.")
333 {
334   return all_stencil_heads ();
335 }