Skip to content

fix(render): count SVG marker images more tightly and fairly before reading them (#2713) - #2801

Merged
jgregoriorincon merged 2 commits into
opengeos:mainfrom
jgregoriorincon:fix/2713-svg-bounds
Nov 23, 2026
Merged

jgregoriorincon merged 2 commits into
opengeos:mainfrom
jgregoriorincon:fix/2713-svg-bounds

Conversation

@jgregoriorincon

@jgregoriorincon jgregoriorincon commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

SVG marker images are counted before they are read (crates/render/src/picture/svg.rs, from #2704). This makes that count tighter against crafted SVGs and fairer to real icons, the bounds part of #2713. The refusal phrases, the other part, follow in their own PR.

Fairer (valid icons that were drawn as a circle):

  • Markers count where they are set. A <marker> used to count on every vertex of the document, so an icon with an unused marker (editors keep arrowheads in <defs>) and about 5,000 points was refused. Now only a marker some marker property names (url(#id); none names none) counts (named_markers), and it counts on the vertices of the shapes that set a marker: by attribute, in a style, inherited from what holds them, or through a <use> that copies them. When a style sheet sets a marker (a url(#id) value, read as usvg reads it; marker: none sets none), it may reach any shape, so every vertex counts, as before.
  • A class named like a marker isn't a marker. Whether a style sets a marker is now read with simplecss, as usvg reads a style: only the properties marker, marker-start, marker-mid and marker-end set one (is_marker). A sheet such as .marker-dot { fill: red } therefore no longer makes two side-by-side markers refused as nested. A comment before the colon (marker-mid/**/:) still counts, as the reader takes it.
  • A radius in % (<circle r="50%">, rx="10%") is resolved against the largest viewport anywhere in the SVG: its view boxes and the sizes of its <svg>, <symbol> and <use> elements (viewport), the root's % of 100 a side as usvg takes it. A nested size in %, em or ex may be any size, so while the SVG has one, every % radius in it is still refused, as all were before. A radius of 0 is 0 in any unit; one in em or ex stays refused.
  • A polyline's points are its pairs of numbers as svgtypes reads them (PointsParser). They used to be runs of digits, which counted 12.345,67.891 as two points.

Tighter (crafted SVGs that took too long):

  • Style sheets. usvg tries every rule on every element, again on each copy a <use> makes (it reads what a <use> repeats again for each). simplecss tries each descendant selector (a b) against every element above, again for each descendant before it, so x g g g g g g rect on 40 nested groups took 0.5 s, and deeper it doesn't finish. The count now runs simplecss's own matcher on each element, with each step counted (css_steps, an Element that counts), each node walked back past to a previous sibling included (comments too, which usvg's own tree leaves out), and an attribute's value matched a step each 64 bytes. Matching stops once all the elements matched so far pass CSS_STEPS (1,000,000, ~11 ms measured), and each <use>'s copy counts against that cap again. Each rule tried on an element is at least one step, so the cap is also about 200 class rules on 2,500 elements; marker icons come to thousands of steps. The sheets are parsed once for this and for the marker checks; usvg parses them again when it reads the SVG. This makes simplecss 0.2.2, already in the lock file through usvg, a direct dependency of rustgis-render.
  • Gradient stops. Past two stops, tiny-skia goes through all of them at each pixel (its gradient stage). Measured: 2 stops take 21 ns a pixel, 1,000 stops 695 ns. Such a gradient now costs covered × stops / 16 more (STOPS).
  • Arcs. One arc may come to at most 200 cubic curves, down from 1,000. The reader handles an arc's curves in quadratic time, and an icon's arcs come to a few (a radius of 10¹² gives about 150).
  • Depth through <use>. A subtree counted where it is shallow was reused, without checking its levels again, where a <use> reached it deeper. Such an SVG passed the count and was then refused by usvg's own depth limit, with the wrong phrase. Each count now keeps its height, checked again wherever it is reached.

Checked and left as they are:

  • Quadratic curves already count as the 2 points usvg builds (usvg/src/parser/shapes.rs:50, quad_to); a test now pins it.
  • A marker's points on each vertex, a plain <rect>'s points and a <use> link with spaces were already counted on main.

Testing

  • Tests written first, each seen failing for its reason before the code: markers counted where they are set, an unused marker beside a used one, marker: none in a sheet, an attribute or a style, comments walked past by :first-child, a long class matched by many rules, a .marker-dot class, a comment before a marker's colon, % and zero radii, the 200-curve arc, polyline numbers, the style sheet bound (many rules, <use> copies, deep descendants, the cap over all elements), gradient stops, depth through a reused subtree. Two tests pin what already held: quadratic points, and hostile geometry drawn without panicking.
  • cargo fmt --all --check, cargo clippy --all-targets -- -D warnings, cargo nextest run --workspace (9122 passed; the local DuckDB test left out), doctests, cargo xtask layers and cargo xtask schema --check: all pass.
  • cargo check -p rustgis-web --target wasm32-unknown-unknown: passes (simplecss is pure Rust, already built for the web app through usvg).
  • Notices: simplecss is already listed in every THIRD-PARTY-LICENSES.txt (it came in through usvg), so they don't change.

Surfaces

The count runs in render's marker cache (picture::svg_in), which every way an image marker is set or read goes through: the layer.marker, layer.lineStrokes and layer.fillLayers commands (desktop, browser, command line, Python, the Assistant and MCP, scripts and tasks alike), project and style loading, the QGIS and ArcGIS style importers, and the Image… picker. No command, parameter, control or undo step changes, so the ribbon, the phone and plugins see the same thing.

UI evidence

Cities with an SVG marker whose rectangle has rx="25%" and whose circle has r="25%". Before (left, main): refused with "…this one is an SVG with more to draw than a 2048 × 2048 image", and drawn as circles. After (right): the icon is drawn.

pr2713-svg-before-after.png

Related issue

Part of #2713 (the bounds on reading; the refusal phrases follow). Part of #2266.

Not in this PR

Summary by CodeRabbit

  • Bug Fixes
    • Improved SVG handling for CSS selectors, gradients with many stops, markers, viewport-relative dimensions, and reused elements.
    • Complex SVGs now have bounds on arc complexity and CSS matching work, helping keep rendering within defined limits.
    • SVG length calculations account for percentage-based dimensions and preserve zero-length values.
  • Documentation
    • Updated SVG guidance to clarify arc limits and how percentage-based marker dimensions are evaluated.

…eading them (opengeos#2713)

Part of opengeos#2713 (bounds on reading; the refusal phrases follow in their own PR).

- A marker counts only on the vertices of the shapes that set it (by attribute,
  style or inheritance, a <use>'s copy included), or on all of them when a
  style sheet sets one: an icon with an unused marker is no longer refused.
- Whether a style sets a marker is read with simplecss, as usvg reads it, so a
  class like .marker-dot sets none and a comment before the colon still counts.
- Matching the style sheets' rules is bounded by the steps simplecss's own
  matcher takes (CSS_STEPS), on each <use> copy as usvg matches them, which also
  bounds descendant selectors deep down. The sheets are parsed once.
- A gradient past two stops costs its stops at each pixel (STOPS).
- An arc may come to at most 200 cubic curves (was 1,000).
- A polyline's points are its pairs of numbers as svgtypes reads them.
- A radius in % is resolved against the largest viewport; 0 in any unit is 0.
- A subtree counted shallow is checked again where a <use> reaches it deeper.
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The SVG reader now bounds CSS matching work, tracks referenced markers and viewport-relative geometry, limits arc curves, and accounts for gradient stops. Tests cover these limits and related SVG parsing behavior.

Changes: SVG reading bounds

Layer / File(s) Summary
Style-aware element and marker accounting
Cargo.toml, crates/render/Cargo.toml, crates/render/src/picture/svg.rs
Adds simplecss and parses stylesheets for SVG element and marker accounting. CSS rule attempts, selector traversal, and attribute matching count toward a shared step limit.
Viewport-relative geometry accounting
crates/render/src/picture/svg.rs, docs/user-guide/cartography.md
Percentage lengths use a viewport basis. Polyline and polygon points use svgtypes::PointsParser. The arc cubic-curve limit decreases from 1,000 to 200. The documentation describes the percentage-radius handling and updated arc limit.
Gradient costs and bounded-input tests
crates/render/src/picture/svg.rs
Linear and radial gradient costs increase for more than two stops. Tests cover CSS matching, gradient costs, subtree depth at <use> sites, and hostile geometry.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Possibly related issues

  • Issue 2800 reports slow SVG reads involving marker stylesheets. This change bounds CSS matching work, but stylesheet parsing cost remains unchanged.
  • Issue 2713 lists SVG reading bounds that include marker use, CSS matching, gradient stops, arc curves, and radius handling. This change implements several of those bounds.
  • Issue 2266 proposes picture markers that accept PNG, JPEG, or SVG images. This change bounds SVG image reading, but does not implement picture markers.

Suggested reviewers: giswqs, pick2510

Merge Risk: Minimal · up to 57b62 · assessed changes only

Path filters excluded changed files. This rating applies only to the assessed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the render crate, the fix type, and the main change: SVG marker images are counted more tightly and fairly before they are read. The raw summary and objectives confirm that the change …
Description check Passed The description follows the template sections (Summary, Testing, UI evidence, Related issue). The Summary explains what changes and why, and it lists fairer and tighter limits. The Testing section lis…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the render crate, the fix type, and the main change: SVG marker images are counted more tightly and fairly before they are read. The raw summary and objectives confirm that the change narrows the marker count and adds bounds, so the title matches the changeset. It does not mention the CSS, gradient, or arc limits, but that is expected for a title.

Full details: Description check

Explanation

The description follows the template sections (Summary, Testing, UI evidence, Related issue). The Summary explains what changes and why, and it lists fairer and tighter limits. The Testing section lists the tests and the checks that passed. The UI evidence section includes before and after screenshots for the visible change. The Related issue section cites #2713 and #2266 and lists what is out of scope. The template comments are removed, and the description is detailed and specific.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the curves in flight
And counts each style rule just right
The markers found, the stops tallied
The viewport measures paths unsteadily
“Two hundred arcs,” it hops with glee
Then bounds the SVG carefully

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 9ee427bf-6735-47e5-98b6-308367edc447
Commits

Reviewing files that changed from the base of the PR and between c795c08 and 57b625d.

Files selected for processing: 1

  • docs/user-guide/cartography.md

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

Comment thread crates/render/src/picture/svg.rs Resolved
Comment thread crates/render/src/picture/svg.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs

  • None found. I checked the step accounting (css_steps, Counter::count, the memo and its height check), the marker vertex and point totals, the use and inheritance paths, and the new arc, percent and gradient costs. I did not find a case that panics, overflows or undercounts.

Security

  • None found.

Performance

  • Attribute-scan cost is not counted (medium confidence). attribute_matches (crates/render/src/picture/svg.rs:266) costs one step per lookup, but the lookup scans every attribute on the element. An element with about 100,000 attributes and 10,000 rules would stay under CSS_STEPS and still take seconds. Suggested fix is in the inline comment.

Quality

  • Two-marker refusal under a sheet (medium confidence). nested_markers (svg.rs:151) refuses any file with two or more markers once a style rule sets a marker, whatever that rule's selector reaches. This predates the PR, but it sits against the fairness goal. Details are in the inline comment.
  • Test/timing note (low confidence). The new code parses the style sheets in read before usvg does, which is the double parse the PR already mentions. The step budget also refuses icons with several hundred class rules on a few thousand elements. Both are judgment calls the PR covers, so I left them alone.

AGENTS.md

  • None found. Checked: no unwrap/expect/indexing in non-test code, simplecss is already in THIRD-PARTY-LICENSES.txt, the crate is pure Rust (wasm-safe), and the test-hygiene rules hold.

Already raised

  • No prior review threads existed, so no earlier concerns were skipped.

Inline comments posted:

  • crates/render/src/picture/svg.rs:266, performance
  • crates/render/src/picture/svg.rs:151, quality

I could not verify the external crate APIs (simplecss, svgtypes, roxmltree) because the cargo registry is not on disk, and I had no Bash access. Those points rest on reading the code.

Change size

Compared with main (merge base dd3d011 to PR head c795c08).

Measure Value
Files changed 4 (0 added, 4 modified, 0 removed, 0 renamed)
Size before 258.7 KB
Size after 280.5 KB
Total size change +21.7 KB
File Before After Change
crates/render/src/picture/svg.rs 45.9 KB 67.5 KB +21.6 KB
Cargo.toml 9.0 KB 9.0 KB +54 B
crates/render/Cargo.toml 1009 B 1019 B +33 B
Cargo.lock 202.9 KB 202.9 KB +14 B

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-2801/
Web app https://preview.rustgis.app/pr-2801/app/
Commit 57b625d

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

@jgregoriorincon
jgregoriorincon marked this pull request as ready for review November 23, 2026 19:33
Comment thread crates/render/src/picture/svg.rs Resolved
Comment thread docs/user-guide/cartography.md Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs. None found. I read the full diff, the helpers it touches, and the read flow. The bounds only get tighter or stay conservative in the cases I traced: marker counting per named vertex, CSS step counting including sibling walks and <use> copies, % radius viewport, and gradient stops. Refusals still come through Refusal::TooMuch, so no data is dropped silently. I could not compile or run tests here (no Bash), so compile and test status rests on the PR's own report.

Security. None found. No new input path bypasses a bound, and the new arithmetic is saturating or checked against CSS_STEPS, ELEMENTS and POINTS. I found no unwrap/expect/indexing in non-test code.

Performance. None found. Per-element CSS matching is bounded by the global step cap, and the other new passes are linear or bounded by the depth limit. Two points were already raised on earlier threads and answered by the author, so I did not repeat them: the linear attribute scan in attribute_matches, and the refusal of any two-marker SVG when a sheet sets a marker.

Quality

  • crates/render/src/picture/svg.rs:68 (confidence high). The STOPS doc says each group of stops costs a unit a pixel, but the code divides by it (each stop past two costs 1/STOPS). The name also hides that it is a divisor. Posted inline.
  • docs/user-guide/cartography.md:113 (confidence medium). "Unless a size inside it is in %, em or ex" leaves out that the root's own em or ex width or height also refuses every % radius. Posted inline.

AGENTS.md. No breaks found. The PR adds no command, no layer data copy, no asset, and no UI surface. The simplecss dependency is pure Rust and already in the lock file. Surfaces and undo are covered in the PR body.

Prior threads. Two earlier concerns were left alone because the author answered them and the code they point at is unchanged: the attribute scan cost, and the sheet-sets-marker refusal. I did not re-raise either.

Change size

Compared with main (merge base dd3d011 to PR head 57b625d).

Measure Value
Files changed 5 (0 added, 5 modified, 0 removed, 0 renamed)
Size before 329.3 KB
Size after 351.2 KB
Total size change +21.8 KB
Path Before After Change
crates/render/src/picture/svg.rs 45.9 KB 67.5 KB +21.6 KB
docs/user-guide/cartography.md 70.6 KB 70.7 KB +114 B
Cargo.toml 9.0 KB 9.0 KB +54 B
crates/render/Cargo.toml 1010 B 1043 B +33 B
Cargo.lock 202.9 KB 202.9 KB +14 B

@jgregoriorincon
jgregoriorincon added this pull request to the merge queue Nov 23, 2026
Merged via the queue into opengeos:main with commit b4bda09 Nov 23, 2026
33 checks passed
@jgregoriorincon
jgregoriorincon deleted the fix/2713-svg-bounds branch November 23, 2026 20:11
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.

1 participant