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} #28

Closed
wants to merge 10 commits into from
Closed
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ggflags
Type: Package
Version: 0.0.3
Version: 0.0.4
Title: Plot flags of the world in ggplot2
Authors@R: c(
person("Baptiste", "Auguie",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# ggflags 0.0.4

- Some more internal rejiggering to ensure that you can use ggflags without explicitly loading the package
- If you're not loading the package and need to use it to plot things, prefix the package functions with `ggflags::` (eg. `ggflags::geom_flag`).
- If you need to access the internal flag list, either load it into the global environment as `lflags` using `data(lflags)` or access it prefixed with `ggflags::lflags`

# ggflags 0.0.3

- Adds Rémi Thériault as an author and contributor!
Expand Down
5 changes: 1 addition & 4 deletions R/geom_flag.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ makeContent.flag <- function(x) {
seq_along(x$country),
function(ii) {
grImport2::pictureGrob(
picture = .flaglist[[x$country[[ii]]]],
picture = ggflags::lflags[[x$country[[ii]]]],
x = x$x[ii], y = x$y[ii],
width = x$size[ii] * grid::unit(1, "mm"),
height = x$size[ii] * grid::unit(1, "mm"),
Expand All @@ -77,6 +77,3 @@ makeContent.flag <- function(x) {
)
grid::setChildren(x, do.call(grid::gList, flag_pics))
}

#' @noRd
utils::globalVariables(c(".flaglist"))
10 changes: 5 additions & 5 deletions R/lflags.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#' List of country flags
#'
#' @docType data
#' @format A data frame with X rows and X variables:
#' @format A list of 256 elements of class S4 (`grImport::Picture`), with:
#' \describe{
#' \item{state.name}{the name of the state}
#' \item{state.abb}{the name of the abbreviation}
#' \item{state.regex}{the regex for that state}
#' \item{content}{PictureGroup}
#' \item{defs}{PictureDefinitions}
#' \item{summary}{PictureSummary}
#' ...
#' }
".flaglist"
"lflags"
Binary file modified data/lflags.rda
Binary file not shown.
55 changes: 52 additions & 3 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))

save(.flaglist, file="lflags.rda")
save(.flaglist, file = "lflags.rda")

# cleanup
unlink("circle-flags.zip")
unlink("circle-flags", recursive = TRUE)
unlink("svg", recursive = TRUE)
unlink("cairo", recursive = TRUE)
14 changes: 7 additions & 7 deletions man/dot-flaglist.Rd → man/lflags.Rd

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