[Rd] PDFs and SVGs containing rasterGrobs don't display correctly in some other software

Henrik Bengtsson henrik.bengtsson at gmail.com
Thu Nov 5 15:59:29 CET 2015


On Nov 5, 2015 03:45, "Richard Cotton" <richierocks at gmail.com> wrote:
>
> I've just been trying to post-process some R-created heatmaps using
> Inkscape, but I can't get them to display correctly in that software.
>
> To reproduce:
>
> library(grid)
> r <- as.raster(matrix(runif(25), 5, 5))
> pdf("test.pdf")
> grid.newpage()
> grid.raster(r, interpolate = FALSE)
> dev.off()
>
> This figure should be a five by five block of grey squares.  This is
> what I see in the R GUI device window, and when I open test.pdf in
> Abode Reader or SumatraPDF.
>
> However, when I open the file in Inkscape or Firefox, each of the
> squares is blurred.

Not sure if it's related to how PNGs are rendered in Firefox, but it sounds
similar to the anti-aliasing used by Firefox when scaling up PNGs:

http://stackoverflow.com/questions/388492/firefox-blurs-an-image-when-scaled-through-external-css-or-inline-style

Firefox made anti-aliasing ("blurring") the new default many years ago.
After complaints they provided a way to control this behavior via (a
Mozilla-specific) CSS specification.  See above thread (which is wire old;
there might be more up-to-date reference out there).

If you output a PNG and insert it in a HTML img where you scale up the
width and height, do you see the same problem in Inkscape as in Firefox?

>
> I tried swapping grDevices::pdf for Cairo::CairoPDF and got the same
> result.  I also tried generating SVGs using both grDevices::svg and
> Cairo::CairoSVG, and also got the same result.

If you inspect your generated SVG, is the raster image embedded as a PNG?
Then SVG is unlikely to change the rendering property compared PNGs
(sic!).  However, you might be able to control the anti-aliasing property
of embedded rather images in SVGs using embedded CSS if you want a
self-contained SVG file that renders properly everywhere. You can also do a
self-contained HTML in a similar way.

Don't know if there exist a workaround for PDFs (didn't even know about it
until you mentioned it).

My $.02

Henrik
(from smartphone so sorry for typos etc)

>
> I see the same thing using R-devel and R3.2.2 under Windows, and (with
> an older version of R) under Linux.
>
> I don't know whether the problem is with grid's rasterGrobs, or how R
> writes PDF and SVG files, or with Inkscape and Firefox's method of
> rendering those files, or with me.  Please can you help me narrow it
> down.
>
> - Can you reproduce my problem?  That is, when you run the above code,
> does the file look OK in a PDF reader but blurry in Inkscape?
> - Do you know of any issues with using rasterGrobs in PDFs or SVGs?
>
> --
> Regards,
> Richie
>
> Learning R
> 4dpiecharts.com
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

	[[alternative HTML version deleted]]



More information about the R-devel mailing list