2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
13 #include "line-interface.hh"
15 #include "dimensions.hh"
17 #include "string-convert.hh"
18 #include "file-path.hh"
20 #include "lily-guile.hh"
23 #include "font-metric.hh"
24 #include "interval.hh"
27 Lookup::dot (Offset p, Real radius)
29 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
30 scm_make_real (p[X_AXIS]),
31 scm_make_real (p[Y_AXIS]),
32 scm_make_real (radius),
35 box.add_point (p - Offset (radius, radius));
36 box.add_point (p + Offset (radius, radius));
37 return Stencil (box, at);
52 * (0,0) x /slope=dy/dx
60 Lookup::beam (Real slope, Real width, Real thick, Real blot)
62 Real height = slope * width;
63 Real min_y = (0 <? height) - thick/2;
64 Real max_y = (0 >? height) + thick/2;
66 Box b (Interval (0, width),
67 Interval (min_y, max_y));
69 SCM at = scm_list_n (ly_symbol2scm ("beam"),
70 scm_make_real (width),
71 scm_make_real (slope),
72 scm_make_real (thick),
75 return Stencil (b, at);
79 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
83 for (int i= 4; i -- ;)
85 l = scm_cons (ly_offset2scm (b.control_[i]), l);
88 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
89 scm_make_real (thick),
94 Box box (Interval (0,0),Interval (0,0));
95 return Stencil (box, at);
101 Lookup::horizontal_line (Interval w, Real th)
103 SCM at = scm_list_n (ly_symbol2scm ("horizontal-line"),
104 scm_make_real (w[LEFT]),
105 scm_make_real (w[RIGHT]),
112 box[Y_AXIS] = Interval (-th/2,th/2);
114 return Stencil (box, at);
119 Lookup::blank (Box b)
121 return Stencil (b, scm_makfrom0str (""));
125 Lookup::filled_box (Box b)
127 SCM at = (scm_list_n (ly_symbol2scm ("filledbox"),
128 scm_make_real (-b[X_AXIS][LEFT]),
129 scm_make_real (b[X_AXIS][RIGHT]),
130 scm_make_real (-b[Y_AXIS][DOWN]),
131 scm_make_real (b[Y_AXIS][UP]),
134 return Stencil (b,at);
140 * __________________________________
145 * |\ _ _ / v \ _ _ /| |
148 * | <------>| | extent
149 * | blot | | (Y_AXIS)
157 * x\_____/______________\_____/|_____v
161 * |<-------------------------->|
162 * Box extent (X_AXIS)
165 Lookup::round_filled_box (Box b, Real blotdiameter)
167 if (b.x ().length () < blotdiameter)
169 blotdiameter = b.x ().length ();
171 if (b.y ().length () < blotdiameter)
173 blotdiameter = b.y ().length ();
176 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
177 scm_make_real (-b[X_AXIS][LEFT]),
178 scm_make_real (b[X_AXIS][RIGHT]),
179 scm_make_real (-b[Y_AXIS][DOWN]),
180 scm_make_real (b[Y_AXIS][UP]),
181 scm_make_real (blotdiameter),
184 return Stencil (b,at);
190 * Create Stencil that represents a filled polygon with round edges.
194 * (a) Only outer (convex) edges are rounded.
196 * (b) This algorithm works as expected only for polygons whose edges
197 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
198 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
199 * will give a strange result. Even non-adjacent edges that just
200 * touch each other will in general not work as expected for non-null
203 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
204 * if there is a natural number k such that blotdiameter is greater
205 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
206 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
207 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
208 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
209 * polygon will exceed the outline of the core polygon. In other
210 * words: Do not draw rounded polygons that have a leg smaller or
211 * thinner than blotdiameter (or set blotdiameter to a sufficiently
212 * small value -- maybe even 0.0)!
214 * NOTE: Limitations (b) and (c) arise from the fact that round edges
215 * are made by moulding sharp edges to round ones rather than adding
216 * to a core filled polygon. For details of these two different
217 * approaches, see the thread upon the ledger lines patch that started
218 * on March 25, 2002 on the devel mailing list. The below version of
219 * round_filled_polygon () sticks to the moulding model, which the
220 * majority of the list participants finally voted for. This,
221 * however, results in the above limitations and a much increased
222 * complexity of the algorithm, since it has to compute a shrinked
223 * polygon -- which is not trivial define precisely and unambigously.
224 * With the other approach, one simply could move a circle of size
225 * blotdiameter along all edges of the polygon (which is what the
226 * postscript routine in the backend effectively does, but on the
227 * shrinked polygon). --jr
230 Lookup::round_filled_polygon (Array<Offset> points, Real blotdiameter)
232 /* TODO: Maybe print a warning if one of the above limitations
233 applies to the given polygon. However, this is quite complicated
236 /* remove consecutive duplicate points */
237 const Real epsilon = 0.01;
238 for (int i = 0; i < points.size ();)
240 int next_i = (i + 1) % points.size ();
241 Real d = (points[i] - points[next_i]).length ();
248 /* special cases: degenerated polygons */
249 if (points.size () == 0)
251 if (points.size () == 1)
252 return dot (points[0], 0.5 * blotdiameter);
253 if (points.size () == 2)
254 return Line_interface::make_line (blotdiameter, points[0], points[1]);
256 /* shrink polygon in size by 0.5 * blotdiameter */
257 Array<Offset> shrinked_points;
258 shrinked_points.set_size (points.size ());
259 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
260 for (int i = 0; i < points.size (); i++)
263 int i1 = (i + 1) % points.size ();
264 int i2 = (i + 2) % points.size ();
265 Offset p0 = points[i0];
266 Offset p1 = points[i1];
267 Offset p2 = points[i2];
268 Offset p10 = p0 - p1;
269 Offset p12 = p2 - p1;
270 if (p10.length () != 0.0)
272 Real phi = p10.arg ();
273 // rotate (p2 - p0) by (-phi)
274 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
278 else if (q[Y_AXIS] < 0)
280 else {} // keep ccw unchanged
282 else {} // keep ccw unchanged
283 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
284 Offset p12n = (1.0 / p12.length ()) * p12;
285 Offset p13n = 0.5 * (p10n + p12n);
286 Offset p14n = 0.5 * (p10n - p12n);
288 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
290 // special case: p0, p1, p2 are on a single line => build
291 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
293 p13[X_AXIS] = p10[Y_AXIS];
294 p13[Y_AXIS] = -p10[X_AXIS];
295 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
298 p13 = (0.5 * blotdiameter / d) * p13n;
299 shrinked_points[i1] = p1 + ((ccw) ? p13 : -p13);
302 /* build scm expression and bounding box */
303 SCM shrinked_points_scm = SCM_EOL;
305 for (int i = 0; i < shrinked_points.size (); i++)
307 SCM x = scm_make_real (shrinked_points[i][X_AXIS]);
308 SCM y = scm_make_real (shrinked_points[i][Y_AXIS]);
309 shrinked_points_scm = scm_cons (x, scm_cons (y, shrinked_points_scm));
310 box.add_point (points[i]);
312 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
313 ly_quote_scm (shrinked_points_scm),
314 scm_make_real (blotdiameter),
317 Stencil polygon = Stencil (box, polygon_scm);
318 shrinked_points.clear ();
327 Lookup::frame (Box b, Real thick, Real blot)
331 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
333 Axis o = Axis ((a+1)%NO_AXES);
337 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
338 edges[o][DOWN] = b[o][DOWN] - thick/2;
339 edges[o][UP] = b[o][UP] + thick/2;
341 m.add_stencil (round_filled_box (edges, blot));
343 while (flip (&d) != LEFT);
349 Make a smooth curve along the points
352 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
354 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
356 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
358 back.control_[1] += perp;
359 back.control_[2] += perp;
361 curve.control_[1] -= perp;
362 curve.control_[2] -= perp;
367 scontrols[ i ] = ly_offset2scm (back.control_[i]);
369 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
372 Need the weird order b.o. the way PS want its arguments
374 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
378 list = scm_cons (scontrols[indices[i]], list);
382 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
384 scm_make_real (linethick),
386 Box b (curve.extent (X_AXIS),
387 curve.extent (Y_AXIS));
389 b[X_AXIS].unite (back.extent (X_AXIS));
390 b[Y_AXIS].unite (back.extent (Y_AXIS));
392 return Stencil (b, at);
419 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
422 Need the weird order b.o. the way PS want its arguments
425 list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
426 list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
427 list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
428 list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
429 list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
430 list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
431 list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
432 list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
434 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
439 Interval x_extent = top_curve.extent (X_AXIS);
440 x_extent.unite (bottom_curve.extent (X_AXIS));
441 Interval y_extent = top_curve.extent (Y_AXIS);
442 y_extent.unite (bottom_curve.extent (Y_AXIS));
443 Box b (x_extent, y_extent);
445 return Stencil (b, horizontal_bend);
452 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
455 String sym = ly_scm2string (ly_car (s));
456 String reg = ly_scm2string (ly_car (ly_cdr (s)));
458 if (sym == "Discant")
460 Stencil r = fm->find_by_name ("accordion-accDiscant");
462 if (reg.left_string (1) == "F")
464 Stencil d = fm->find_by_name ("accordion-accDot");
465 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
467 reg = reg.right_string (reg.length ()-1);
470 if (reg.left_string (3) == "EEE")
473 reg = reg.right_string (reg.length ()-3);
475 else if (reg.left_string (2) == "EE")
478 reg = reg.right_string (reg.length ()-2);
480 else if (reg.left_string (2) == "Eh")
483 reg = reg.right_string (reg.length ()-2);
485 else if (reg.left_string (1) == "E")
488 reg = reg.right_string (reg.length ()-1);
492 Stencil d = fm->find_by_name ("accordion-accDot");
493 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
498 Stencil d = fm->find_by_name ("accordion-accDot");
499 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
500 d.translate_axis (0.8 * staff_space PT, X_AXIS);
505 Stencil d = fm->find_by_name ("accordion-accDot");
506 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
507 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
510 if (reg.left_string (2) == "SS")
512 Stencil d = fm->find_by_name ("accordion-accDot");
513 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
514 d.translate_axis (0.4 * staff_space PT, X_AXIS);
516 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
518 reg = reg.right_string (reg.length ()-2);
520 if (reg.left_string (1) == "S")
522 Stencil d = fm->find_by_name ("accordion-accDot");
523 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
525 reg = reg.right_string (reg.length ()-1);
528 else if (sym == "Freebase")
530 Stencil r = fm->find_by_name ("accordion-accFreebase");
532 if (reg.left_string (1) == "F")
534 Stencil d = fm->find_by_name ("accordion-accDot");
535 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
537 reg = reg.right_string (reg.length ()-1);
541 Stencil d = fm->find_by_name ("accordion-accDot");
542 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
546 else if (sym == "Bayanbase")
548 Stencil r = fm->find_by_name ("accordion-accBayanbase");
550 if (reg.left_string (1) == "T")
552 Stencil d = fm->find_by_name ("accordion-accDot");
553 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
555 reg = reg.right_string (reg.length ()-1);
557 /* include 4' reed just for completeness. You don't want to use this. */
558 if (reg.left_string (1) == "F")
560 Stencil d = fm->find_by_name ("accordion-accDot");
561 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
563 reg = reg.right_string (reg.length ()-1);
565 if (reg.left_string (2) == "EE")
567 Stencil d = fm->find_by_name ("accordion-accDot");
568 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
569 d.translate_axis (0.4 * staff_space PT, X_AXIS);
571 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
573 reg = reg.right_string (reg.length ()-2);
575 if (reg.left_string (1) == "E")
577 Stencil d = fm->find_by_name ("accordion-accDot");
578 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
580 reg = reg.right_string (reg.length ()-1);
583 else if (sym == "Stdbase")
585 Stencil r = fm->find_by_name ("accordion-accStdbase");
587 if (reg.left_string (1) == "T")
589 Stencil d = fm->find_by_name ("accordion-accDot");
590 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
592 reg = reg.right_string (reg.length ()-1);
594 if (reg.left_string (1) == "F")
596 Stencil d = fm->find_by_name ("accordion-accDot");
597 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
599 reg = reg.right_string (reg.length ()-1);
601 if (reg.left_string (1) == "M")
603 Stencil d = fm->find_by_name ("accordion-accDot");
604 d.translate_axis (staff_space * 2 PT, Y_AXIS);
605 d.translate_axis (staff_space PT, X_AXIS);
607 reg = reg.right_string (reg.length ()-1);
609 if (reg.left_string (1) == "E")
611 Stencil d = fm->find_by_name ("accordion-accDot");
612 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
614 reg = reg.right_string (reg.length ()-1);
616 if (reg.left_string (1) == "S")
618 Stencil d = fm->find_by_name ("accordion-accDot");
619 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
621 reg = reg.right_string (reg.length ()-1);
624 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
626 else if (sym == "SB")
628 Stencil r = fm->find_by_name ("accordion-accSB");
631 else if (sym == "BB")
633 Stencil r = fm->find_by_name ("accordion-accBB");
636 else if (sym == "OldEE")
638 Stencil r = fm->find_by_name ("accordion-accOldEE");
641 else if (sym == "OldEES")
643 Stencil r = fm->find_by_name ("accordion-accOldEES");
650 Lookup::repeat_slash (Real w, Real s, Real t)
652 SCM wid = scm_make_real (w);
653 SCM sl = scm_make_real (s);
654 SCM thick = scm_make_real (t);
655 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
656 wid, sl, thick, SCM_UNDEFINED);
658 Box b (Interval (0, w + sqrt (sqr (t/s) + sqr (t))),
659 Interval (0, w * s));
661 return Stencil (b, slashnodot); // http://slashnodot.org
666 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
669 Axis other = Axis ((a+1)%2);
671 b[other] = Interval (-1, 1) * thick * 0.5;
673 Stencil m = round_filled_box (b, blot);
675 b[a] = Interval (iv[UP] - thick, iv[UP]);
676 Interval oi = Interval (-thick/2, thick/2 + fabs (protude)) ;
677 oi *= sign (protude);
679 m.add_stencil (round_filled_box (b, blot));
680 b[a] = Interval (iv[DOWN], iv[DOWN] +thick);
681 m.add_stencil (round_filled_box (b,blot));
687 Lookup::triangle (Interval iv, Real thick, Real protude)
691 b[Y_AXIS] = Interval (0 <? protude , 0 >? protude);
693 SCM s = scm_list_n (ly_symbol2scm ("symmetric-x-triangle"),
694 scm_make_real (thick),
695 scm_make_real (iv.length ()),
696 scm_make_real (protude), SCM_UNDEFINED);
698 return Stencil (b, s);
702 LY_DEFINE (ly_bracket ,"ly:bracket",
704 (SCM a, SCM iv, SCM t, SCM p),
705 "Make a bracket in direction @var{a}. The extent of the bracket is "
706 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
707 "may be negative. The thickness is given by @var{t}.")
709 SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
710 SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
711 SCM_ASSERT_TYPE (ly_c_number_p (t), a, SCM_ARG3, __FUNCTION__, "number") ;
712 SCM_ASSERT_TYPE (ly_c_number_p (p), a, SCM_ARG4, __FUNCTION__, "number") ;
715 return Lookup::bracket ((Axis)ly_scm2int (a), ly_scm2interval (iv),
718 0.95 * ly_scm2double (t)).smobbed_copy ();
723 LY_DEFINE (ly_filled_box ,"ly:round-filled-box",
725 (SCM xext, SCM yext, SCM blot),
726 "Make a @code{Stencil} "
727 "that prints a black box of dimensions @var{xext}, "
728 "@var{yext} and roundness @var{blot}."
731 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair") ;
732 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair") ;
733 SCM_ASSERT_TYPE (ly_c_number_p (blot), blot, SCM_ARG3, __FUNCTION__, "number") ;
735 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
736 ly_scm2double (blot)).smobbed_copy ();