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