Skip to content

Commit

Permalink
Add switch etc
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Mar 13, 2024
1 parent 693e413 commit 2613986
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 61 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ LinkingTo:
Config/Needs/website: ggplot2, devoid, magick, bench, tidyr, ggridges,
hexbin, sessioninfo, pkgdown, tidyverse/tidytemplate
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
SystemRequirements: freetype2, libpng, libtiff, libjpeg
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Added supported for new graphics enigine features: Groups, paths, luminance
masks, and glyphs
* Add a switch (defaults to on) to snapping rectangles to the pixel grid when
they are only drawn with fill (no stroke)
* Silence a bug in clang-ASAN that incorrectly reported sanitiser issues

# ragg 1.2.7
Expand Down
41 changes: 24 additions & 17 deletions R/agg_dev.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#' into a layout, but you find that the result appears to small, you can
#' increase the `scaling` argument to make everything appear bigger at the
#' same resolution.
#' @param snap_rect Should axis-aligned rectangles drawn with only fill snap to
#' the pixel grid. This will prevent anti-aliasing artifacts when two
#' rectangles are touching at their border.
#' @param bg Same as `background` for compatibility with old graphic device APIs
#'
#' @export
Expand All @@ -38,7 +41,7 @@
#'
agg_ppm <- function(filename = 'Rplot%03d.ppm', width = 480, height = 480,
units = 'px', pointsize = 12, background = 'white',
res = 72, scaling = 1, bg) {
res = 72, scaling = 1, snap_rect = TRUE, bg) {
if (environmentName(parent.env(parent.frame())) == "knitr" &&
deparse(sys.call(), nlines = 1, width.cutoff = 500) == 'dev(filename = filename, width = dim[1], height = dim[2], ...)') {
units <- 'in'
Expand All @@ -47,7 +50,8 @@ agg_ppm <- function(filename = 'Rplot%03d.ppm', width = 480, height = 480,
dim <- get_dims(width, height, units, res)
background <- if (missing(bg)) background else bg
.Call("agg_ppm_c", file, dim[1], dim[2], as.numeric(pointsize), background,
as.numeric(res), as.numeric(scaling), PACKAGE = 'ragg')
as.numeric(res), as.numeric(scaling), as.logical(snap_rect),
PACKAGE = 'ragg')
invisible()
}

Expand Down Expand Up @@ -78,7 +82,7 @@ agg_ppm <- function(filename = 'Rplot%03d.ppm', width = 480, height = 480,
#'
agg_png <- function(filename = 'Rplot%03d.png', width = 480, height = 480,
units = 'px', pointsize = 12, background = 'white',
res = 72, scaling = 1, bitsize = 8, bg) {
res = 72, scaling = 1, snap_rect = TRUE, bitsize = 8, bg) {
if (environmentName(parent.env(parent.frame())) == "knitr" &&
deparse(sys.call(), nlines = 1, width.cutoff = 500) == 'dev(filename = filename, width = dim[1], height = dim[2], ...)') {
units <- 'in'
Expand All @@ -90,8 +94,8 @@ agg_png <- function(filename = 'Rplot%03d.png', width = 480, height = 480,
dim <- get_dims(width, height, units, res)
background <- if (missing(bg)) background else bg
.Call("agg_png_c", file, dim[1], dim[2], as.numeric(pointsize), background,
as.numeric(res), as.numeric(scaling), as.integer(bitsize),
PACKAGE = 'ragg')
as.numeric(res), as.numeric(scaling), as.logical(snap_rect),
as.integer(bitsize), PACKAGE = 'ragg')
invisible()
}
#' Draw to a TIFF file
Expand Down Expand Up @@ -132,7 +136,8 @@ agg_png <- function(filename = 'Rplot%03d.png', width = 480, height = 480,
#'
agg_tiff <- function(filename = 'Rplot%03d.tiff', width = 480, height = 480,
units = 'px', pointsize = 12, background = 'white',
res = 72, scaling = 1, compression = 'none', bitsize = 8, bg) {
res = 72, scaling = 1, snap_rect = TRUE,
compression = 'none', bitsize = 8, bg) {
if (environmentName(parent.env(parent.frame())) == "knitr" &&
deparse(sys.call(), nlines = 1, width.cutoff = 500) == 'dev(filename = filename, width = dim[1], height = dim[2], ...)') {
units <- 'in'
Expand All @@ -155,8 +160,8 @@ agg_tiff <- function(filename = 'Rplot%03d.tiff', width = 480, height = 480,
dim <- get_dims(width, height, units, res)
background <- if (missing(bg)) background else bg
.Call("agg_tiff_c", file, dim[1], dim[2], as.numeric(pointsize), background,
as.numeric(res), as.numeric(scaling), as.integer(bitsize), compression,
encoding, PACKAGE = 'ragg')
as.numeric(res), as.numeric(scaling), as.logical(snap_rect),
as.integer(bitsize), compression, encoding, PACKAGE = 'ragg')
invisible()
}
#' Draw to a JPEG file
Expand Down Expand Up @@ -196,8 +201,8 @@ agg_tiff <- function(filename = 'Rplot%03d.tiff', width = 480, height = 480,
#'
agg_jpeg <- function(filename = 'Rplot%03d.jpeg', width = 480, height = 480,
units = 'px', pointsize = 12, background = 'white',
res = 72, scaling = 1, quality = 75, smoothing = FALSE,
method = 'slow', bg) {
res = 72, scaling = 1, snap_rect = TRUE, quality = 75,
smoothing = FALSE, method = 'slow', bg) {
if (environmentName(parent.env(parent.frame())) == "knitr" &&
deparse(sys.call(), nlines = 1, width.cutoff = 500) == 'dev(filename = filename, width = dim[1], height = dim[2], ...)') {
units <- 'in'
Expand All @@ -211,8 +216,8 @@ agg_jpeg <- function(filename = 'Rplot%03d.jpeg', width = 480, height = 480,
dim <- get_dims(width, height, units, res)
background <- if (missing(bg)) background else bg
.Call("agg_jpeg_c", file, dim[1], dim[2], as.numeric(pointsize), background,
as.numeric(res), as.numeric(scaling), as.integer(quality),
as.integer(smoothing), method, PACKAGE = 'ragg')
as.numeric(res), as.numeric(scaling), as.logical(snap_rect),
as.integer(quality), as.integer(smoothing), method, PACKAGE = 'ragg')
invisible()
}
#' Draw to a PNG file, modifying transparency on the fly
Expand All @@ -237,7 +242,7 @@ agg_jpeg <- function(filename = 'Rplot%03d.jpeg', width = 480, height = 480,
agg_supertransparent <- function(filename = 'Rplot%03d.png', width = 480,
height = 480, units = 'px', pointsize = 12,
background = 'white', res = 72, scaling = 1,
alpha_mod = 1, bg) {
snap_rect = TRUE, alpha_mod = 1, bg) {
if (environmentName(parent.env(parent.frame())) == "knitr" &&
deparse(sys.call(), nlines = 1, width.cutoff = 500) == 'dev(filename = filename, width = dim[1], height = dim[2], ...)') {
units <- 'in'
Expand All @@ -246,8 +251,8 @@ agg_supertransparent <- function(filename = 'Rplot%03d.png', width = 480,
dim <- get_dims(width, height, units, res)
background <- if (missing(bg)) background else bg
.Call("agg_supertransparent_c", file, dim[1], dim[2], as.numeric(pointsize),
background, as.numeric(res), as.numeric(scaling), as.double(alpha_mod),
PACKAGE = 'ragg')
background, as.numeric(res), as.numeric(scaling), as.logical(snap_rect),
as.double(alpha_mod), PACKAGE = 'ragg')
invisible()
}

Expand Down Expand Up @@ -286,7 +291,8 @@ agg_supertransparent <- function(filename = 'Rplot%03d.png', width = 480,
#' plot(as.raster(raster))
#'
agg_capture <- function(width = 480, height = 480, units = 'px', pointsize = 12,
background = 'white', res = 72, scaling = 1, bg) {
background = 'white', res = 72, scaling = 1,
snap_rect = TRUE, bg) {
if (environmentName(parent.env(parent.frame())) == "knitr" &&
deparse(sys.call(), nlines = 1, width.cutoff = 500) == 'dev(filename = filename, width = dim[1], height = dim[2], ...)') {
units <- 'in'
Expand All @@ -295,7 +301,8 @@ agg_capture <- function(width = 480, height = 480, units = 'px', pointsize = 12,
background <- if (missing(bg)) background else bg
name <- paste0('agg_capture_', sample(.Machine$integer.max, 1))
.Call("agg_capture_c", name, dim[1], dim[2], as.numeric(pointsize),
background, as.numeric(res), as.numeric(scaling), PACKAGE = 'ragg')
background, as.numeric(res), as.numeric(scaling), as.logical(snap_rect),
PACKAGE = 'ragg')
cap <- function(native = FALSE) {
current_dev = dev.cur()
if (names(current_dev)[1] == name) {
Expand Down
5 changes: 5 additions & 0 deletions man/agg_capture.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/agg_jpeg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/agg_png.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/agg_ppm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/agg_supertransparent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/agg_tiff.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/AggDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AggDevice {
double res_real;
double res_mod;
double lwd_mod;
bool snap_rect;

double x_trans;
double y_trans;
Expand Down Expand Up @@ -102,7 +103,7 @@ class AggDevice {

// Lifecycle methods
AggDevice(const char* fp, int w, int h, double ps, int bg, double res,
double scaling);
double scaling, bool snap);
virtual ~AggDevice();
virtual void newPage(unsigned int bg);
void close();
Expand Down Expand Up @@ -467,7 +468,8 @@ class AggDevice {
*/
template<class PIXFMT, class R_COLOR, typename BLNDFMT>
AggDevice<PIXFMT, R_COLOR, BLNDFMT>::AggDevice(const char* fp, int w, int h, double ps,
int bg, double res, double scaling) :
int bg, double res, double scaling,
bool snap) :
converter(),
width(w),
height(h),
Expand All @@ -483,6 +485,7 @@ AggDevice<PIXFMT, R_COLOR, BLNDFMT>::AggDevice(const char* fp, int w, int h, dou
res_real(res),
res_mod(scaling * res / 72.0),
lwd_mod(scaling * res / 96.0),
snap_rect(snap),
x_trans(0.0),
y_trans(0.0),
t_ren(),
Expand Down Expand Up @@ -1154,7 +1157,7 @@ void AggDevice<PIXFMT, R_COLOR, BLNDFMT>::drawRect(double x0, double y0, double
x1 += x_trans;
y0 += y_trans;
y1 += y_trans;
if (draw_fill && !draw_stroke) {
if (snap_rect && draw_fill && !draw_stroke) {
x0 = std::round(x0);
x1 = std::round(x1);
y0 = std::round(y0);
Expand Down
4 changes: 2 additions & 2 deletions src/AggDevice16.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class AggDevice16 : public AggDevice<PIXFMT, agg::rgba16, pixfmt_type_64> {
double alpha_mod;

AggDevice16(const char* fp, int w, int h, double ps, int bg, double res,
double scaling, double alpha_mod = 1.0) :
AggDevice<PIXFMT, agg::rgba16, pixfmt_type_64>(fp, w, h, ps, bg, res, scaling),
double scaling, bool snap, double alpha_mod = 1.0) :
AggDevice<PIXFMT, agg::rgba16, pixfmt_type_64>(fp, w, h, ps, bg, res, scaling, snap),
alpha_mod(alpha_mod)
{
this->background = convertColour(this->background_int);
Expand Down
4 changes: 2 additions & 2 deletions src/AggDeviceCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class AggDeviceCapture : public AggDevice<PIXFMT> {
bool can_capture = true;

AggDeviceCapture(const char* fp, int w, int h, double ps, int bg, double res,
double scaling) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling)
double scaling, bool snap) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling, snap)
{

}
Expand Down
4 changes: 2 additions & 2 deletions src/AggDeviceJpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class AggDeviceJpeg : public AggDevice<PIXFMT> {
int smoothing;
int method;
public:
AggDeviceJpeg(const char* fp, int w, int h, double ps, int bg, double res, double scaling, int qual, int smooth, int meth) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling),
AggDeviceJpeg(const char* fp, int w, int h, double ps, int bg, double res, double scaling, bool snap, int qual, int smooth, int meth) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling, snap),
quality(qual),
smoothing(smooth),
method(meth)
Expand Down
8 changes: 4 additions & 4 deletions src/AggDevicePng.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extern "C" {
template<class PIXFMT>
class AggDevicePng : public AggDevice<PIXFMT> {
public:
AggDevicePng(const char* fp, int w, int h, double ps, int bg, double res, double scaling) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling)
AggDevicePng(const char* fp, int w, int h, double ps, int bg, double res, double scaling, bool snap) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling, snap)
{

}
Expand Down Expand Up @@ -81,8 +81,8 @@ typedef AggDevicePng<pixfmt_type_32> AggDevicePngAlpha;
template<class PIXFMT>
class AggDevicePng16 : public AggDevice16<PIXFMT> {
public:
AggDevicePng16(const char* fp, int w, int h, double ps, int bg, double res, double scaling, double alpha_mod = 1.0) :
AggDevice16<PIXFMT>(fp, w, h, ps, bg, res, scaling, alpha_mod)
AggDevicePng16(const char* fp, int w, int h, double ps, int bg, double res, double scaling, bool snap, double alpha_mod = 1.0) :
AggDevice16<PIXFMT>(fp, w, h, ps, bg, res, scaling, snap, alpha_mod)
{

}
Expand Down
4 changes: 2 additions & 2 deletions src/AggDevicePpm.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
template<class PIXFMT>
class AggDevicePpm : public AggDevice<PIXFMT> {
public:
AggDevicePpm(const char* fp, int w, int h, double ps, int bg, double res, double scaling) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling)
AggDevicePpm(const char* fp, int w, int h, double ps, int bg, double res, double scaling, bool snap) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling, snap)
{

}
Expand Down
8 changes: 4 additions & 4 deletions src/AggDeviceTiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class AggDeviceTiff : public AggDevice<PIXFMT> {
int encoding;
public:
AggDeviceTiff(const char* fp, int w, int h, double ps, int bg, double res,
double scaling, int comp = 0, int enc = 0) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling),
double scaling, bool snap, int comp = 0, int enc = 0) :
AggDevice<PIXFMT>(fp, w, h, ps, bg, res, scaling, snap),
compression(comp),
encoding(enc)
{
Expand Down Expand Up @@ -83,8 +83,8 @@ class AggDeviceTiff16 : public AggDevice16<PIXFMT> {
int encoding;
public:
AggDeviceTiff16(const char* fp, int w, int h, double ps, int bg, double res,
double scaling, int comp = 0, int enc = 0) :
AggDevice16<PIXFMT>(fp, w, h, ps, bg, res, scaling),
double scaling, bool snap, int comp = 0, int enc = 0) :
AggDevice16<PIXFMT>(fp, w, h, ps, bg, res, scaling, snap),
compression(comp),
encoding(enc)
{
Expand Down
Loading

0 comments on commit 2613986

Please sign in to comment.