icon/draw.go

Functions

Functions

func Draw

Draw renders an icon's path operations onto the canvas within the given

bounds, using the specified color.

 

The icon is uniformly scaled to fit the bounds while preserving aspect ratio

(the viewbox is square). The stroke width scales proportionally.

 

Draw can be called directly for custom rendering, or is used internally by

[IconWidget].

func Draw(canvas widget.Canvas, data IconData, bounds geometry.Rect, color widget.Color) {
	if bounds.IsEmpty() {
		return
	}

	// Priority 1: Full SVG XML → direct rendering (JetBrains pipeline).
	// Renders all SVG elements (path, circle, rect) with proper fill/stroke,
	// fill-rule, linecap, etc. Color override replaces all fill/stroke colors.
	if len(data.SVGXML) > 0 {
		if renderer, ok := canvas.(widget.SVGRenderer); ok {
			renderer.RenderSVG(data.SVGXML, bounds, color)
			return
		}
	}

	// Priority 2: SVG path data → fill rendering.
	if data.SVGData != "" {
		if filler, ok := canvas.(widget.SVGFiller); ok {
			filler.FillSVGPath(data.SVGData, data.ViewBox, bounds, color)
			return
		}
	}

	// Priority 3: PathOp stroke rendering (manual icons, fallback).
	if len(data.Ops) == 0 || data.ViewBox <= 0 {
		return
	}
	scale, offsetX, offsetY := computeTransform(data.ViewBox, bounds)
	sw := defaultStrokeWidth
	if data.StrokeWidth > 0 {
		sw = data.StrokeWidth
	}
	strokeW := sw * scale
	drawOps(canvas, data.Ops, color, strokeW, scale, offsetX, offsetY)
}

func DrawMulti

DrawMulti renders a multi-color icon onto the canvas within the given bounds.

 

Each [PathGroup] in the icon is drawn with the color mapped by its ColorKey

in the palette. If a key is not found in the palette, the group is drawn

with [widget.ColorGray] as a fallback.

func DrawMulti(canvas widget.Canvas, data MultiColorIcon, bounds geometry.Rect, palette Palette) {
	if len(data.Groups) == 0 || data.ViewBox <= 0 {
		return
	}
	if bounds.IsEmpty() {
		return
	}

	scale, offsetX, offsetY := computeTransform(data.ViewBox, bounds)
	strokeW := defaultStrokeWidth * scale

	for _, group := range data.Groups {
		color, ok := palette[group.ColorKey]
		if !ok {
			color = widget.ColorGray
		}
		drawOps(canvas, group.Ops, color, strokeW, scale, offsetX, offsetY)
	}
}