Skip to content

Commit 1f20e00

Browse files
committed
update the embedded svglite implementation
1 parent 9412f37 commit 1f20e00

29 files changed

+8168
-655
lines changed

DESCRIPTION

+3-9
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,13 @@ Depends:
3030
Imports:
3131
devtools,
3232
diffobj,
33-
fontquiver (>= 0.2.0),
34-
freetypeharfbuzz (>= 0.2.5),
35-
gdtools,
3633
glue,
3734
grDevices,
3835
htmlwidgets (>= 0.6),
3936
htmltools,
4037
purrr (>= 0.2.0),
4138
rlang,
4239
R6,
43-
Rcpp,
4440
shiny,
4541
testthat (>= 1.0.0),
4642
usethis (>= 1.4.0),
@@ -53,11 +49,9 @@ Suggests:
5349
withr,
5450
yaml
5551
LinkingTo:
56-
freetypeharfbuzz,
57-
gdtools,
58-
Rcpp,
59-
BH
60-
RoxygenNote: 7.1.1
52+
cpp11
53+
RoxygenNote: 7.1.1.9001
6154
Roxygen: list(markdown = TRUE)
6255
URL: https://github.com/r-lib/vdiffr
6356
BugReports: https://github.com/r-lib/vdiffr/issues
57+
SystemRequirements: C++11

NAMESPACE

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export(widget_toggle_)
2727
export(write_svg)
2828
import(rlang)
2929
importFrom(R6,R6Class)
30-
importFrom(Rcpp,sourceCpp)
31-
importFrom(gdtools,raster_view)
3230
importFrom(glue,glue)
3331
importFrom(purrr,compact)
3432
importFrom(purrr,every)

R/RcppExports.R

-31
This file was deleted.

R/cpp11.R

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by cpp11: do not edit by hand
2+
3+
compare_files <- function(expected, test) {
4+
.Call(`_vdiffr_compare_files`, expected, test)
5+
}
6+
7+
svglite_ <- function(file, bg, width, height, pointsize, standalone, always_valid) {
8+
.Call(`_vdiffr_svglite_`, file, bg, width, height, pointsize, standalone, always_valid)
9+
}
10+
11+
svgstring_ <- function(env, bg, width, height, pointsize, standalone) {
12+
.Call(`_vdiffr_svgstring_`, env, bg, width, height, pointsize, standalone)
13+
}
14+
15+
get_svg_content <- function(p) {
16+
.Call(`_vdiffr_get_svg_content`, p)
17+
}

R/embed-svglite.R

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#' @importFrom gdtools raster_view
2-
NULL
3-
41
svglite_path <- function(...) {
52
file.path("R", "svglite", ...)
63
}

R/svg.R

+1-6
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,10 @@ make_testcase_file <- function(fig_name) {
2626
#'
2727
#' @export
2828
write_svg <- function(plot, file, title = "") {
29-
svglite(file, user_fonts = get_aliases())
29+
svglite(file)
3030
on.exit(grDevices::dev.off())
3131
print_plot(plot, title)
3232
}
33-
get_aliases <- function() {
34-
aliases <- fontquiver::font_families("Liberation")
35-
aliases$symbol$symbol <- fontquiver::font_symbol("Symbola")
36-
aliases
37-
}
3833

3934
print_plot <- function(p, title = "") {
4035
UseMethod("print_plot")

R/svglite/SVG.R

+26-42
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,43 @@
1313
#' used to create the svg, and the computer used to render the
1414
#' svg. See the \code{fonts} vignette for more information.
1515
#'
16-
#' @param file The file where output will appear.
16+
#' @param filename The file where output will appear.
1717
#' @param height,width Height and width in inches.
1818
#' @param bg Default background color for the plot (defaults to "white").
1919
#' @param pointsize Default point size.
2020
#' @param standalone Produce a standalone svg file? If \code{FALSE}, omits
2121
#' xml header and default namespace.
22-
#' @param system_fonts Named list of font names to be aliased with
23-
#' fonts installed on your system. If unspecified, the R default
24-
#' families \code{sans}, \code{serif}, \code{mono} and \code{symbol}
25-
#' are aliased to the family returned by
26-
#' \code{\link[gdtools]{match_family}()}.
27-
#' @param user_fonts Named list of fonts to be aliased with font files
28-
#' provided by the user rather than fonts properly installed on the
29-
#' system. The aliases can be fonts from the fontquiver package,
30-
#' strings containing a path to a font file, or a list containing
31-
#' \code{name} and \code{file} elements with \code{name} indicating
32-
#' the font alias in the SVG output and \code{file} the path to a
33-
#' font file.
22+
#' @param always_valid Should the svgfile be a valid svg file while it is being
23+
#' written to? Setting this to `TRUE` will incur a considerable performance
24+
#' hit (>50% additional rendering time) so this should only be set to `TRUE`
25+
#' if the file is being parsed while it is still being written to.
26+
#' @param file Identical to `filename`. Provided for backward compatibility.
3427
#' @references \emph{W3C Scalable Vector Graphics (SVG)}:
35-
#' \url{http://www.w3.org/Graphics/SVG/Overview.htm8}
28+
#' \url{http://www.w3.org/Graphics/SVG/}
3629
#' @author This driver was written by T Jake Luciani
3730
#' \email{jakeluciani@@yahoo.com} 2012: updated by Matthieu Decorde
3831
#' \email{matthieu.decorde@@ens-lyon.fr}
3932
#' @seealso \code{\link{pictex}}, \code{\link{postscript}}, \code{\link{Devices}}
33+
#'
4034
#' @examples
4135
#' # Save to file
42-
#' svglite("Rplots.svg")
36+
#' svglite(tempfile("Rplots.svg"))
4337
#' plot(1:11, (-5:5)^2, type = 'b', main = "Simple Example")
4438
#' dev.off()
4539
#'
46-
#' # Supply system font aliases. First check the font can be located:
47-
#' gdtools::match_family("Verdana")
48-
#'
49-
#' # Then supply a list of aliases:
50-
#' fonts <- list(sans = "Verdana", mono = "Times New Roman")
51-
#' svglite("Rplots.svg", system_fonts = fonts)
52-
#' plot.new()
53-
#' text(0.5, 0.5, "Some text", family = "mono")
54-
#' dev.off()
55-
#'
56-
#' # See the fonts vignettes for more options to deal with fonts
57-
#'
5840
#' @keywords device
59-
#' @useDynLib svglite
60-
#' @importFrom Rcpp sourceCpp
61-
#' @importFrom gdtools raster_view
41+
#' @useDynLib svglite, .registration = TRUE
42+
#' @importFrom systemfonts match_font
6243
#' @export
63-
svglite <- function(file = "Rplots.svg", width = 10, height = 8,
44+
svglite <- function(filename = "Rplot%03d.svg", width = 10, height = 8,
6445
bg = "white", pointsize = 12, standalone = TRUE,
65-
system_fonts = list(), user_fonts = list()) {
66-
aliases <- validate_aliases(system_fonts, user_fonts)
67-
invisible(svglite_(file, bg, width, height, pointsize, standalone, aliases))
46+
always_valid = FALSE, file) {
47+
if (!missing(file)) {
48+
filename <- file
49+
}
50+
if (invalid_filename(filename))
51+
stop("invalid 'file': ", filename)
52+
invisible(svglite_(filename, bg, width, height, pointsize, standalone, always_valid))
6853
}
6954

7055
#' Access current SVG as a string.
@@ -75,7 +60,6 @@ svglite <- function(file = "Rplots.svg", width = 10, height = 8,
7560
#' See \code{\link{svglite}()} documentation for information about
7661
#' specifying fonts.
7762
#'
78-
#' @param ... Arguments passed on to \code{\link{svglite}}.
7963
#' @return A function with no arguments: call the function to get the
8064
#' current value of the string.
8165
#' @examples
@@ -92,19 +76,19 @@ svglite <- function(file = "Rplots.svg", width = 10, height = 8,
9276
#' @inheritParams svglite
9377
#' @export
9478
svgstring <- function(width = 10, height = 8, bg = "white",
95-
pointsize = 12, standalone = TRUE,
96-
system_fonts = list(), user_fonts = list()) {
97-
aliases <- validate_aliases(system_fonts, user_fonts)
98-
79+
pointsize = 12, standalone = TRUE) {
9980
env <- new.env(parent = emptyenv())
10081
string_src <- svgstring_(env, width = width, height = height, bg = bg,
101-
pointsize = pointsize, standalone = standalone, aliases = aliases)
82+
pointsize = pointsize, standalone = standalone)
10283

10384
function() {
104-
svgstr <- if(env$is_closed) env$svg_string else get_svg_content(string_src)
85+
svgstr <- env$svg_string
86+
if(!env$is_closed) {
87+
svgstr <- c(svgstr, get_svg_content(string_src))
88+
}
10589
structure(svgstr, class = "svg")
10690
}
10791
}
10892

10993
#' @export
110-
print.svg <- function(x, ...) cat(x, "\n", sep = "")
94+
print.svg <- function(x, ...) cat(x, sep = "\n")

R/svglite/fonts.R

-98
This file was deleted.

R/svglite/inlineSVG.R

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
#' Run plotting code and view svg in RStudio Viewer or web broswer.
1+
#' Run plotting code and view svg in RStudio Viewer or web browser.
22
#'
33
#' This is useful primarily for testing. Requires the \code{htmltools}
44
#' package.
55
#'
6-
#' @inheritParams svglite
76
#' @param code Plotting code to execute.
87
#' @param ... Other arguments passed on to \code{\link{svglite}}.
8+
#' @keywords internal
99
#' @export
1010
#' @examples
11-
#' if (require("htmltools")) {
11+
#' if (interactive() && require("htmltools")) {
1212
#' htmlSVG(plot(1:10))
1313
#' htmlSVG(hist(rnorm(100)))
1414
#' }
@@ -26,6 +26,7 @@ htmlSVG <- function(code, ...) {
2626
#' @return A \code{xml2::xml_document} object.
2727
#' @inheritParams htmlSVG
2828
#' @inheritParams svglite
29+
#' @keywords internal
2930
#' @export
3031
#' @examples
3132
#' if (require("xml2")) {
@@ -48,6 +49,7 @@ xmlSVG <- function(code, ..., standalone = FALSE, height = 7, width = 7) {
4849
#'
4950
#' @inheritParams htmlSVG
5051
#' @inheritParams svglite
52+
#' @keywords internal
5153
#' @export
5254
#' @examples
5355
#' if (interactive()) {
@@ -72,6 +74,7 @@ editSVG <- function(code, ..., width = NA, height = NA) {
7274
#' alternative to \code{\link{svgstring}()}.
7375
#'
7476
#' @inheritParams htmlSVG
77+
#' @keywords internal
7578
#' @export
7679
#' @examples
7780
#' stringSVG(plot(1:10))

0 commit comments

Comments
 (0)