]> git.donarmstrong.com Git - lilypond.git/blob - lily/stencil-scheme.cc
* lily/stencil-scheme.cc:
[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 LY_DEFINE (ly_stencil_set_extent_x,"ly:stencil-set-extent!",
14            3, 0, 0, (SCM stil, SCM axis, SCM np),
15            "Set the extent of @var{stil} "
16            "(@var{extent} must be a pair of numbers) "
17            "in @var{axis} direction (0 or 1 for x- and y-axis respectively).")
18 {
19   Stencil *s = unsmob_stencil (stil);
20   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
21   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
22   SCM_ASSERT_TYPE (is_number_pair (np), np, SCM_ARG3, __FUNCTION__, "number pair");
23
24   Interval iv = ly_scm2interval (np);
25   s->dim_[Axis (gh_scm2int (axis))] = iv;
26
27   return SCM_UNDEFINED;
28 }
29
30 LY_DEFINE (ly_translate_stencil_axis, "ly:stencil-translate-axis",
31            3, 0, 0, (SCM stil, SCM amount, SCM axis),
32            "Return a @var{stil}, "
33            "but translated by @var{amount} in @var{axis} direction.")
34 {
35   Stencil *s = unsmob_stencil (stil);
36   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
37   SCM_ASSERT_TYPE (gh_number_p (amount), amount, SCM_ARG2, __FUNCTION__, "number pair");
38   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
39
40   Stencil q (*s);
41   q.translate_axis (gh_scm2double (amount), Axis (gh_scm2int (axis)));
42
43   return q.smobbed_copy();
44 }
45
46 LY_DEFINE (ly_translate_stencil,"ly:stencil-translate",
47            2, 0, 0, (SCM stil, SCM offset),
48            "Return a @var{stil}, "
49            "but translated by @var{offset} (a pair of numbers).")
50 {
51   Stencil *s = unsmob_stencil (stil);
52   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
53   SCM_ASSERT_TYPE (is_number_pair (offset), offset, SCM_ARG2, __FUNCTION__, "number pair");
54   Offset o = ly_scm2offset (offset);
55
56   Stencil q (*s);
57   q.translate (o);
58   return q.smobbed_copy();
59 }
60
61 LY_DEFINE (ly_stencil_get_expr, "ly:stencil-get-expr",
62            1 , 0, 0, (SCM stil),
63            "Return the expression of @var{stil}.")
64 {
65   Stencil *s = unsmob_stencil (stil);
66   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
67   return s->get_expr ();
68 }
69
70 LY_DEFINE (ly_stencil_get_extent,
71            "ly:stencil-get-extent", 2 , 0, 0,  (SCM stil, SCM axis),
72            "Return a pair of numbers signifying the extent of @var{stil} in "
73            "@var{axis} direction (0 or 1 for x and y axis respectively).")
74 {
75   Stencil *s = unsmob_stencil (stil);
76   SCM_ASSERT_TYPE (s, stil, SCM_ARG1, __FUNCTION__, "stencil");
77   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
78
79   return ly_interval2scm (s->extent (Axis (gh_scm2int (axis))));
80 }
81
82 LY_DEFINE (ly_stencil_combined_at_edge,
83            "ly:stencil-combine-at-edge",
84            4, 2, 0,  (SCM first, SCM axis, SCM direction,
85                       SCM second,
86                       SCM padding,
87                       SCM minimum),
88            "Construct a stencil by putting @var{second} next to @var{first}. "
89            "@var{axis} can be 0 (x-axis) or 1 (y-axis), "
90            "@var{direction} can be -1 (left or down) or 1 (right or up). "
91            "The stencils are juxtaposed with  @var{padding} as extra space. "
92            "If this puts the reference points closer than @var{minimum}, "
93            "they are moved by the latter amount.")
94 {
95   Stencil *s1 = unsmob_stencil (first);
96   Stencil *s2 = unsmob_stencil (second);
97   Stencil result;
98
99   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
100   SCM_ASSERT_TYPE (is_direction (direction), direction, SCM_ARG4, __FUNCTION__, "dir");
101
102   Real p = 0.0;
103   if (padding != SCM_UNDEFINED)
104     {
105       SCM_ASSERT_TYPE(gh_number_p (padding), padding, SCM_ARG5, __FUNCTION__, "number");
106       p = gh_scm2double (padding);
107     }
108   Real m = 0.0;
109   if (minimum != SCM_UNDEFINED)
110     {
111       SCM_ASSERT_TYPE(gh_number_p (minimum), minimum, SCM_ARG6, __FUNCTION__, "number");
112       m = gh_scm2double (minimum);
113     }
114
115   if (s1)
116     result = *s1;
117   if (s2)
118     result.add_at_edge (Axis (gh_scm2int (axis)),
119                         Direction (gh_scm2int (direction)), *s2, p, m);
120
121   return result.smobbed_copy ();
122 }
123
124 /*  FIXME: support variable number of arguments.  */
125 LY_DEFINE (ly_stencil_add ,
126            "ly:stencil-add", 0, 0, 1, (SCM args),
127            "Combine stencils. Takes any number of arguments.")
128 {
129 #define FUNC_NAME __FUNCTION__
130   SCM_VALIDATE_REST_ARGUMENT (args);
131
132   Stencil result;
133
134   while (!SCM_NULLP (args))
135     {
136       Stencil *s = unsmob_stencil (gh_car (args));
137       if (!s)
138         SCM_ASSERT_TYPE (s, gh_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
139
140       result.add_stencil (*s);
141       args = gh_cdr (args);
142     }
143
144   return result.smobbed_copy ();
145 }
146
147 LY_DEFINE (ly_make_stencil,
148            "ly:make-stencil", 3, 0, 0,  (SCM expr, SCM xext, SCM yext),
149            " \n"
150            "Stencils are a device independent output expressions."
151            "They carry two pieces of information: \n\n"
152            "1: a specification of how to print this object. "
153            "This specification is processed by the output backends, "
154            " for example @file{scm/output-tex.scm}.\n\n"
155            "2: the vertical and horizontal extents of the object.\n\n")
156 {
157   SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG2, __FUNCTION__, "number pair");
158   SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG3, __FUNCTION__, "number pair");
159
160   Box b (ly_scm2interval (xext), ly_scm2interval(yext));
161   Stencil s (b, expr);
162   return s.smobbed_copy ();
163 }
164
165 SCM
166 fontify_atom (Font_metric const *met, SCM f)
167 {
168   if (f == SCM_EOL)
169     return f;
170   else
171     return  scm_list_n (ly_symbol2scm ("fontify"),
172                         ly_quote_scm (met->description_), f, SCM_UNDEFINED);
173 }
174
175 LY_DEFINE (ly_fontify_atom,"ly:fontify-atom", 2, 0, 0,
176            (SCM met, SCM f),
177            "Add a font selection command for the font metric @var{met} "
178            "to @var{f}.")
179 {
180   SCM_ASSERT_TYPE(unsmob_metrics (met), met, SCM_ARG1, __FUNCTION__, "font metric");
181
182   return fontify_atom (unsmob_metrics (met), f);
183 }
184
185 LY_DEFINE (ly_align_to_x, "ly:stencil-align-to!",
186            3, 0, 0, (SCM stil, SCM axis, SCM dir),
187            "Align @var{stil} using its own extents. "
188            "@var{dir} is a number -1, 1 are left and right respectively. "
189            "Other values are interpolated (so 0 means the center. ")
190 {
191   SCM_ASSERT_TYPE (unsmob_stencil (stil), stil, SCM_ARG1, __FUNCTION__, "stencil");
192   SCM_ASSERT_TYPE (is_axis (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
193   SCM_ASSERT_TYPE (gh_number_p (dir), dir, SCM_ARG3, __FUNCTION__, "number");
194
195   unsmob_stencil (stil)->align_to ((Axis)gh_scm2int (axis),
196                                    gh_scm2double (dir));
197   return SCM_UNDEFINED;
198 }