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