Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP proposed use of {rsvg} (continued) #31

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Description: A ggplot2 extension that allows you to plot the flags of the world.
as geom_point does, requiring, at minimum, a two-letter lowercase country code for the country aesthetic,
and x and y aesthetics. You can also adjust the size.
LazyData: TRUE
LazyDataCompression: xz
Depends:
R (>= 3.5.0)
Imports:
Expand Down
2 changes: 1 addition & 1 deletion R/lflags.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' List of country flags
#'
#' @docType data
#' @format A list of 256 elements of class S4 (`grImport::Picture`), with:
#' @format A list of 248 elements of class S4 (`grImport::Picture`), with:
#' \describe{
#' \item{content}{PictureGroup}
#' \item{defs}{PictureDefinitions}
Expand Down
Binary file modified data/lflags.rda
Binary file not shown.
59 changes: 54 additions & 5 deletions inst/store_flags.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
library(grConvert)
library(rsvg)
library(grImport2)

download.file(
"https://github.com/HatScripts/circle-flags/archive/refs/heads/gh-pages.zip",
"circle-flags.zip")
unzip("circle-flags.zip", exdir = "circle-flags")
dir.create("svg", showWarnings = FALSE)
flag_svgs <- list.files("circle-flags/circle-flags-gh-pages/flags",
pattern = glob2rx("*.svg"), full.names = TRUE)
file.copy(flag_svgs, "svg")

lf <- list.files(path = "svg", pattern = glob2rx("*.svg"), full.names = FALSE)

# note - looking to restrict the use of flags to the existing 2-letter codes
# until i can follow up a few things on licensing
lf <- lf[which(nchar(lf) == 6)]

process_flag <- function(input_file) {

# get svg string
svg_string <- paste(readLines(file.path("svg", input_file)), collapse = "\n")

# bail if it doesn't look like a valid svg
# (likely because a couple of them are symbolic links on github.com)
stopifnot("Not a valid SVG" = length(grep("^<svg", svg_string)) > 0)

# swap circular <mask> for a <clipPath>
# (assu)
# svg_string <- sub(
# "<mask id=\"a\"><circle cx=\"256\" cy=\"256\" r=\"256\" fill=\"#fff\"/></mask>",
# "<clipPath id=\"a\"><circle cx=\"256\" cy=\"256\" r=\"256\"/></clipPath>",
# svg_string,
# fixed = TRUE)
# svg_string <- sub(
# pattern = "<g mask=\"url(#a)",
# replacement = "<g clip-path=\"url(#a)",
# svg_string,
# fixed = TRUE)

# convert to raw and write out
rsvg_svg(charToRaw(svg_string), file.path("cairo", input_file))
}

dir.create("cairo", showWarnings = FALSE)

# process each flag
for (srcfile in lf)
{
print(srcfile)
convertPicture(paste0("svg/", srcfile), paste0("cairo/", srcfile))
try({ process_flag(srcfile) })
}

# finally, get the processed flags, import with grImport2 and save out
lf <- list.files(path = "cairo", pattern = glob2rx("*.svg"), full.names = TRUE)

.flaglist <- lapply(lf, readPicture)
names(.flaglist) <- tolower(gsub("^cairo/|\\.svg", "", lf))
lflags <- lapply(lf, readPicture)
names(lflags) <- tolower(gsub("^cairo/|\\.svg", "", lf))

save(lflags, file = "lflags.rda", compress = "xz")

save(.flaglist, file="lflags.rda")
# cleanup
unlink("circle-flags.zip")
unlink("circle-flags", recursive = TRUE)
unlink("svg", recursive = TRUE)
unlink("cairo", recursive = TRUE)