Skip to content

Port resvg to harfrust #922

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
- name: Install toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.67.1
toolchain: 1.79.0
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this (and the other rust-version modifications) be 1.75.0 not 79?


- name: Build
run: cargo build
Expand Down
123 changes: 85 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.45.1"
keywords = ["svg", "render", "raster", "c-api"]
license.workspace = true
edition = "2021"
rust-version = "1.67.1"
rust-version = "1.79.0"
workspace = "../.."

[lib]
Expand Down
2 changes: 1 addition & 1 deletion crates/resvg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.45.1"
keywords = ["svg", "render", "raster"]
license.workspace = true
edition = "2021"
rust-version = "1.67.1"
rust-version = "1.79.0"
description = "An SVG rendering library."
repository = "https://github.com/linebender/resvg"
exclude = ["tests"]
Expand Down
7 changes: 4 additions & 3 deletions crates/usvg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.45.1"
keywords = ["svg"]
license.workspace = true
edition = "2021"
rust-version = "1.65.0"
rust-version = "1.79.0"
description = "An SVG simplification library."
categories = ["multimedia::images"]
repository = "https://github.com/linebender/resvg"
Expand Down Expand Up @@ -37,7 +37,8 @@ siphasher = "1.0" # perfect hash implementation

# text
fontdb = { version = "0.23.0", default-features = false, optional = true }
rustybuzz = { version = "0.20.1", optional = true }
ttf-parser = { version = "0.25.1", optional = true }
harfrust = { version = "0.1", optional = true }
unicode-bidi = { version = "0.3", optional = true }
unicode-script = { version = "0.5", optional = true }
unicode-vo = { version = "0.1", optional = true }
Expand All @@ -49,7 +50,7 @@ once_cell = "1.5"
default = ["text", "system-fonts", "memmap-fonts"]
# Enables text-to-path conversion support.
# Adds around 400KiB to your binary.
text = ["fontdb", "rustybuzz", "unicode-bidi", "unicode-script", "unicode-vo"]
text = ["fontdb", "ttf-parser", "harfrust", "unicode-bidi", "unicode-script", "unicode-vo"]
# Enables system fonts loading.
system-fonts = ["fontdb/fs", "fontdb/fontconfig"]
# Enables font files memmaping for faster loading.
Expand Down
1 change: 0 additions & 1 deletion crates/usvg/src/text/colr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::parser::OptionLog;
use rustybuzz::ttf_parser;

struct Builder<'a>(&'a mut String);

Expand Down
3 changes: 1 addition & 2 deletions crates/usvg/src/text/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use std::mem;
use std::sync::Arc;

use fontdb::{Database, ID};
use rustybuzz::ttf_parser;
use rustybuzz::ttf_parser::{GlyphId, RasterImageFormat, RgbaColor};
use tiny_skia_path::{NonZeroRect, Size, Transform};
use ttf_parser::{GlyphId, RasterImageFormat, RgbaColor};
use xmlwriter::XmlWriter;

use crate::text::colr::GlyphPainter;
Expand Down
35 changes: 24 additions & 11 deletions crates/usvg/src/text/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use std::sync::Arc;

use fontdb::{Database, ID};
use kurbo::{ParamCurve, ParamCurveArclen, ParamCurveDeriv};
use rustybuzz::ttf_parser;
use rustybuzz::ttf_parser::{GlyphId, Tag};
use strict_num::NonZeroPositiveF32;
use tiny_skia_path::{NonZeroRect, Transform};
use ttf_parser::GlyphId;
use unicode_script::UnicodeScript;

use crate::tree::{BBox, IsValidLength};
Expand Down Expand Up @@ -1361,7 +1360,9 @@ fn shape_text_with_font(
fontdb: &fontdb::Database,
) -> Option<Vec<Glyph>> {
fontdb.with_face_data(font.id, |font_data, face_index| -> Option<Vec<Glyph>> {
let rb_font = rustybuzz::Face::from_slice(font_data, face_index)?;
use harfrust::{Feature, ShaperData, ShaperInstance, Tag, UnicodeBuffer, Variation};

let hr_font = harfrust::FontRef::from_index(font_data, face_index).ok()?;

let bidi_info = unicode_bidi::BidiInfo::new(text, Some(unicode_bidi::Level::ltr()));
let paragraph = &bidi_info.paragraphs[0];
Expand All @@ -1377,26 +1378,38 @@ fn shape_text_with_font(
}

let ltr = levels[run.start].is_ltr();
let hb_direction = if ltr {
rustybuzz::Direction::LeftToRight
let direction = if ltr {
harfrust::Direction::LeftToRight
} else {
rustybuzz::Direction::RightToLeft
harfrust::Direction::RightToLeft
};

let mut buffer = rustybuzz::UnicodeBuffer::new();
let mut buffer = UnicodeBuffer::new();
buffer.push_str(sub_text);
buffer.set_direction(hb_direction);
buffer.set_direction(direction);

let mut features = Vec::new();
if small_caps {
features.push(rustybuzz::Feature::new(Tag::from_bytes(b"smcp"), 1, ..));
features.push(Feature::new(Tag::new(b"smcp"), 1, ..));
}

if !apply_kerning {
features.push(rustybuzz::Feature::new(Tag::from_bytes(b"kern"), 0, ..));
features.push(Feature::new(Tag::new(b"kern"), 0, ..));
}

let output = rustybuzz::shape(&rb_font, &features, buffer);
let variations: Vec<Variation> = Vec::new();

let shaper_data = ShaperData::new(&hr_font);
let instance_data = ShaperInstance::from_variations(&hr_font, &variations);
let shaper = shaper_data
// Initialize the builder for the given font
.shaper(&hr_font)
// Set the instance
.instance(Some(&instance_data))
// Build the shaper
.build();

let output = shaper.shape(buffer, &features);

let positions = output.glyph_positions();
let infos = output.glyph_infos();
Expand Down
Loading