MCPcopy
hub / github.com/tdewolff/canvas / parseDefs

Method parseDefs

svg.go:450–770  ·  view source on GitHub ↗
(l *xml.Lexer)

Source from the content-addressed store, hash-verified

448}
449
450func (svg *svgParser) parseDefs(l *xml.Lexer) {
451 for {
452 tag := svg.parseTag(l)
453 if tag == nil {
454 break
455 }
456 id := tag.attrs["id"]
457 if id == "" {
458 continue
459 }
460 switch tag.name {
461 case "linearGradient":
462 if _, ok := tag.attrs["x2"]; !ok {
463 tag.attrs["x2"] = "100%"
464 }
465 x1p := strings.HasSuffix(tag.attrs["x1"], "%")
466 y1p := strings.HasSuffix(tag.attrs["y1"], "%")
467 x2p := strings.HasSuffix(tag.attrs["x2"], "%")
468 y2p := strings.HasSuffix(tag.attrs["y2"], "%")
469 x1 := svg.parseDimension(tag.attrs["x1"], 1.0)
470 x2 := svg.parseDimension(tag.attrs["x2"], 1.0)
471 y1 := svg.parseDimension(tag.attrs["y1"], 1.0)
472 y2 := svg.parseDimension(tag.attrs["y2"], 1.0)
473
474 grad := Grad{}
475 for _, tag := range tag.content {
476 if tag.name != "stop" {
477 continue
478 }
479
480 offset := svg.parseNumber(tag.attrs["offset"])
481 stopColor := svg.parseColor(tag.attrs["stop-color"])
482 if v, ok := tag.attrs["stop-opacity"]; ok {
483 stopOpacity := svg.parseNumber(v)
484 stopColor = ToOpacity(stopColor, stopOpacity)
485 }
486 grad.Add(offset, stopColor)
487 }
488 svg.defs[id] = func(attr string, c *Canvas) {
489 layers := c.layers[c.zindex]
490 if len(layers) == 0 || layers[len(layers)-1].path == nil {
491 return
492 }
493 layer := &layers[len(layers)-1]
494
495 rect := layer.path.FastBounds()
496 x1t, y1t, x2t, y2t := x1, y1, x2, y2
497 if x1p {
498 x1t = rect.X0 + rect.W()*x1t
499 }
500 if y1p {
501 y1t = rect.Y0 + rect.H()*y1t
502 }
503 if x2p {
504 x2t = rect.X0 + rect.W()*x2t
505 }
506 if y2p {
507 y2t = rect.Y0 + rect.H()*y2t

Callers 1

ParseSVGFunction · 0.95

Calls 15

parseTagMethod · 0.95
parseDimensionMethod · 0.95
parseNumberMethod · 0.95
parseColorMethod · 0.95
AddMethod · 0.95
ToLinearMethod · 0.95
ToRadialMethod · 0.95
parseViewBoxMethod · 0.95
pushMethod · 0.95
initMethod · 0.95
setStylingMethod · 0.95
drawShapeMethod · 0.95

Tested by

no test coverage detected