-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathREADME.qmd
169 lines (132 loc) · 5.25 KB
/
README.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
---
title: "string2path"
format: gfm
editor: visual
default-image-extension: "" # quarto-dev/quarto-cli#6092
---
```{r}
#| label: "setup"
#| include: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.width = 6,
out.width = "75%",
dev = "ragg_png"
)
```
<!-- badges: start -->
[](https://github.com/yutannihilation/string2path/actions) [](https://lifecycle.r-lib.org/articles/stages.html#experimental) [](https://CRAN.R-project.org/package=string2path) [](https://yutannihilation.r-universe.dev)
<!-- badges: end -->
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](https://yutannihilation.github.io/savvy/guide/) to use these two Rust crates:
- [ttf-parser](https://github.com/harfbuzz/ttf-parser) for parsing font data. TrueType font (`.ttf`) and OpenType font (`.otf`) are supported.
- [lyon](https://github.com/nical/lyon/) for tessellation of polygons and flattening the curves.
## Installation
``` r
install.packages("string2path")
```
### Development version
The development version is available on [R-universe](https://r-universe.dev/):
``` r
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://www.rust-lang.org/tools/install> for the installation instructions.
## Example
### `string2path()`
```{r}
#| label: example
#| message: false
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 SS08"` 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"`).
```{r}
#| label: dump
dump_fontdb()
```
You can also specify the font file directly. Pomicons is a font available on [gabrielelana/pomicons](https://github.com/gabrielelana/pomicons), licensed under SIL OFL 1.1.
```{r}
#| label: icon_font
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()`
```{r}
#| label: example2
d <- string2fill("abc", "Iosevka SS08", 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()`
```{r}
#| label: string2stroke
#| animation.hook: gifski
for (w in 1:9 * 0.01) {
d <- string2stroke("abc","Iosevka SS08", 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](https://docs.rs/lyon_geom/latest/lyon_geom/#flattening).
```{r}
#| label: example3
#| animation.hook: gifski
for (tolerance in c(1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7)) {
d <- string2fill("abc", "Iosevka SS08", 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)
}
```