Correct left text alignment of DynamicTextSpanner.
[lilypond.git] / lily / stencil-scheme.cc
blobde39fca5c340ca19e20cfaf14a41e9b4a0d12761
1 /*
2 stencil-scheme.cc -- implement Stencil scheme accessors
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
10 #include "font-metric.hh"
11 #include "libc-extension.hh"
12 #include "lookup.hh"
13 #include "stencil.hh"
16 TODO: naming add/combine.
19 LY_DEFINE (ly_stencil_translate_axis, "ly:stencil-translate-axis",
20 3, 0, 0, (SCM stil, SCM amount, SCM axis),
21 "Return a copy of @var{stil} but translated by @var{amount}"
22 " in @var{axis} direction.")
24 Stencil *s = unsmob_stencil (stil);
25 LY_ASSERT_SMOB (Stencil, stil, 1);
26 LY_ASSERT_TYPE (scm_is_number, amount, 2);
27 LY_ASSERT_TYPE (is_axis, axis, 3);
29 Real real_amount = scm_to_double (amount);
31 SCM new_s = s->smobbed_copy ();
32 Stencil *q = unsmob_stencil (new_s);
33 q->translate_axis (real_amount, Axis (scm_to_int (axis)));
34 return new_s;
37 LY_DEFINE (ly_stencil_translate, "ly:stencil-translate",
38 2, 0, 0, (SCM stil, SCM offset),
39 "Return a @var{stil}, but translated by @var{offset}"
40 " (a pair of numbers).")
42 Stencil *s = unsmob_stencil (stil);
43 LY_ASSERT_SMOB (Stencil, stil, 1);
44 LY_ASSERT_TYPE (is_number_pair, offset, 2);
45 Offset o = ly_scm2offset (offset);
47 SCM new_s = s->smobbed_copy ();
48 Stencil *q = unsmob_stencil (new_s);
49 q->translate (o);
50 return new_s;
53 LY_DEFINE (ly_stencil_expr, "ly:stencil-expr",
54 1, 0, 0, (SCM stil),
55 "Return the expression of @var{stil}.")
57 Stencil *s = unsmob_stencil (stil);
58 LY_ASSERT_SMOB (Stencil, stil, 1);
59 return s->expr ();
62 LY_DEFINE (ly_stencil_extent, "ly:stencil-extent",
63 2, 0, 0, (SCM stil, SCM axis),
64 "Return a pair of numbers signifying the extent of @var{stil} in"
65 " @var{axis} direction (@code{0} or @code{1} for x and"
66 " y@tie{}axis, respectively).")
68 Stencil *s = unsmob_stencil (stil);
69 LY_ASSERT_SMOB (Stencil, stil, 1);
70 LY_ASSERT_TYPE (is_axis, axis, 2);
72 return ly_interval2scm (s->extent (Axis (scm_to_int (axis))));
75 LY_DEFINE (ly_stencil_empty_p, "ly:stencil-empty?",
76 1, 0, 0, (SCM stil),
77 "Return whether @var{stil} is empty.")
79 Stencil *s = unsmob_stencil (stil);
80 LY_ASSERT_SMOB (Stencil, stil, 1);
81 return scm_from_bool (s->is_empty ());
84 LY_DEFINE (ly_stencil_combine_at_edge, "ly:stencil-combine-at-edge",
85 4, 2, 0, (SCM first, SCM axis, SCM direction,
86 SCM second,
87 SCM padding,
88 SCM minimum),
89 "Construct a stencil by putting @var{second} next to @var{first}."
90 " @var{axis} can be 0 (x-axis) or@tie{}1 (y-axis)."
91 " @var{direction} can be -1 (left or down) or@tie{}1 (right or"
92 " up). The stencils are juxtaposed with @var{padding} as extra"
93 " space. If this puts the reference points closer than"
94 " @var{minimum}, they are moved by the latter amount."
95 " @var{first} and @var{second} may also be @code{'()} or"
96 " @code{#f}.")
98 Stencil *s1 = unsmob_stencil (first);
99 Stencil *s2 = unsmob_stencil (second);
100 Stencil result;
102 SCM_ASSERT_TYPE (s1 || first == SCM_BOOL_F || first == SCM_EOL,
103 first, SCM_ARG1, __FUNCTION__, "Stencil, #f or ()");
104 SCM_ASSERT_TYPE (s2 || second == SCM_BOOL_F || second == SCM_EOL,
105 second, SCM_ARG4, __FUNCTION__, "Stencil, #f or ()");
106 LY_ASSERT_TYPE (is_axis, axis, 2);
107 LY_ASSERT_TYPE (is_direction, direction, 3);
109 Real p = 0.0;
110 if (padding != SCM_UNDEFINED)
112 LY_ASSERT_TYPE (scm_is_number, padding, 5);
113 p = scm_to_double (padding);
115 Real m = 0.0;
116 if (minimum != SCM_UNDEFINED)
118 LY_ASSERT_TYPE (scm_is_number, minimum, 6);
119 m = scm_to_double (minimum);
122 if (s1)
123 result = *s1;
125 if (s2)
126 result.add_at_edge (Axis (scm_to_int (axis)),
127 Direction (scm_to_int (direction)), *s2, p);
129 return result.smobbed_copy ();
132 LY_DEFINE (ly_stencil_add, "ly:stencil-add",
133 0, 0, 1, (SCM args),
134 "Combine stencils. Takes any number of arguments.")
136 #define FUNC_NAME __FUNCTION__
137 SCM_VALIDATE_REST_ARGUMENT (args);
139 SCM expr = SCM_EOL;
140 SCM *tail = &expr;
141 Box extent;
142 extent.set_empty ();
144 while (!SCM_NULLP (args))
146 Stencil *s = unsmob_stencil (scm_car (args));
147 if (!s)
148 SCM_ASSERT_TYPE (s, scm_car (args), SCM_ARGn, __FUNCTION__, "Stencil");
150 extent.unite (s->extent_box ());
151 *tail = scm_cons (s->expr (), SCM_EOL);
152 tail = SCM_CDRLOC (*tail);
153 args = scm_cdr (args);
156 expr = scm_cons (ly_symbol2scm ("combine-stencil"), expr);
157 return Stencil (extent, expr).smobbed_copy ();
160 LY_DEFINE (ly_make_stencil, "ly:make-stencil",
161 1, 2, 0, (SCM expr, SCM xext, SCM yext),
162 "Stencils are device independent output expressions."
163 " They carry two pieces of information:\n"
164 "\n"
165 "@enumerate\n"
166 "@item\n"
167 "A specification of how to print this object."
168 " This specification is processed by the output backends,"
169 " for example @file{scm/output-ps.scm}.\n"
170 "\n"
171 "@item\n"
172 "The vertical and horizontal extents of the object, given as"
173 " pairs. If an extent is unspecified (or if you use"
174 " @code{(1000 . -1000)} as its value), it is taken to be empty.\n"
175 "@end enumerate\n")
177 SCM_ASSERT_TYPE (!scm_is_pair (expr)
178 || is_stencil_head (scm_car (expr)),
179 expr, SCM_ARG1, __FUNCTION__, "registered stencil expression");
182 Interval x;
183 if (xext != SCM_UNDEFINED)
185 LY_ASSERT_TYPE (is_number_pair, xext, 2);
186 x = ly_scm2interval (xext);
189 Interval y;
190 if (yext != SCM_UNDEFINED)
192 LY_ASSERT_TYPE (is_number_pair, yext, 3);
193 y = ly_scm2interval (yext);
196 Box b (x, y);
197 Stencil s (b, expr);
198 return s.smobbed_copy ();
201 LY_DEFINE (ly_stencil_aligned_to, "ly:stencil-aligned-to",
202 3, 0, 0, (SCM stil, SCM axis, SCM dir),
203 "Align @var{stil} using its own extents. @var{dir} is a number."
204 " @code{-1} and @code{1} are left and right, respectively."
205 " Other values are interpolated (so @code{0} means the center).")
207 LY_ASSERT_SMOB (Stencil, stil, 1);
208 LY_ASSERT_TYPE (is_axis, axis, 2);
209 LY_ASSERT_TYPE (scm_is_number, dir, 3);
211 Stencil target = *unsmob_stencil (stil);
213 target.align_to ((Axis)scm_to_int (axis),
214 scm_to_double (dir));
215 return target.smobbed_copy ();
218 LY_DEFINE (ly_stencil_fonts, "ly:stencil-fonts",
219 1, 0, 0, (SCM s),
220 "Analyze @var{s}, and return a list of fonts used"
221 " in@tie{}@var{s}.")
223 LY_ASSERT_SMOB (Stencil, s, 1);
224 Stencil *stil = unsmob_stencil (s);
225 return find_expression_fonts (stil->expr ());
228 LY_DEFINE (ly_stencil_in_color, "ly:stencil-in-color",
229 4, 0, 0, (SCM stc, SCM r, SCM g, SCM b),
230 "Put @var{stc} in a different color.")
232 LY_ASSERT_SMOB (Stencil, stc, 1);
233 Stencil *stil = unsmob_stencil (stc);
234 return Stencil (stil->extent_box (),
235 scm_list_3 (ly_symbol2scm ("color"),
236 scm_list_3 (r, g, b),
237 stil->expr ())).smobbed_copy ();
240 struct Stencil_interpret_arguments
242 SCM func;
243 SCM arg1;
246 void stencil_interpret_in_scm (void *p, SCM expr)
248 Stencil_interpret_arguments *ap = (Stencil_interpret_arguments *) p;
249 scm_call_2 (ap->func, ap->arg1, expr);
252 LY_DEFINE (ly_interpret_stencil_expression, "ly:interpret-stencil-expression",
253 4, 0, 0, (SCM expr, SCM func, SCM arg1, SCM offset),
254 "Parse @var{expr}, feed bits to @var{func} with first arg"
255 " @var{arg1} having offset @var{offset}.")
257 LY_ASSERT_TYPE (ly_is_procedure, func, 2);
259 Stencil_interpret_arguments a;
260 a.func = func;
261 a.arg1 = arg1;
262 Offset o = ly_scm2offset (offset);
264 interpret_stencil_expression (expr, stencil_interpret_in_scm, (void *) & a, o);
266 return SCM_UNSPECIFIED;
269 LY_DEFINE (ly_bracket, "ly:bracket",
270 4, 0, 0,
271 (SCM a, SCM iv, SCM t, SCM p),
272 "Make a bracket in direction@tie{}@var{a}. The extent of the"
273 " bracket is given by @var{iv}. The wings protrude by an amount"
274 " of@tie{}@var{p}, which may be negative. The thickness is given"
275 " by@tie{}@var{t}.")
277 LY_ASSERT_TYPE (is_axis, a, 1);
278 LY_ASSERT_TYPE (is_number_pair, iv, 2);
279 LY_ASSERT_TYPE (scm_is_number, t, 3);
280 LY_ASSERT_TYPE (scm_is_number, p, 4);
282 return Lookup::bracket ((Axis)scm_to_int (a), ly_scm2interval (iv),
283 scm_to_double (t),
284 scm_to_double (p),
285 0.95 * scm_to_double (t)).smobbed_copy ();
288 LY_DEFINE (ly_stencil_rotate, "ly:stencil-rotate",
289 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
290 "Return a stencil @var{stil} rotated @var{angle} degrees around"
291 " the relative offset (@var{x}, @var{y}). E.g. an offset of"
292 " (-1, 1) will rotate the stencil around the left upper corner.")
294 Stencil *s = unsmob_stencil (stil);
295 LY_ASSERT_SMOB (Stencil, stil, 1);
296 LY_ASSERT_TYPE (scm_is_number, angle, 2);
297 LY_ASSERT_TYPE (scm_is_number, x, 3);
298 LY_ASSERT_TYPE (scm_is_number, y, 4);
299 Real a = scm_to_double (angle);
300 Real x_off = scm_to_double (x);
301 Real y_off = scm_to_double (y);
303 SCM new_s = s->smobbed_copy ();
304 Stencil *q = unsmob_stencil (new_s);
305 q->rotate_degrees (a, Offset (x_off, y_off));
306 return new_s;
309 LY_DEFINE (ly_stencil_rotate_absolute, "ly:stencil-rotate-absolute",
310 4, 0, 0, (SCM stil, SCM angle, SCM x, SCM y),
311 "Return a stencil @var{stil} rotated @var{angle} degrees around"
312 " point (@var{x}, @var{y}), given in absolute coordinates.")
314 Stencil *s = unsmob_stencil (stil);
315 LY_ASSERT_SMOB (Stencil, stil, 1);
316 LY_ASSERT_TYPE (scm_is_number, angle, 2);
317 LY_ASSERT_TYPE (scm_is_number, x, 3);
318 LY_ASSERT_TYPE (scm_is_number, y, 4);
319 Real a = scm_to_double (angle);
320 Real x_off = scm_to_double (x);
321 Real y_off = scm_to_double (y);
323 SCM new_s = s->smobbed_copy ();
324 Stencil *q = unsmob_stencil (new_s);
325 q->rotate_degrees_absolute (a, Offset (x_off, y_off));
326 return new_s;
329 LY_DEFINE (ly_round_filled_box, "ly:round-filled-box",
330 3, 0, 0,
331 (SCM xext, SCM yext, SCM blot),
332 "Make a @code{Stencil} object that prints a black box of"
333 " dimensions @var{xext}, @var{yext} and roundness @var{blot}.")
335 LY_ASSERT_TYPE (is_number_pair, xext, 1);
336 LY_ASSERT_TYPE (is_number_pair, yext, 2);
337 LY_ASSERT_TYPE (scm_is_number, blot, 3);
339 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
340 scm_to_double (blot)).smobbed_copy ();
343 LY_DEFINE (ly_round_filled_polygon, "ly:round-filled-polygon",
344 2, 0, 0,
345 (SCM points, SCM blot),
346 "Make a @code{Stencil} object that prints a black polygon with "
347 "corners at the points defined by @var{points} (list of coordinate "
348 "pairs) and roundness @var{blot}.")
350 SCM_ASSERT_TYPE (scm_ilength (points) > 0, points, SCM_ARG1, __FUNCTION__, "list of coordinate pairs");
351 LY_ASSERT_TYPE (scm_is_number, blot, 2);
352 vector<Offset> pts;
353 for (SCM p = points; scm_is_pair (p); p = scm_cdr (p))
355 SCM scm_pt = scm_car (p);
356 if (scm_is_pair (scm_pt)) {
357 pts.push_back (ly_scm2offset (scm_pt));
358 } else {
359 // TODO: Print out warning
362 return Lookup::round_filled_polygon (pts, scm_to_double (blot)).smobbed_copy ();
365 LY_DEFINE (ly_register_stencil_expression, "ly:register-stencil-expression",
366 1, 0, 0,
367 (SCM symbol),
368 "Add @var{symbol} as head of a stencil expression.")
370 LY_ASSERT_TYPE (ly_is_symbol, symbol, 1);
371 register_stencil_head (symbol);
372 return SCM_UNSPECIFIED;
375 LY_DEFINE (ly_all_stencil_expressions, "ly:all-stencil-expressions",
376 0, 0, 0,
378 "Return all symbols recognized as stencil expressions.")
380 return all_stencil_heads ();