2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
16 #include "line-interface.hh"
18 #include "dimensions.hh"
20 #include "string-convert.hh"
21 #include "file-path.hh"
23 #include "lily-guile.hh"
24 #include "font-metric.hh"
27 Lookup::dot (Offset p, Real radius)
29 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
30 scm_from_double (p[X_AXIS]),
31 scm_from_double (p[Y_AXIS]),
32 scm_from_double (radius),
35 box.add_point (p - Offset (radius, radius));
36 box.add_point (p + Offset (radius, radius));
37 return Stencil (box, at);
41 Lookup::beam (Real slope, Real width, Real thick, Real blot)
47 p = Offset (0, thick/2);
49 p += Offset (1,-1) * (blot/2);
53 points = scm_cons (scm_from_double (p[X_AXIS]),
54 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]),
67 p = Offset (width, width * slope - thick/2);
69 p += Offset (-1, 1) * (blot/2);
71 points = scm_cons (scm_from_double (p[X_AXIS]),
72 scm_cons (scm_from_double (p[Y_AXIS]),
76 p = Offset (width, width * slope + thick/2);
78 p += Offset (-1, -1) * (blot/2);
80 points = scm_cons (scm_from_double (p[X_AXIS]),
81 scm_cons (scm_from_double (p[Y_AXIS]),
84 SCM expr = scm_list_n (ly_symbol2scm ("polygon"),
85 ly_quote_scm (points),
86 scm_from_double (blot),
90 return Stencil (b, expr);
94 Lookup::dashed_slur (Bezier b, Real thick, Real dash_period, Real dash_fraction)
98 Real on = dash_fraction * dash_period;
99 Real off = dash_period - on;
101 for (int i = 4; i--;)
103 l = scm_cons (ly_offset2scm (b.control_[i]), l);
106 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
107 scm_from_double (thick),
108 scm_from_double (on),
109 scm_from_double (off),
113 Box box (Interval (0, 0), Interval (0, 0));
114 return Stencil (box, at);
118 Lookup::horizontal_line (Interval w, Real th)
120 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
121 scm_from_double (th),
122 scm_from_double (w[LEFT]),
124 scm_from_double (w[RIGHT]),
130 box[Y_AXIS] = Interval (-th / 2, th / 2);
132 return Stencil (box, at);
136 Lookup::blank (Box b)
138 return Stencil (b, scm_makfrom0str (""));
142 Lookup::filled_box (Box b)
144 return round_filled_box (b, 0.0);
150 * __________________________________
155 * |\ _ _ / v \ _ _ /| |
158 * | <------>| | extent
159 * | blot | | (Y_AXIS)
167 * x\_____/______________\_____/|_____v
171 * |<-------------------------->|
172 * Box extent (X_AXIS)
175 Lookup::round_filled_box (Box b, Real blotdiameter)
177 if (b.x ().length () < blotdiameter)
179 blotdiameter = b.x ().length ();
181 if (b.y ().length () < blotdiameter)
183 blotdiameter = b.y ().length ();
186 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
187 scm_from_double (-b[X_AXIS][LEFT]),
188 scm_from_double (b[X_AXIS][RIGHT]),
189 scm_from_double (-b[Y_AXIS][DOWN]),
190 scm_from_double (b[Y_AXIS][UP]),
191 scm_from_double (blotdiameter),
194 return Stencil (b, at);
198 * Create Stencil that represents a filled polygon with round edges.
202 * (a) Only outer (convex) edges are rounded.
204 * (b) This algorithm works as expected only for polygons whose edges
205 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
206 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
207 * will give a strange result. Even non-adjacent edges that just
208 * touch each other will in general not work as expected for non-null
211 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
212 * if there is a natural number k such that blotdiameter is greater
213 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
214 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
215 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
216 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
217 * polygon will exceed the outline of the core polygon. In other
218 * words: Do not draw rounded polygons that have a leg smaller or
219 * thinner than blotdiameter (or set blotdiameter to a sufficiently
220 * small value -- maybe even 0.0)!
222 * NOTE: Limitations (b) and (c) arise from the fact that round edges
223 * are made by moulding sharp edges to round ones rather than adding
224 * to a core filled polygon. For details of these two different
225 * approaches, see the thread upon the ledger lines patch that started
226 * on March 25, 2002 on the devel mailing list. The below version of
227 * round_filled_polygon () sticks to the moulding model, which the
228 * majority of the list participants finally voted for. This,
229 * however, results in the above limitations and a much increased
230 * complexity of the algorithm, since it has to compute a shrinked
231 * polygon -- which is not trivial define precisely and unambigously.
232 * With the other approach, one simply could move a circle of size
233 * blotdiameter along all edges of the polygon (which is what the
234 * postscript routine in the backend effectively does, but on the
235 * shrinked polygon). --jr
238 Lookup::round_filled_polygon (Array<Offset> const &points,
241 /* TODO: Maybe print a warning if one of the above limitations
242 applies to the given polygon. However, this is quite complicated
245 const Real epsilon = 0.01;
248 /* remove consecutive duplicate points */
249 for (int i = 0; i < points.size (); i++)
251 int next_i = (i + 1) % points.size ();
252 Real d = (points[i] - points[next_i]).length ();
254 programming_error ("Polygon should not have duplicate points");
258 /* special cases: degenerated polygons */
259 if (points.size () == 0)
261 if (points.size () == 1)
262 return dot (points[0], 0.5 * blotdiameter);
263 if (points.size () == 2)
264 return Line_interface::make_line (blotdiameter, points[0], points[1]);
266 /* shrink polygon in size by 0.5 * blotdiameter */
267 Array<Offset> shrunk_points;
268 shrunk_points.set_size (points.size ());
269 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
270 for (int i = 0; i < points.size (); i++)
273 int i1 = (i + 1) % points.size ();
274 int i2 = (i + 2) % points.size ();
275 Offset p0 = points[i0];
276 Offset p1 = points[i1];
277 Offset p2 = points[i2];
278 Offset p10 = p0 - p1;
279 Offset p12 = p2 - p1;
280 if (p10.length () != 0.0)
282 Real phi = p10.arg ();
283 // rotate (p2 - p0) by (-phi)
284 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
288 else if (q[Y_AXIS] < 0)
290 else {} // keep ccw unchanged
292 else {} // keep ccw unchanged
293 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
294 Offset p12n = (1.0 / p12.length ()) * p12;
295 Offset p13n = 0.5 * (p10n + p12n);
296 Offset p14n = 0.5 * (p10n - p12n);
298 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
300 // special case: p0, p1, p2 are on a single line => build
301 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
303 p13[X_AXIS] = p10[Y_AXIS];
304 p13[Y_AXIS] = -p10[X_AXIS];
305 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
308 p13 = (0.5 * blotdiameter / d) * p13n;
309 shrunk_points[i1] = p1 + ((ccw) ? p13 : -p13);
312 /* build scm expression and bounding box */
313 SCM shrunk_points_scm = SCM_EOL;
315 for (int i = 0; i < shrunk_points.size (); i++)
317 SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
318 SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
319 shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
320 box.add_point (points[i]);
322 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
323 ly_quote_scm (shrunk_points_scm),
324 scm_from_double (blotdiameter),
328 Stencil polygon = Stencil (box, polygon_scm);
329 shrunk_points.clear ();
337 Lookup::frame (Box b, Real thick, Real blot)
341 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
343 Axis o = Axis ((a + 1)%NO_AXES);
347 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
348 edges[o][DOWN] = b[o][DOWN] - thick / 2;
349 edges[o][UP] = b[o][UP] + thick / 2;
351 m.add_stencil (round_filled_box (edges, blot));
353 while (flip (&d) != LEFT);
359 Make a smooth curve along the points
362 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
364 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
366 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
368 back.control_[1] += perp;
369 back.control_[2] += perp;
371 curve.control_[1] -= perp;
372 curve.control_[2] -= perp;
376 for (int i = 4; i--;)
377 scontrols[ i ] = ly_offset2scm (back.control_[i]);
378 for (int i = 4; i--;)
379 scontrols[i + 4] = ly_offset2scm (curve.control_[i]);
382 Need the weird order b.o. the way PS want its arguments
384 int indices[] = {5, 6, 7, 4, 1, 2, 3, 0};
386 for (int i = 8; i--;)
388 list = scm_cons (scontrols[indices[i]], list);
391 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
393 scm_from_double (linethick),
395 Box b (curve.extent (X_AXIS),
396 curve.extent (Y_AXIS));
398 b[X_AXIS].unite (back.extent (X_AXIS));
399 b[Y_AXIS].unite (back.extent (Y_AXIS));
401 return Stencil (b, at);
428 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
431 Need the weird order b.o. the way PS want its arguments
434 list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
435 list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
436 list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
437 list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
438 list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
439 list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
440 list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
441 list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
443 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
445 scm_from_double (0.0),
448 Interval x_extent = top_curve.extent (X_AXIS);
449 x_extent.unite (bottom_curve.extent (X_AXIS));
450 Interval y_extent = top_curve.extent (Y_AXIS);
451 y_extent.unite (bottom_curve.extent (Y_AXIS));
452 Box b (x_extent, y_extent);
454 return Stencil (b, horizontal_bend);
461 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
464 String sym = ly_scm2string (scm_car (s));
465 String reg = ly_scm2string (scm_car (scm_cdr (s)));
467 if (sym == "Discant")
469 Stencil r = fm->find_by_name ("accordion.accDiscant");
471 if (reg.left_string (1) == "F")
473 Stencil d = fm->find_by_name ("accordion.accDot");
474 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
476 reg = reg.right_string (reg.length () - 1);
479 if (reg.left_string (3) == "EEE")
482 reg = reg.right_string (reg.length () - 3);
484 else if (reg.left_string (2) == "EE")
487 reg = reg.right_string (reg.length () - 2);
489 else if (reg.left_string (2) == "Eh")
492 reg = reg.right_string (reg.length () - 2);
494 else if (reg.left_string (1) == "E")
497 reg = reg.right_string (reg.length () - 1);
501 Stencil d = fm->find_by_name ("accordion.accDot");
502 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
507 Stencil d = fm->find_by_name ("accordion.accDot");
508 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
509 d.translate_axis (0.8 * staff_space PT, X_AXIS);
514 Stencil d = fm->find_by_name ("accordion.accDot");
515 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
516 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
519 if (reg.left_string (2) == "SS")
521 Stencil d = fm->find_by_name ("accordion.accDot");
522 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
523 d.translate_axis (0.4 * staff_space PT, X_AXIS);
525 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
527 reg = reg.right_string (reg.length () - 2);
529 if (reg.left_string (1) == "S")
531 Stencil d = fm->find_by_name ("accordion.accDot");
532 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
534 reg = reg.right_string (reg.length () - 1);
537 else if (sym == "Freebase")
539 Stencil r = fm->find_by_name ("accordion.accFreebase");
541 if (reg.left_string (1) == "F")
543 Stencil d = fm->find_by_name ("accordion.accDot");
544 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
546 reg = reg.right_string (reg.length () - 1);
550 Stencil d = fm->find_by_name ("accordion.accDot");
551 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
555 else if (sym == "Bayanbase")
557 Stencil r = fm->find_by_name ("accordion.accBayanbase");
559 if (reg.left_string (1) == "T")
561 Stencil d = fm->find_by_name ("accordion.accDot");
562 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
564 reg = reg.right_string (reg.length () - 1);
566 /* include 4' reed just for completeness. You don't want to use this. */
567 if (reg.left_string (1) == "F")
569 Stencil d = fm->find_by_name ("accordion.accDot");
570 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
572 reg = reg.right_string (reg.length () - 1);
574 if (reg.left_string (2) == "EE")
576 Stencil d = fm->find_by_name ("accordion.accDot");
577 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
578 d.translate_axis (0.4 * staff_space PT, X_AXIS);
580 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
582 reg = reg.right_string (reg.length () - 2);
584 if (reg.left_string (1) == "E")
586 Stencil d = fm->find_by_name ("accordion.accDot");
587 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
589 reg = reg.right_string (reg.length () - 1);
592 else if (sym == "Stdbase")
594 Stencil r = fm->find_by_name ("accordion.accStdbase");
596 if (reg.left_string (1) == "T")
598 Stencil d = fm->find_by_name ("accordion.accDot");
599 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
601 reg = reg.right_string (reg.length () - 1);
603 if (reg.left_string (1) == "F")
605 Stencil d = fm->find_by_name ("accordion.accDot");
606 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
608 reg = reg.right_string (reg.length () - 1);
610 if (reg.left_string (1) == "M")
612 Stencil d = fm->find_by_name ("accordion.accDot");
613 d.translate_axis (staff_space * 2 PT, Y_AXIS);
614 d.translate_axis (staff_space PT, X_AXIS);
616 reg = reg.right_string (reg.length () - 1);
618 if (reg.left_string (1) == "E")
620 Stencil d = fm->find_by_name ("accordion.accDot");
621 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
623 reg = reg.right_string (reg.length () - 1);
625 if (reg.left_string (1) == "S")
627 Stencil d = fm->find_by_name ("accordion.accDot");
628 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
630 reg = reg.right_string (reg.length () - 1);
633 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
635 else if (sym == "SB")
637 Stencil r = fm->find_by_name ("accordion.accSB");
640 else if (sym == "BB")
642 Stencil r = fm->find_by_name ("accordion.accBB");
645 else if (sym == "OldEE")
647 Stencil r = fm->find_by_name ("accordion.accOldEE");
650 else if (sym == "OldEES")
652 Stencil r = fm->find_by_name ("accordion.accOldEES");
659 Lookup::repeat_slash (Real w, Real s, Real t)
662 Array<Offset> points ;
663 Real blotdiameter = 0.0;
670 return Lookup::round_filled_polygon (points, blotdiameter);
673 SCM wid = scm_from_double (w);
674 SCM sl = scm_from_double (s);
675 SCM thick = scm_from_double (t);
676 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
677 wid, sl, thick, SCM_UNDEFINED);
679 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
680 Interval (0, w * s));
682 return Stencil (b, slashnodot); // http://slashnodot.org
686 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
689 Axis other = Axis ((a + 1)%2);
691 b[other] = Interval (-1, 1) * thick * 0.5;
693 Stencil m = round_filled_box (b, blot);
695 b[a] = Interval (iv[UP] - thick, iv[UP]);
696 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protude));
697 oi *= sign (protude);
699 m.add_stencil (round_filled_box (b, blot));
700 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
701 m.add_stencil (round_filled_box (b, blot));
707 Lookup::triangle (Interval iv, Real thick, Real protude)
710 b[X_AXIS] = Interval (0, iv.length ());
711 b[Y_AXIS] = Interval (min (0., protude), max (0.0, protude));
713 Offset z1 (iv[LEFT], 0);
714 Offset z2 (iv[RIGHT], 0);
715 Offset z3 ((z1 + z2)[X_AXIS] / 2, protude);
718 TODO: move Triangle to Line_interface ?
720 Stencil tri = Line_interface::make_line (thick, z1, z2);
721 tri.add_stencil (Line_interface::make_line (thick, z2, z3));
722 tri.add_stencil (Line_interface::make_line (thick, z3, z1));