Skip to content

Commit a1df16b

Browse files
committed
Switch infinite color/gradient rendering to a more robust approach
1 parent 944a6ee commit a1df16b

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

node-graph/gsvg-renderer/src/renderer.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,9 +1385,6 @@ impl Render for Table<Raster<GPU>> {
13851385
}
13861386
}
13871387

1388-
const ALMOST_INF: f64 = 2_000_000_000.;
1389-
const ALMOST_INF_OFFSET: f64 = ALMOST_INF / -2.;
1390-
13911388
// Since colors and gradients are technically infinitely big, we have to implement
13921389
// workarounds for rendering them correctly in a way which still allows us
13931390
// to cache the intermediate render data (SVG string/Vello scene).
@@ -1397,11 +1394,10 @@ const ALMOST_INF_OFFSET: f64 = ALMOST_INF / -2.;
13971394
impl Render for Table<Color> {
13981395
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
13991396
for row in self.iter() {
1400-
render.leaf_tag("rect", |attributes| {
1401-
attributes.push("width", ALMOST_INF.to_string());
1402-
attributes.push("height", ALMOST_INF.to_string());
1403-
attributes.push("x", ALMOST_INF_OFFSET.to_string());
1404-
attributes.push("y", ALMOST_INF_OFFSET.to_string());
1397+
render.leaf_tag("polyline", |attributes| {
1398+
// Chrome doesn't like drawing centered rectangles bigger than ~20 million so we draw a polyline quad instead
1399+
let max = u64::MAX;
1400+
attributes.push("points", format!("{max},{max} -{max},{max} -{max},-{max} {max},-{max}"));
14051401

14061402
let color = row.element;
14071403
attributes.push("fill", format!("#{}", color.to_rgb_hex_srgb_from_gamma()));
@@ -1452,13 +1448,13 @@ impl Render for Table<Color> {
14521448
}
14531449

14541450
impl Render for Table<GradientStops> {
1451+
// TODO: Fix infinite gradient rendering
14551452
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
14561453
for row in self.iter() {
14571454
render.leaf_tag("rect", |attributes| {
1458-
attributes.push("width", ALMOST_INF.to_string());
1459-
attributes.push("height", ALMOST_INF.to_string());
1460-
attributes.push("x", ALMOST_INF_OFFSET.to_string());
1461-
attributes.push("y", ALMOST_INF_OFFSET.to_string());
1455+
// Chrome doesn't like drawing centered rectangles bigger than ~20 million so we draw a polyline quad instead
1456+
let max = u64::MAX;
1457+
attributes.push("points", format!("{max},{max} -{max},{max} -{max},-{max} {max},-{max}"));
14621458

14631459
let mut stop_string = String::new();
14641460
for (position, color) in row.element.0.iter() {
@@ -1514,6 +1510,7 @@ impl Render for Table<GradientStops> {
15141510
}
15151511
}
15161512

1513+
// TODO: Fix infinite gradient rendering
15171514
#[cfg(feature = "vello")]
15181515
fn render_to_vello(&self, scene: &mut Scene, _parent_transform: DAffine2, _context: &mut RenderContext, render_params: &RenderParams) {
15191516
use vello::peniko;

0 commit comments

Comments
 (0)