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

Add scripts to generate logos #39

Merged
merged 8 commits into from
Jan 7, 2025
Merged

Add scripts to generate logos #39

merged 8 commits into from
Jan 7, 2025

Conversation

llrs-roche
Copy link
Contributor

This PR adds two scripts:

  • scripts/convert_formats.R: To convert logos to svg and other formats (except ICO)
  • scripts/save_logos.R: To copy those logos to the repositories of packages at the right place (man/figures/logo.svg)

This is simplifies the process of SVG generation for all the packages.
If this is enough I can commit all the JPG and SVG generated with the script.

Helps with the packages that don't have SVG logos from: insightsengineering/nestdevs-tasks#93

Copy link

github-actions bot commented Dec 19, 2024

✅ All contributors have signed the CLA
Posted by the CLA Assistant Lite bot.

@llrs-roche
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

scripts/convert_formats.R Outdated Show resolved Hide resolved
scripts/save_logos.R Outdated Show resolved Hide resolved
@m7pr
Copy link

m7pr commented Dec 20, 2024

Hey @llrs-roche I wrote quickly some dirty code. I was trying to remove testing artifacts (like browser calls and exploratory outputs) ensures the script is clean and production-ready. I also put some repetitive code into functions. Lastly, by focusing only on incomplete cases and avoiding unnecessary loops or operations, the script is optimized for performance.

# Professional Script for Automatic Logo Conversion

library("magick")

# Function to retrieve file information from a directory
get_file_info <- function(path) {
  files <- list.files(path = path, full.names = TRUE, recursive = FALSE)
  tools::file_path_sans_ext(basename(files))
}

# Get file names (sans extensions) from each format directory
svg_files <- get_file_info("SVG")
png_files <- get_file_info("PNG")
ico_files <- get_file_info("ICO")
jpg_files <- get_file_info("jpg")

# Consolidate all unique file names
all_icons <- unique(c(svg_files, png_files, ico_files, jpg_files))

# Create a dataframe indicating the presence of each format
icons_df <- data.frame(
  icons = all_icons,
  svg = all_icons %in% svg_files,
  png = all_icons %in% png_files,
  ico = all_icons %in% ico_files,
  jpg = all_icons %in% jpg_files
)

# Filter files that need conversion (not present in all formats)
files_to_convert <- icons_df[rowSums(icons_df[, -1]) != ncol(icons_df) - 1, ]

# Function to convert and save logos in missing formats
convert_logos <- function(row) {
  file_name <- row["icons"]
  available_formats <- names(row[-1])[unlist(row[-1])]
  missing_formats <- setdiff(c("svg", "png", "jpeg", "ico"), available_formats)

  # Determine the best base format to use for conversion
  format_preference <- c("svg", "jpg", "png", "ico")
  base_format <- format_preference[min(match(available_formats, format_preference, nomatch = Inf))]

  input_file <- file.path(toupper(base_format), paste0(file_name, ".", base_format))
  image <- if (base_format == "svg") {
    image_read_svg(input_file)
  } else {
    image_read(input_file)
  }

  message("Converting ", file_name, " from ", base_format, " to ", paste(missing_formats, collapse = ", "))

  for (format in missing_formats) {
    if (format == "ico") next # Skip ICO conversion (optional handling)
    output_file <- file.path(toupper(format), paste0(file_name, ".", format))
    converted_image <- image_convert(image, format = format, matte = TRUE)
    image_write(converted_image, path = output_file, format = format, depth = 8)
  }

  image_destroy(image)
}

# Perform conversion for each incomplete file
apply(files_to_convert, 1, convert_logos)

# Move SVG logos to corresponding package directories
move_logos_to_packages <- function() {
  svg_logos <- list.files("SVG", full.names = TRUE, recursive = FALSE)
  package_dirs <- list.dirs("..", recursive = FALSE)
  package_dirs <- package_dirs[!endsWith(package_dirs, ".Rcheck")]

  logo_names <- tools::file_path_sans_ext(basename(svg_logos))
  matching_packages <- intersect(tolower(logo_names), basename(package_dirs))

  destination_paths <- file.path("..", matching_packages, "man", "figures", "logo.svg")
  origin_paths <- svg_logos[tools::file_path_sans_ext(basename(svg_logos)) %in% matching_packages]

  valid_destinations <- dir.exists(dirname(destination_paths)) & !file.exists(destination_paths)

  if (any(valid_destinations)) {
    file.copy(from = origin_paths[valid_destinations], to = destination_paths[valid_destinations])
  }

  # Remove existing PNG logos if applicable
  png_logos <- file.path("..", matching_packages, "man", "figures", "logo.png")
  if (any(file.exists(png_logos))) {
    file.remove(png_logos[file.exists(png_logos)])
  }
}

move_logos_to_packages()

@llrs-roche
Copy link
Contributor Author

Using apply converts the data.frame to a matrix which then leads to an error, I tweaked it a little bit.
As I don't see any comment on the conversion of the files (even if the svg are an encoded image and not a really xml content) I'll add the new logos formats on this PR.

m7pr pushed a commit to insightsengineering/ggplot2.utils that referenced this pull request Dec 23, 2024
This replaces the png logo by the new generated svg logo (see
insightsengineering/hex-stickers#39) to a slight
reduction on package size.

Related to
insightsengineering/nestdevs-tasks#93



![image](https://github.com/user-attachments/assets/220eda56-b5e5-4a30-b5a7-6d8cce871a6d)
m7pr pushed a commit to insightsengineering/cards that referenced this pull request Dec 23, 2024
**What changes are proposed in this pull request?**
Use svg logo instead of the png to reduce package size

This is in reference to
insightsengineering/nestdevs-tasks#93
The logo was generated with the script at:
insightsengineering/hex-stickers#39


![image](https://github.com/user-attachments/assets/acdbc13c-7068-4454-9ac6-0dd73463360e)




--------------------------------------------------------------------------------

Pre-review Checklist (if item does not apply, mark is as complete)
- [ ] **All** GitHub Action workflows pass with a ✅
- [x] PR branch has pulled the most recent updates from master branch:
`usethis::pr_merge_main()`
- [x] If a bug was fixed, a unit test was added.
- [x] Code coverage is suitable for any new functions/features
(generally, 100% coverage for new code): `devtools::test_coverage()`
- [x] Request a reviewer

Reviewer Checklist (if item does not apply, mark is as complete)

- [ ] If a bug was fixed, a unit test was added.
- [ ] Run `pkgdown::build_site()`. Check the R console for errors, and
review the rendered website.
- [ ] Code coverage is suitable for any new functions/features:
`devtools::test_coverage()`

When the branch is ready to be merged:
- [ ] Update `NEWS.md` with the changes from this pull request under the
heading "`# cards (development version)`". If there is an issue
associated with the pull request, reference it in parentheses at the end
update (see `NEWS.md` for examples).
- [ ] **All** GitHub Action workflows pass with a ✅
- [ ] Approve Pull Request
- [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
m7pr pushed a commit to insightsengineering/cardx that referenced this pull request Dec 23, 2024
**What changes are proposed in this pull request?**
Uses svg logo instead of the png to reduce package size

This is in reference to
insightsengineering/nestdevs-tasks#93
The logo was generated with the script at:
insightsengineering/hex-stickers#39



--------------------------------------------------------------------------------

Pre-review Checklist (if item does not apply, mark is as complete)
- [x] **All** GitHub Action workflows pass with a ✅
- [x] PR branch has pulled the most recent updates from master branch:
`usethis::pr_merge_main()`
- [x] If a bug was fixed, a unit test was added.
- [x] If a new `ard_*()` function was added, it passes the ARD
structural checks from `cards::check_ard_structure()`.
- [x] If a new `ard_*()` function was added, `set_cli_abort_call()` has
been set.
- [x] If a new `ard_*()` function was added and it depends on another
package (such as, `broom`), `is_pkg_installed("broom")` has been set in
the function call and the following added to the roxygen comments:
`@examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg =
"broom""))`
- [x] Code coverage is suitable for any new functions/features
(generally, 100% coverage for new code): `devtools::test_coverage()`

Reviewer Checklist (if item does not apply, mark is as complete)

- [ ] If a bug was fixed, a unit test was added.
- [ ] Code coverage is suitable for any new functions/features:
`devtools::test_coverage()`

When the branch is ready to be merged:
- [ ] Update `NEWS.md` with the changes from this pull request under the
heading "`# cardx (development version)`". If there is an issue
associated with the pull request, reference it in parentheses at the end
update (see `NEWS.md` for examples).
- [ ] **All** GitHub Action workflows pass with a ✅
- [ ] Approve Pull Request
- [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
scripts/convert_formats.R Outdated Show resolved Hide resolved
Copy link

@m7pr m7pr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one comment

@llrs-roche llrs-roche merged commit 866cfe8 into main Jan 7, 2025
1 check passed
@llrs-roche llrs-roche deleted the formats branch January 7, 2025 11:29
@github-actions github-actions bot locked and limited conversation to collaborators Jan 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants