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