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