Skip to content

Fix crash caused by glyph splitting #929

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

Merged
merged 2 commits into from
Jul 14, 2025
Merged
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
1 change: 1 addition & 0 deletions crates/resvg/tests/integration/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,7 @@ use crate::render;
#[test] fn text_text_filter_bbox() { assert_eq!(render("tests/text/text/filter-bbox"), 0); }
#[test] fn text_text_ligatures_handling_in_mixed_fonts_1() { assert_eq!(render("tests/text/text/ligatures-handling-in-mixed-fonts-1"), 0); }
#[test] fn text_text_ligatures_handling_in_mixed_fonts_2() { assert_eq!(render("tests/text/text/ligatures-handling-in-mixed-fonts-2"), 0); }
#[test] fn text_text_glyph_splitting() { assert_eq!(render("tests/text/text/glyph-splitting"), 0); }
#[test] fn text_text_mm_coordinates() { assert_eq!(render("tests/text/text/mm-coordinates"), 0); }
#[test] fn text_text_nested() { assert_eq!(render("tests/text/text/nested"), 0); }
#[test] fn text_text_no_coordinates() { assert_eq!(render("tests/text/text/no-coordinates"), 0); }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions crates/resvg/tests/tests/text/text/glyph-splitting.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions crates/usvg/src/text/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022 the Resvg Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::num::NonZeroU16;
use std::sync::Arc;

Expand Down Expand Up @@ -882,6 +882,11 @@ fn process_chunk(
// but some can use `fi` (U+FB01) instead.
// Meaning that during merging we have to overwrite not individual glyphs, but clusters.

// Glyph splitting assigns distinct glyphs to the same index in the original text, we need to
// store previously used indices to make sure we do not re-use the same index while overwriting
// span glyphs.
let mut positions = HashSet::new();

let mut glyphs = Vec::new();
for span in &chunk.spans {
let font = match fonts_cache.get(&span.font) {
Expand All @@ -904,17 +909,25 @@ fn process_chunk(
continue;
}

positions.clear();

// Overwrite span's glyphs.
let mut iter = tmp_glyphs.into_iter();
while let Some(new_glyph) = iter.next() {
if !span_contains(span, new_glyph.byte_idx) {
continue;
}

let Some(idx) = glyphs.iter().position(|g| g.byte_idx == new_glyph.byte_idx) else {
let Some(idx) = glyphs
.iter()
.position(|g| g.byte_idx == new_glyph.byte_idx)
.filter(|pos| !positions.contains(pos))
else {
continue;
};

positions.insert(idx);

let prev_cluster_len = glyphs[idx].cluster_len;
if prev_cluster_len < new_glyph.cluster_len {
// If the new font represents the same cluster with fewer glyphs
Expand Down