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