ToPDF returns a string that represents the path in the PDF data format.
()
| 2425 | |
| 2426 | // ToPDF returns a string that represents the path in the PDF data format. |
| 2427 | func (p *Path) ToPDF() string { |
| 2428 | if p.Empty() { |
| 2429 | return "" |
| 2430 | } |
| 2431 | p = p.ReplaceArcs() |
| 2432 | |
| 2433 | sb := strings.Builder{} |
| 2434 | var x, y float64 |
| 2435 | for i := 0; i < len(p.d); { |
| 2436 | cmd := p.d[i] |
| 2437 | switch cmd { |
| 2438 | case MoveToCmd: |
| 2439 | x, y = p.d[i+1], p.d[i+2] |
| 2440 | fmt.Fprintf(&sb, " %v %v m", dec(x), dec(y)) |
| 2441 | case LineToCmd: |
| 2442 | x, y = p.d[i+1], p.d[i+2] |
| 2443 | fmt.Fprintf(&sb, " %v %v l", dec(x), dec(y)) |
| 2444 | case QuadToCmd, CubeToCmd: |
| 2445 | var start, cp1, cp2 Point |
| 2446 | start = Point{x, y} |
| 2447 | if cmd == QuadToCmd { |
| 2448 | x, y = p.d[i+3], p.d[i+4] |
| 2449 | cp1, cp2 = quadraticToCubicBezier(start, Point{p.d[i+1], p.d[i+2]}, Point{x, y}) |
| 2450 | } else { |
| 2451 | cp1 = Point{p.d[i+1], p.d[i+2]} |
| 2452 | cp2 = Point{p.d[i+3], p.d[i+4]} |
| 2453 | x, y = p.d[i+5], p.d[i+6] |
| 2454 | } |
| 2455 | fmt.Fprintf(&sb, " %v %v %v %v %v %v c", dec(cp1.X), dec(cp1.Y), dec(cp2.X), dec(cp2.Y), dec(x), dec(y)) |
| 2456 | case ArcToCmd: |
| 2457 | panic("arcs should have been replaced") |
| 2458 | case CloseCmd: |
| 2459 | x, y = p.d[i+1], p.d[i+2] |
| 2460 | fmt.Fprintf(&sb, " h") |
| 2461 | } |
| 2462 | i += cmdLen(cmd) |
| 2463 | } |
| 2464 | return sb.String()[1:] // remove the first space |
| 2465 | } |
| 2466 | |
| 2467 | // ToVectorRasterizer rasterizes the path to *vector.Rasterizer using the given rasterizer and resolution. |
| 2468 | func (p *Path) ToVectorRasterizer(ras *vector.Rasterizer, resolution Resolution) { |