]> 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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "font-metric.hh"
10 #include "stencil.hh"
11
12 /*
13   TODO: naming add/combine.
14  */
15 /*
16   UGH. Junk all mutators.
17  */
18 LY_DEFINE (ly_stencil_set_extent_x, "ly:stencil-set-extent!",
19            3, 0, 0, (SCM stil, SCM axis, SCM np),
20            "Set the extent of @var{stil} "
21            "(@var{extent} must be a pair of numbers) "
22            "in @var{axis} direction (0 or 1 for x- and y-axis respectively).")
23 {
24   Stencil *s = unsmob_stencil (stil);
25   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
26   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
27   SCM_ASSERT_TYPE (is_number_pair (np), np, SCM_ARG3, __FUNCTION__,
28                    "number pair");
29
30   Interval iv = ly_scm2interval (np);
31   s->dim_[Axis (scm_to_int (axis))] = iv;
32
33   return SCM_UNSPECIFIED;
34 }
35
36 LY_DEFINE (ly_translate_stencil_axis, "ly:stencil-translate-axis",
37            3, 0, 0, (SCM stil, SCM amount, SCM axis),
38            "Return a copy of @var{stil} but translated by @var{amount} in @var{axis} direction.")
39 {
40   Stencil *s = unsmob_stencil (stil);
41   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
42   SCM_ASSERT_TYPE (scm_is_number (amount), amount, SCM_ARG2, __FUNCTION__, "number pair");
43   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
44
45   SCM new_s = s->smobbed_copy ();
46   Stencil *q = unsmob_stencil (new_s);
47   q->translate_axis (scm_to_double (amount), Axis (scm_to_int (axis)));
48   return new_s;
49
50 }
51
52 LY_DEFINE (ly_translate_stencil, "ly:stencil-translate",
53            2, 0, 0, (SCM stil, SCM offset),
54            "Return a @var{stil}, "
55            "but translated by @var{offset} (a pair of numbers).")
56 {
57   Stencil *s = unsmob_stencil (stil);
58   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
59   SCM_ASSERT_TYPE (is_number_pair (offset), offset, SCM_ARG2, __FUNCTION__, "number pair");
60   Offset o = ly_scm2offset (offset);
61
62   SCM new_s = s->smobbed_copy ();
63   Stencil *q = unsmob_stencil (new_s);
64   q->translate (o);
65   return new_s;
66 }
67
68 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
69            1, 0, 0, (SCM stil),
70            "Return the expression of @var{stil}.")
71 {
72   Stencil *s = unsmob_stencil (stil);
73   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
74   return s->expr ();
75 }
76
77 LY_DEFINE (ly_stencil_get_extent, "ly:stencil-extent",
78            2, 0, 0, (SCM stil, SCM axis),
79            "Return a pair of numbers signifying the extent of @var{stil} in "
80            "@var{axis} direction (0 or 1 for x and y axis respectively).")
81 {
82   Stencil *s = unsmob_stencil (stil);
83   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
84   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
85
86   return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
87 }
88
89
90 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
91            1, 0, 0, (SCM stil),
92            "Return whether @var{stil} is empty ")
93 {
94   Stencil *s = unsmob_stencil (stil);
95   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
96   return scm_from_bool (s->is_empty ());
97 }
98
99
100 LY_DEFINE (ly_stencil_origin, "ly:stencil-origin",
101            2, 0, 0, (SCM stil, SCM axis),
102            "Return a pair of numbers signifying the origin @var{stil} in "
103            "@var{axis} direction (0 or 1 for x and y axis respectively).")
104 {
105   Stencil *s = unsmob_stencil (stil);
106   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
107   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
108
109   return scm_from_double (s->origin()[Axis (scm_to_int (axis))]);
110 }
111
112
113 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
114            4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
115                      SCM padding, SCM minimum),
116            "Similar to @code{ly:stencil-combine-edge}, but returns "
117            "@var{second} positioned to be next to @var{first}. ")
118 {
119   /*
120     C&P from combine-at-edge.
121    */
122   Stencil *s1 = unsmob_stencil (first);
123   Stencil *s2 = unsmob_stencil (second);
124   Stencil first_stencil;
125
126   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
127   SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
128
129   Real p = 0.0;
130   if (padding != SCM_UNDEFINED)
131     {
132       SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
133       p = scm_to_double (padding);
134     }
135   Real m = 0.0;
136   if (minimum != SCM_UNDEFINED)
137     {
138       SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
139       m = scm_to_double (minimum);
140     }
141
142   if (s1)
143     first_stencil = *s1;
144
145   if (s2)
146     return first_stencil.moved_to_edge (Axis (scm_to_int (axis)),
147                                         Direction (scm_to_int (direction)),
148                                         *s2, p, m).smobbed_copy ();
149   else
150     return Stencil().smobbed_copy ();
151 }
152
153
154
155 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
156            4, 2, 0,  (SCM first, SCM axis, SCM direction,
157                       SCM second,
158                       SCM padding,
159                       SCM minimum),
160            "Construct a stencil by putting @var{second} next to @var{first}. "
161            "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
162            "@var{direction} can be -1 (left or down) or 1 (right or up). "
163            "The stencils are juxtaposed with  @var{padding} as extra space. "
164            "If this puts the reference points closer than @var{minimum}, "
165            "they are moved by the latter amount."
166            "@var{first} and @var{second} may also be '() or #f.")
167 {
168   Stencil *s1 = unsmob_stencil (first);
169   Stencil *s2 = unsmob_stencil (second);
170   Stencil result;
171
172   SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first  == SCM_EOL,
173                    first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
174   SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second  == SCM_EOL,
175                    second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
176   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
177   SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG3, __FUNCTION__, "dir");
178
179   Real p = 0.0;
180   if (padding != SCM_UNDEFINED)
181     {
182       SCM_ASSERT_TYPE (scm_is_number (padding), padding, SCM_ARG5, __FUNCTION__, "number");
183       p = scm_to_double (padding);
184     }
185   Real m = 0.0;
186   if (minimum != SCM_UNDEFINED)
187     {
188       SCM_ASSERT_TYPE (scm_is_number (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
189       m = scm_to_double (minimum);
190     }
191
192   if (s1)
193     result = *s1;
194   
195   if (s2)
196     result.add_at_edge (Axis (scm_to_int (axis)),
197                         Direction (scm_to_int (direction)), *s2, p, m);
198
199   return result.smobbed_copy ();
200 }
201
202 LY_DEFINE (ly_stencil_add , "ly:stencil-add",
203            0, 0, 1, (SCM args),
204            "Combine stencils. Takes any number of arguments.")
205 {
206 #define FUNC_NAME __FUNCTION__
207   SCM_VALIDATE_REST_ARGUMENT (args);
208
209   Stencil result;
210
211   while (!SCM_NULLP (args))
212     {
213       Stencil *s = unsmob_stencil (scm_car (args));
214       if (!s)
215         SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
216
217       result.add_stencil (*s);
218       args = scm_cdr (args);
219     }
220
221   return result.smobbed_copy ();
222 }
223
224 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
225            3, 0, 0,  (SCM expr, SCM xext, SCM yext),
226            " \n"
227            "Stencils are a device independent output expressions."
228            "They carry two pieces of information: \n\n"
229            "1: a specification of how to print this object. "
230            "This specification is processed by the output backends, "
231            " for example @file{scm/output-tex.scm}.\n\n"
232            "2: the vertical and horizontal extents of the object.\n\n")
233 {
234   SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
235   SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
236
237   Box b (ly_scm2interval (xext), ly_scm2interval (yext));
238   Stencil s (b, expr);
239   return s.smobbed_copy ();
240 }
241
242
243 LY_DEFINE (ly_stencil_align_to_x, "ly:stencil-align-to!",
244            3, 0, 0, (SCM stil, SCM axis, SCM dir),
245            "Align @var{stil} using its own extents. "
246            "@var{dir} is a number -1, 1 are left and right respectively. "
247            "Other values are interpolated (so 0 means the center. ")
248 {
249   SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
250   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
251   SCM_ASSERT_TYPE (scm_is_number (dir), dir, SCM_ARG3, __FUNCTION__, "number");
252
253   unsmob_stencil (stil)->align_to ((Axis)scm_to_int (axis),
254                                    scm_to_double (dir));
255   return stil;
256 }