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