svglite icon indicating copy to clipboard operation
svglite copied to clipboard

new graphics engine features

Open trevorld opened this issue 1 year ago • 3 comments

Introduced in R 4.2

  • [ ] affine transformations
  • [ ] compositing operators
  • [ ] luminance masks
  • [ ] stroking and filling paths

Introduced in R 4.3

  • [ ] glyphs

In a {vdiffr} issue comment @teunbrand writes:

In addition to pattern support, it would be nice to have support for other newer graphical features as well (clipping paths, masks, compositing/blending, affine transformations, glyphs and fill/stroke paths).

Presumably it would be easier to test these features with {vdiffr} if they were supported by {svglite}

trevorld avatar Jun 20 '24 17:06 trevorld

glyphs will sadly never work in svglite, since there is no way to pass on the information provided by the graphics engine to svg

thomasp85 avatar Oct 23 '24 11:10 thomasp85

vdiffr is indeed based on svglite but is embedded as its own graphic device. This is done so that changes in svglite does not break visual tests in vdiffr because of slight changes to the produced svg. For the same reason we are very conservative with changing anything in the differ graphics device

thomasp85 avatar Oct 23 '24 11:10 thomasp85

vdiffr is indeed based on svglite but is embedded as its own graphic device...

Note you can tell {vdiffr} to directly use svglite::svglite() instead of its own (more conservative but more stable) version. Some developers (including myself) use svglite::svglite() directly in {vdiffr} for tests of features introduced in R 4.1 (e.g. patterns, alpha masks, etc.) which have been supported in svglite::svglite() for a while but aren't yet supported in {vdiffr}'s more conservative version.

trevorld avatar Oct 23 '24 14:10 trevorld

I had a need for sheared rasters and svglite crashed the R session (see details). That is just to say, I'd like to add my voice to this request.

Uncomment to have the reprex crash:

library(grid)

rect <- rectGrob(width = 0.5, height = 0.5)

# svglite::svglite()
grid.newpage()
grid.define(rect, name = "my_rect")
grid.use("my_rect", transform = function(...) {
  viewportTransform(..., shear = groupShear(0, 0.5))
})

# dev.off()

Created on 2024-12-15 with reprex v2.1.1

teunbrand avatar Dec 15 '24 14:12 teunbrand

All of the above should now have the best possible support. For inexplicable reasons, the SVG format has horrible support for Porter Duff composition so those blend modes are not supported

thomasp85 avatar Apr 28 '25 09:04 thomasp85

Did some testing around and the new affine transformation support look good to me but the stroking and filling paths don't seem to render for me. When I look at them in Google Chrome, Firefox, or Inkscape I just see a blank screen:

library("grid")
library("ragg")
library("svglite")

t_o <- textGrob("O", gp = gpar(cex = 18, col = "yellow"))

gp <- gpar(fill = "yellow", col = "black")
f_o <- fillGrob(t_o, gp = gp)
s_o <- strokeGrob(t_o, gp = gp)
fs_o <- fillStrokeGrob(t_o, gp = gp)

svglite("text.svg")
grid.draw(t_o)
dev.off()

svglite("fill.svg")
grid.draw(f_o)
dev.off()

svglite("fill_stroke.svg")
grid.draw(fs_o)
dev.off()

svglite("stroke.svg")
grid.draw(s_o)
dev.off()

agg_png("fill.png")
grid.draw(f_o)
dev.off()

agg_png("fill_stroke.png")
grid.draw(fs_o)
dev.off()

agg_png("stroke.png")
grid.draw(s_o)
dev.off()

trevorld avatar Apr 28 '25 16:04 trevorld

You can't stroke/fill text since they are not converted to polygons in svglite. In the same vein you can't use text in a clipping mask in svglite

thomasp85 avatar Apr 28 '25 17:04 thomasp85

  • Okay, I had mistakenly thought since cairo_pdf() (seemingly) doesn't convert normal text to polygons (i.e. you can copy and paste text from the pdf) but does convert stroke/fill text to polygons then it might also work that way in svglite() too. Good to know what the (current) limitations are.

trevorld avatar Apr 30 '25 19:04 trevorld