svglite/0000755000176200001440000000000015075730132011732 5ustar liggesuserssvglite/tests/0000755000176200001440000000000014672302377013104 5ustar liggesuserssvglite/tests/testthat/0000755000176200001440000000000015075730132014734 5ustar liggesuserssvglite/tests/testthat/test-text.R0000644000176200001440000000425615075363746017044 0ustar liggesuserslibrary(xml2) test_that("par(cex) affects strwidth", { xmlSVG({ plot.new() w1 <- strwidth("X") par(cex = 4) w4 <- strwidth("X") }) expect_equal(w4 / w1, 4, tolerance = 1e-2) }) test_that("cex affects strwidth", { inlineSVG(height = 7, width = 7, { plot.new() w1 <- strwidth("X") w4 <- strwidth("X", cex = 4) }) expect_equal(w4 / w1, 4, tolerance = 1e-2) }) test_that("special characters are escaped", { x <- xmlSVG({ plot.new() text(0.5, 0.5, "<&>") }) # xml_text unescapes for us - this still tests that the # file parses, which it wouldn't otherwise expect_equal(xml_text(xml_find_first(x, ".//text")), "<&>") }) test_that("utf-8 characters are preserved", { skip_on_os("windows") # skip because of xml2 buglet skip_if_not(l10n_info()$`UTF-8`) x <- xmlSVG({ plot.new() text(0.5, 0.5, "\u00b5") }) # xml_text unescapes for us - this still tests that the # file parses, which it wouldn't otherwise expect_equal(xml_text(xml_find_first(x, ".//text")), "\u00b5") }) test_that("special characters are escaped", { x <- xmlSVG({ plot.new() text(0.5, 0.5, "a", col = "#113399") }) # xml_text unescapes for us - this still tests that the # file parses, which it wouldn't otherwise expect_equal(style_attr(xml_find_first(x, ".//text"), "fill"), "#113399") }) test_that("default point size is 12", { x <- xmlSVG({ plot.new() text(0.5, 0.5, "a") }) expect_equal(style_attr(xml_find_first(x, ".//text"), "font-size"), "12.00px") }) test_that("cex generates fractional font sizes", { x <- xmlSVG({ plot.new() text(0.5, 0.5, "a", cex = 0.1) }) expect_equal(style_attr(xml_find_first(x, ".//text"), "font-size"), "1.20px") }) test_that("a symbol has width greater than 0", { xmlSVG({ plot.new() strw <- strwidth(expression(symbol("\042"))) }) expect_lt(.Machine$double.eps, strw) }) test_that("strwidth and height correctly computed", { svglite("test-text.svg", 4, 4, user_fonts = bitstream) on.exit(dev.off()) plot.new() str <- "This is a string" text(0.5, 0.5, str) h <- strheight(str) w <- strwidth(str) rect(0.5 - w / 2, 0.5 - h / 2, 0.5 + w / 2, 0.5 + h / 2) }) svglite/tests/testthat/test-path.R0000644000176200001440000000352015003643026016764 0ustar liggesuserslibrary(xml2) test_that("paths with winding fill mode", { x <- xmlSVG({ plot.new() polypath( c(.1, .1, .9, .9, NA, .2, .2, .8, .8), c(.1, .9, .9, .1, NA, .2, .8, .8, .2), col = rgb(0.5, 0.5, 0.5, 0.3), border = rgb(1, 0, 0, 0.3), rule = "winding" ) }) path <- xml_find_first(x, ".//path") expect_equal(style_attr(path, "fill-rule"), "nonzero") expect_equal(style_attr(path, "fill"), rgb(0.5, 0.5, 0.5)) expect_equal(style_attr(path, "fill-opacity"), "0.30") expect_equal(style_attr(path, "stroke"), rgb(1, 0, 0)) expect_equal(style_attr(path, "stroke-opacity"), "0.30") }) test_that("paths with evenodd fill mode", { x <- xmlSVG({ plot.new() polypath( c(.1, .1, .9, .9, NA, .2, .2, .8, .8), c(.1, .9, .9, .1, NA, .2, .8, .8, .2), col = rgb(0.5, 0.5, 0.5, 0.3), border = rgb(1, 0, 0, 0.3), rule = "evenodd" ) }) path <- xml_find_first(x, ".//path") expect_equal(style_attr(path, "fill-rule"), "evenodd") expect_equal(style_attr(path, "fill"), rgb(0.5, 0.5, 0.5)) expect_equal(style_attr(path, "fill-opacity"), "0.30") expect_equal(style_attr(path, "stroke"), rgb(1, 0, 0)) expect_equal(style_attr(path, "stroke-opacity"), "0.30") }) test_that("paths with no filling color", { x <- xmlSVG({ plot.new() polypath( c(.1, .1, .9, .9, NA, .2, .2, .8, .8), c(.1, .9, .9, .1, NA, .2, .8, .8, .2), col = NA, border = rgb(1, 0, 0, 0.3), rule = "winding" ) }) style <- xml_text(xml_find_first(x, "//style")) expect_match(style, "fill: none;") path <- xml_find_first(x, ".//path") expect_equal(style_attr(path, "fill-rule"), "nonzero") expect_equal(style_attr(path, "fill"), NA_character_) expect_equal(style_attr(path, "stroke"), rgb(1, 0, 0)) expect_equal(style_attr(path, "stroke-opacity"), "0.30") }) svglite/tests/testthat/test-clip.svg0000644000176200001440000000370615075362764017403 0ustar liggesusers Clipping svglite/tests/testthat/test-points.R0000644000176200001440000000261015003643026017343 0ustar liggesuserslibrary(xml2) test_that("radius is not given in points", { x <- xmlSVG({ plot.new() points(0.5, 0.5, cex = 20) text(0.5, 0.5, cex = 20) }) circle <- xml_find_all(x, ".//circle") expect_equal(xml_attr(circle, "r"), "54.00") }) test_that("points are given stroke and fill", { x <- xmlSVG({ plot.new() points(0.5, 0.5, pch = 21, col = "red", bg = "blue", cex = 20) }) circle <- xml_find_all(x, ".//circle") expect_equal(style_attr(circle, "stroke"), rgb(1, 0, 0)) expect_equal(style_attr(circle, "fill"), rgb(0, 0, 1)) }) test_that("points get alpha stroke and fill given stroke and fill", { x <- xmlSVG({ plot.new() points( 0.5, 0.5, pch = 21, col = rgb(1, 0, 0, 0.1), bg = rgb(0, 0, 1, 0.1), cex = 20 ) }) circle <- xml_find_all(x, ".//circle") expect_equal(style_attr(circle, "stroke"), rgb(1, 0, 0)) expect_equal(style_attr(circle, "stroke-opacity"), "0.10") expect_equal(style_attr(circle, "fill"), rgb(0, 0, 1)) expect_equal(style_attr(circle, "fill-opacity"), "0.10") }) test_that("points are given stroke and fill", { x <- xmlSVG({ plot.new() points(0.5, 0.5, pch = 21, col = "red", bg = NA, cex = 20) }) style <- xml_text(xml_find_first(x, "//style")) expect_match(style, "fill: none;") circle <- xml_find_all(x, ".//circle") expect_equal(style_attr(circle, "fill"), NA_character_) }) svglite/tests/testthat/test-text-fonts.R0000644000176200001440000000533415003643026020150 0ustar liggesuserstest_that("font sets weight/style", { x <- xmlSVG({ plot.new() text(0.5, seq(0.9, 0.1, length = 4), "a", font = 1:4) }) text <- xml_find_all(x, ".//text") skip_on_os("windows") # win-builder has issues with systemfonts ATM expect_equal(style_attr(text, "font-weight"), c(NA, "bold", NA, "bold")) expect_equal(style_attr(text, "font-style"), c(NA, NA, "italic", "italic")) }) test_that("metrics are computed for different weight/style", { x <- xmlSVG(user_fonts = fontquiver::font_families("Bitstream Vera"), { plot.new() text(1, 1, "text") text(1, 1, "text", font = 2) text(1, 1, "text", font = 4) }) text <- xml_find_all(x, ".//text") x <- xml_attr(text, "textLength") expect_false(any(x[2:3] == x[1])) }) test_that("symbol font family is 'Symbol'", { symbol_font <- alias_lookup()["symbol"] matched_symbol_font <- paste0('"', match_family(symbol_font), '"') x <- xmlSVG({ plot(c(0, 2), c(0, 2), type = "n", axes = FALSE, xlab = "", ylab = "") text(1, 1, expression(symbol("\042"))) }) text <- xml_find_all(x, ".//text") expect_equal(style_attr(text, "font-family"), matched_symbol_font) }) test_that("throw on malformed alias", { expect_snapshot(validate_aliases(list(mono = letters), list()), error = TRUE) skip_on_cran() skip_on_os(c("windows", "linux")) expect_snapshot(validate_aliases(list(sans = "foobar"), list())) }) test_that("fonts are aliased", { matched <- match_family("cursive") x <- xmlSVG( system_fonts = list(sans = matched), user_fonts = list(mono = fontquiver::font_faces("Bitstream Vera", "Mono")), { plot.new() text(0.5, 0.1, "a", family = "serif") text(0.5, 0.5, "a", family = "sans") text(0.5, 0.9, "a", family = "mono") } ) text <- xml_find_all(x, ".//text") families <- style_attr(text, "font-family") expect_false(families[[1]] == '"serif"') expect_true(all( families[2:3] == paste0('"', c(matched, "Bitstream Vera Sans Mono"), '"') )) }) test_that("metrics are computed for different fonts", { aliases <- fontquiver::font_families("Bitstream Vera") x <- xmlSVG(user_fonts = aliases, { plot.new() text(0.5, 0.9, "a", family = "serif") text(0.5, 0.9, "a", family = "mono") }) text <- xml_find_all(x, ".//text") x_attr <- xml_attr(text, "textLength") y_attr <- xml_attr(text, "y") expect_false(x_attr[[1]] == x_attr[[2]]) expect_false(y_attr[[1]] == y_attr[[2]]) }) test_that("unicode characters in plotmath are handled", { rho <- as.name("\u03c1") expr <- call("*", rho, rho) x <- xmlSVG({ plot.new() text(0.5, 0.5, as.expression(expr)) }) text <- xml_find_all(x, ".//text") x_attr <- as.double(xml_attr(text, "x")) expect_true(x_attr[2] - x_attr[1] > 0) }) svglite/tests/testthat/test-scale-text.html0000644000176200001440000000045214672302426020654 0ustar liggesusers This test requires the Liberation Sans family to be installed on the system.

foobar
svglite/tests/testthat/test-scale.R0000644000176200001440000000251415003643026017121 0ustar liggesuserslibrary("grid") # Tests requiring manual oversight are registered as such. They # specify files that should be opened with multiple browsers to make # sure the SVG appearance is consistent. Use open_manual_tests() after # running testthat to open them in your default browser. test_that("text has correct dimensions", { register_manual_test("test-scale-text.html") ttf <- fontquiver::font("Liberation", "Sans", "Regular")$ttf w <- systemfonts::string_width("foobar", path = ttf, index = 0L, res = 1e4) * 72 / 1e4 h <- max(vapply( systemfonts::glyph_info("foobar", path = ttf, index = 0L, res = 1e4)$bbox, `[[`, numeric(1), "ymax" )) * 72 / 1e4 svglite( "test-scale-text.svg", width = w / 72, height = h / 72, user_fonts = fontquiver::font_families("Liberation") ) on.exit(dev.off()) grid.newpage() grid.rect( 0, 1, width = unit(w, "bigpts"), height = unit(h, "bigpts"), hjust = 0, vjust = 1, gp = gpar(col = "red", lwd = 1) ) grid.text("foobar", 0, 1, hjust = 0, vjust = 1, gp = gpar(fontsize = 12)) pushViewport(viewport()) }) test_that("lwd has correct dimensions", { x <- xmlSVG({ plot.new() segments(0, 1, 0, 0, lwd = 96 / 72) }) line <- xml_find_all(x, "//line") expect_equal(xml_attr(line, "style"), "stroke-width: 1.00;") }) svglite/tests/testthat/test-raster.R0000644000176200001440000000046315003643026017333 0ustar liggesuserslibrary(xml2) test_that("raster exists", { x <- xmlSVG( { image(matrix(runif(64), nrow = 8), useRaster = TRUE) }, standalone = TRUE ) ns <- xml_ns(x) img <- xml_attr( xml_find_all(x, ".//d1:image", ns = ns), "xlink:href", ns = ns ) expect_gt(nchar(img), 1000) }) svglite/tests/testthat/test-clip.R0000644000176200001440000000123114672302426016763 0ustar liggesuserstest_that("regression test for no clipping", { svglite("test-no-clip.svg", 4, 4, user_fonts = bitstream) on.exit(dev.off()) mini_plot(c(-1, 1), c(-1, 1), asp = 1, type = "n") rect(-0.5, -0.5, 0.5, 0.5, col = "blue") text(0, 0.5, "Clipping", cex = 2, srt = 30) abline(h = 0.5, col = "red") }) test_that("regression test for clipping", { svglite("test-clip.svg", 4, 4, user_fonts = bitstream) on.exit(dev.off()) mini_plot(c(-1, 1), c(-1, 1), asp = 1, type = "n") clip(-1, 0, -1, 0) rect(-0.5, -0.5, 0.5, 0.5, col = "blue") clip(0, 1, 0, 1) text(0, 0.5, "Clipping", cex = 2, srt = 30) clip(-1, 0, 0, 1) abline(h = 0.5, col = "red") }) svglite/tests/testthat/test-rect.R0000644000176200001440000000122214705727404016775 0ustar liggesuserslibrary(xml2) test_that("rects equivalent regardless of direction", { x1 <- xmlSVG({ plot.new() rect(0.2, 0.2, 0.8, 0.8) }) x2 <- xmlSVG({ plot.new() rect(0.8, 0.8, 0.2, 0.2) }) rect1 <- xml_attrs(xml_find_all(x1, "./g/rect")[[1]]) rect2 <- xml_attrs(xml_find_all(x2, "./g/rect")[[1]]) expect_equal(rect1, rect2) }) test_that("fill and stroke colors", { x <- xmlSVG({ plot.new() rect(0.2, 0.2, 0.8, 0.8, col = "blue", border = "red") }) rectangle <- xml_find_all(x, "./g/g/rect")[[1]] expect_equal(style_attr(rectangle, "fill"), rgb(0, 0, 1)) expect_equal(style_attr(rectangle, "stroke"), rgb(1, 0, 0)) }) svglite/tests/testthat/test-ids.R0000644000176200001440000000115615003643026016612 0ustar liggesuserstest_that("ids are assigned as expecter", { sd <- svgstring() plot(1:10, 1:10) plot(1:10, 1:10) dev.off() svg <- sd() expect_identical(svg[1], svg[2]) sd <- svgstring(id = "test") plot(1:10, 1:10) plot(1:10, 1:10) dev.off() svg <- sd() expect_identical(svg[1], svg[2]) expect_true(grepl("id='test'", svg[1])) sd <- svgstring(id = c("test", "test2")) plot(1:10, 1:10) plot(1:10, 1:10) expect_snapshot_warning(plot(1:10, 1:10)) dev.off() svg <- sd() expect_true(grepl("id='test'", svg[1])) expect_true(grepl("id='test2'", svg[2])) expect_false(grepl("id='test", svg[3])) }) svglite/tests/testthat/test-colour.R0000644000176200001440000000115314672302426017342 0ustar liggesuserslibrary(xml2) test_that("transparent blacks are written", { x <- xmlSVG({ plot.new() points(0.5, 0.5, col = rgb(0, 0, 0, 0.25)) points(0.5, 0.5, col = rgb(0, 0, 0, 0.50)) points(0.5, 0.5, col = rgb(0, 0, 0, 0.75)) }) circle <- xml_find_all(x, ".//circle") expect_equal(style_attr(circle, "stroke"), rep("#000000", 3)) expect_equal(style_attr(circle, "stroke-opacity"), c("0.25", "0.50", "0.75")) }) test_that("transparent colours are not written", { x <- xmlSVG({ plot.new() points(0.5, 0.5, col = NA) }) circle <- xml_find_all(x, ".//circle") expect_length(circle, 0) }) svglite/tests/testthat/test-output.R0000644000176200001440000000127515003643026017375 0ustar liggesuserstest_that("different string and file output produce identical svg", { ## 1. Write to a file f1 <- tempfile() svglite(f1) plot(1:5) dev.off() out1 <- readLines(f1) ## 2. Write to a string stream s <- svgstring() plot(1:5) dev.off() out2 <- strsplit(s(), "\n")[[1]] expect_equal(out1, out2) }) test_that("intermediate outputs are always valid svg if always_valid=TRUE", { path <- tempfile() svglite(path, always_valid = TRUE) expect_valid_svg <- function() { expect_no_error(xml2::read_xml(path)) } mini_plot(1:10) expect_valid_svg() rect(2, 2, 3, 3) expect_valid_svg() segments(5, 5, 6, 6) expect_valid_svg() dev.off() expect_valid_svg() }) svglite/tests/testthat/test-lines.R0000644000176200001440000000776115003643026017155 0ustar liggesuserslibrary(xml2) test_that("segments don't have fill", { x <- xmlSVG({ plot.new() segments(0.5, 0.5, 1, 1) }) style <- xml_text(xml_find_first(x, "//style")) expect_match(style, "fill: none;") expect_equal(style_attr(xml_find_first(x, ".//line"), "fill"), NA_character_) }) test_that("lines don't have fill", { x <- xmlSVG({ plot.new() lines(c(0.5, 1, 0.5), c(0.5, 1, 1)) }) expect_equal( style_attr(xml_find_first(x, ".//polyline"), "fill"), NA_character_ ) }) test_that("polygons do have fill", { x <- xmlSVG({ plot.new() polygon(c(0.5, 1, 0.5), c(0.5, 1, 1), col = "red", border = "blue") }) polygon <- xml_find_first(x, ".//polygon") expect_equal(style_attr(polygon, "fill"), rgb(1, 0, 0)) expect_equal(style_attr(polygon, "stroke"), rgb(0, 0, 1)) }) test_that("polygons without border", { x <- xmlSVG({ plot.new() polygon(c(0.5, 1, 0.5), c(0.5, 1, 1), col = "red", border = NA) }) polygon <- xml_find_first(x, ".//polygon") expect_equal(style_attr(polygon, "fill"), rgb(1, 0, 0)) expect_equal(style_attr(polygon, "stroke"), "none") }) test_that("blank lines are omitted", { x <- xmlSVG(mini_plot(1:3, lty = "blank", type = "l")) expect_equal(length(xml_find_all(x, "//polygon")), 0) }) test_that("lines lty becomes stroke-dasharray", { expect_equal(dash_array(lty = 1), NA_integer_) expect_equal(dash_array(lty = 2), c(4, 4)) expect_equal(dash_array(lty = 3), c(1, 3)) expect_equal(dash_array(lty = 4), c(1, 3, 4, 3)) expect_equal(dash_array(lty = 5), c(7, 3)) expect_equal(dash_array(lty = 6), c(2, 2, 6, 2)) expect_equal(dash_array(lty = "1F"), c(1, 15)) expect_equal(dash_array(lty = "1234"), c(1, 2, 3, 4)) }) test_that("stroke-dasharray scales with lwd > 1", { expect_equal(dash_array(lty = 2, lwd = 1), c(4, 4)) expect_equal(dash_array(lty = 2, lwd = 1 / 2), c(4, 4)) expect_equal(dash_array(lty = 2, lwd = 1.1), c(4.4, 4.4)) expect_equal(dash_array(lty = 2, lwd = 2), c(8, 8)) }) test_that("line end shapes", { x1 <- xmlSVG({ plot.new() lines(c(0.3, 0.7), c(0.5, 0.5), lwd = 15, lend = "round") }) x2 <- xmlSVG({ plot.new() lines(c(0.3, 0.7), c(0.5, 0.5), lwd = 15, lend = "butt") }) x3 <- xmlSVG({ plot.new() lines(c(0.3, 0.7), c(0.5, 0.5), lwd = 15, lend = "square") }) style <- xml_text(xml_find_first(x1, "//style")) expect_match(style, "stroke-linecap: round;") expect_equal( style_attr(xml_find_first(x1, ".//polyline"), "stroke-linecap"), NA_character_ ) expect_equal( style_attr(xml_find_first(x2, ".//polyline"), "stroke-linecap"), "butt" ) expect_equal( style_attr(xml_find_first(x3, ".//polyline"), "stroke-linecap"), "square" ) }) test_that("line join shapes", { x1 <- xmlSVG({ plot.new() lines(c(0.3, 0.5, 0.7), c(0.1, 0.9, 0.1), lwd = 15, ljoin = "round") }) x2 <- xmlSVG({ plot.new() lines( c(0.3, 0.5, 0.7), c(0.1, 0.9, 0.1), lwd = 15, ljoin = "mitre", lmitre = 10 ) }) x3 <- xmlSVG({ plot.new() lines( c(0.3, 0.5, 0.7), c(0.1, 0.9, 0.1), lwd = 15, ljoin = "mitre", lmitre = 4 ) }) x4 <- xmlSVG({ plot.new() lines(c(0.3, 0.5, 0.7), c(0.1, 0.9, 0.1), lwd = 15, ljoin = "bevel") }) style <- xml_text(xml_find_first(x1, "//style")) expect_match(style, "stroke-linejoin: round;") expect_match(style, "stroke-miterlimit: 10.00;") expect_equal( style_attr(xml_find_all(x1, ".//polyline"), "stroke-linejoin"), NA_character_ ) expect_equal( style_attr(xml_find_all(x2, ".//polyline"), "stroke-linejoin"), "miter" ) expect_equal( style_attr(xml_find_all(x2, ".//polyline"), "stroke-miterlimit"), NA_character_ ) expect_equal( style_attr(xml_find_all(x3, ".//polyline"), "stroke-linejoin"), "miter" ) expect_equal( style_attr(xml_find_all(x3, ".//polyline"), "stroke-miterlimit"), "4.00" ) expect_equal( style_attr(xml_find_all(x4, ".//polyline"), "stroke-linejoin"), "bevel" ) }) svglite/tests/testthat/test-text.svg0000644000176200001440000000262215075362764017434 0ustar liggesusers This is a string svglite/tests/testthat/test-scale-text.svg0000644000176200001440000000223615075362764020522 0ustar liggesusers foobar svglite/tests/testthat/_snaps/0000755000176200001440000000000015075361012016214 5ustar liggesuserssvglite/tests/testthat/_snaps/text-fonts.md0000644000176200001440000000114215075362764020666 0ustar liggesusers# throw on malformed alias Code validate_aliases(list(mono = letters), list()) Condition Error in `FUN()`: ! `alias` must be a single string, not a character vector. --- Code validate_aliases(list(sans = "foobar"), list()) Condition Warning: System font "foobar" not found. Closest match is "Helvetica" Output $system $system$sans [1] "Helvetica" $system$serif [1] "Times" $system$mono [1] "Courier" $system$symbol [1] "Symbol" $user list() svglite/tests/testthat/_snaps/ids.md0000644000176200001440000000010215075362764017325 0ustar liggesusers# ids are assigned as expecter No id supplied for page no 3 svglite/tests/testthat/test-devSVG.R0000644000176200001440000000533015003643026017167 0ustar liggesuserslibrary(xml2) style_attr <- function(nodes, attr) { style <- xml_attr(nodes, "style") ifelse( grepl(sprintf("%s: [^;]*;", attr), style), gsub(sprintf(".*%s: ([^;]*);.*", attr), "\\1", style), NA_character_ ) } test_that("adds default background", { x <- xmlSVG(plot.new()) expect_equal(style_attr(xml_find_first(x, ".//rect"), "fill"), "#FFFFFF") }) test_that("adds background set by device driver", { x <- xmlSVG(plot.new(), bg = "red") expect_equal(style_attr(xml_find_first(x, ".//rect"), "fill"), rgb(1, 0, 0)) }) test_that("default background respects par", { x <- xmlSVG({ par(bg = "red") plot.new() }) expect_equal(style_attr(xml_find_first(x, ".//rect"), "fill"), rgb(1, 0, 0)) }) test_that("if bg is transparent in par(), use device driver background", { x <- xmlSVG( { par(bg = NA) plot.new() }, bg = "blue" ) style <- xml_text(xml_find_first(x, "//style")) expect_match(style, "fill: none;") expect_equal(style_attr(xml_find_first(x, ".//rect"), "fill"), rgb(0, 0, 1)) }) test_that("creating multiple pages is identical to creating multiple individual svgs", { set.seed(42) df <- data.frame(x = rnorm(20), y = rnorm(20)) plot_one <- function() plot(df$x, df$y) plot_two <- function() plot(df$x, df$y + 10) # strings s_multiple <- svgstring() plot_one() plot_two() dev.off() s_1 <- svgstring() plot_one() dev.off() s_2 <- svgstring() plot_two() dev.off() # index also in s_x to drop the class attribute expect_length(s_multiple(), 2L) expect_identical(s_multiple()[1L], s_1()[1L], label = "svgstring first plot") expect_identical(s_multiple()[2L], s_2()[1L], label = "svgstring second plot") # same with devices dir <- tempdir() f_multiple <- file.path(dir, "test-multiple-%03d.svg") f_single_1 <- file.path(dir, "test-single-1.svg") f_single_2 <- file.path(dir, "test-single-2.svg") f_multiple_1 <- sprintf(f_multiple, 1L) f_multiple_2 <- sprintf(f_multiple, 2L) on.exit(file.remove(f_multiple_1, f_multiple_2, f_single_1, f_single_2)) svglite(f_multiple) plot_one() plot_two() dev.off() svglite(f_single_1) plot_one() dev.off() svglite(f_single_2) plot_two() dev.off() expect_identical( readLines(f_multiple_1), readLines(f_single_1), label = "svglite first plot" ) expect_identical( readLines(f_multiple_2), readLines(f_single_2), label = "svglite second plot" ) }) test_that("ensure text leading white space will be rendered", { x <- xmlSVG(plot.new()) expect_true( grepl( "white-space: pre", xml_text(xml_find_first(x, ".//defs/style[@type = 'text/css']")) ) ) expect_equal(style_attr(xml_find_first(x, ".//rect"), "fill"), "#FFFFFF") }) svglite/tests/testthat/test-no-clip.svg0000644000176200001440000000303215075362764020005 0ustar liggesusers Clipping svglite/tests/testthat/helper-manual.R0000644000176200001440000000002415003643026017601 0ustar liggesusersinit_manual_tests() svglite/tests/testthat/helper-style.R0000644000176200001440000000064314672302426017502 0ustar liggesusersstyle_attr <- function(nodes, attr) { style <- xml2::xml_attr(nodes, "style") ifelse( grepl(sprintf("%s: [^;]*;", attr), style), gsub(sprintf(".*%s: ([^;]*);.*", attr), "\\1", style), NA_character_ ) } dash_array <- function(...) { x <- xmlSVG(mini_plot(1:3, ..., type = "l")) dash <- style_attr(xml2::xml_find_first(x, "//polyline"), "stroke-dasharray") as.numeric(strsplit(dash, ",")[[1]]) } svglite/tests/testthat/helper-aliases.R0000644000176200001440000000007115003643026017747 0ustar liggesusersbitstream <- fontquiver::font_families("Bitstream Vera") svglite/tests/testthat.R0000644000176200001440000000056614672302426015071 0ustar liggesusers# This file is part of the standard setup for testthat. # It is recommended that you do not modify it. # # Where should you do additional test configuration? # Learn more about the roles of various files in: # * https://r-pkgs.org/tests.html # * https://testthat.r-lib.org/reference/test_package.html#special-files library(testthat) library(svglite) test_check("svglite") svglite/MD50000644000176200001440000001020215075730132012235 0ustar liggesusersebd4c61e00cf14197805014d1a62a5ff *DESCRIPTION 5ef37ad5ca67e690e519f8b583b14e27 *NAMESPACE d922d10482913b19e812a4cb6ed5fffa *NEWS.md ecc10e4e7c8b069f169b88f4b32dbbc3 *R/SVG.R 1cffcfacb96b32f07c1975b43ff35087 *R/aaa.R 8f75c1ad19b049d9b7d071aad81d88d7 *R/cpp11.R 5cbf3e673d905d3a217805fb992ee6cb *R/fonts.R 799bdb3344a8c0c98ff29fa5f7055f01 *R/import-standalone-obj-type.R 62545d8f97ad53b42ec2efcbde7de6bc *R/import-standalone-types-check.R ca837fce6dbaf857776372aca2175701 *R/inlineSVG.R cde3edd535e59a4af1a4609167c3680a *R/svglite-package.R 62816279dfceadd824221f4bceb5ca94 *R/utils.R 81e688255ff08ff176644871fb409eec *README.md 8e5e85290a4fa268af5c6cf9e8360dc2 *build/vignette.rds 248976c59504edd8120c60d2f45e7d65 *inst/doc/scaling.Rmd 0838a100e8c5dc42589a1c7f82564ccd *inst/doc/scaling.html 255de159ca5640aed549615c3e3a8f35 *man/add_web_fonts.Rd ac04add3779e55e62e1cde3c1fc239c3 *man/create_svgz.Rd bfd3a61bee9f119ff2534b0b3aebbca3 *man/editSVG.Rd c4e52de0dfd38f9d9f30fdb2a92b39ae *man/figures/README-unnamed-chunk-3-1.png cb1e46f469cfbbbde29c8b5113e1d789 *man/figures/lifecycle-archived.svg c0d2e5a54f1fa4ff02bf9533079dd1f7 *man/figures/lifecycle-defunct.svg 391f696f961e28914508628a7af31b74 *man/figures/lifecycle-deprecated.svg 691b1eb2aec9e1bec96b79d11ba5e631 *man/figures/lifecycle-experimental.svg 952b59dc07b171b97d5d982924244f61 *man/figures/lifecycle-maturing.svg 27b879bf3677ea76e3991d56ab324081 *man/figures/lifecycle-questioning.svg ed42e3fbd7cc30bc6ca8fa9b658e24a8 *man/figures/lifecycle-stable.svg bf2f1ad432ecccee3400afe533404113 *man/figures/lifecycle-superseded.svg 4fef49c80ca83ae59abc53e235cc4fe7 *man/figures/logo.png 52c8a056fd265be4d1203cefb1b0adb0 *man/figures/logo.svg d242ae4b91e1304f9007df879a464d05 *man/font_face.Rd 76e4a25b79d39c88c819075d5d38790e *man/htmlSVG.Rd d21ef75f59e47f821779428a21fd6e8a *man/reexports.Rd 255cb508a9de0a43596088b07c429902 *man/stringSVG.Rd 06888bf131c5ad540b549437b13868af *man/svglite-package.Rd 8c030b30547f45a0a5247ce0a3dd76ce *man/svglite.Rd 9c116402a6690abb2b1a1ead2e6aa9e8 *man/svgstring.Rd 255698eee7f2dc4c4165f55490ced341 *man/xmlSVG.Rd 5f05efe55b70273f72fc1174e7da90c4 *src/Makevars 5f05efe55b70273f72fc1174e7da90c4 *src/Makevars.ucrt b48a6378658af3ebc24c61d69c272a9c *src/Makevars.win c8e7bef2bdcd4b8c0bcbc5e07a1bf0f0 *src/SvgStream.cpp c1d2aec3507604217a05f1cf736c21c5 *src/SvgStream.h e068a75315145a7eb6911dbb44863a3b *src/cpp11.cpp fc599de71b35fc755f801f970f1770be *src/devSVG.cpp 3ca7236a09d486462e6c63c161c5d489 *src/svglite_types.h e26b8b3bfef1f45f93d853c8136e437e *src/tinyformat.h 1c23035463477982cbec3812461a2161 *src/utils.h 22d0a3a305e1336c51f1370f37355484 *tests/testthat.R d89ff23900d693cbbfbc2af3f1d01856 *tests/testthat/_snaps/ids.md e8c4c2263ba9acc4a0a4cbdb2c18bee8 *tests/testthat/_snaps/text-fonts.md 4b977e8cd0ecd1b44cc6cb1428409c07 *tests/testthat/helper-aliases.R 854f7aaf45384083d6a45100ba2de187 *tests/testthat/helper-manual.R c1cdd21f147d378ac892863b9835cf61 *tests/testthat/helper-style.R 619f65a742e24b6f058b90f8bf699b60 *tests/testthat/test-clip.R 7c56eee3288e336dc8f23139ba230b2a *tests/testthat/test-clip.svg f3dfe22c8a14149d7751dabbfa588b7d *tests/testthat/test-colour.R 76c26eb1299e37f32a14bb3f2105e502 *tests/testthat/test-devSVG.R 1501a5666a7feaff9c69298134875c0a *tests/testthat/test-ids.R abf9ad896a0ae70753fb0b8055ce2b40 *tests/testthat/test-lines.R 41037a728c56781feac4cd502d1c5953 *tests/testthat/test-no-clip.svg e9966b9ec968e63d4123b70aef740a6a *tests/testthat/test-output.R 0167d20936957195149ba2c0fb5b471f *tests/testthat/test-path.R eb2a79c89b7e275afc06d22870d4d05d *tests/testthat/test-points.R 06434fa9ad6ea7594a843d916ba22ef1 *tests/testthat/test-raster.R 4841572de20113a0fc9f4f506bbb712b *tests/testthat/test-rect.R e60a5c429f6025cb2c3cd93f0c2fbb5c *tests/testthat/test-scale-text.html 330d2e41bf3adf432937a2d5f3626d6e *tests/testthat/test-scale-text.svg 58f0536853293d1341c5f96eb92105f2 *tests/testthat/test-scale.R dcf06b932318fe01bf96a3c925378c2e *tests/testthat/test-text-fonts.R 274fe46802bbb5d4769e4aca0669b06e *tests/testthat/test-text.R 330fc0ae162416fb4831fe1eb0db6e31 *tests/testthat/test-text.svg 94df8e63522d0eb03c44e1b3406a51c1 *tools/winlibs.R 248976c59504edd8120c60d2f45e7d65 *vignettes/scaling.Rmd svglite/R/0000755000176200001440000000000015075362760012143 5ustar liggesuserssvglite/R/import-standalone-types-check.R0000644000176200001440000003047415003643026020137 0ustar liggesusers# Standalone file: do not edit by hand # Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-types-check.R # Generated by: usethis::use_standalone("r-lib/rlang", "types-check") # ---------------------------------------------------------------------- # # --- # repo: r-lib/rlang # file: standalone-types-check.R # last-updated: 2023-03-13 # license: https://unlicense.org # dependencies: standalone-obj-type.R # imports: rlang (>= 1.1.0) # --- # # ## Changelog # # 2024-08-15: # - `check_character()` gains an `allow_na` argument (@martaalcalde, #1724) # # 2023-03-13: # - Improved error messages of number checkers (@teunbrand) # - Added `allow_infinite` argument to `check_number_whole()` (@mgirlich). # - Added `check_data_frame()` (@mgirlich). # # 2023-03-07: # - Added dependency on rlang (>= 1.1.0). # # 2023-02-15: # - Added `check_logical()`. # # - `check_bool()`, `check_number_whole()`, and # `check_number_decimal()` are now implemented in C. # # - For efficiency, `check_number_whole()` and # `check_number_decimal()` now take a `NULL` default for `min` and # `max`. This makes it possible to bypass unnecessary type-checking # and comparisons in the default case of no bounds checks. # # 2022-10-07: # - `check_number_whole()` and `_decimal()` no longer treat # non-numeric types such as factors or dates as numbers. Numeric # types are detected with `is.numeric()`. # # 2022-10-04: # - Added `check_name()` that forbids the empty string. # `check_string()` allows the empty string by default. # # 2022-09-28: # - Removed `what` arguments. # - Added `allow_na` and `allow_null` arguments. # - Added `allow_decimal` and `allow_infinite` arguments. # - Improved errors with absent arguments. # # # 2022-09-16: # - Unprefixed usage of rlang functions with `rlang::` to # avoid onLoad issues when called from rlang (#1482). # # 2022-08-11: # - Added changelog. # # nocov start # Scalars ----------------------------------------------------------------- .standalone_types_check_dot_call <- .Call check_bool <- function(x, ..., allow_na = FALSE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x) && .standalone_types_check_dot_call(ffi_standalone_is_bool_1.0.7, x, allow_na, allow_null)) { return(invisible(NULL)) } stop_input_type( x, c("`TRUE`", "`FALSE`"), ..., allow_na = allow_na, allow_null = allow_null, arg = arg, call = call ) } check_string <- function(x, ..., allow_empty = TRUE, allow_na = FALSE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { is_string <- .rlang_check_is_string( x, allow_empty = allow_empty, allow_na = allow_na, allow_null = allow_null ) if (is_string) { return(invisible(NULL)) } } stop_input_type( x, "a single string", ..., allow_na = allow_na, allow_null = allow_null, arg = arg, call = call ) } .rlang_check_is_string <- function(x, allow_empty, allow_na, allow_null) { if (is_string(x)) { if (allow_empty || !is_string(x, "")) { return(TRUE) } } if (allow_null && is_null(x)) { return(TRUE) } if (allow_na && (identical(x, NA) || identical(x, na_chr))) { return(TRUE) } FALSE } check_name <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { is_string <- .rlang_check_is_string( x, allow_empty = FALSE, allow_na = FALSE, allow_null = allow_null ) if (is_string) { return(invisible(NULL)) } } stop_input_type( x, "a valid name", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } IS_NUMBER_true <- 0 IS_NUMBER_false <- 1 IS_NUMBER_oob <- 2 check_number_decimal <- function(x, ..., min = NULL, max = NULL, allow_infinite = TRUE, allow_na = FALSE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (missing(x)) { exit_code <- IS_NUMBER_false } else if (0 == (exit_code <- .standalone_types_check_dot_call( ffi_standalone_check_number_1.0.7, x, allow_decimal = TRUE, min, max, allow_infinite, allow_na, allow_null ))) { return(invisible(NULL)) } .stop_not_number( x, ..., exit_code = exit_code, allow_decimal = TRUE, min = min, max = max, allow_na = allow_na, allow_null = allow_null, arg = arg, call = call ) } check_number_whole <- function(x, ..., min = NULL, max = NULL, allow_infinite = FALSE, allow_na = FALSE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (missing(x)) { exit_code <- IS_NUMBER_false } else if (0 == (exit_code <- .standalone_types_check_dot_call( ffi_standalone_check_number_1.0.7, x, allow_decimal = FALSE, min, max, allow_infinite, allow_na, allow_null ))) { return(invisible(NULL)) } .stop_not_number( x, ..., exit_code = exit_code, allow_decimal = FALSE, min = min, max = max, allow_na = allow_na, allow_null = allow_null, arg = arg, call = call ) } .stop_not_number <- function(x, ..., exit_code, allow_decimal, min, max, allow_na, allow_null, arg, call) { if (allow_decimal) { what <- "a number" } else { what <- "a whole number" } if (exit_code == IS_NUMBER_oob) { min <- min %||% -Inf max <- max %||% Inf if (min > -Inf && max < Inf) { what <- sprintf("%s between %s and %s", what, min, max) } else if (x < min) { what <- sprintf("%s larger than or equal to %s", what, min) } else if (x > max) { what <- sprintf("%s smaller than or equal to %s", what, max) } else { abort("Unexpected state in OOB check", .internal = TRUE) } } stop_input_type( x, what, ..., allow_na = allow_na, allow_null = allow_null, arg = arg, call = call ) } check_symbol <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_symbol(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "a symbol", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } check_arg <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_symbol(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "an argument name", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } check_call <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_call(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "a defused call", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } check_environment <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_environment(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "an environment", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } check_function <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_function(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "a function", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } check_closure <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_closure(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "an R function", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } check_formula <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_formula(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "a formula", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } # Vectors ----------------------------------------------------------------- # TODO: Figure out what to do with logical `NA` and `allow_na = TRUE` check_character <- function(x, ..., allow_na = TRUE, allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_character(x)) { if (!allow_na && any(is.na(x))) { abort( sprintf("`%s` can't contain NA values.", arg), arg = arg, call = call ) } return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "a character vector", ..., allow_null = allow_null, arg = arg, call = call ) } check_logical <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is_logical(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "a logical vector", ..., allow_na = FALSE, allow_null = allow_null, arg = arg, call = call ) } check_data_frame <- function(x, ..., allow_null = FALSE, arg = caller_arg(x), call = caller_env()) { if (!missing(x)) { if (is.data.frame(x)) { return(invisible(NULL)) } if (allow_null && is_null(x)) { return(invisible(NULL)) } } stop_input_type( x, "a data frame", ..., allow_null = allow_null, arg = arg, call = call ) } # nocov end svglite/R/fonts.R0000644000176200001440000002376315075361003013417 0ustar liggesusersr_font_families <- c("sans", "serif", "mono", "symbol") r_font_faces <- c("plain", "bold", "italic", "bolditalic", "symbol") alias_lookup <- function() { if (.Platform$OS.type == "windows") { serif_font <- "Times New Roman" symbol_font <- "Standard Symbols L" } else { serif_font <- "Times" symbol_font <- "Symbol" } c( sans = "Arial", serif = serif_font, mono = "Courier", symbol = symbol_font ) } #' @importFrom systemfonts font_info match_family <- function(font, bold = FALSE, italic = FALSE) { font_info(font, weight = if (bold) "bold" else "normal", italic = italic)$family[1] } validate_aliases <- function(system_fonts, user_fonts) { system_fonts <- compact(lapply(system_fonts, compact)) user_fonts <- compact(lapply(user_fonts, compact)) system_fonts <- lapply(system_fonts, validate_system_alias) user_fonts <- ilapply(user_fonts, validate_user_alias) aliases <- c(names(system_fonts), names(user_fonts)) if (any(duplicated(aliases))) { cli::cli_abort(c( "Cannot provided multiple fonts with the same alias", i = "Problematic aliases: {unique(aliases[duplicated(aliases)])}" )) } # Add missing system fonts for base families missing_aliases <- setdiff(r_font_families, aliases) system_fonts[missing_aliases] <- lapply( alias_lookup()[missing_aliases], match_family ) list( system = system_fonts, user = user_fonts ) } validate_system_alias <- function(alias) { check_string(alias, allow_empty = FALSE) matched <- match_family(alias) if (alias != matched) { cli::cli_warn( "System font {.val {alias}} not found. Closest match is {.val {matched}}" ) } matched } is_user_alias <- function(x) { is.list(x) && (is_scalar_character(x$file) || is_scalar_character(x$ttf)) && (is_scalar_character(x$alias) || is_scalar_character(x$name)) } validate_user_alias <- function(default_name, family) { if (!all(names(family) %in% r_font_faces)) { cli::cli_abort( "{.arg family} must can only include elements named {r_font_faces}" ) } is_alias_object <- vapply(family, is_user_alias, logical(1)) is_alias_plain <- vapply(family, is_scalar_character, logical(1)) is_valid_alias <- is_alias_object | is_alias_plain if (any(!is_valid_alias)) { cli::cli_abort( "The following faces are invalid for {.val {default_name}}: {.val {names(family)[!is_valid_alias]}}" ) } names <- ifelse(is_alias_plain, default_name, family) names <- lapply_if(names, is_alias_object, function(obj) { obj$alias %||% obj$name }) files <- lapply_if(family, is_alias_object, function(obj) { obj$file %||% obj$ttf }) file_exists <- vapply(files, file.exists, logical(1)) if (any(!file_exists)) { missing <- unlist(files)[!file_exists] cli::cli_abort("Could not find the following font file{?s}: {missing}") } zip(list(name = names, file = files)) } #' Create a font-face specification #' #' Webfonts in SVG and HTML can either be specified manually using the #' `@font-face` at-rule, or imported from e.g. Google Fonts using the `@import` #' at-rule. `font_face()` helps you create a valid `@font-face` block for the #' `web_fonts` argument in [svglite()] and [svgstring()] functions. #' #' @param family The font family name this font should respond to. #' @param woff2,woff,ttf,otf URLs to the font in different formats. At #' least one must be given. Best browser support is provided by the woff #' format. #' @param eot,svg `r lifecycle::badge("deprecated")` #' @param local One or more font names that local installations of the font may #' have. If a local font is found with either of the given names it will be #' used and no download will happen. #' @param weight An optional value for the `font-weight` descriptor #' @param style An optional value for the `font-style` descriptor #' @param range An optional value for the `unicode-range` descriptor Will give #' the range of unicode values that this font will support #' @param variant An optional value for the `font-variant` descriptor #' @param stretch An optional value for the `font-stretch` descriptor #' @param feature_setting An optional value for the `font-feature-settings` #' descriptor It is recommended to avoid using this if possible #' @param variation_setting An optional value for the `font-variation-settings` #' descriptor. #' @param embed Should the font data be embedded directly in the SVG #' #' @return A character string with the `@font-face` block. #' #' @export #' @examples #' font_face( #' family = "MyHelvetica", #' ttf = "MgOpenModernaBold.ttf", #' local = c("Helvetica Neue Bold", "HelveticaNeue-Bold"), #' weight = "bold" #' ) #' font_face <- function( family, woff2 = NULL, woff = NULL, ttf = NULL, otf = NULL, eot = deprecated(), svg = deprecated(), local = NULL, weight = NULL, style = NULL, range = NULL, variant = NULL, stretch = NULL, feature_setting = NULL, variation_setting = NULL, embed = FALSE ) { if (lifecycle::is_present(eot)) { lifecycle::deprecate_stop("2.2.0", "font_face(eot)") } if (lifecycle::is_present(svg)) { lifecycle::deprecate_stop("2.2.0", "font_face(svg)") } sources <- list( local = local, woff2 = woff2, woff = woff, otf = otf, ttf = ttf ) sources <- lapply(seq_along(sources), function(i) { location <- sources[[i]] type <- names(sources)[i] if (embed) { if (type == "local") { location <- systemfonts::font_info(location)$path[1] type <- tolower(tools::file_ext(location)) if (!type %in% c("woff2", "woff", "otf", "ttf")) { stop(paste0("Unsupported file type for embedding: ", type)) } } mime <- switch( type, woff2 = "font/woff2", woff = "font/woff", otf = "font/otf", ttf = "font/ttf" ) location <- paste0( "data:", mime, ";charset=utf-8;base64,", base64enc::base64encode(location) ) } else { location <- paste0('"', location, '"') } format <- switch( type, local = '', woff2 = ' format("woff2")', woff = ' format("woff")', otf = ' format("opentype")', ttf = ' format("truetype")' ) prefix <- if (type == "local") "local" else "url" paste0(prefix, "(", location, ")", format) }) sources <- unlist(sources) if (length(sources) == 0) { cli::cli_abort("At least one font source must be given") } # fmt: skip x <- c( ' @font-face {\n', ' font-family: "', family, '";\n', ' src: ', paste0(paste(sources, collapse = ",\n "), ';\n'), if (!is.null(range)) paste0( ' unicode-range: ', range[1], ';\n'), if (!is.null(variant)) paste0( ' font-variant: ', variant[1], ';\n'), if (!is.null(feature_setting)) paste0( ' font-feature-settings: ', feature_setting[1], ';\n'), if (!is.null(variation_setting)) paste0( ' font-variation-settings: ', variation_setting[1], ';\n'), if (!is.null(stretch)) paste0( ' font-stretch: ', stretch[1], ';\n'), if (!is.null(weight)) paste0( ' font-weight: ', weight[1], ';\n'), if (!is.null(style)) paste0( ' font-style: ', style[1], ';\n'), ' }' ) x <- paste(x, collapse = "") class(x) <- c("font_face", "character") x } #' @export print.font_face <- function(x, ...) { cat(x) invisible(x) } is_font_face <- function(x) inherits(x, "font_face") validate_web_fonts <- function(x) { if (length(x) == 0) { return("") } x <- lapply(x, function(f) { if (is_font_face(f)) { return(f) } if (grepl("^\\s*@import", f)) { return(sub("^\\s*@import", " @import", f)) } if (grepl("^https?://", f) || grepl("^data:", f)) { return(paste0(' @import url("', f, '");')) } if (grepl("^", svg))) { if (is_string) { cli::cli_warn( "SVG was not created by svglite. Not inserting font import" ) } else { cli::cli_warn( "{.file {f}} was not created by svglite. Not inserting font import" ) } next } style <- grep("\n"; (*stream) << "\n"; (*stream) << "fill) != 0) { write_style_col(stream, "fill", gc->fill); } else { write_style_col(stream, "fill", dd->startfill); } write_style_end(stream); (*stream) << "/>\n"; // Initialise clipping - make sure the stored clipping is bogus to force // svg_clip to do its thing svgd->clipx0 = R_PosInf; svgd->clipy0 = R_NegInf; svgd->clipx1 = R_NegInf; svgd->clipy1 = R_PosInf; svgd->is_inited = true; svg_clip(0, dd->right, dd->bottom, 0, dd); svgd->stream->flush(); svgd->pageno++; } void svg_close(pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (svgd->is_inited) { svgd->stream->finish(true); } delete(svgd); } void svg_line(double x1, double y1, double x2, double y2, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (!svgd->is_inited || svgd->is_recording_clip) { return; } SvgStreamPtr stream = svgd->stream; (*stream) << "current_mask); write_style_begin(stream); write_style_linetype(stream, gc, svgd->scaling, true); write_style_end(stream); (*stream) << " />\n"; stream->flush(); } void svg_poly(int n, double *x, double *y, int filled, const pGEcontext gc, pDevDesc dd, const char* node_name) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (n == 0 || !svgd->is_inited || (!filled && svgd->is_recording_clip)) { return; } SvgStreamPtr stream = svgd->stream; if (svgd->is_recording_clip) { (*stream) << "M " << x[0] << ',' << y[0] << ' '; for (int i = 1; i < n; i++) { (*stream) << "L " << x[i] << ',' << y[i] << ' '; } stream->put('Z'); return; } (*stream) << "<" << node_name << " points='"; for (int i = 0; i < n; i++) { (*stream) << x[i] << ',' << y[i] << ' '; } stream->put('\''); write_attr_mask(stream, svgd->current_mask); write_style_begin(stream); write_style_linetype(stream, gc, svgd->scaling, true); if (filled) { write_style_fill(stream, gc); } write_style_end(stream); (*stream) << " />\n"; stream->flush(); } void svg_polyline(int n, double *x, double *y, const pGEcontext gc, pDevDesc dd) { svg_poly(n, x, y, 0, gc, dd, "polyline"); } void svg_polygon(int n, double *x, double *y, const pGEcontext gc, pDevDesc dd) { svg_poly(n, x, y, 1, gc, dd, "polygon"); } void svg_path(double *x, double *y, int npoly, int *nper, Rboolean winding, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (!svgd->is_inited) { return; } SvgStreamPtr stream = svgd->stream; // Create path data if (!svgd->is_recording_clip) { (*stream) << "put('Z'); } if (svgd->is_recording_clip) { return; } // Finish path data stream->put('\''); write_attr_mask(stream, svgd->current_mask); write_style_begin(stream); // Specify fill rule write_style_str(stream, "fill-rule", winding ? "nonzero" : "evenodd", true); write_style_fill(stream, gc); write_style_linetype(stream, gc, svgd->scaling); write_style_end(stream); (*stream) << " />\n"; stream->flush(); } double svg_strwidth(const char *str, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; std::string family_name; FontSettings font = get_font( gc->fontfamily, gc->fontface, svgd->user_aliases, svgd->system_aliases, family_name ); double width = 0.0; int error = textshaping::string_width( str, font, gc->ps * gc->cex * svgd->scaling, 72.0, 1, &width ); if (error != 0) { width = 0.0; } return width; } void svg_rect(double x0, double y0, double x1, double y1, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (!svgd->is_inited) { return; } SvgStreamPtr stream = svgd->stream; if (svgd->is_recording_clip) { (*stream) << "M " << x0 << ',' << y0 << " L " << x0 << ',' << y1 << " L " << x1 << ',' << y1 << " L " << x1 << ',' << y0; stream->put('Z'); return; } // x and y give top-left position (*stream) << "current_mask); write_style_begin(stream); write_style_linetype(stream, gc, svgd->scaling, true); write_style_fill(stream, gc); write_style_end(stream); (*stream) << " />\n"; stream->flush(); } void svg_circle(double x, double y, double r, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (!svgd->is_inited) { return; } SvgStreamPtr stream = svgd->stream; if (svgd->is_recording_clip) { (*stream) << "M " << x-r << ',' << y << " a " << r << ',' << r << " 0 1,1 " << r*2 << ",0 " << " a " << r << ',' << r << " 0 1,1 " << -r*2 << ",0 "; stream->put('Z'); return; } (*stream) << "current_mask); write_style_begin(stream); write_style_linetype(stream, gc, svgd->scaling, true); write_style_fill(stream, gc); write_style_end(stream); (*stream) << " />\n"; stream->flush(); } inline void mat_mult(double* t, double a, double b, double c, double d, double tx, double ty) { double a_ = t[0] * a + t[2] * b; double b_ = t[1] * a + t[3] * b; double c_ = t[0] * c + t[2] * d; double d_ = t[1] * c + t[3] * d; t[4] = t[0] * tx + t[2] * ty + t[4]; t[5] = t[1] * tx + t[3] * ty + t[5]; t[0] = a_; t[1] = b_; t[2] = c_; t[3] = d_; } void svg_text(double x, double y, const char *str, double rot, double hadj, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (!svgd->is_inited) { return; } SvgStreamPtr stream = svgd->stream; std::string family_name; FontSettings font = get_font( gc->fontfamily, gc->fontface, svgd->user_aliases, svgd->system_aliases, family_name ); if (svgd->is_recording_clip) { std::vector loc_buffer; std::vector id_buffer; std::vector cluster_buffer; std::vector font_buffer; std::vector fallback_buffer; std::vector scaling_buffer; int err = textshaping::string_shape( str, font, gc->ps * gc->cex * svgd->scaling, 72.0, loc_buffer, id_buffer, cluster_buffer, font_buffer, fallback_buffer, scaling_buffer ); if (err == 0) { double x_adj = 0; if (hadj != 0) { textshaping::string_width( str, font, gc->ps * gc->cex * svgd->scaling, 72.0, 1, &x_adj ); x_adj *= -hadj; } // transformation to apply: // Apply x_adj // Reflect along x // Apply rotation // translate by x, y double transform[6] = {1, 0, 0, 1, x, y}; if (rot != 0.0) { rot = -6.2831853072 * rot / 360.0; mat_mult(transform, std::cos(rot), std::sin(rot), -std::sin(rot), std::cos(rot), 0.0, 0.0); } if (x_adj != 0.0) { mat_mult(transform, 1.0, 0.0, 0.0, 1.0, x_adj, 0.0); } mat_mult(transform, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0); for (size_t i = 0; i < loc_buffer.size(); ++i) { mat_mult(transform, 1.0, 0.0, 0.0, 1.0, loc_buffer[i].x, loc_buffer[i].y); bool no_outline = true; (*stream) << get_glyph_path( id_buffer[i], transform, fallback_buffer[font_buffer[i]].file, fallback_buffer[font_buffer[i]].index, gc->ps * gc->cex * svgd->scaling, &no_outline ); mat_mult(transform, 1.0, 0.0, 0.0, 1.0, -loc_buffer[i].x, -loc_buffer[i].y); } } return; } (*stream) << "cex * gc->ps; if (hadj == 0.5) { write_attr_str(stream, "text-anchor", "middle"); } else if (hadj == 1) { write_attr_str(stream, "text-anchor", "end"); } write_attr_mask(stream, svgd->current_mask); write_style_begin(stream); write_style_fontsize(stream, fontsize * svgd->scaling, true); int weight = get_font_weight(font.file, font.index); if (weight != 400) { if (weight == 700) { write_style_str(stream, "font-weight", "bold"); } else { write_style_int(stream, "font-weight", weight); } } if (is_italic(gc->fontface)) write_style_str(stream, "font-style", "italic"); if (!is_black(gc->col)) write_style_col(stream, "fill", gc->col); family_name = "\"" + family_name + "\""; write_style_str(stream, "font-family", family_name.c_str()); if (font.n_features > 0) { (*stream) << " font-feature-settings: "; for (int i = 0; i < font.n_features; ++i) { std::string feature = ""; feature += font.features[i].feature[0]; feature += font.features[i].feature[1]; feature += font.features[i].feature[2]; feature += font.features[i].feature[3]; (*stream) << "\"" << feature << "\" " << font.features[i].setting; (*stream) << (i == font.n_features - 1 ? ";" : ","); } } write_style_end(stream); if (svgd->fix_text_size) { double width = svg_strwidth(str, gc, dd); (*stream) << " textLength='" << width << "px'"; (*stream) << " lengthAdjust='spacingAndGlyphs'"; } stream->put('>'); write_escaped(stream, str); (*stream) << ""; stream->put('\n'); stream->flush(); } void svg_size(double *left, double *right, double *bottom, double *top, pDevDesc dd) { *left = dd->left; *right = dd->right; *bottom = dd->bottom; *top = dd->top; } void svg_raster(unsigned int *raster, int w, int h, double x, double y, double width, double height, double rot, Rboolean interpolate, const pGEcontext gc, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (!svgd->is_inited || svgd->is_recording_clip) { return; } SvgStreamPtr stream = svgd->stream; if (height < 0) height = -height; std::string base64_str = raster_to_string(raster, w, h, width, height, interpolate); (*stream) << "current_mask); if (!interpolate) { write_attr_str(stream, "image-rendering", "pixelated"); } if( rot != 0 ){ (*stream) << tfm::format(" transform='rotate(%0.0f,%.2f,%.2f)'", -1.0 * rot, x, y); } (*stream) << " xlink:href='data:image/png;base64," << base64_str << '\''; (*stream) << "/>"; stream->put('\n'); stream->flush(); } SEXP svg_set_pattern(SEXP pattern, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (Rf_isNull(pattern)) { return Rf_ScalarInteger(-1); } int key = svgd->pattern_cache_next_id; svgd->pattern_cache_next_id++; #if R_GE_version >= 13 SvgStreamPtr stream = svgd->stream; std::string extend = "spreadMethod="; // Cache current clipping and break out of clipping group bool was_clipping = svgd->is_clipping; std::string old_clipid = svgd->clipid; double clipx0 = svgd->clipx0; double clipx1 = svgd->clipx1; double clipy0 = svgd->clipy0; double clipy1 = svgd->clipy1; if (was_clipping) { (*stream) << "\n"; } svgd->set_clipping(false); (*stream) << "\n"; switch(R_GE_patternType(pattern)) { case R_GE_linearGradientPattern: switch(R_GE_linearGradientExtend(pattern)) { case R_GE_patternExtendNone: ; case R_GE_patternExtendPad: extend += "'pad'"; break; case R_GE_patternExtendReflect: extend += "'reflect'"; break; case R_GE_patternExtendRepeat: extend += "'repeat"; break; } (*stream) << "\n"; for (int i = 0; i < R_GE_linearGradientNumStops(pattern); ++i) { int col = R_GE_linearGradientColour(pattern, i); (*stream) << " \n"; } (*stream) << "\n"; break; case R_GE_radialGradientPattern: switch(R_GE_radialGradientExtend(pattern)) { case R_GE_patternExtendNone: ; case R_GE_patternExtendPad: extend += "'pad'"; break; case R_GE_patternExtendReflect: extend += "'reflect'"; break; case R_GE_patternExtendRepeat: extend += "'repeat"; break; } (*stream) << "\n"; for (int i = 0; i < R_GE_radialGradientNumStops(pattern); ++i) { int col = R_GE_radialGradientColour(pattern, i); (*stream) << " \n"; } (*stream) << "\n"; break; case R_GE_tilingPattern: (*stream) << "\n\n"; int old_mask = svgd->current_mask; svgd->current_mask = -1; SEXP R_fcall = PROTECT(Rf_lang1(R_GE_tilingPatternFunction(pattern))); Rf_eval(R_fcall, R_GlobalEnv); UNPROTECT(1); svgd->current_mask = old_mask; if (svgd->is_clipping) { (*stream) << "\n"; } svgd->set_clipping(false); (*stream) << "\n\n"; break; } (*stream) << "\n"; // Resume old clipping if it was happening if (was_clipping) { (*stream) << "clipid = old_clipid; svgd->clipx0 = clipx0; svgd->clipx1 = clipx1; svgd->clipy0 = clipy0; svgd->clipy1 = clipy1; write_attr_clip(stream, svgd->clipid); (*stream) << ">\n"; svgd->set_clipping(true); } #endif svgd->pattern_cache.insert(key); return Rf_ScalarInteger(key); } void svg_release_pattern(SEXP ref, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (Rf_isNull(ref)) { svgd->pattern_cache.clear(); return; } unsigned int key = INTEGER(ref)[0]; auto it = svgd->pattern_cache.find(key); // Check if path exists if (it != svgd->pattern_cache.end()) { svgd->pattern_cache.erase(it); } } SEXP svg_set_clip_path(SEXP path, SEXP ref, pDevDesc dd) { int key; if (Rf_isNull(path)) { return Rf_ScalarInteger(-1); } SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (Rf_isNull(ref)) { key = svgd->clip_cache_next_id; svgd->clip_cache_next_id++; } else { key = INTEGER(ref)[0]; if (key < 0) { return Rf_ScalarInteger(key); } } SvgStreamPtr stream = svgd->stream; if (svgd->is_clipping) { (*stream) << "\n"; } auto clip_cache_iter = svgd->clip_cache.find(key); // Check if path exists if (clip_cache_iter == svgd->clip_cache.end()) { bool new_clip_is_even_odd = false; #if R_GE_version >= 15 new_clip_is_even_odd = R_GE_clipPathFillRule(path) == R_GE_evenOddRule; #endif (*stream) << "\n"; (*stream) << " \n"; (*stream) << " \n \n"; (*stream) << "\n"; svgd->clip_cache.insert(key); } svgd->clipid = "-" + std::to_string(key); svgd->clipx0 = 0; svgd->clipx1 = 0; svgd->clipy0 = 0; svgd->clipy1 = 0; (*stream) << "clipid); (*stream) << ">\n"; svgd->set_clipping(true); return Rf_ScalarInteger(key); } void svg_release_clip_path(SEXP ref, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (Rf_isNull(ref)) { svgd->clip_cache.clear(); return; } int key = INTEGER(ref)[0]; if (key < 0) { return; } auto it = svgd->clip_cache.find(key); // Check if path exists if (it != svgd->clip_cache.end()) { svgd->clip_cache.erase(it); } } SEXP svg_set_mask(SEXP path, SEXP ref, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; int key; if (Rf_isNull(path)) { svgd->current_mask = -1; return Rf_ScalarInteger(-1); } if (Rf_isNull(ref)) { key = svgd->mask_cache_next_id; svgd->mask_cache_next_id++; } else { key = INTEGER(ref)[0]; if (key < 0) { svgd->current_mask = -1; return Rf_ScalarInteger(key); } } SvgStreamPtr stream = svgd->stream; auto mask_cache_iter = svgd->mask_cache.find(key); // Check if path exists if (mask_cache_iter == svgd->mask_cache.end()) { // Cache current clipping and break out of clipping group bool was_clipping = svgd->is_clipping; std::string old_clipid = svgd->clipid; double clipx0 = svgd->clipx0; double clipx1 = svgd->clipx1; double clipy0 = svgd->clipy0; double clipy1 = svgd->clipy1; if (was_clipping) { (*stream) << "\n"; } svgd->set_clipping(false); (*stream) << "\n"; #if R_GE_version >= 15 if (R_GE_maskType(path) == R_GE_alphaMask) { (*stream) << " \n"; } else { (*stream) << " \n"; } #else (*stream) << " \n"; #endif SEXP R_fcall = PROTECT(Rf_lang1(path)); Rf_eval(R_fcall, R_GlobalEnv); UNPROTECT(1); // Clipping may have happened above. End it before terminating mask if (svgd->is_clipping) { (*stream) << "\n"; } svgd->set_clipping(false); (*stream) << " \n"; (*stream) << "\n"; // Resume old clipping if it was happening if (was_clipping) { (*stream) << "clipid = old_clipid; svgd->clipx0 = clipx0; svgd->clipx1 = clipx1; svgd->clipy0 = clipy0; svgd->clipy1 = clipy1; write_attr_clip(stream, svgd->clipid); (*stream) << ">\n"; svgd->set_clipping(true); } svgd->mask_cache.insert(key); } svgd->current_mask = key; return Rf_ScalarInteger(key); } void svg_release_mask(SEXP ref, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (Rf_isNull(ref)) { svgd->mask_cache.clear(); return; } unsigned int key = INTEGER(ref)[0]; auto it = svgd->mask_cache.find(key); // Check if path exists if (it != svgd->mask_cache.end()) { svgd->mask_cache.erase(it); } } // Adapts stubs from `grDevices/src/devPS.c` as recommended by Paul Murrell // They quietly do nothing (without even a warning) but their existence // seems to prevent segfaults when users try to use these new features inline std::string composite_operator(int op) { std::string comp_op = "normal"; #if R_GE_version >= 15 switch(op) { case R_GE_compositeDestOver: case R_GE_compositeDestIn: case R_GE_compositeDestOut: case R_GE_compositeIn: case R_GE_compositeOut: case R_GE_compositeAtop: case R_GE_compositeXor: case R_GE_compositeSource: case R_GE_compositeDestAtop: cpp11::warning("Unsupported composition operator. Fallowing back to `over`"); case R_GE_compositeOver: comp_op = "normal"; break; case R_GE_compositeDest: comp_op = "destination"; break; // We can fake this as a blend mode case R_GE_compositeClear: comp_op = "clear"; break; // We can fake this as a blend mode case R_GE_compositeAdd: comp_op = "plus-lighter"; break; case R_GE_compositeSaturate: comp_op = "saturation"; break; case R_GE_compositeMultiply: comp_op = "multiply"; break; case R_GE_compositeScreen: comp_op = "screen"; break; case R_GE_compositeOverlay: comp_op = "overlay"; break; case R_GE_compositeDarken: comp_op = "darken"; break; case R_GE_compositeLighten: comp_op = "lighten"; break; case R_GE_compositeColorDodge: comp_op = "color-dodge"; break; case R_GE_compositeColorBurn: comp_op = "color-burn"; break; case R_GE_compositeHardLight: comp_op = "hard-light"; break; case R_GE_compositeSoftLight: comp_op = "soft-light"; break; case R_GE_compositeDifference: comp_op = "difference"; break; case R_GE_compositeExclusion: comp_op = "exclusion"; break; } #endif return comp_op; } SEXP svg_define_group(SEXP source, int op, SEXP destination, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; SvgStreamPtr stream = svgd->stream; int key = -1; #if R_GE_version >= 15 key = svgd->group_cache_next_id; svgd->group_cache_next_id++; // Cache current clipping and break out of clipping group bool was_clipping = svgd->is_clipping; std::string old_clipid = svgd->clipid; double clipx0 = svgd->clipx0; double clipx1 = svgd->clipx1; double clipy0 = svgd->clipy0; double clipy1 = svgd->clipy1; int temp_mask = svgd->current_mask; svgd->current_mask = -1; if (was_clipping) { (*stream) << "\n"; } svgd->set_clipping(false); (*stream) << "\n"; if (op == R_GE_compositeClear) { source = R_NilValue; destination = R_NilValue; op = R_GE_compositeOver; } else if (op == R_GE_compositeDest) { source = R_NilValue; op = R_GE_compositeOver; } bool is_simple = op == R_GE_compositeOver; std::string blend_op = composite_operator(op); (*stream) << " \n"; if (destination != R_NilValue) { SEXP R_fcall = PROTECT(Rf_lang1(destination)); Rf_eval(R_fcall, R_GlobalEnv); UNPROTECT(1); // Clipping may have happened above. End it before terminating mask if (svgd->is_clipping) { (*stream) << "\n"; } svgd->set_clipping(false); } if (source != R_NilValue) { if (!is_simple) { (*stream) << " \n"; } SEXP R_fcall1 = PROTECT(Rf_lang1(source)); Rf_eval(R_fcall1, R_GlobalEnv); UNPROTECT(1); // Clipping may have happened above. End it before terminating mask if (svgd->is_clipping) { (*stream) << "\n"; } svgd->set_clipping(false); if (!is_simple) { (*stream) << " \n"; } } (*stream) << " \n"; (*stream) << "\n"; // Resume old clipping if it was happening if (was_clipping) { (*stream) << "clipid = old_clipid; svgd->clipx0 = clipx0; svgd->clipx1 = clipx1; svgd->clipy0 = clipy0; svgd->clipy1 = clipy1; write_attr_clip(stream, svgd->clipid); (*stream) << ">\n"; svgd->set_clipping(true); } svgd->current_mask = temp_mask; svgd->group_cache.insert(key); #endif return Rf_ScalarInteger(key); } void svg_use_group(SEXP ref, SEXP trans, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; SvgStreamPtr stream = svgd->stream; if (Rf_isNull(ref)) { return; } int key = INTEGER(ref)[0]; if (key < 0) { cpp11::warning("Unknown group, %i", key); return; } auto it = svgd->group_cache.find(key); if (it == svgd->group_cache.end()) { cpp11::warning("Unknown group, %i", key); return; } bool has_transform = trans != R_NilValue; if (has_transform) { (*stream) << " \n"; } (*stream) << " \n"; if (has_transform) { (*stream) << " \n"; } return; } void svg_release_group(SEXP ref, pDevDesc dd) { SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; if (Rf_isNull(ref)) { svgd->group_cache.clear(); return; } unsigned int key = INTEGER(ref)[0]; auto it = svgd->group_cache.find(key); // Check if path exists if (it != svgd->group_cache.end()) { svgd->group_cache.erase(it); } } void svg_stroke(SEXP path, const pGEcontext gc, pDevDesc dd) { if (Rf_isNull(path)) { return; } SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; SvgStreamPtr stream = svgd->stream; // Create path data if (!svgd->is_recording_clip) { (*stream) << "current_mask); write_style_begin(stream); write_style_linetype(stream, gc, svgd->scaling, true); write_style_end(stream); (*stream) << " />\n"; stream->flush(); } void svg_fill(SEXP path, int rule, const pGEcontext gc, pDevDesc dd) { if (Rf_isNull(path)) { return; } SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; SvgStreamPtr stream = svgd->stream; // Create path data if (!svgd->is_recording_clip) { (*stream) << "current_mask); write_style_begin(stream); #if R_GE_version >= 15 // Specify fill rule write_style_str(stream, "fill-rule", rule == R_GE_nonZeroWindingRule ? "nonzero" : "evenodd", true); #endif write_style_fill(stream, gc); write_style_str(stream, "stroke", "none"); write_style_end(stream); (*stream) << " />\n"; stream->flush(); } void svg_fill_stroke(SEXP path, int rule, const pGEcontext gc, pDevDesc dd) { if (Rf_isNull(path)) { return; } SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; SvgStreamPtr stream = svgd->stream; // Create path data if (!svgd->is_recording_clip) { (*stream) << "current_mask); write_style_begin(stream); #if R_GE_version >= 15 // Specify fill rule write_style_str(stream, "fill-rule", rule == R_GE_nonZeroWindingRule ? "nonzero" : "evenodd", true); #endif write_style_fill(stream, gc); write_style_linetype(stream, gc, svgd->scaling); write_style_end(stream); (*stream) << " />\n"; stream->flush(); } void svg_glyph(int n, int *glyphs, double *x, double *y, SEXP font, double size, int colour, double rot, pDevDesc dd) { #if R_GE_version >= 16 SVGDesc *svgd = (SVGDesc*) dd->deviceSpecific; double cos_rot = 1.0; double sin_rot = 0.0; if (rot != 0.0) { double rot_rad = -6.2831853072 * rot / 360.0; cos_rot = std::cos(rot_rad); sin_rot = std::sin(rot_rad); } SvgStreamPtr stream = svgd->stream; size *= svgd->scaling; bool no_outline = false; if (!svgd->is_recording_clip) { (*stream) << "current_mask); write_style_begin(stream); write_style_col(stream, "fill", colour); write_style_end(stream); (*stream) << ">\n"; } for (int i = 0; i < n; ++i) { // Create path data double transform[6] = {1.0, 0.0, 0.0, 1.0, x[i], y[i]}; if (rot != 0) { mat_mult(transform, cos_rot, sin_rot, -sin_rot, cos_rot, 0.0, 0.0); } mat_mult(transform, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0); std::string p = get_glyph_path( glyphs[i], transform, R_GE_glyphFontFile(font), R_GE_glyphFontIndex(font), size, &no_outline ); if (no_outline) { if (svgd->is_recording_clip) continue; SEXP raster = PROTECT(get_glyph_raster( glyphs[i], R_GE_glyphFontFile(font), R_GE_glyphFontIndex(font), size, 300.0, colour )); if (!Rf_isNull(raster)) { SEXP r_size = PROTECT(Rf_getAttrib(raster, Rf_mkString("size"))); SEXP offset = PROTECT(Rf_getAttrib(raster, Rf_mkString("offset"))); double x_off = cos_rot * REAL(offset)[1] - sin_rot * (REAL(r_size)[0] - REAL(offset)[0]); double y_off = sin_rot * REAL(offset)[1] + cos_rot * (REAL(r_size)[0] - REAL(offset)[0]); svg_raster( (unsigned int*) (INTEGER(raster)), Rf_ncols(raster), Rf_nrows(raster), x[i] + x_off, y[i] + y_off, REAL(r_size)[1], REAL(r_size)[0], rot, (Rboolean) true, nullptr, dd ); UNPROTECT(2); } UNPROTECT(1); } else { if (p.empty()) continue; if (!svgd->is_recording_clip) { (*stream) << "\n"; } } if (!svgd->is_recording_clip) { (*stream) << "\n"; } stream->flush(); #endif } SEXP svg_capabilities(SEXP capabilities) { #if R_GE_version >= 15 // Pattern support SEXP pat = PROTECT(Rf_allocVector(INTSXP, 3)); INTEGER(pat)[0] = R_GE_linearGradientPattern; INTEGER(pat)[1] = R_GE_radialGradientPattern; INTEGER(pat)[2] = R_GE_tilingPattern; SET_VECTOR_ELT(capabilities, R_GE_capability_patterns, pat); UNPROTECT(1); // Clipping path support SET_VECTOR_ELT(capabilities, R_GE_capability_clippingPaths, Rf_ScalarInteger(1)); // Mask support SEXP masks = PROTECT(Rf_allocVector(INTSXP, 2)); INTEGER(masks)[0] = R_GE_alphaMask; INTEGER(masks)[1] = R_GE_luminanceMask; SET_VECTOR_ELT(capabilities, R_GE_capability_masks, masks); UNPROTECT(1); // Group composition SEXP compositing = PROTECT(Rf_allocVector(INTSXP, 16)); INTEGER(compositing)[0] = R_GE_compositeMultiply; INTEGER(compositing)[1] = R_GE_compositeScreen; INTEGER(compositing)[2] = R_GE_compositeOverlay; INTEGER(compositing)[3] = R_GE_compositeDarken; INTEGER(compositing)[4] = R_GE_compositeLighten; INTEGER(compositing)[5] = R_GE_compositeColorDodge; INTEGER(compositing)[6] = R_GE_compositeColorBurn; INTEGER(compositing)[7] = R_GE_compositeHardLight; INTEGER(compositing)[8] = R_GE_compositeSoftLight; INTEGER(compositing)[9] = R_GE_compositeDifference; INTEGER(compositing)[10] = R_GE_compositeExclusion; INTEGER(compositing)[11] = R_GE_compositeAdd; INTEGER(compositing)[12] = R_GE_compositeSaturate; INTEGER(compositing)[13] = R_GE_compositeOver; INTEGER(compositing)[14] = R_GE_compositeClear; INTEGER(compositing)[15] = R_GE_compositeDest; SET_VECTOR_ELT(capabilities, R_GE_capability_compositing, compositing); UNPROTECT(1); // Group transformation SET_VECTOR_ELT(capabilities, R_GE_capability_transformations, Rf_ScalarInteger(1)); // Path stroking and filling SET_VECTOR_ELT(capabilities, R_GE_capability_paths, Rf_ScalarInteger(1)); #endif #if R_GE_version >= 16 // Glyph rendering SET_VECTOR_ELT(capabilities, R_GE_capability_glyphs, Rf_ScalarInteger(1)); #endif return capabilities; } pDevDesc svg_driver_new(SvgStreamPtr stream, int bg, double width, double height, double pointsize, bool standalone, cpp11::list& aliases, const std::string& webfonts, const std::string& file, cpp11::strings id, bool fix_text_size, double scaling, bool always_valid) { pDevDesc dd = (DevDesc*) calloc(1, sizeof(DevDesc)); if (dd == NULL) return dd; dd->startfill = bg; dd->startcol = R_RGB(0, 0, 0); dd->startps = pointsize; dd->startlty = 0; dd->startfont = 1; dd->startgamma = 1; // Callbacks dd->activate = NULL; dd->deactivate = NULL; dd->close = svg_close; dd->clip = svg_clip; dd->size = svg_size; dd->newPage = svg_new_page; dd->line = svg_line; dd->text = svg_text; dd->strWidth = svg_strwidth; dd->rect = svg_rect; dd->circle = svg_circle; dd->polygon = svg_polygon; dd->polyline = svg_polyline; dd->path = svg_path; dd->mode = NULL; dd->metricInfo = svg_metric_info; dd->cap = NULL; dd->raster = svg_raster; #if R_GE_version >= 13 dd->setPattern = svg_set_pattern; dd->releasePattern = svg_release_pattern; dd->setClipPath = svg_set_clip_path; dd->releaseClipPath = svg_release_clip_path; dd->setMask = svg_set_mask; dd->releaseMask = svg_release_mask; #endif // UTF-8 support dd->wantSymbolUTF8 = (Rboolean) 1; dd->hasTextUTF8 = (Rboolean) 1; dd->textUTF8 = svg_text; dd->strWidthUTF8 = svg_strwidth; // Screen Dimensions in pts dd->left = 0; dd->top = 0; dd->right = width * 72; dd->bottom = height * 72; // Magic constants copied from other graphics devices // nominal character sizes in pts dd->cra[0] = 0.9 * pointsize * scaling; dd->cra[1] = 1.2 * pointsize * scaling; // character alignment offsets dd->xCharOffset = 0.4900; dd->yCharOffset = 0.3333; dd->yLineBias = 0.2; // inches per pt dd->ipr[0] = 1.0 / (72.0 * scaling); dd->ipr[1] = 1.0 / (72.0 * scaling); // Capabilities #if R_GE_version >= 15 dd->defineGroup = svg_define_group; dd->useGroup = svg_use_group; dd->releaseGroup = svg_release_group; dd->stroke = svg_stroke; dd->fill = svg_fill; dd->fillStroke = svg_fill_stroke; dd->capabilities = svg_capabilities; #endif #if R_GE_version >= 16 dd->glyph = svg_glyph; #endif dd->canClip = TRUE; #if R_GE_version >= 14 dd->deviceClip = TRUE; #endif dd->canHAdj = 1; dd->canChangeGamma = FALSE; dd->displayListOn = FALSE; dd->haveTransparency = 2; dd->haveRaster = 2; dd->haveTransparentBg = 3; /* background can be semi-transparent */ #if R_GE_version >= 13 dd->deviceVersion = 16; //R_GE_glyph; #endif dd->deviceSpecific = new SVGDesc(stream, standalone, aliases, webfonts, file, id, fix_text_size, scaling, always_valid); return dd; } void makeDevice(SvgStreamPtr stream, std::string bg_, double width, double height, double pointsize, bool standalone, cpp11::list& aliases, const std::string& webfonts, const std::string& file, cpp11::strings id, bool fix_text_size, double scaling, bool always_valid) { int bg = R_GE_str2col(bg_.c_str()); R_GE_checkVersionOrDie(R_GE_version); R_CheckDeviceAvailable(); BEGIN_SUSPEND_INTERRUPTS { pDevDesc dev = svg_driver_new(stream, bg, width, height, pointsize, standalone, aliases, webfonts, file, id, fix_text_size, scaling, always_valid); if (dev == NULL) cpp11::stop("Failed to start SVG device"); pGEDevDesc dd = GEcreateDevDesc(dev); GEaddDevice2(dd, "devSVG"); GEinitDisplayList(dd); } END_SUSPEND_INTERRUPTS; } [[cpp11::register]] bool svglite_(std::string file, std::string bg, double width, double height, double pointsize, bool standalone, cpp11::list aliases, std::string webfonts, cpp11::strings id, bool fix_text_size, double scaling, bool always_valid) { SvgStreamPtr stream(new SvgStreamFile(file, 1, always_valid)); makeDevice(stream, bg, width, height, pointsize, standalone, aliases, webfonts, file, id, fix_text_size, scaling, always_valid); return true; } [[cpp11::register]] cpp11::external_pointer svgstring_(cpp11::environment env, std::string bg, double width, double height, double pointsize, bool standalone, cpp11::list aliases, std::string webfonts, cpp11::strings id, bool fix_text_size, double scaling) { SvgStreamPtr stream(new SvgStreamString(env)); makeDevice(stream, bg, width, height, pointsize, standalone, aliases, webfonts, "", id, fix_text_size, scaling, true); SvgStreamString* strstream = static_cast(stream.get()); return {strstream->string_src(), false}; } [[cpp11::register]] std::string get_svg_content(cpp11::external_pointer p) { p->flush(); std::string svgstr = p->str(); // If the current SVG is empty, we also make the string empty // Otherwise append "" to make it a valid SVG if(!svgstr.empty()) { svgstr.append("\n"); } return svgstr; } svglite/src/Makevars.win0000644000176200001440000000042614672302426015016 0ustar liggesusersVERSION = 2.7.4 RWINLIB = ../windows/harfbuzz-${VERSION} PKG_CPPFLAGS = -I${RWINLIB}/include PKG_LIBS = -L${RWINLIB}/lib${R_ARCH}${CRT} -lpng -lz all: clean winlibs winlibs: "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" "../tools/winlibs.R" ${VERSION} clean: rm -f $(OBJECTS) svglite/src/SvgStream.h0000644000176200001440000001340015075362214014605 0ustar liggesusers#pragma once #include #include #include #include #include #include #include #include #include #include #include "utils.h" #include namespace svglite { namespace internal { template void write_double(T& stream, double data) { std::streamsize prec = stream.precision(); int newprec = std::fabs(data) >= 1 || data == 0. ? prec : std::ceil(-std::log10(std::fabs(data))) + 1; newprec = newprec > UINT8_MAX ? UINT8_MAX : newprec; stream << std::setprecision(newprec) << data << std::setprecision(prec); } }} // namespace svglite::internal class SvgStream { std::unordered_set clip_ids; bool clipping = false; public: bool has_clip_id(std::string id) { return clip_ids.find(id) != clip_ids.end(); } void set_clipping(bool clip) { clipping = clip; } void add_clip_id(std::string id) { clip_ids.insert(id); } void clear_clip_ids() { clip_ids.clear(); } bool is_clipping() {return clipping;} virtual ~SvgStream() {}; virtual void write(int data) = 0; virtual void write(double data) = 0; virtual void write(const char* data) = 0; virtual void write(const std::string& data) = 0; virtual void write(char data) = 0; virtual bool is_file_stream() = 0; void put(char data) { write(data); } virtual void flush() = 0; virtual void finish(bool close) = 0; }; template SvgStream& operator<<(SvgStream& object, const T& data) { object.write(data); return object; } class SvgStreamFile : public SvgStream { std::ofstream stream_; bool compress = false; std::string file = ""; bool always_valid = false; public: SvgStreamFile(const std::string& path, bool _always_valid = false) : always_valid(_always_valid) { std::string svgz_ext = path.size() > 5 ? path.substr(path.size() - 5) : ""; std::string gz_ext = path.size() > 3 ? path.substr(path.size() - 3) : ""; compress = iequals(svgz_ext, ".svgz") || iequals(gz_ext, ".gz"); file = R_ExpandFileName(path.c_str()); stream_.open(file.c_str()); if (stream_.fail()) cpp11::stop("cannot open stream %s", path.c_str()); stream_ << std::fixed << std::setprecision(2); } SvgStreamFile(const std::string& path, int pageno, bool _always_valid = false) : always_valid(_always_valid) { std::string svgz_ext = path.size() > 5 ? path.substr(path.size() - 5) : ""; std::string gz_ext = path.size() > 3 ? path.substr(path.size() - 3) : ""; compress = iequals(svgz_ext, ".svgz") || iequals(gz_ext, ".gz"); char buf[PATH_MAX+1]; snprintf(buf, PATH_MAX, path.c_str(), pageno); buf[PATH_MAX] = '\0'; file = R_ExpandFileName(buf); stream_.open(file.c_str()); if (stream_.fail()) cpp11::stop("cannot open stream %s", buf); stream_ << std::fixed << std::setprecision(2); } void write(int data) { stream_ << data; } void write(double data) { svglite::internal::write_double(stream_, data); } void write(const char* data) { stream_ << data; } void write(char data) { stream_ << data; } void write(const std::string& data) { stream_ << data; } bool is_file_stream() {return true; } // Adding a final newline here creates problems on Windows when // seeking back to original position. So we only write the newline // in finish() void flush() { if (!always_valid) { return; } #ifdef _WIN32 int offset = -12; #else int offset = -11; #endif if (is_clipping()) { // We don't do newline here just to avoid having to deal with windows stream_ << ""; offset -= 4; } stream_ << "\n"; stream_.seekp(offset, std::ios_base::cur); } void finish(bool close) { const auto compressor = cpp11::package("svglite")["create_svgz"]; if (is_clipping()) { stream_ << "\n"; } stream_ << "\n\n"; stream_.flush(); clear_clip_ids(); if (compress) { compressor(cpp11::r_string(file)); } } ~SvgStreamFile() { stream_.close(); } }; class SvgStreamString : public SvgStream { std::stringstream stream_; cpp11::environment env_; public: SvgStreamString(cpp11::environment env): env_(env) { stream_ << std::fixed << std::setprecision(2); env_["is_closed"] = false; } void write(int data) { stream_ << data; } void write(double data) { svglite::internal::write_double(stream_, data); } void write(const char* data) { stream_ << data; } void write(char data) { stream_ << data; } void write(const std::string& data) { stream_ << data; } bool is_file_stream() {return false; } void flush() { } void finish(bool close) { // When device is closed, stream_ will be destroyed, so we can no longer // get the svg string from stream_. In this case, we save the final string // to the environment env, so that R can read from env$svg_string even // after device is closed. env_["is_closed"] = close; stream_.flush(); std::string svgstr = stream_.str(); // If the current svg is empty, we also make the string empty // Otherwise append "" to make it a valid SVG if(!svgstr.empty()) { if (is_clipping()) { svgstr.append("\n"); } svgstr.append("\n"); } if (env_.exists("svg_string")) { cpp11::writable::strings str(env_["svg_string"]); str.push_back(svgstr.c_str()); env_["svg_string"] = str; } else { env_["svg_string"] = svgstr; } // clear the stream stream_.str(std::string()); stream_.clear(); clear_clip_ids(); } std::stringstream* string_src() { return &stream_; } }; svglite/src/Makevars.ucrt0000644000176200001440000000002514672302426015171 0ustar liggesusersPKG_LIBS = -lpng -lz svglite/src/utils.h0000644000176200001440000000074614705720057014045 0ustar liggesusers#pragma once #include #include #include inline static double dbl_format(double x) { if (std::abs(x) < std::numeric_limits::epsilon()) return 0.00; else return x; } inline bool iequals(const std::string& a, const std::string& b) { unsigned int sz = a.size(); if (b.size() != sz) { return false; } for (unsigned int i = 0; i < sz; ++i) { if (tolower(a[i]) != tolower(b[i])) { return false; } } return true; } svglite/src/cpp11.cpp0000644000176200001440000000610215075362760014160 0ustar liggesusers// Generated by cpp11: do not edit by hand // clang-format off #include "svglite_types.h" #include "cpp11/declarations.hpp" #include // devSVG.cpp bool svglite_(std::string file, std::string bg, double width, double height, double pointsize, bool standalone, cpp11::list aliases, std::string webfonts, cpp11::strings id, bool fix_text_size, double scaling, bool always_valid); extern "C" SEXP _svglite_svglite_(SEXP file, SEXP bg, SEXP width, SEXP height, SEXP pointsize, SEXP standalone, SEXP aliases, SEXP webfonts, SEXP id, SEXP fix_text_size, SEXP scaling, SEXP always_valid) { BEGIN_CPP11 return cpp11::as_sexp(svglite_(cpp11::as_cpp>(file), cpp11::as_cpp>(bg), cpp11::as_cpp>(width), cpp11::as_cpp>(height), cpp11::as_cpp>(pointsize), cpp11::as_cpp>(standalone), cpp11::as_cpp>(aliases), cpp11::as_cpp>(webfonts), cpp11::as_cpp>(id), cpp11::as_cpp>(fix_text_size), cpp11::as_cpp>(scaling), cpp11::as_cpp>(always_valid))); END_CPP11 } // devSVG.cpp cpp11::external_pointer svgstring_(cpp11::environment env, std::string bg, double width, double height, double pointsize, bool standalone, cpp11::list aliases, std::string webfonts, cpp11::strings id, bool fix_text_size, double scaling); extern "C" SEXP _svglite_svgstring_(SEXP env, SEXP bg, SEXP width, SEXP height, SEXP pointsize, SEXP standalone, SEXP aliases, SEXP webfonts, SEXP id, SEXP fix_text_size, SEXP scaling) { BEGIN_CPP11 return cpp11::as_sexp(svgstring_(cpp11::as_cpp>(env), cpp11::as_cpp>(bg), cpp11::as_cpp>(width), cpp11::as_cpp>(height), cpp11::as_cpp>(pointsize), cpp11::as_cpp>(standalone), cpp11::as_cpp>(aliases), cpp11::as_cpp>(webfonts), cpp11::as_cpp>(id), cpp11::as_cpp>(fix_text_size), cpp11::as_cpp>(scaling))); END_CPP11 } // devSVG.cpp std::string get_svg_content(cpp11::external_pointer p); extern "C" SEXP _svglite_get_svg_content(SEXP p) { BEGIN_CPP11 return cpp11::as_sexp(get_svg_content(cpp11::as_cpp>>(p))); END_CPP11 } extern "C" { static const R_CallMethodDef CallEntries[] = { {"_svglite_get_svg_content", (DL_FUNC) &_svglite_get_svg_content, 1}, {"_svglite_svglite_", (DL_FUNC) &_svglite_svglite_, 12}, {"_svglite_svgstring_", (DL_FUNC) &_svglite_svgstring_, 11}, {NULL, NULL, 0} }; } extern "C" attribute_visible void R_init_svglite(DllInfo* dll){ R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); R_useDynamicSymbols(dll, FALSE); R_forceSymbols(dll, TRUE); } svglite/src/Makevars0000644000176200001440000000002515006073745014216 0ustar liggesusersPKG_LIBS = -lpng -lz svglite/src/svglite_types.h0000644000176200001440000000002314672302426015571 0ustar liggesusers#include svglite/src/tinyformat.h0000644000176200001440000013640614672302426015103 0ustar liggesusers// tinyformat.h // Copyright (C) 2011, Chris Foster [chris42f (at) gmail (d0t) com] // // Boost Software License - Version 1.0 // // Permission is hereby granted, free of charge, to any person or organization // obtaining a copy of the software and accompanying documentation covered by // this license (the "Software") to use, reproduce, display, distribute, // execute, and transmit the Software, and to prepare derivative works of the // Software, and to permit third-parties to whom the Software is furnished to // do so, all subject to the following: // // The copyright notices in the Software and this entire statement, including // the above license grant, this restriction and the following disclaimer, // must be included in all copies of the Software, in whole or in part, and // all derivative works of the Software, unless such copies or derivative // works are solely in the form of machine-executable object code generated by // a source language processor. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. //------------------------------------------------------------------------------ // Tinyformat: A minimal type safe printf replacement // // tinyformat.h is a type safe printf replacement library in a single C++ // header file. Design goals include: // // * Type safety and extensibility for user defined types. // * C99 printf() compatibility, to the extent possible using std::ostream // * POSIX extension for positional arguments // * Simplicity and minimalism. A single header file to include and distribute // with your projects. // * Augment rather than replace the standard stream formatting mechanism // * C++98 support, with optional C++11 niceties // // // Main interface example usage // ---------------------------- // // To print a date to std::cout for American usage: // // std::string weekday = "Wednesday"; // const char* month = "July"; // size_t day = 27; // long hour = 14; // int min = 44; // // tfm::printf("%s, %s %d, %.2d:%.2d\n", weekday, month, day, hour, min); // // POSIX extension for positional arguments is available. // The ability to rearrange formatting arguments is an important feature // for localization because the word order may vary in different languages. // // Previous example for German usage. Arguments are reordered: // // tfm::printf("%1$s, %3$d. %2$s, %4$d:%5$.2d\n", weekday, month, day, hour, min); // // The strange types here emphasize the type safety of the interface; it is // possible to print a std::string using the "%s" conversion, and a // size_t using the "%d" conversion. A similar result could be achieved // using either of the tfm::format() functions. One prints on a user provided // stream: // // tfm::format(std::cerr, "%s, %s %d, %.2d:%.2d\n", // weekday, month, day, hour, min); // // The other returns a std::string: // // std::string date = tfm::format("%s, %s %d, %.2d:%.2d\n", // weekday, month, day, hour, min); // std::cout << date; // // These are the three primary interface functions. There is also a // convenience function printfln() which appends a newline to the usual result // of printf() for super simple logging. // // // User defined format functions // ----------------------------- // // Simulating variadic templates in C++98 is pretty painful since it requires // writing out the same function for each desired number of arguments. To make // this bearable tinyformat comes with a set of macros which are used // internally to generate the API, but which may also be used in user code. // // The three macros TINYFORMAT_ARGTYPES(n), TINYFORMAT_VARARGS(n) and // TINYFORMAT_PASSARGS(n) will generate a list of n argument types, // type/name pairs and argument names respectively when called with an integer // n between 1 and 16. We can use these to define a macro which generates the // desired user defined function with n arguments. To generate all 16 user // defined function bodies, use the macro TINYFORMAT_FOREACH_ARGNUM. For an // example, see the implementation of printf() at the end of the source file. // // Sometimes it's useful to be able to pass a list of format arguments through // to a non-template function. The FormatList class is provided as a way to do // this by storing the argument list in a type-opaque way. Continuing the // example from above, we construct a FormatList using makeFormatList(): // // FormatListRef formatList = tfm::makeFormatList(weekday, month, day, hour, min); // // The format list can now be passed into any non-template function and used // via a call to the vformat() function: // // tfm::vformat(std::cout, "%s, %s %d, %.2d:%.2d\n", formatList); // // // Additional API information // -------------------------- // // Error handling: Define TINYFORMAT_ERROR to customize the error handling for // format strings which are unsupported or have the wrong number of format // specifiers (calls assert() by default). // // User defined types: Uses operator<< for user defined types by default. // Overload formatValue() for more control. #ifndef TINYFORMAT_H_INCLUDED #define TINYFORMAT_H_INCLUDED namespace tinyformat {} //------------------------------------------------------------------------------ // Config section. Customize to your liking! // Namespace alias to encourage brevity namespace tfm = tinyformat; // Error handling; calls assert() by default. // #define TINYFORMAT_ERROR(reasonString) your_error_handler(reasonString) // Define for C++11 variadic templates which make the code shorter & more // general. If you don't define this, C++11 support is autodetected below. // #define TINYFORMAT_USE_VARIADIC_TEMPLATES //------------------------------------------------------------------------------ // Implementation details. #include #include #include #ifndef TINYFORMAT_ASSERT # include # define TINYFORMAT_ASSERT(cond) assert(cond) #endif #ifndef TINYFORMAT_ERROR # include # define TINYFORMAT_ERROR(reason) assert(0 && reason) #endif #if !defined(TINYFORMAT_USE_VARIADIC_TEMPLATES) && !defined(TINYFORMAT_NO_VARIADIC_TEMPLATES) # ifdef __GXX_EXPERIMENTAL_CXX0X__ # define TINYFORMAT_USE_VARIADIC_TEMPLATES # endif #endif #if defined(__GLIBCXX__) && __GLIBCXX__ < 20080201 // std::showpos is broken on old libstdc++ as provided with macOS. See // http://gcc.gnu.org/ml/libstdc++/2007-11/msg00075.html # define TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND #endif #ifdef __APPLE__ // Workaround macOS linker warning: Xcode uses different default symbol // visibilities for static libs vs executables (see issue #25) # define TINYFORMAT_HIDDEN __attribute__((visibility("hidden"))) #else # define TINYFORMAT_HIDDEN #endif namespace tinyformat { //------------------------------------------------------------------------------ namespace detail { // Test whether type T1 is convertible to type T2 template struct is_convertible { private: // two types of different size struct fail { char dummy[2]; }; struct succeed { char dummy; }; // Try to convert a T1 to a T2 by plugging into tryConvert static fail tryConvert(...); static succeed tryConvert(const T2&); static const T1& makeT1(); public: # ifdef _MSC_VER // Disable spurious loss of precision warnings in tryConvert(makeT1()) # pragma warning(push) # pragma warning(disable:4244) # pragma warning(disable:4267) # endif // Standard trick: the (...) version of tryConvert will be chosen from // the overload set only if the version taking a T2 doesn't match. // Then we compare the sizes of the return types to check which // function matched. Very neat, in a disgusting kind of way :) static const bool value = sizeof(tryConvert(makeT1())) == sizeof(succeed); # ifdef _MSC_VER # pragma warning(pop) # endif }; // Detect when a type is not a wchar_t string template struct is_wchar { typedef int tinyformat_wchar_is_not_supported; }; template<> struct is_wchar {}; template<> struct is_wchar {}; template struct is_wchar {}; template struct is_wchar {}; // Format the value by casting to type fmtT. This default implementation // should never be called. template::value> struct formatValueAsType { static void invoke(std::ostream& /*out*/, const T& /*value*/) { TINYFORMAT_ASSERT(0); } }; // Specialized version for types that can actually be converted to fmtT, as // indicated by the "convertible" template parameter. template struct formatValueAsType { static void invoke(std::ostream& out, const T& value) { out << static_cast(value); } }; #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND template::value> struct formatZeroIntegerWorkaround { static bool invoke(std::ostream& /**/, const T& /**/) { return false; } }; template struct formatZeroIntegerWorkaround { static bool invoke(std::ostream& out, const T& value) { if (static_cast(value) == 0 && out.flags() & std::ios::showpos) { out << "+0"; return true; } return false; } }; #endif // TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND // Convert an arbitrary type to integer. The version with convertible=false // throws an error. template::value> struct convertToInt { static int invoke(const T& /*value*/) { TINYFORMAT_ERROR("tinyformat: Cannot convert from argument type to " "integer for use as variable width or precision"); return 0; } }; // Specialization for convertToInt when conversion is possible template struct convertToInt { static int invoke(const T& value) { return static_cast(value); } }; // Format at most ntrunc characters to the given stream. template inline void formatTruncated(std::ostream& out, const T& value, int ntrunc) { std::ostringstream tmp; tmp << value; std::string result = tmp.str(); out.write(result.c_str(), (std::min)(ntrunc, static_cast(result.size()))); } #define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type) \ inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \ { \ std::streamsize len = 0; \ while (len < ntrunc && value[len] != 0) \ ++len; \ out.write(value, len); \ } // Overload for const char* and char*. Could overload for signed & unsigned // char too, but these are technically unneeded for printf compatibility. TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(const char) TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(char) #undef TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR } // namespace detail //------------------------------------------------------------------------------ // Variable formatting functions. May be overridden for user-defined types if // desired. /// Format a value into a stream, delegating to operator<< by default. /// /// Users may override this for their own types. When this function is called, /// the stream flags will have been modified according to the format string. /// The format specification is provided in the range [fmtBegin, fmtEnd). For /// truncating conversions, ntrunc is set to the desired maximum number of /// characters, for example "%.7s" calls formatValue with ntrunc = 7. /// /// By default, formatValue() uses the usual stream insertion operator /// operator<< to format the type T, with special cases for the %c and %p /// conversions. template inline void formatValue(std::ostream& out, const char* /*fmtBegin*/, const char* fmtEnd, int ntrunc, const T& value) { #ifndef TINYFORMAT_ALLOW_WCHAR_STRINGS // Since we don't support printing of wchar_t using "%ls", make it fail at // compile time in preference to printing as a void* at runtime. typedef typename detail::is_wchar::tinyformat_wchar_is_not_supported DummyType; (void) DummyType(); // avoid unused type warning with gcc-4.8 #endif // The mess here is to support the %c and %p conversions: if these // conversions are active we try to convert the type to a char or const // void* respectively and format that instead of the value itself. For the // %p conversion it's important to avoid dereferencing the pointer, which // could otherwise lead to a crash when printing a dangling (const char*). const bool canConvertToChar = detail::is_convertible::value; const bool canConvertToVoidPtr = detail::is_convertible::value; if (canConvertToChar && *(fmtEnd-1) == 'c') detail::formatValueAsType::invoke(out, value); else if (canConvertToVoidPtr && *(fmtEnd-1) == 'p') detail::formatValueAsType::invoke(out, value); #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND else if (detail::formatZeroIntegerWorkaround::invoke(out, value)) /**/; #endif else if (ntrunc >= 0) { // Take care not to overread C strings in truncating conversions like // "%.4s" where at most 4 characters may be read. detail::formatTruncated(out, value, ntrunc); } else out << value; } // Overloaded version for char types to support printing as an integer #define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \ inline void formatValue(std::ostream& out, const char* /*fmtBegin*/, \ const char* fmtEnd, int /**/, charType value) \ { \ switch (*(fmtEnd-1)) { \ case 'u': case 'd': case 'i': case 'o': case 'X': case 'x': \ out << static_cast(value); break; \ default: \ out << value; break; \ } \ } // per 3.9.1: char, signed char and unsigned char are all distinct types TINYFORMAT_DEFINE_FORMATVALUE_CHAR(char) TINYFORMAT_DEFINE_FORMATVALUE_CHAR(signed char) TINYFORMAT_DEFINE_FORMATVALUE_CHAR(unsigned char) #undef TINYFORMAT_DEFINE_FORMATVALUE_CHAR //------------------------------------------------------------------------------ // Tools for emulating variadic templates in C++98. The basic idea here is // stolen from the boost preprocessor metaprogramming library and cut down to // be just general enough for what we need. #define TINYFORMAT_ARGTYPES(n) TINYFORMAT_ARGTYPES_ ## n #define TINYFORMAT_VARARGS(n) TINYFORMAT_VARARGS_ ## n #define TINYFORMAT_PASSARGS(n) TINYFORMAT_PASSARGS_ ## n #define TINYFORMAT_PASSARGS_TAIL(n) TINYFORMAT_PASSARGS_TAIL_ ## n // To keep it as transparent as possible, the macros below have been generated // using python via the excellent cog code generation script. This avoids // the need for a bunch of complex (but more general) preprocessor tricks as // used in boost.preprocessor. // // To rerun the code generation in place, use `cog -r tinyformat.h` // (see http://nedbatchelder.com/code/cog). Alternatively you can just create // extra versions by hand. /*[[[cog maxParams = 16 def makeCommaSepLists(lineTemplate, elemTemplate, startInd=1): for j in range(startInd,maxParams+1): list = ', '.join([elemTemplate % {'i':i} for i in range(startInd,j+1)]) cog.outl(lineTemplate % {'j':j, 'list':list}) makeCommaSepLists('#define TINYFORMAT_ARGTYPES_%(j)d %(list)s', 'class T%(i)d') cog.outl() makeCommaSepLists('#define TINYFORMAT_VARARGS_%(j)d %(list)s', 'const T%(i)d& v%(i)d') cog.outl() makeCommaSepLists('#define TINYFORMAT_PASSARGS_%(j)d %(list)s', 'v%(i)d') cog.outl() cog.outl('#define TINYFORMAT_PASSARGS_TAIL_1') makeCommaSepLists('#define TINYFORMAT_PASSARGS_TAIL_%(j)d , %(list)s', 'v%(i)d', startInd = 2) cog.outl() cog.outl('#define TINYFORMAT_FOREACH_ARGNUM(m) \\\n ' + ' '.join(['m(%d)' % (j,) for j in range(1,maxParams+1)])) ]]]*/ #define TINYFORMAT_ARGTYPES_1 class T1 #define TINYFORMAT_ARGTYPES_2 class T1, class T2 #define TINYFORMAT_ARGTYPES_3 class T1, class T2, class T3 #define TINYFORMAT_ARGTYPES_4 class T1, class T2, class T3, class T4 #define TINYFORMAT_ARGTYPES_5 class T1, class T2, class T3, class T4, class T5 #define TINYFORMAT_ARGTYPES_6 class T1, class T2, class T3, class T4, class T5, class T6 #define TINYFORMAT_ARGTYPES_7 class T1, class T2, class T3, class T4, class T5, class T6, class T7 #define TINYFORMAT_ARGTYPES_8 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8 #define TINYFORMAT_ARGTYPES_9 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9 #define TINYFORMAT_ARGTYPES_10 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10 #define TINYFORMAT_ARGTYPES_11 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11 #define TINYFORMAT_ARGTYPES_12 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12 #define TINYFORMAT_ARGTYPES_13 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13 #define TINYFORMAT_ARGTYPES_14 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14 #define TINYFORMAT_ARGTYPES_15 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 #define TINYFORMAT_ARGTYPES_16 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15, class T16 #define TINYFORMAT_VARARGS_1 const T1& v1 #define TINYFORMAT_VARARGS_2 const T1& v1, const T2& v2 #define TINYFORMAT_VARARGS_3 const T1& v1, const T2& v2, const T3& v3 #define TINYFORMAT_VARARGS_4 const T1& v1, const T2& v2, const T3& v3, const T4& v4 #define TINYFORMAT_VARARGS_5 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5 #define TINYFORMAT_VARARGS_6 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6 #define TINYFORMAT_VARARGS_7 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7 #define TINYFORMAT_VARARGS_8 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8 #define TINYFORMAT_VARARGS_9 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9 #define TINYFORMAT_VARARGS_10 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10 #define TINYFORMAT_VARARGS_11 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11 #define TINYFORMAT_VARARGS_12 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12 #define TINYFORMAT_VARARGS_13 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13 #define TINYFORMAT_VARARGS_14 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14 #define TINYFORMAT_VARARGS_15 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14, const T15& v15 #define TINYFORMAT_VARARGS_16 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14, const T15& v15, const T16& v16 #define TINYFORMAT_PASSARGS_1 v1 #define TINYFORMAT_PASSARGS_2 v1, v2 #define TINYFORMAT_PASSARGS_3 v1, v2, v3 #define TINYFORMAT_PASSARGS_4 v1, v2, v3, v4 #define TINYFORMAT_PASSARGS_5 v1, v2, v3, v4, v5 #define TINYFORMAT_PASSARGS_6 v1, v2, v3, v4, v5, v6 #define TINYFORMAT_PASSARGS_7 v1, v2, v3, v4, v5, v6, v7 #define TINYFORMAT_PASSARGS_8 v1, v2, v3, v4, v5, v6, v7, v8 #define TINYFORMAT_PASSARGS_9 v1, v2, v3, v4, v5, v6, v7, v8, v9 #define TINYFORMAT_PASSARGS_10 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 #define TINYFORMAT_PASSARGS_11 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11 #define TINYFORMAT_PASSARGS_12 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 #define TINYFORMAT_PASSARGS_13 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13 #define TINYFORMAT_PASSARGS_14 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14 #define TINYFORMAT_PASSARGS_15 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 #define TINYFORMAT_PASSARGS_16 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16 #define TINYFORMAT_PASSARGS_TAIL_1 #define TINYFORMAT_PASSARGS_TAIL_2 , v2 #define TINYFORMAT_PASSARGS_TAIL_3 , v2, v3 #define TINYFORMAT_PASSARGS_TAIL_4 , v2, v3, v4 #define TINYFORMAT_PASSARGS_TAIL_5 , v2, v3, v4, v5 #define TINYFORMAT_PASSARGS_TAIL_6 , v2, v3, v4, v5, v6 #define TINYFORMAT_PASSARGS_TAIL_7 , v2, v3, v4, v5, v6, v7 #define TINYFORMAT_PASSARGS_TAIL_8 , v2, v3, v4, v5, v6, v7, v8 #define TINYFORMAT_PASSARGS_TAIL_9 , v2, v3, v4, v5, v6, v7, v8, v9 #define TINYFORMAT_PASSARGS_TAIL_10 , v2, v3, v4, v5, v6, v7, v8, v9, v10 #define TINYFORMAT_PASSARGS_TAIL_11 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11 #define TINYFORMAT_PASSARGS_TAIL_12 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 #define TINYFORMAT_PASSARGS_TAIL_13 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13 #define TINYFORMAT_PASSARGS_TAIL_14 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14 #define TINYFORMAT_PASSARGS_TAIL_15 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 #define TINYFORMAT_PASSARGS_TAIL_16 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16 #define TINYFORMAT_FOREACH_ARGNUM(m) \ m(1) m(2) m(3) m(4) m(5) m(6) m(7) m(8) m(9) m(10) m(11) m(12) m(13) m(14) m(15) m(16) //[[[end]]] namespace detail { // Type-opaque holder for an argument to format(), with associated actions on // the type held as explicit function pointers. This allows FormatArg's for // each argument to be allocated as a homogeneous array inside FormatList // whereas a naive implementation based on inheritance does not. class FormatArg { public: FormatArg() : m_value(NULL), m_formatImpl(NULL), m_toIntImpl(NULL) { } template FormatArg(const T& value) // C-style cast here allows us to also remove volatile; we put it // back in the *Impl functions before dereferencing to avoid UB. : m_value((const void*)(&value)), m_formatImpl(&formatImpl), m_toIntImpl(&toIntImpl) { } void format(std::ostream& out, const char* fmtBegin, const char* fmtEnd, int ntrunc) const { TINYFORMAT_ASSERT(m_value); TINYFORMAT_ASSERT(m_formatImpl); m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value); } int toInt() const { TINYFORMAT_ASSERT(m_value); TINYFORMAT_ASSERT(m_toIntImpl); return m_toIntImpl(m_value); } private: template TINYFORMAT_HIDDEN static void formatImpl(std::ostream& out, const char* fmtBegin, const char* fmtEnd, int ntrunc, const void* value) { formatValue(out, fmtBegin, fmtEnd, ntrunc, *static_cast(value)); } template TINYFORMAT_HIDDEN static int toIntImpl(const void* value) { return convertToInt::invoke(*static_cast(value)); } const void* m_value; void (*m_formatImpl)(std::ostream& out, const char* fmtBegin, const char* fmtEnd, int ntrunc, const void* value); int (*m_toIntImpl)(const void* value); }; // Parse and return an integer from the string c, as atoi() // On return, c is set to one past the end of the integer. inline int parseIntAndAdvance(const char*& c) { int i = 0; for (;*c >= '0' && *c <= '9'; ++c) i = 10*i + (*c - '0'); return i; } // Parse width or precision `n` from format string pointer `c`, and advance it // to the next character. If an indirection is requested with `*`, the argument // is read from `args[argIndex]` and `argIndex` is incremented (or read // from `args[n]` in positional mode). Returns true if one or more // characters were read. inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode, const detail::FormatArg* args, int& argIndex, int numArgs) { if (*c >= '0' && *c <= '9') { n = parseIntAndAdvance(c); } else if (*c == '*') { ++c; n = 0; if (positionalMode) { int pos = parseIntAndAdvance(c) - 1; if (*c != '$') TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one"); if (pos >= 0 && pos < numArgs) n = args[pos].toInt(); else TINYFORMAT_ERROR("tinyformat: Positional argument out of range"); ++c; } else { if (argIndex < numArgs) n = args[argIndex++].toInt(); else TINYFORMAT_ERROR("tinyformat: Not enough arguments to read variable width or precision"); } } else { return false; } return true; } // Print literal part of format string and return next format spec position. // // Skips over any occurrences of '%%', printing a literal '%' to the output. // The position of the first % character of the next nontrivial format spec is // returned, or the end of string. inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt) { const char* c = fmt; for (;; ++c) { if (*c == '\0') { out.write(fmt, c - fmt); return c; } else if (*c == '%') { out.write(fmt, c - fmt); if (*(c+1) != '%') return c; // for "%%", tack trailing % onto next literal section. fmt = ++c; } } } // Parse a format string and set the stream state accordingly. // // The format mini-language recognized here is meant to be the one from C99, // with the form "%[flags][width][.precision][length]type" with POSIX // positional arguments extension. // // POSIX positional arguments extension: // Conversions can be applied to the nth argument after the format in // the argument list, rather than to the next unused argument. In this case, // the conversion specifier character % (see below) is replaced by the sequence // "%n$", where n is a decimal integer in the range [1,{NL_ARGMAX}], // giving the position of the argument in the argument list. This feature // provides for the definition of format strings that select arguments // in an order appropriate to specific languages. // // The format can contain either numbered argument conversion specifications // (that is, "%n$" and "*m$"), or unnumbered argument conversion specifications // (that is, % and * ), but not both. The only exception to this is that %% // can be mixed with the "%n$" form. The results of mixing numbered and // unnumbered argument specifications in a format string are undefined. // When numbered argument specifications are used, specifying the Nth argument // requires that all the leading arguments, from the first to the (N-1)th, // are specified in the format string. // // In format strings containing the "%n$" form of conversion specification, // numbered arguments in the argument list can be referenced from the format // string as many times as required. // // Formatting options which can't be natively represented using the ostream // state are returned in spacePadPositive (for space padded positive numbers) // and ntrunc (for truncating conversions). argIndex is incremented if // necessary to pull out variable width and precision. The function returns a // pointer to the character after the end of the current format spec. inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode, bool& spacePadPositive, int& ntrunc, const char* fmtStart, const detail::FormatArg* args, int& argIndex, int numArgs) { TINYFORMAT_ASSERT(*fmtStart == '%'); // Reset stream state to defaults. out.width(0); out.precision(6); out.fill(' '); // Reset most flags; ignore irrelevant unitbuf & skipws. out.unsetf(std::ios::adjustfield | std::ios::basefield | std::ios::floatfield | std::ios::showbase | std::ios::boolalpha | std::ios::showpoint | std::ios::showpos | std::ios::uppercase); bool precisionSet = false; bool widthSet = false; int widthExtra = 0; const char* c = fmtStart + 1; // 1) Parse an argument index (if followed by '$') or a width possibly // preceded with '0' flag. if (*c >= '0' && *c <= '9') { const char tmpc = *c; int value = parseIntAndAdvance(c); if (*c == '$') { // value is an argument index if (value > 0 && value <= numArgs) argIndex = value - 1; else TINYFORMAT_ERROR("tinyformat: Positional argument out of range"); ++c; positionalMode = true; } else if (positionalMode) { TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one"); } else { if (tmpc == '0') { // Use internal padding so that numeric values are // formatted correctly, eg -00010 rather than 000-10 out.fill('0'); out.setf(std::ios::internal, std::ios::adjustfield); } if (value != 0) { // Nonzero value means that we parsed width. widthSet = true; out.width(value); } } } else if (positionalMode) { TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one"); } // 2) Parse flags and width if we did not do it in previous step. if (!widthSet) { // Parse flags for (;; ++c) { switch (*c) { case '#': out.setf(std::ios::showpoint | std::ios::showbase); continue; case '0': // overridden by left alignment ('-' flag) if (!(out.flags() & std::ios::left)) { // Use internal padding so that numeric values are // formatted correctly, eg -00010 rather than 000-10 out.fill('0'); out.setf(std::ios::internal, std::ios::adjustfield); } continue; case '-': out.fill(' '); out.setf(std::ios::left, std::ios::adjustfield); continue; case ' ': // overridden by show positive sign, '+' flag. if (!(out.flags() & std::ios::showpos)) spacePadPositive = true; continue; case '+': out.setf(std::ios::showpos); spacePadPositive = false; widthExtra = 1; continue; default: break; } break; } // Parse width int width = 0; widthSet = parseWidthOrPrecision(width, c, positionalMode, args, argIndex, numArgs); if (widthSet) { if (width < 0) { // negative widths correspond to '-' flag set out.fill(' '); out.setf(std::ios::left, std::ios::adjustfield); width = -width; } out.width(width); } } // 3) Parse precision if (*c == '.') { ++c; int precision = 0; parseWidthOrPrecision(precision, c, positionalMode, args, argIndex, numArgs); // Presence of `.` indicates precision set, unless the inferred value // was negative in which case the default is used. precisionSet = precision >= 0; if (precisionSet) out.precision(precision); } // 4) Ignore any C99 length modifier while (*c == 'l' || *c == 'h' || *c == 'L' || *c == 'j' || *c == 'z' || *c == 't') { ++c; } // 5) We're up to the conversion specifier character. // Set stream flags based on conversion specifier (thanks to the // boost::format class for forging the way here). bool intConversion = false; switch (*c) { case 'u': case 'd': case 'i': out.setf(std::ios::dec, std::ios::basefield); intConversion = true; break; case 'o': out.setf(std::ios::oct, std::ios::basefield); intConversion = true; break; case 'X': out.setf(std::ios::uppercase); // Falls through case 'x': case 'p': out.setf(std::ios::hex, std::ios::basefield); intConversion = true; break; case 'E': out.setf(std::ios::uppercase); // Falls through case 'e': out.setf(std::ios::scientific, std::ios::floatfield); out.setf(std::ios::dec, std::ios::basefield); break; case 'F': out.setf(std::ios::uppercase); // Falls through case 'f': out.setf(std::ios::fixed, std::ios::floatfield); break; case 'A': out.setf(std::ios::uppercase); // Falls through case 'a': # ifdef _MSC_VER // Workaround https://developercommunity.visualstudio.com/content/problem/520472/hexfloat-stream-output-does-not-ignore-precision-a.html // by always setting maximum precision on MSVC to avoid precision // loss for doubles. out.precision(13); # endif out.setf(std::ios::fixed | std::ios::scientific, std::ios::floatfield); break; case 'G': out.setf(std::ios::uppercase); // Falls through case 'g': out.setf(std::ios::dec, std::ios::basefield); // As in boost::format, let stream decide float format. out.flags(out.flags() & ~std::ios::floatfield); break; case 'c': // Handled as special case inside formatValue() break; case 's': if (precisionSet) ntrunc = static_cast(out.precision()); // Make %s print Booleans as "true" and "false" out.setf(std::ios::boolalpha); break; case 'n': // Not supported - will cause problems! TINYFORMAT_ERROR("tinyformat: %n conversion spec not supported"); break; case '\0': TINYFORMAT_ERROR("tinyformat: Conversion spec incorrectly " "terminated by end of string"); return c; default: break; } if (intConversion && precisionSet && !widthSet) { // "precision" for integers gives the minimum number of digits (to be // padded with zeros on the left). This isn't really supported by the // iostreams, but we can approximately simulate it with the width if // the width isn't otherwise used. out.width(out.precision() + widthExtra); out.setf(std::ios::internal, std::ios::adjustfield); out.fill('0'); } return c+1; } //------------------------------------------------------------------------------ inline void formatImpl(std::ostream& out, const char* fmt, const detail::FormatArg* args, int numArgs) { // Saved stream state std::streamsize origWidth = out.width(); std::streamsize origPrecision = out.precision(); std::ios::fmtflags origFlags = out.flags(); char origFill = out.fill(); // "Positional mode" means all format specs should be of the form "%n$..." // with `n` an integer. We detect this in `streamStateFromFormat`. bool positionalMode = false; int argIndex = 0; while (true) { fmt = printFormatStringLiteral(out, fmt); if (*fmt == '\0') { if (!positionalMode && argIndex < numArgs) { TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string"); } break; } bool spacePadPositive = false; int ntrunc = -1; const char* fmtEnd = streamStateFromFormat(out, positionalMode, spacePadPositive, ntrunc, fmt, args, argIndex, numArgs); // NB: argIndex may be incremented by reading variable width/precision // in `streamStateFromFormat`, so do the bounds check here. if (argIndex >= numArgs) { TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string"); return; } const FormatArg& arg = args[argIndex]; // Format the arg into the stream. if (!spacePadPositive) { arg.format(out, fmt, fmtEnd, ntrunc); } else { // The following is a special case with no direct correspondence // between stream formatting and the printf() behaviour. Simulate // it crudely by formatting into a temporary string stream and // munging the resulting string. std::ostringstream tmpStream; tmpStream.copyfmt(out); tmpStream.setf(std::ios::showpos); arg.format(tmpStream, fmt, fmtEnd, ntrunc); std::string result = tmpStream.str(); // allocates... yuck. for (size_t i = 0, iend = result.size(); i < iend; ++i) { if (result[i] == '+') result[i] = ' '; } out << result; } if (!positionalMode) ++argIndex; fmt = fmtEnd; } // Restore stream state out.width(origWidth); out.precision(origPrecision); out.flags(origFlags); out.fill(origFill); } } // namespace detail /// List of template arguments format(), held in a type-opaque way. /// /// A const reference to FormatList (typedef'd as FormatListRef) may be /// conveniently used to pass arguments to non-template functions: All type /// information has been stripped from the arguments, leaving just enough of a /// common interface to perform formatting as required. class FormatList { public: FormatList(detail::FormatArg* args, int N) : m_args(args), m_N(N) { } friend void vformat(std::ostream& out, const char* fmt, const FormatList& list); private: const detail::FormatArg* m_args; int m_N; }; /// Reference to type-opaque format list for passing to vformat() typedef const FormatList& FormatListRef; namespace detail { // Format list subclass with fixed storage to avoid dynamic allocation template class FormatListN : public FormatList { public: #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES template FormatListN(const Args&... args) : FormatList(&m_formatterStore[0], N), m_formatterStore { FormatArg(args)... } { static_assert(sizeof...(args) == N, "Number of args must be N"); } #else // C++98 version void init(int) {} # define TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR(n) \ \ template \ FormatListN(TINYFORMAT_VARARGS(n)) \ : FormatList(&m_formatterStore[0], n) \ { TINYFORMAT_ASSERT(n == N); init(0, TINYFORMAT_PASSARGS(n)); } \ \ template \ void init(int i, TINYFORMAT_VARARGS(n)) \ { \ m_formatterStore[i] = FormatArg(v1); \ init(i+1 TINYFORMAT_PASSARGS_TAIL(n)); \ } TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR) # undef TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR #endif FormatListN(const FormatListN& other) : FormatList(&m_formatterStore[0], N) { std::copy(&other.m_formatterStore[0], &other.m_formatterStore[N], &m_formatterStore[0]); } private: FormatArg m_formatterStore[N]; }; // Special 0-arg version - MSVC says zero-sized C array in struct is nonstandard template<> class FormatListN<0> : public FormatList { public: FormatListN() : FormatList(0, 0) {} }; } // namespace detail //------------------------------------------------------------------------------ // Primary API functions #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES /// Make type-agnostic format list from list of template arguments. /// /// The exact return type of this function is an implementation detail and /// shouldn't be relied upon. Instead it should be stored as a FormatListRef: /// /// FormatListRef formatList = makeFormatList( /*...*/ ); template detail::FormatListN makeFormatList(const Args&... args) { return detail::FormatListN(args...); } #else // C++98 version inline detail::FormatListN<0> makeFormatList() { return detail::FormatListN<0>(); } #define TINYFORMAT_MAKE_MAKEFORMATLIST(n) \ template \ detail::FormatListN makeFormatList(TINYFORMAT_VARARGS(n)) \ { \ return detail::FormatListN(TINYFORMAT_PASSARGS(n)); \ } TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_MAKEFORMATLIST) #undef TINYFORMAT_MAKE_MAKEFORMATLIST #endif /// Format list of arguments to the stream according to the given format string. /// /// The name vformat() is chosen for the semantic similarity to vprintf(): the /// list of format arguments is held in a single function argument. inline void vformat(std::ostream& out, const char* fmt, FormatListRef list) { detail::formatImpl(out, fmt, list.m_args, list.m_N); } #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES /// Format list of arguments to the stream according to given format string. template void format(std::ostream& out, const char* fmt, const Args&... args) { vformat(out, fmt, makeFormatList(args...)); } /// Format list of arguments according to the given format string and return /// the result as a string. template std::string format(const char* fmt, const Args&... args) { std::ostringstream oss; format(oss, fmt, args...); return oss.str(); } /// Format list of arguments to std::cout, according to the given format string template void printf(const char* fmt, const Args&... args) { format(std::cout, fmt, args...); } template void printfln(const char* fmt, const Args&... args) { format(std::cout, fmt, args...); std::cout << '\n'; } #else // C++98 version inline void format(std::ostream& out, const char* fmt) { vformat(out, fmt, makeFormatList()); } inline std::string format(const char* fmt) { std::ostringstream oss; format(oss, fmt); return oss.str(); } inline void printf(const char* fmt) { format(std::cout, fmt); } inline void printfln(const char* fmt) { format(std::cout, fmt); std::cout << '\n'; } #define TINYFORMAT_MAKE_FORMAT_FUNCS(n) \ \ template \ void format(std::ostream& out, const char* fmt, TINYFORMAT_VARARGS(n)) \ { \ vformat(out, fmt, makeFormatList(TINYFORMAT_PASSARGS(n))); \ } \ \ template \ std::string format(const char* fmt, TINYFORMAT_VARARGS(n)) \ { \ std::ostringstream oss; \ format(oss, fmt, TINYFORMAT_PASSARGS(n)); \ return oss.str(); \ } \ \ template \ void printf(const char* fmt, TINYFORMAT_VARARGS(n)) \ { \ format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \ } \ \ template \ void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \ { \ format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \ std::cout << '\n'; \ } TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS) #undef TINYFORMAT_MAKE_FORMAT_FUNCS #endif } // namespace tinyformat #endif // TINYFORMAT_H_INCLUDED svglite/src/SvgStream.cpp0000644000176200001440000000036614706125775015160 0ustar liggesusers#include "SvgStream.h" template <> SvgStream& operator<<(SvgStream& object, const double& data) { // Make sure negative zeros are converted to positive zero for // reproducibility of SVGs object.write(dbl_format(data)); return object; } svglite/NAMESPACE0000644000176200001440000000144115004650175013151 0ustar liggesusers# Generated by roxygen2: do not edit by hand S3method(print,font_face) S3method(print,svg) export(add_fonts) export(add_web_fonts) export(create_svgz) export(editSVG) export(font_face) export(font_feature) export(fonts_as_import) export(htmlSVG) export(register_font) export(register_variant) export(require_font) export(stringSVG) export(svglite) export(svgstring) export(xmlSVG) import(rlang) importFrom(lifecycle,deprecated) importFrom(systemfonts,add_fonts) importFrom(systemfonts,font_feature) importFrom(systemfonts,font_info) importFrom(systemfonts,fonts_as_import) importFrom(systemfonts,match_font) importFrom(systemfonts,register_font) importFrom(systemfonts,register_variant) importFrom(systemfonts,require_font) importFrom(textshaping,text_width) useDynLib(svglite, .registration = TRUE) svglite/NEWS.md0000644000176200001440000001472315075455714013051 0ustar liggesusers# svglite 2.2.2 * Avoid sanitizer error when calculating precision (#198) * Fix a test bug on newer versions of macOS # svglite 2.2.1 * Fix a compilation issue on Fedora # svglite 2.2.0 * Added support for luminance masking (#174) * Fixed a bug in calculating text widths when using font features (#175) * Added support for path stroking and filling (#174) * Added support for groups along with affine transformation and color blending. No support for Porter-Duff composition as it is porly supported in SVG (#174) * Added better support for specifying webfonts as embedded in the SVG (#182) * Added support for glyphs by rendering them as paths or rasters. This means the text is not editable in post-production but it is better than nothing. # svglite 2.1.3 * Fix a stack imbalance bug # svglite 2.1.2 * Windows: use libpng included with Rtools on R 4.2 and up. * Add support for `dev.capabilities()` # svglite 2.1.1 * Fix `` include at request of CRAN # svglite 2.1.0 * Add new graphics engine features: - Path clipping - Masking - Pattern and gradient fills * Font family names are now quoted (#136) * svglite now renders leading white-space in nodes. (#131, @hrbrmstr) # svglite 2.0.0 * svglite now uses systemfonts for text metric calculations and font family lookup. * svglite can now encode OpenType font features into the CSS if the used font contains registered features * svglite now directly encodes raster data into png instead of rendering it through cairo. If the provided raster is smaller than the final requested size it will be resized. * SVG's can now get a top-level id so that style definitions doesn't spill into the surrounding HTML (#91) * Dimensions are now encoded into the top-level `` tag (#90) * Starting a new page creates a new file if the filename supports it (#98, @vandenman). * The _inline_ devices now defaults to the same dimensions as `svglite()` (#89) * Clip defs are now only written if they don't already exist (#110) * Clipping is now defined with outer groups instead of on each element (#109) * svglite now uses cpp11 instead of Rcpp (#100) * svgz output is now supported natively (#6) * Text adjustments are now encoded in css where possible (#107) * The use of textLength CSS property can now be turned off (#118) * web font imports can now be given when creating an svg (#108) * Add scaling argument to devices to control line-width and text scaling (#115) * svg files that are being written are now only valid at all times if `always_valid` is set to `TRUE` in the `svglite()` call. * svglite now guards against attempts at writing to the device before a new page has been requested (#126) # svglite 1.2.3 * The radius of circles is no longer expressed in pt (#93, @vandenman). * Dimensions smaller than 1 now retain two significant figures (#94, @ilia-kats). * @thomasp85 takes over as maintainer # svglite 1.2.2 * Improvements to reproducibility of generated SVGs: Negative zeros are now treated as positive, and the clip ID are now generated from truncated elements. * svglite now uses the `polygon` SVG element. This ensures that polygons are properly closed (#82). * Text metrics are now correctly computed for Unicode characters in plotmath expressions (#81). # svglite 1.2.1 This release makes svglite compatible with gdtools 0.1.6 # svglite 1.2.0 ## New features * The device functions gain `system_fonts` and `user_fonts` arguments. * Two new vignettes: `vignette("fonts")` and `vignette("scaling")`. The vignette on fonts explains in detail how to use the new fonts arguments and why. The vignette on scaling goes over scaling issues, e.g. when embedding svglite outputs in a web page. * `xmlSVG()` gains `height` and `width` arguments (#66). * New `stringSVG()` device function for quick testing. ## Improvements * Greatly improves the performance of `svgstring()` (#58). * Clip paths now get a unique identifier to avoid collisions when multiple plots are included in a document (#67). * Raster graphics are now correctly cropped (#64) and handle transparency properly. * The dimensions of text fields are now hardcoded in the SVGs to prevent alignment issues. ## Bug fixes * `editSVG()` works again (#56). * The dashes in lines with `lwd < 1` are scaled better (#68). * Transparent blacks are written correctly (#62, #63). * Text is now scaled correctly (#72, #59). See also the new vignette on scaling. # svglite 1.1.0 * Text metrics now converted from points to pixels (#45, #48) - this fixes text alignment issues. * Intermediate outputs are always valid SVG (#53). * New `svgstring()` returns plot as a string (#40, @yixuan). * Use raster test compatible with older versions of R. * Add support for `clip()`. This also fixes a number of minor issues with grid graphics (@yixuan, #47 and #49). * Fix incorrect device size (#50). # svglite 1.0.0 svglite is fully featured svg graphics device that works on all platforms, forked from RSvgDevice. It supports all graphics device features: * All types of line dashing are supported (#15). All line end and line join styles are supported (#24). * Text is now coloured, and uses the same default fonts as R (Arial, Times New Roman, and Courier). Font metrics are computed using the gdtools package so that `plotmath()` and `strwidth()` now work. * Transparent colours are now generated with `rgba()` rather than using `stroke-opacity` and `fill-opacity` styles (#16). NA fills and colours are translated to "none". * `par(bg)` affects the background colour (#8). * Rasters are supported by embedding base64-encoded pngs in a data url (#2). * `polypath()` is now supported, which also allows the `showtext` package to render fonts correctly with this device (#36). We also made a few other tweaks to the rendered SVG: * Only the `viewBox` attribute of `` is set (not `width` and `height`): I'm reasonably certain this makes it easier to use in more places (#12). * Default styling properties are specified in a global `