]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
* lily/include/lily-guile.hh: is_x -> ly_c_X_p naming.
[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   UMGH. junkme!
17   
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 (ly_scm2int (axis))] = iv;
33
34   return SCM_UNDEFINED;
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 (ly_c_number_p (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 (ly_scm2double (amount), Axis (ly_scm2int (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_get_expr, "ly:stencil-get-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->get_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 (ly_scm2int (axis))));
88 }
89
90 LY_DEFINE (ly_stencil_moved_to_edge, "ly:stencil-moved-to-edge",
91            4, 2, 0, (SCM first, SCM axis, SCM direction, SCM second,
92                      SCM padding, SCM minimum),
93            "Similar to @code{ly:stencil-combine-edge}, but returns "
94            "@var{second} positioned to be next to @var{first}. ")
95 {
96   /*
97     C&P from combine-at-edge.
98    */
99   Stencil *s1 = unsmob_stencil (first);
100   Stencil *s2 = unsmob_stencil (second);
101   Stencil first_stencil;
102
103   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
104   SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
105
106   Real p = 0.0;
107   if (padding != SCM_UNDEFINED)
108     {
109       SCM_ASSERT_TYPE (ly_c_number_p (padding), padding, SCM_ARG5, __FUNCTION__, "number");
110       p = ly_scm2double (padding);
111     }
112   Real m = 0.0;
113   if (minimum != SCM_UNDEFINED)
114     {
115       SCM_ASSERT_TYPE (ly_c_number_p (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
116       m = ly_scm2double (minimum);
117     }
118
119   if (s1)
120     first_stencil = *s1;
121
122   if (s2)
123     return first_stencil.moved_to_edge (Axis (ly_scm2int (axis)),
124                                         Direction (ly_scm2int (direction)),
125                                         *s2, p, m).smobbed_copy ();
126   else
127     return Stencil().smobbed_copy ();
128 }
129
130
131   
132 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
133            4, 2, 0,  (SCM first, SCM axis, SCM direction,
134                       SCM second,
135                       SCM padding,
136                       SCM minimum),
137            "Construct a stencil by putting @var{second} next to @var{first}. "
138            "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
139            "@var{direction} can be -1 (left or down) or 1 (right or up). "
140            "The stencils are juxtaposed with  @var{padding} as extra space. "
141            "If this puts the reference points closer than @var{minimum}, "
142            "they are moved by the latter amount.")
143 {
144   Stencil *s1 = unsmob_stencil (first);
145   Stencil *s2 = unsmob_stencil (second);
146   Stencil result;
147
148   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
149   SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
150
151   Real p = 0.0;
152   if (padding != SCM_UNDEFINED)
153     {
154       SCM_ASSERT_TYPE (ly_c_number_p (padding), padding, SCM_ARG5, __FUNCTION__, "number");
155       p = ly_scm2double (padding);
156     }
157   Real m = 0.0;
158   if (minimum != SCM_UNDEFINED)
159     {
160       SCM_ASSERT_TYPE (ly_c_number_p (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
161       m = ly_scm2double (minimum);
162     }
163
164   if (s1)
165     result = *s1;
166   if (s2)
167     result.add_at_edge (Axis (ly_scm2int (axis)),
168                         Direction (ly_scm2int (direction)), *s2, p, m);
169
170   return result.smobbed_copy ();
171 }
172
173 LY_DEFINE (ly_stencil_add , "ly:stencil-add",
174            0, 0, 1, (SCM args),
175            "Combine stencils. Takes any number of arguments.")
176 {
177 #define FUNC_NAME __FUNCTION__
178   SCM_VALIDATE_REST_ARGUMENT (args);
179
180   Stencil result;
181
182   while (!SCM_NULLP (args))
183     {
184       Stencil *s = unsmob_stencil (ly_car (args));
185       if (!s)
186         SCM_ASSERT_TYPE (s, ly_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
187
188       result.add_stencil (*s);
189       args = ly_cdr (args);
190     }
191
192   return result.smobbed_copy ();
193 }
194
195 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
196            3, 0, 0,  (SCM expr, SCM xext, SCM yext),
197            " \n"
198            "Stencils are a device independent output expressions."
199            "They carry two pieces of information: \n\n"
200            "1: a specification of how to print this object. "
201            "This specification is processed by the output backends, "
202            " for example @file{scm/output-tex.scm}.\n\n"
203            "2: the vertical and horizontal extents of the object.\n\n")
204 {
205   SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
206   SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
207
208   Box b (ly_scm2interval (xext), ly_scm2interval (yext));
209   Stencil s (b, expr);
210   return s.smobbed_copy ();
211 }
212
213
214 LY_DEFINE (ly_align_to_x, "ly:stencil-align-to!",
215            3, 0, 0, (SCM stil, SCM axis, SCM dir),
216            "Align @var{stil} using its own extents. "
217            "@var{dir} is a number -1, 1 are left and right respectively. "
218            "Other values are interpolated (so 0 means the center. ")
219 {
220   SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
221   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
222   SCM_ASSERT_TYPE (ly_c_number_p (dir), dir, SCM_ARG3, __FUNCTION__, "number");
223
224   unsmob_stencil (stil)->align_to ((Axis)ly_scm2int (axis),
225                                    ly_scm2double (dir));
226   return SCM_UNDEFINED;
227 }