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::horizontal_line (Interval w, Real th)
116 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
117 scm_from_double (th),
118 scm_from_double (w[LEFT]),
120 scm_from_double (w[RIGHT]),
126 box[Y_AXIS] = Interval (-th / 2, th / 2);
128 return Stencil (box, at);
132 Lookup::blank (Box b)
134 return Stencil (b, scm_makfrom0str (""));
138 Lookup::filled_box (Box b)
140 return round_filled_box (b, 0.0);
146 * __________________________________
151 * |\ _ _ / v \ _ _ /| |
154 * | <------>| | extent
155 * | blot | | (Y_AXIS)
163 * x\_____/______________\_____/|_____v
167 * |<-------------------------->|
168 * Box extent (X_AXIS)
171 Lookup::round_filled_box (Box b, Real blotdiameter)
173 if (b.x ().length () < blotdiameter)
174 blotdiameter = b.x ().length ();
175 if (b.y ().length () < blotdiameter)
176 blotdiameter = b.y ().length ();
178 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
179 scm_from_double (-b[X_AXIS][LEFT]),
180 scm_from_double (b[X_AXIS][RIGHT]),
181 scm_from_double (-b[Y_AXIS][DOWN]),
182 scm_from_double (b[Y_AXIS][UP]),
183 scm_from_double (blotdiameter),
186 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 (vector<Offset> const &points,
233 /* TODO: Maybe print a warning if one of the above limitations
234 applies to the given polygon. However, this is quite complicated
237 const Real epsilon = 0.01;
240 /* remove consecutive duplicate points */
241 for (vsize i = 0; i < points.size (); i++)
243 int next_i = (i + 1) % points.size ();
244 Real d = (points[i] - points[next_i]).length ();
246 programming_error ("Polygon should not have duplicate points");
250 /* special cases: degenerated polygons */
251 if (points.size () == 0)
253 if (points.size () == 1)
254 return dot (points[0], 0.5 * blotdiameter);
255 if (points.size () == 2)
256 return Line_interface::make_line (blotdiameter, points[0], points[1]);
258 /* shrink polygon in size by 0.5 * blotdiameter */
259 vector<Offset> shrunk_points;
260 shrunk_points.resize (points.size ());
261 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
262 for (vsize i = 0; i < points.size (); i++)
265 int i1 = (i + 1) % points.size ();
266 int i2 = (i + 2) % points.size ();
267 Offset p0 = points[i0];
268 Offset p1 = points[i1];
269 Offset p2 = points[i2];
270 Offset p10 = p0 - p1;
271 Offset p12 = p2 - p1;
272 if (p10.length () != 0.0)
274 Real phi = p10.arg ();
275 // rotate (p2 - p0) by (-phi)
276 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
280 else if (q[Y_AXIS] < 0)
282 else {} // keep ccw unchanged
284 else {} // keep ccw unchanged
285 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
286 Offset p12n = (1.0 / p12.length ()) * p12;
287 Offset p13n = 0.5 * (p10n + p12n);
288 Offset p14n = 0.5 * (p10n - p12n);
290 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
292 // special case: p0, p1, p2 are on a single line => build
293 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
295 p13[X_AXIS] = p10[Y_AXIS];
296 p13[Y_AXIS] = -p10[X_AXIS];
297 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
300 p13 = (0.5 * blotdiameter / d) * p13n;
301 shrunk_points[i1] = p1 + ((ccw) ? p13 : -p13);
304 /* build scm expression and bounding box */
305 SCM shrunk_points_scm = SCM_EOL;
307 for (vsize i = 0; i < shrunk_points.size (); i++)
309 SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
310 SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
311 shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
312 box.add_point (points[i]);
314 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
315 ly_quote_scm (shrunk_points_scm),
316 scm_from_double (blotdiameter),
320 Stencil polygon = Stencil (box, polygon_scm);
321 shrunk_points.clear ();
329 Lookup::frame (Box b, Real thick, Real blot)
333 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
335 Axis o = Axis ((a + 1)%NO_AXES);
339 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
340 edges[o][DOWN] = b[o][DOWN] - thick / 2;
341 edges[o][UP] = b[o][UP] + thick / 2;
343 m.add_stencil (round_filled_box (edges, blot));
345 while (flip (&d) != LEFT);
351 Make a smooth curve along the points
354 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
356 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
358 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
360 back.control_[1] += perp;
361 back.control_[2] += perp;
363 curve.control_[1] -= perp;
364 curve.control_[2] -= perp;
368 for (int i = 0; i < 4; i++)
369 scontrols[i] = ly_offset2scm (back.control_[i]);
370 for (int i = 0; i < 4; i++)
371 scontrols[i + 4] = ly_offset2scm (curve.control_[i]);
374 Need the weird order b.o. the way PS want its arguments
376 int indices[] = {5, 6, 7, 4, 1, 2, 3, 0};
378 for (int i = 8; i--;)
379 list = scm_cons (scontrols[indices[i]], list);
381 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
383 scm_from_double (linethick),
385 Box b (curve.extent (X_AXIS),
386 curve.extent (Y_AXIS));
388 b[X_AXIS].unite (back.extent (X_AXIS));
389 b[Y_AXIS].unite (back.extent (Y_AXIS));
391 return Stencil (b, at);
418 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
421 Need the weird order b.o. the way PS want its arguments
424 list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
425 list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
426 list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
427 list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
428 list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
429 list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
430 list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
431 list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
433 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
435 scm_from_double (0.0),
438 Interval x_extent = top_curve.extent (X_AXIS);
439 x_extent.unite (bottom_curve.extent (X_AXIS));
440 Interval y_extent = top_curve.extent (Y_AXIS);
441 y_extent.unite (bottom_curve.extent (Y_AXIS));
442 Box b (x_extent, y_extent);
444 return Stencil (b, horizontal_bend);
451 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
454 string sym = ly_scm2string (scm_car (s));
455 string reg = ly_scm2string (scm_car (scm_cdr (s)));
457 if (sym == "Discant")
459 Stencil r = fm->find_by_name ("accordion.accDiscant");
461 if (reg.substr (0, 1) == "F")
463 Stencil d = fm->find_by_name ("accordion.accDot");
464 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
466 reg = reg.substr (1);
469 if (reg.substr (0, 3) == "EEE")
472 reg = reg.substr (3);
474 else if (reg.substr (0, 2) == "EE")
477 reg = reg.substr (2);
479 else if (reg.substr (0, 2) == "Eh")
482 reg = reg.substr (2);
484 else if (reg.substr (0, 1) == "E")
487 reg = reg.substr (1);
491 Stencil d = fm->find_by_name ("accordion.accDot");
492 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
497 Stencil d = fm->find_by_name ("accordion.accDot");
498 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
499 d.translate_axis (0.8 * staff_space PT, X_AXIS);
504 Stencil d = fm->find_by_name ("accordion.accDot");
505 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
506 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
509 if (reg.substr (0, 2) == "SS")
511 Stencil d = fm->find_by_name ("accordion.accDot");
512 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
513 d.translate_axis (0.4 * staff_space PT, X_AXIS);
515 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
517 reg = reg.substr (2);
519 if (reg.substr (0, 1) == "S")
521 Stencil d = fm->find_by_name ("accordion.accDot");
522 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
524 reg = reg.substr (1);
527 else if (sym == "Freebase")
529 Stencil r = fm->find_by_name ("accordion.accFreebase");
531 if (reg.substr (0, 1) == "F")
533 Stencil d = fm->find_by_name ("accordion.accDot");
534 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
536 reg = reg.substr (1);
540 Stencil d = fm->find_by_name ("accordion.accDot");
541 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
545 else if (sym == "Bayanbase")
547 Stencil r = fm->find_by_name ("accordion.accBayanbase");
549 if (reg.substr (0, 1) == "T")
551 Stencil d = fm->find_by_name ("accordion.accDot");
552 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
554 reg = reg.substr (1);
556 /* include 4' reed just for completeness. You don't want to use this. */
557 if (reg.substr (0, 1) == "F")
559 Stencil d = fm->find_by_name ("accordion.accDot");
560 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
562 reg = reg.substr (1);
564 if (reg.substr (0, 2) == "EE")
566 Stencil d = fm->find_by_name ("accordion.accDot");
567 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
568 d.translate_axis (0.4 * staff_space PT, X_AXIS);
570 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
572 reg = reg.substr (2);
574 if (reg.substr (0, 1) == "E")
576 Stencil d = fm->find_by_name ("accordion.accDot");
577 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
579 reg = reg.substr (1);
582 else if (sym == "Stdbase")
584 Stencil r = fm->find_by_name ("accordion.accStdbase");
586 if (reg.substr (0, 1) == "T")
588 Stencil d = fm->find_by_name ("accordion.accDot");
589 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
591 reg = reg.substr (1);
593 if (reg.substr (0, 1) == "F")
595 Stencil d = fm->find_by_name ("accordion.accDot");
596 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
598 reg = reg.substr (1);
600 if (reg.substr (0, 1) == "M")
602 Stencil d = fm->find_by_name ("accordion.accDot");
603 d.translate_axis (staff_space * 2 PT, Y_AXIS);
604 d.translate_axis (staff_space PT, X_AXIS);
606 reg = reg.substr (1);
608 if (reg.substr (0, 1) == "E")
610 Stencil d = fm->find_by_name ("accordion.accDot");
611 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
613 reg = reg.substr (1);
615 if (reg.substr (0, 1) == "S")
617 Stencil d = fm->find_by_name ("accordion.accDot");
618 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
620 reg = reg.substr (1);
623 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
625 else if (sym == "SB")
627 Stencil r = fm->find_by_name ("accordion.accSB");
630 else if (sym == "BB")
632 Stencil r = fm->find_by_name ("accordion.accBB");
635 else if (sym == "OldEE")
637 Stencil r = fm->find_by_name ("accordion.accOldEE");
640 else if (sym == "OldEES")
642 Stencil r = fm->find_by_name ("accordion.accOldEES");
649 Lookup::repeat_slash (Real w, Real s, Real t)
652 vector<Offset> points;
653 Real blotdiameter = 0.0;
656 Offset p2 (w, w * s);
658 return Lookup::round_filled_polygon (points, blotdiameter);
661 SCM wid = scm_from_double (w);
662 SCM sl = scm_from_double (s);
663 SCM thick = scm_from_double (t);
664 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
665 wid, sl, thick, SCM_UNDEFINED);
667 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
668 Interval (0, w * s));
670 return Stencil (b, slashnodot); // http://slashnodot.org
674 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
677 Axis other = Axis ((a + 1)%2);
679 b[other] = Interval (-1, 1) * thick * 0.5;
681 Stencil m = round_filled_box (b, blot);
683 b[a] = Interval (iv[UP] - thick, iv[UP]);
684 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protude));
685 oi *= sign (protude);
687 m.add_stencil (round_filled_box (b, blot));
688 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
689 m.add_stencil (round_filled_box (b, blot));
695 Lookup::triangle (Interval iv, Real thick, Real protude)
698 b[X_AXIS] = Interval (0, iv.length ());
699 b[Y_AXIS] = Interval (min (0., protude), max (0.0, protude));
701 Offset z1 (iv[LEFT], 0);
702 Offset z2 (iv[RIGHT], 0);
703 Offset z3 ((z1 + z2)[X_AXIS] / 2, protude);
706 TODO: move Triangle to Line_interface ?
708 Stencil tri = Line_interface::make_line (thick, z1, z2);
709 tri.add_stencil (Line_interface::make_line (thick, z2, z3));
710 tri.add_stencil (Line_interface::make_line (thick, z3, z1));