MCPcopy Index your code
hub / github.com/tdewolff/canvas / ToPDF

Method ToPDF

path.go:2427–2465  ·  view source on GitHub ↗

ToPDF returns a string that represents the path in the PDF data format.

()

Source from the content-addressed store, hash-verified

2425
2426// ToPDF returns a string that represents the path in the PDF data format.
2427func (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.
2468func (p *Path) ToVectorRasterizer(ras *vector.Rasterizer, resolution Resolution) {

Callers 2

TestPathToPDFFunction · 0.80
RenderPathMethod · 0.80

Calls 6

EmptyMethod · 0.95
ReplaceArcsMethod · 0.95
quadraticToCubicBezierFunction · 0.85
cmdLenFunction · 0.85
decTypeAlias · 0.70
StringMethod · 0.45

Tested by 1

TestPathToPDFFunction · 0.64