]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
replace SCM_ASSERT_TYPE with LY_ASSERT_TYPE and friends
[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   LY_ASSERT_FIRST_SMOB(Stencil, stil);
25   LY_ASSERT_TYPE(scm_is_number, 2);
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   LY_ASSERT_TYPE(is_axis, 3);
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   LY_ASSERT_FIRST_SMOB(Stencil, stil);
49   LY_ASSERT_TYPE(is_number_pair, 2);
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   LY_ASSERT_FIRST_SMOB(Stencil, stil);
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   LY_ASSERT_FIRST_SMOB(Stencil, stil);
74   LY_ASSERT_TYPE(is_axis, 2);
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   LY_ASSERT_FIRST_SMOB(Stencil, stil);
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   LY_FUNC_NOTE_FIRST_ARG(first);
106   SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
107                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
108   SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
109                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
110   LY_ASSERT_TYPE(is_axis, 2);
111   LY_ASSERT_TYPE(is_direction, 3);
112
113   Real p = 0.0;
114   if (padding != SCM_UNDEFINED)
115     {
116       LY_ASSERT_TYPE(scm_is_number, 5);
117       p = scm_to_double (padding);
118     }
119   Real m = 0.0;
120   if (minimum != SCM_UNDEFINED)
121     {
122       LY_ASSERT_TYPE(scm_is_number, 6);
123       m = scm_to_double (minimum);
124     }
125
126   if (s1)
127     result = *s1;
128
129   if (s2)
130     result.add_at_edge (Axis (scm_to_int (axis)),
131                         Direction (scm_to_int (direction)), *s2, p);
132
133   return result.smobbed_copy ();
134 }
135
136 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
137            0, 0, 1, (SCM args),
138            "Combine stencils. Takes any number of arguments.")
139 {
140 #define FUNC_NAME __FUNCTION__
141   SCM_VALIDATE_REST_ARGUMENT (args);
142
143   SCM expr = SCM_EOL;
144   SCM *tail = &expr;
145   Box extent;
146   extent.set_empty ();
147
148   while (!SCM_NULLP (args))
149     {
150       Stencil *s = unsmob_stencil (scm_car (args));
151       if (!s)
152         SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
153
154       extent.unite (s->extent_box ());
155       *tail = scm_cons (s->expr (), SCM_EOL);
156       tail = SCM_CDRLOC (*tail);
157       args = scm_cdr (args);
158     }
159
160   expr = scm_cons (ly_symbol2scm ("combine-stencil"), expr);
161   return Stencil (extent, expr).smobbed_copy ();
162 }
163
164 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
165            1, 2, 0, (SCM expr, SCM xext, SCM yext),
166            " \n"
167            "Stencils are a device independent output expressions."
168            "They carry two pieces of information: \n\n"
169            "1: 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\n"
172            "2: the vertical and horizontal extents of the object.\n\n"
173            "If the extents are unspecified, they are taken  to be empty."
174            )
175 {
176   LY_FUNC_NOTE_FIRST_ARG (expr);
177   SCM_ASSERT_TYPE (!scm_is_pair (expr)
178                    || is_stencil_head (scm_car (expr)),
179                    expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
180
181
182   Interval x; 
183   if (xext != SCM_UNDEFINED)
184     {
185       LY_ASSERT_TYPE(is_number_pair, 2);
186       x = ly_scm2interval (xext);
187     }
188
189   Interval y; 
190   if (yext != SCM_UNDEFINED)
191     {
192       LY_ASSERT_TYPE(is_number_pair, 3);
193       y = ly_scm2interval (yext);
194     }
195
196   Box b (x, y);
197   Stencil s (b, expr);
198   return s.smobbed_copy ();
199 }
200
201 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
202            3, 0, 0, (SCM stil, SCM axis, SCM dir),
203            "Align @var{stil} using its own extents. "
204            "@var{dir} is a number -1, 1 are left and right respectively. "
205            "Other values are interpolated (so 0 means the center).")
206 {
207   LY_ASSERT_FIRST_SMOB(Stencil, stil);
208   LY_ASSERT_TYPE(is_axis, 2);
209   LY_ASSERT_TYPE(scm_is_number, 3);
210
211   Stencil target = *unsmob_stencil (stil);
212
213   target.align_to ((Axis)scm_to_int (axis),
214                    scm_to_double (dir));
215   return target.smobbed_copy ();
216 }
217
218 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
219            1, 0, 0, (SCM s),
220            " Analyse @var{s}, and return a list of fonts used in @var{s}.")
221 {
222   LY_ASSERT_FIRST_SMOB (Stencil, s);
223   Stencil *stil = unsmob_stencil (s);
224   return find_expression_fonts (stil->expr ());
225 }
226
227 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
228            4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
229            "Put @var{stc} in a different color.")
230 {
231   LY_ASSERT_FIRST_SMOB (Stencil, stc);
232   Stencil *stil = unsmob_stencil (stc);
233   return Stencil (stil->extent_box (),
234                   scm_list_3 (ly_symbol2scm ("color"),
235                               scm_list_3 (r, g, b),
236                               stil->expr ())).smobbed_copy ();
237 }
238
239 struct Stencil_interpret_arguments
240 {
241   SCM func;
242   SCM arg1;
243 };
244
245 void stencil_interpret_in_scm (void *p, SCM expr)
246 {
247   Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
248   scm_call_2 (ap->func, ap->arg1, expr);
249 }
250
251 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
252            4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
253            "Parse EXPR, feed bits to FUNC with first arg ARG1")
254 {
255   LY_FUNC_NOTE_FIRST_ARG(expr);
256   LY_ASSERT_TYPE (ly_is_procedure, 2);
257
258   Stencil_interpret_arguments a;
259   a.func = func;
260   a.arg1 = arg1;
261   Offset o = ly_scm2offset (offset);
262
263   interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
264
265   return SCM_UNSPECIFIED;
266 }
267
268 LY_DEFINE (ly_bracket, "ly:bracket",
269            4, 0, 0,
270            (SCM a, SCM iv, SCM t, SCM p),
271            "Make a bracket in direction @var{a}. The extent of the bracket is "
272            "given by @var{iv}. The wings protude by an amount of @var{p}, which "
273            "may be negative. The thickness is given by @var{t}.")
274 {
275   LY_ASSERT_FIRST_TYPE(is_axis, a);
276   LY_ASSERT_TYPE(is_number_pair, 2);
277   LY_ASSERT_TYPE(scm_is_number, 3);
278   LY_ASSERT_TYPE(scm_is_number, 4);
279
280   return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
281                           scm_to_double (t),
282                           scm_to_double (p),
283                           0.95 * scm_to_double (t)).smobbed_copy ();
284 }
285
286 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
287            4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
288            "Return a @var{stil} rotated @var{angle} degrees around point (@var{x}, @var{y}).")
289 {
290   Stencil *s = unsmob_stencil (stil);
291   LY_ASSERT_FIRST_SMOB(Stencil, stil);
292   LY_ASSERT_TYPE(scm_is_number, 2);
293   LY_ASSERT_TYPE(scm_is_number, 3);
294   LY_ASSERT_TYPE(scm_is_number, 4);
295   Real a = scm_to_double (angle);
296   Real x_off = scm_to_double (x);
297   Real y_off = scm_to_double (y);
298
299   SCM new_s = s->smobbed_copy ();
300   Stencil *q = unsmob_stencil (new_s);
301   q->rotate (a, Offset (x_off, y_off));
302   return new_s;
303 }
304
305 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
306            3, 0, 0,
307            (SCM xext, SCM yext, SCM blot),
308            "Make a @code{Stencil} "
309            "that prints a black box of dimensions @var{xext}, "
310            "@var{yext} and roundness @var{blot}.")
311 {
312   LY_ASSERT_FIRST_TYPE(is_number_pair, xext);
313   LY_ASSERT_TYPE(is_number_pair, 2);
314   LY_ASSERT_TYPE(scm_is_number, 3);
315
316   return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
317                                    scm_to_double (blot)).smobbed_copy ();
318 }
319
320 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
321            1, 0, 0,
322            (SCM symbol),
323            "Add @var{symbol} as head of a stencil expression")
324 {
325   LY_ASSERT_FIRST_TYPE(ly_is_symbol, symbol);
326   register_stencil_head (symbol);
327   return SCM_UNSPECIFIED;
328 }
329
330 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
331            0, 0, 0,
332            (),
333            "Return all symbols recognized as stencil expressions.")
334 {
335   return all_stencil_heads ();
336 }