2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
17 #include "line-interface.hh"
19 #include "dimensions.hh"
21 #include "string-convert.hh"
22 #include "file-path.hh"
24 #include "lily-guile.hh"
25 #include "font-metric.hh"
28 Lookup::dot (Offset p, Real radius)
30 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
31 scm_from_double (p[X_AXIS]),
32 scm_from_double (p[Y_AXIS]),
33 scm_from_double (radius),
36 box.add_point (p - Offset (radius, radius));
37 box.add_point (p + Offset (radius, radius));
38 return Stencil (box, at);
42 Lookup::beam (Real slope, Real width, Real thick, Real blot)
48 p = Offset (0, thick / 2);
50 p += Offset (1, -1) * (blot / 2);
54 points = scm_cons (scm_from_double (p[X_AXIS]),
55 scm_cons (scm_from_double (p[Y_AXIS]),
58 p = Offset (0, -thick / 2);
60 p += Offset (1, 1) * (blot / 2);
62 points = scm_cons (scm_from_double (p[X_AXIS]),
63 scm_cons (scm_from_double (p[Y_AXIS]),
66 p = Offset (width, width * slope - thick / 2);
68 p += Offset (-1, 1) * (blot / 2);
70 points = scm_cons (scm_from_double (p[X_AXIS]),
71 scm_cons (scm_from_double (p[Y_AXIS]),
74 p = Offset (width, width * slope + thick / 2);
76 p += Offset (-1, -1) * (blot / 2);
78 points = scm_cons (scm_from_double (p[X_AXIS]),
79 scm_cons (scm_from_double (p[Y_AXIS]),
82 SCM expr = scm_list_n (ly_symbol2scm ("polygon"),
83 ly_quote_scm (points),
84 scm_from_double (blot),
88 return Stencil (b, expr);
92 Lookup::dashed_slur (Bezier b, Real thick, Real dash_period, Real dash_fraction)
96 Real on = dash_fraction * dash_period;
97 Real off = dash_period - on;
100 l = scm_cons (ly_offset2scm (b.control_[i]), l);
102 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
103 scm_from_double (thick),
104 scm_from_double (on),
105 scm_from_double (off),
109 Box box (Interval (0, 0), Interval (0, 0));
110 return Stencil (box, at);
114 Lookup::rotated_box (Real slope, Real width, Real thick, Real blot)
117 Offset rot (1, slope);
121 rot /= sqrt (1 + slope*slope);
122 pts.push_back (Offset (0, -thick / 2) * rot);
123 pts.push_back (Offset (width, -thick / 2) * rot);
124 pts.push_back (Offset (width, thick / 2) * rot);
125 pts.push_back (Offset (0, thick / 2) * rot);
126 return Lookup::round_filled_polygon (pts, blot);
130 Lookup::horizontal_line (Interval w, Real th)
132 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
133 scm_from_double (th),
134 scm_from_double (w[LEFT]),
136 scm_from_double (w[RIGHT]),
142 box[Y_AXIS] = Interval (-th / 2, th / 2);
144 return Stencil (box, at);
148 Lookup::blank (Box b)
150 return Stencil (b, scm_makfrom0str (""));
154 Lookup::filled_box (Box b)
156 return round_filled_box (b, 0.0);
162 * __________________________________
167 * |\ _ _ / v \ _ _ /| |
170 * | <------>| | extent
171 * | blot | | (Y_AXIS)
179 * x\_____/______________\_____/|_____v
183 * |<-------------------------->|
184 * Box extent (X_AXIS)
187 Lookup::round_filled_box (Box b, Real blotdiameter)
189 if (b.x ().length () < blotdiameter)
190 blotdiameter = b.x ().length ();
191 if (b.y ().length () < blotdiameter)
192 blotdiameter = b.y ().length ();
194 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
195 scm_from_double (-b[X_AXIS][LEFT]),
196 scm_from_double (b[X_AXIS][RIGHT]),
197 scm_from_double (-b[Y_AXIS][DOWN]),
198 scm_from_double (b[Y_AXIS][UP]),
199 scm_from_double (blotdiameter),
202 return Stencil (b, at);
206 * Create Stencil that represents a filled polygon with round edges.
210 * (a) Only outer (convex) edges are rounded.
212 * (b) This algorithm works as expected only for polygons whose edges
213 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
214 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
215 * will give a strange result. Even non-adjacent edges that just
216 * touch each other will in general not work as expected for non-null
219 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
220 * if there is a natural number k such that blotdiameter is greater
221 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
222 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
223 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
224 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
225 * polygon will exceed the outline of the core polygon. In other
226 * words: Do not draw rounded polygons that have a leg smaller or
227 * thinner than blotdiameter (or set blotdiameter to a sufficiently
228 * small value -- maybe even 0.0)!
230 * NOTE: Limitations (b) and (c) arise from the fact that round edges
231 * are made by moulding sharp edges to round ones rather than adding
232 * to a core filled polygon. For details of these two different
233 * approaches, see the thread upon the ledger lines patch that started
234 * on March 25, 2002 on the devel mailing list. The below version of
235 * round_filled_polygon () sticks to the moulding model, which the
236 * majority of the list participants finally voted for. This,
237 * however, results in the above limitations and a much increased
238 * complexity of the algorithm, since it has to compute a shrinked
239 * polygon -- which is not trivial define precisely and unambigously.
240 * With the other approach, one simply could move a circle of size
241 * blotdiameter along all edges of the polygon (which is what the
242 * postscript routine in the backend effectively does, but on the
243 * shrinked polygon). --jr
246 Lookup::round_filled_polygon (vector<Offset> const &points,
249 /* TODO: Maybe print a warning if one of the above limitations
250 applies to the given polygon. However, this is quite complicated
253 const Real epsilon = 0.01;
256 /* remove consecutive duplicate points */
257 for (vsize i = 0; i < points.size (); i++)
259 int next_i = (i + 1) % points.size ();
260 Real d = (points[i] - points[next_i]).length ();
262 programming_error ("Polygon should not have duplicate points");
266 /* special cases: degenerated polygons */
267 if (points.size () == 0)
269 if (points.size () == 1)
270 return dot (points[0], 0.5 * blotdiameter);
271 if (points.size () == 2)
272 return Line_interface::make_line (blotdiameter, points[0], points[1]);
274 /* shrink polygon in size by 0.5 * blotdiameter */
275 vector<Offset> shrunk_points;
276 shrunk_points.resize (points.size ());
277 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
278 for (vsize i = 0; i < points.size (); i++)
281 int i1 = (i + 1) % points.size ();
282 int i2 = (i + 2) % points.size ();
283 Offset p0 = points[i0];
284 Offset p1 = points[i1];
285 Offset p2 = points[i2];
286 Offset p10 = p0 - p1;
287 Offset p12 = p2 - p1;
288 if (p10.length () != 0.0)
290 Real phi = p10.arg ();
291 // rotate (p2 - p0) by (-phi)
292 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
296 else if (q[Y_AXIS] < 0)
298 else {} // keep ccw unchanged
300 else {} // keep ccw unchanged
301 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
302 Offset p12n = (1.0 / p12.length ()) * p12;
303 Offset p13n = 0.5 * (p10n + p12n);
304 Offset p14n = 0.5 * (p10n - p12n);
306 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
308 // special case: p0, p1, p2 are on a single line => build
309 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
311 p13[X_AXIS] = p10[Y_AXIS];
312 p13[Y_AXIS] = -p10[X_AXIS];
313 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
316 p13 = (0.5 * blotdiameter / d) * p13n;
317 shrunk_points[i1] = p1 + ((ccw) ? p13 : -p13);
320 /* build scm expression and bounding box */
321 SCM shrunk_points_scm = SCM_EOL;
323 for (vsize i = 0; i < shrunk_points.size (); i++)
325 SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
326 SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
327 shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
328 box.add_point (points[i]);
330 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
331 ly_quote_scm (shrunk_points_scm),
332 scm_from_double (blotdiameter),
336 Stencil polygon = Stencil (box, polygon_scm);
337 shrunk_points.clear ();
345 Lookup::frame (Box b, Real thick, Real blot)
349 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
351 Axis o = Axis ((a + 1)%NO_AXES);
355 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
356 edges[o][DOWN] = b[o][DOWN] - thick / 2;
357 edges[o][UP] = b[o][UP] + thick / 2;
359 m.add_stencil (round_filled_box (edges, blot));
361 while (flip (&d) != LEFT);
367 Make a smooth curve along the points
370 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
372 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
374 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
376 back.control_[1] += perp;
377 back.control_[2] += perp;
379 curve.control_[1] -= perp;
380 curve.control_[2] -= perp;
384 for (int i = 0; i < 4; i++)
385 scontrols[i] = ly_offset2scm (back.control_[i]);
386 for (int i = 0; i < 4; i++)
387 scontrols[i + 4] = ly_offset2scm (curve.control_[i]);
390 Need the weird order b.o. the way PS want its arguments
392 int indices[] = {5, 6, 7, 4, 1, 2, 3, 0};
394 for (int i = 8; i--;)
395 list = scm_cons (scontrols[indices[i]], list);
397 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
399 scm_from_double (linethick),
401 Box b (curve.extent (X_AXIS),
402 curve.extent (Y_AXIS));
404 b[X_AXIS].unite (back.extent (X_AXIS));
405 b[Y_AXIS].unite (back.extent (Y_AXIS));
407 return Stencil (b, at);
434 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
437 Need the weird order b.o. the way PS want its arguments
440 list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
441 list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
442 list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
443 list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
444 list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
445 list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
446 list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
447 list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
449 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
451 scm_from_double (0.0),
454 Interval x_extent = top_curve.extent (X_AXIS);
455 x_extent.unite (bottom_curve.extent (X_AXIS));
456 Interval y_extent = top_curve.extent (Y_AXIS);
457 y_extent.unite (bottom_curve.extent (Y_AXIS));
458 Box b (x_extent, y_extent);
460 return Stencil (b, horizontal_bend);
467 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
470 string sym = ly_scm2string (scm_car (s));
471 string reg = ly_scm2string (scm_car (scm_cdr (s)));
473 if (sym == "Discant")
475 Stencil r = fm->find_by_name ("accordion.accDiscant");
477 if (reg.substr (0, 1) == "F")
479 Stencil d = fm->find_by_name ("accordion.accDot");
480 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
482 reg = reg.substr (1);
485 if (reg.substr (0, 3) == "EEE")
488 reg = reg.substr (3);
490 else if (reg.substr (0, 2) == "EE")
493 reg = reg.substr (2);
495 else if (reg.substr (0, 2) == "Eh")
498 reg = reg.substr (2);
500 else if (reg.substr (0, 1) == "E")
503 reg = reg.substr (1);
507 Stencil d = fm->find_by_name ("accordion.accDot");
508 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
513 Stencil d = fm->find_by_name ("accordion.accDot");
514 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
515 d.translate_axis (0.8 * staff_space PT, X_AXIS);
520 Stencil d = fm->find_by_name ("accordion.accDot");
521 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
522 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
525 if (reg.substr (0, 2) == "SS")
527 Stencil d = fm->find_by_name ("accordion.accDot");
528 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
529 d.translate_axis (0.4 * staff_space PT, X_AXIS);
531 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
533 reg = reg.substr (2);
535 if (reg.substr (0, 1) == "S")
537 Stencil d = fm->find_by_name ("accordion.accDot");
538 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
540 reg = reg.substr (1);
543 else if (sym == "Freebase")
545 Stencil r = fm->find_by_name ("accordion.accFreebase");
547 if (reg.substr (0, 1) == "F")
549 Stencil d = fm->find_by_name ("accordion.accDot");
550 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
552 reg = reg.substr (1);
556 Stencil d = fm->find_by_name ("accordion.accDot");
557 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
561 else if (sym == "Bayanbase")
563 Stencil r = fm->find_by_name ("accordion.accBayanbase");
565 if (reg.substr (0, 1) == "T")
567 Stencil d = fm->find_by_name ("accordion.accDot");
568 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
570 reg = reg.substr (1);
572 /* include 4' reed just for completeness. You don't want to use this. */
573 if (reg.substr (0, 1) == "F")
575 Stencil d = fm->find_by_name ("accordion.accDot");
576 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
578 reg = reg.substr (1);
580 if (reg.substr (0, 2) == "EE")
582 Stencil d = fm->find_by_name ("accordion.accDot");
583 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
584 d.translate_axis (0.4 * staff_space PT, X_AXIS);
586 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
588 reg = reg.substr (2);
590 if (reg.substr (0, 1) == "E")
592 Stencil d = fm->find_by_name ("accordion.accDot");
593 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
595 reg = reg.substr (1);
598 else if (sym == "Stdbase")
600 Stencil r = fm->find_by_name ("accordion.accStdbase");
602 if (reg.substr (0, 1) == "T")
604 Stencil d = fm->find_by_name ("accordion.accDot");
605 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
607 reg = reg.substr (1);
609 if (reg.substr (0, 1) == "F")
611 Stencil d = fm->find_by_name ("accordion.accDot");
612 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
614 reg = reg.substr (1);
616 if (reg.substr (0, 1) == "M")
618 Stencil d = fm->find_by_name ("accordion.accDot");
619 d.translate_axis (staff_space * 2 PT, Y_AXIS);
620 d.translate_axis (staff_space PT, X_AXIS);
622 reg = reg.substr (1);
624 if (reg.substr (0, 1) == "E")
626 Stencil d = fm->find_by_name ("accordion.accDot");
627 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
629 reg = reg.substr (1);
631 if (reg.substr (0, 1) == "S")
633 Stencil d = fm->find_by_name ("accordion.accDot");
634 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
636 reg = reg.substr (1);
639 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
641 else if (sym == "SB")
643 Stencil r = fm->find_by_name ("accordion.accSB");
646 else if (sym == "BB")
648 Stencil r = fm->find_by_name ("accordion.accBB");
651 else if (sym == "OldEE")
653 Stencil r = fm->find_by_name ("accordion.accOldEE");
656 else if (sym == "OldEES")
658 Stencil r = fm->find_by_name ("accordion.accOldEES");
665 Lookup::repeat_slash (Real w, Real s, Real t)
668 vector<Offset> points;
669 Real blotdiameter = 0.0;
672 Offset p2 (w, w * s);
674 return Lookup::round_filled_polygon (points, blotdiameter);
677 SCM wid = scm_from_double (w);
678 SCM sl = scm_from_double (s);
679 SCM thick = scm_from_double (t);
680 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
681 wid, sl, thick, SCM_UNDEFINED);
683 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
684 Interval (0, w * s));
686 return Stencil (b, slashnodot); // http://slashnodot.org
690 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
693 Axis other = Axis ((a + 1)%2);
695 b[other] = Interval (-1, 1) * thick * 0.5;
697 Stencil m = round_filled_box (b, blot);
699 b[a] = Interval (iv[UP] - thick, iv[UP]);
700 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protude));
701 oi *= sign (protude);
703 m.add_stencil (round_filled_box (b, blot));
704 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
705 m.add_stencil (round_filled_box (b, blot));
711 Lookup::triangle (Interval iv, Real thick, Real protude)
714 b[X_AXIS] = Interval (0, iv.length ());
715 b[Y_AXIS] = Interval (min (0., protude), max (0.0, protude));
717 Offset z1 (iv[LEFT], 0);
718 Offset z2 (iv[RIGHT], 0);
719 Offset z3 ((z1 + z2)[X_AXIS] / 2, protude);
722 TODO: move Triangle to Line_interface ?
724 Stencil tri = Line_interface::make_line (thick, z1, z2);
725 tri.add_stencil (Line_interface::make_line (thick, z2, z3));
726 tri.add_stencil (Line_interface::make_line (thick, z3, z1));