Skip to content

Add SVG parsing and rendering support#8

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/support-svg-parsing-and-rendering
Draft

Add SVG parsing and rendering support#8
Copilot wants to merge 3 commits intomainfrom
copilot/support-svg-parsing-and-rendering

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 30, 2026

Adds a pure-Rust SVG renderer (no new external dependencies) so .svg files can be drawn onto a canvas via a new draw_svg CLI command.

New module: canvas/src/svg.rs

  • XML parser – zero-dependency tokenizer/parser; handles attributes, nested elements, XML entities, comments, CDATA
  • SVG path flattener – full command set (M/L/H/V/C/S/Q/T/A/Z + relative variants); cubic/quadratic béziers use adaptive De Casteljau subdivision; arcs use SVG spec §B.2.4 endpoint-to-center parameterization
  • Shape renderers<rect> (+ rounded corners), <circle>, <ellipse>, <line>, <polyline>, <polygon>, <path>, <g>
  • Style inheritancefill, stroke, stroke-width, opacity, fill-opacity, stroke-opacity cascade through <g> groups; inline style="" + presentation attributes
  • Transform enginetranslate, scale, rotate, matrix, skewX/Y with full affine matrix concatenation
  • viewBox – coordinate mapping with preserveAspectRatio="xMidYMid meet" (default)

Public API (canvas/src/lib.rs)

// Render SVG bytes → ImageData at given pixel size (0 = intrinsic)
pub fn render_svg(svg_bytes: &[u8], dest_width: u32, dest_height: u32) -> Option<ImageData>;

// Draw SVG directly onto an existing Context2D
pub fn draw_svg(ctx: &mut Context2D, svg_bytes: &[u8], dx: f64, dy: f64, dw: u32, dh: u32) -> bool;

CLI (cli/src/main.rs)

draw_svg <path> <dx> <dy>           # draw at intrinsic SVG size
draw_svg <path> <dx> <dy> <dw> <dh> # draw scaled to dw×dh pixels

Copilot AI linked an issue Apr 30, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits April 30, 2026 08:23
Copilot AI changed the title [WIP] Add support for parsing and rendering SVG Add SVG parsing and rendering support Apr 30, 2026
Copilot AI requested a review from echosoar April 30, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

支持解析 svg 并渲染

2 participants