Richard Cotton
2015-Nov-05 11:43 UTC
[Rd] PDFs and SVGs containing rasterGrobs don't display correctly in some other software
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. 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. 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
Hadley Wickham
2015-Nov-05 13:04 UTC
[Rd] PDFs and SVGs containing rasterGrobs don't display correctly in some other software
On Thu, Nov 5, 2015 at 5:43 AM, 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. > > 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. > > 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?Yes - many viewers (esp. OS X preview) interpolate them even when the PDF requests that they shouldn't be. I worked through this with Paul Murrell a couple of months ago - we're reasonably certain that R is doing the right thing, it's the renderers that are failing. Hadley -- http://had.co.nz/
Henrik Bengtsson
2015-Nov-05 14:59 UTC
[Rd] PDFs and SVGs containing rasterGrobs don't display correctly in some other software
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]]