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