Skip to content

Commit

Permalink
Do not interpolate path ID (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation authored Sep 28, 2024
1 parent 2917128 commit 22f97ec
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 63 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* `string2path()` now generates the same outline as `string2fill()` and
`string2stroke()` (#69).

* `path_id` is now 1-origin.

# string2path 0.1.8

* This is a maintenance release to comply with the CRAN repository policy.
Expand Down
14 changes: 13 additions & 1 deletion src/rust/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct LyonPathBuilder<T: BuildPath> {
pub cur_glyph_id: u32,
pub cur_path_id: u32,

// path ID to glyph ID
pub glyph_id_map: HashMap<u32, u32>,

// This transformation is of COLR format.
base_transform: lyon::geom::euclid::Transform2D<f32, UnknownUnit, UnknownUnit>,

Expand Down Expand Up @@ -176,6 +179,7 @@ impl LyonPathBuilderForPath {
cur_layer: 0,
cur_glyph_id: 0,
cur_path_id: 0,
glyph_id_map: HashMap::new(),
base_transform: lyon::geom::euclid::Transform2D::identity(),
scale_factor: 1.,
offset_x: 0.,
Expand Down Expand Up @@ -215,6 +219,7 @@ impl LyonPathBuilderForStrokeAndFill {
cur_layer: 0,
cur_glyph_id: 0,
cur_path_id: 0,
glyph_id_map: HashMap::new(),
base_transform: lyon::geom::euclid::Transform2D::identity(),
scale_factor: 1.,
offset_x: 0.,
Expand All @@ -229,6 +234,14 @@ impl LyonPathBuilderForStrokeAndFill {

impl<T: BuildPath> ttf_parser::OutlineBuilder for LyonPathBuilder<T> {
fn move_to(&mut self, x: f32, y: f32) {
self.cur_path_id += 1;

// While it's not very cool, path ID can be re-calculated later. So, if
// the corresponding glyph ID is recorded here, it can be acquired when
// constructing a result data frame.
self.glyph_id_map
.insert(self.cur_path_id, self.cur_glyph_id);

let at = self.point(x, y);
let custom_attributes = &self.ids();
self.cur_builder().begin(at, custom_attributes);
Expand Down Expand Up @@ -259,7 +272,6 @@ impl<T: BuildPath> ttf_parser::OutlineBuilder for LyonPathBuilder<T> {

fn close(&mut self) {
self.cur_builder().end(true);
self.cur_path_id += 1;
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/rust/src/into_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ impl LyonPathBuilderForPath {
}) => format!("#{red:02x}{green:02x}{blue:02x}{alpha:02x}",),
None => "#00000000".to_string(),
};
for p in path.iter_with_attributes() {
let mut cur_path_id: u32 = 0;
for p in path.iter() {
let point = match p {
lyon::path::Event::Begin { at } => Some(at),
lyon::path::Event::Begin { at } => {
cur_path_id += 1;
Some(at)
}
lyon::path::Event::Line { to, .. } => Some(to),
lyon::path::Event::Quadratic { to, .. } => Some(to),
lyon::path::Event::Cubic { to, .. } => Some(to),
Expand All @@ -45,11 +49,14 @@ impl LyonPathBuilderForPath {
}
};

if let Some(point) = point {
result.glyph_id.push(point.1[0] as _);
result.path_id.push(point.1[1] as _);
result.x.push(point.0.x as _);
result.y.push(point.0.y as _);
if let Some(pos) = point {
result.x.push(pos.x as _);
result.y.push(pos.y as _);
result
.glyph_id
.push(*self.glyph_id_map.get(&cur_path_id).unwrap_or(&0) as _);
result.path_id.push(cur_path_id as _);

if let Some(v) = result.color.as_mut() {
v.push(paint_color.clone())
}
Expand Down
110 changes: 55 additions & 55 deletions tests/testthat/_snaps/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
# A tibble: 4 x 4
x y glyph_id path_id
<dbl> <dbl> <int> <int>
1 0 0 0 0
2 0.800 0.800 0 0
3 0 0.800 0 0
4 0 0 0 0
1 0 0 0 1
2 0.800 0.800 0 1
3 0 0.800 0 1
4 0 0 0 1

---

Expand All @@ -19,24 +19,24 @@
# A tibble: 18 x 5
x y glyph_id path_id triangle_id
<dbl> <dbl> <int> <int> <int>
1 0.836 0.815 0 0 0
2 0.764 0.785 0 0 0
3 0.0150 0.785 0 0 0
4 0.836 0.815 0 0 1
5 0.0150 0.785 0 0 1
6 -0.0150 0.815 0 0 1
7 -0.0150 0.815 0 0 2
8 0.0150 0.785 0 0 2
9 0.0150 0.0362 0 0 2
10 -0.0150 0.815 0 0 3
11 0.0150 0.0362 0 0 3
12 -0.0150 -0.0362 0 0 3
13 -0.0150 -0.0362 0 0 4
14 0.0150 0.0362 0 0 4
15 0.764 0.785 0 0 4
16 -0.0150 -0.0362 0 0 5
17 0.764 0.785 0 0 5
18 0.836 0.815 0 0 5
1 0.836 0.815 0 1 0
2 0.764 0.785 0 1 0
3 0.0150 0.785 0 1 0
4 0.836 0.815 0 1 1
5 0.0150 0.785 0 1 1
6 -0.0150 0.815 0 1 1
7 -0.0150 0.815 0 1 2
8 0.0150 0.785 0 1 2
9 0.0150 0.0362 0 1 2
10 -0.0150 0.815 0 1 3
11 0.0150 0.0362 0 1 3
12 -0.0150 -0.0362 0 1 3
13 -0.0150 -0.0362 0 1 4
14 0.0150 0.0362 0 1 4
15 0.764 0.785 0 1 4
16 -0.0150 -0.0362 0 1 5
17 0.764 0.785 0 1 5
18 0.836 0.815 0 1 5

---

Expand All @@ -46,9 +46,9 @@
# A tibble: 3 x 5
x y glyph_id path_id triangle_id
<dbl> <dbl> <int> <int> <int>
1 0 0 0 0 0
2 0 0.800 0 0 0
3 0.800 0.800 0 0 0
1 0 0 0 1 0
2 0 0.800 0 1 0
3 0.800 0.800 0 1 0

# the data extracted from installed font are as expected

Expand All @@ -58,16 +58,16 @@
# A tibble: 27 x 4
x y glyph_id path_id
<dbl> <dbl> <int> <int>
1 -0.00131 0 0 0
2 0.245 0.641 0 0
3 0.336 0.641 0 0
4 0.598 0 0 0
5 0.502 0 0 0
6 0.427 0.194 0 0
7 0.159 0.194 0 0
8 0.0887 0 0 0
9 -0.00131 0 0 0
10 0.184 0.263 0 1
1 -0.00131 0 0 1
2 0.245 0.641 0 1
3 0.336 0.641 0 1
4 0.598 0 0 1
5 0.502 0 0 1
6 0.427 0.194 0 1
7 0.159 0.194 0 1
8 0.0887 0 0 1
9 -0.00131 0 0 1
10 0.184 0.263 0 2
# i 17 more rows

---
Expand All @@ -78,16 +78,16 @@
# A tibble: 150 x 5
x y glyph_id path_id triangle_id
<dbl> <dbl> <int> <int> <int>
1 0.255 0.626 0 0 0
2 0.234 0.656 0 0 0
3 0.346 0.656 0 0 0
4 0.255 0.626 0 0 1
5 0.346 0.656 0 0 1
6 0.326 0.626 0 0 1
7 0.326 0.626 0 0 2
8 0.346 0.656 0 0 2
9 0.621 -0.0150 0 0 2
10 0.326 0.626 0 0 3
1 0.255 0.626 0 1 0
2 0.234 0.656 0 1 0
3 0.346 0.656 0 1 0
4 0.255 0.626 0 1 1
5 0.346 0.656 0 1 1
6 0.326 0.626 0 1 1
7 0.326 0.626 0 1 2
8 0.346 0.656 0 1 2
9 0.621 -0.0150 0 1 2
10 0.326 0.626 0 1 3
# i 140 more rows

---
Expand All @@ -98,15 +98,15 @@
# A tibble: 75 x 5
x y glyph_id path_id triangle_id
<dbl> <dbl> <int> <int> <int>
1 0.0887 0 0 0 0
2 -0.00131 0 0 0 0
3 0.159 0.194 0 0 0
4 0.427 0.194 0 0 1
5 0.159 0.194 0 0 1
6 0.184 0.263 0 1 1
7 0.159 0.194 0 0 2
8 -0.00131 0 0 0 2
9 0.184 0.263 0 1 2
10 0.184 0.263 0 1 3
1 0.0887 0 0 1 0
2 -0.00131 0 0 1 0
3 0.159 0.194 0 1 0
4 0.427 0.194 0 1 1
5 0.159 0.194 0 1 1
6 0.184 0.263 0 2 1
7 0.159 0.194 0 1 2
8 -0.00131 0 0 1 2
9 0.184 0.263 0 2 2
10 0.184 0.263 0 2 3
# i 65 more rows

0 comments on commit 22f97ec

Please sign in to comment.