GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - yutannihilation/string2path: An experimental R package to convert string to path

BranchesTags
Open more actions menu

Latest commit

 

History

480 Commits
480 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
.vscode
.vscode
 
 
R
R
 
 
inst
inst
 
 
man
man
 
 
src
src
 
 
tests
tests
 
 
tools
tools
 
 
.Rbuildignore
.Rbuildignore
 
 
.gitignore
.gitignore
 
 
DESCRIPTION
DESCRIPTION
 
 
LICENSE
LICENSE
 
 
LICENSE.md
LICENSE.md
 
 
LICENSE.note
LICENSE.note
 
 
NAMESPACE
NAMESPACE
 
 
NEWS.md
NEWS.md
 
 
README.md
README.md
 
 
README.qmd
README.qmd
 
 
_pkgdown.yml
_pkgdown.yml
 
 
air.toml
air.toml
 
 
cleanup
cleanup
 
 
cleanup.win
cleanup.win
 
 
configure
configure
 
 
configure.win
configure.win
 
 
cran-comments.md
cran-comments.md
 
 
string2path.Rproj
string2path.Rproj
 
 
update_authors.R
update_authors.R
 
 

Repository files navigation

string2path

R-CMD-checkLifecycle: experimental CRAN status string2path status badge

The string2path R package converts a text to paths of the outlines of each glyph, based on a font data. Under the hood, this package is powered by the savvy framework to use these two Rust crates:

  • ttf-parser for parsing font data. TrueType font (.ttf) and OpenType font (.otf) are supported.
  • lyon for tessellation of polygons and flattening the curves.

Installation

install.packages("string2path")

Development version

The development version is available on R-universe:

install.packages("string2path",
  repos = c(
    yutannihilation = "https://yutannihilation.r-universe.dev",
    CRAN = "https://cloud.r-project.org"
  )
)

If you want to install from source, you need to have Rust toolchain installed before trying to install this package. See https://rust-lang.org/tools/install/ for the installation instructions.

Example

string2path()

library(string2path)
library(ggplot2)

d <- string2path("カラテが\n高まる。", "Noto Sans JP", font_weight = "bold")
d <- tibble::rowid_to_column(d)

ggplot(d) +
  geom_path(
    aes(x, y, group = path_id, colour = factor(glyph_id)),
    linewidth = 1.5
  ) +
  theme_minimal() +
  coord_equal() +
  theme(legend.position = "top") +
  scale_colour_viridis_d(option = "H")

library(gganimate)
d <- string2path("蹴", "Noto Sans JP")
d <- tibble::rowid_to_column(d)

ggplot(d) +
  geom_path(
    aes(x, y, group = path_id),
    linewidth = 2,
    colour = "purple2",
    lineend = "round"
  ) +
  theme_minimal() +
  coord_equal() +
  transition_reveal(rowid)

dump_fontdb()

Note that "Noto Sans JP" above (and "Iosevka" below) is the font installed on my local machine, so the same code might not run on your environment. You can use dump_fontdb() to see the available combination of font family (e.g. "Arial"), weight (e.g. "bold"), and style (e.g. "italic").

dump_fontdb()
#> # A tibble: 479 × 4
#>    index family           weight style 
#>    <int> <chr>             <dbl> <chr> 
#>  1     0 Arial               400 normal
#>  2     0 Arial               400 italic
#>  3     0 Arial               700 normal
#>  4     0 Arial               700 italic
#>  5     0 Arial               900 normal
#>  6     0 Sitka Subheading    400 normal
#>  7     0 Sitka Subheading    400 italic
#>  8     0 等线                300 normal
#>  9     0 等线                400 normal
#> 10     0 等线                700 normal
#> # ℹ 469 more rows

You can also specify the font file directly. Pomicons is a font available on gabrielelana/pomicons, licensed under SIL OFL 1.1.

pomicons_file <- here::here("fonts", "Pomicons.ttf")
if (!file.exists(pomicons_file)) {
  dir.create(dirname(pomicons_file))
  curl::curl_download(
    "https://github.com/gabrielelana/pomicons/blob/master/fonts/Pomicons.ttf?raw=true",
    destfile = pomicons_file
  )
}

d_tmp <- string2path("\uE007", pomicons_file)

ggplot(d_tmp) +
  geom_path(aes(x, y, group = path_id), linewidth = 5, colour = "#26d1a9") +
  theme_minimal() +
  coord_equal()

string2fill()

d <- string2fill(
  "abc",
  "Iosevka",
  font_weight = "bold",
  font_style = "italic"
)

ggplot(d) +
  geom_polygon(
    aes(x, y, group = triangle_id, fill = factor(triangle_id %% 7)),
    colour = "grey",
    linewidth = 0.1
  ) +
  theme_minimal() +
  coord_equal() +
  theme(legend.position = "none") +
  scale_fill_viridis_d(option = "H")

string2stroke()

for (w in 1:9 * 0.01) {
  d <- string2stroke(
    "abc",
    "Iosevka",
    font_weight = "bold",
    font_style = "italic",
    line_width = w
  )

  p <- ggplot(d) +
    geom_polygon(
      aes(x, y, group = triangle_id, fill = factor(triangle_id %% 2)),
      colour = "grey",
      linewidth = 0.1
    ) +
    theme_minimal() +
    coord_equal() +
    theme(legend.position = "none") +
    scale_fill_manual(values = c("purple", "pink"))
  plot(p)
}

tolerance

tolerance controls resolution of the tessellation. You can reduce tolerance to get higher resolutions. In most of the cases, 1e-5 ~ 1e-6 should be enough. For more details, please refer to lyon’s official document.

for (tolerance in c(1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7)) {
  d <- string2fill(
    "abc",
    "Iosevka",
    font_weight = "bold",
    font_style = "italic",
    tolerance = tolerance
  )

  p <- ggplot(d) +
    geom_polygon(
      aes(x, y, group = triangle_id),
      fill = "transparent",
      colour = "black",
      linewidth = 0.5
    ) +
    theme_minimal() +
    coord_equal() +
    ggtitle(paste0("tolerance: ", tolerance))
  plot(p)
}

About

An experimental R package to convert string to path

yutannihilation.github.io/string2path/

Resources

Readme
Activity

Stars

105 stars

Watchers

2 watching

Forks

5 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.