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