Skip to content

Truly independent web browser

LibGfx: Lay out glyph outline paths in Rust

Aliaksandr Kalenikc9a577b2ed99
diff --git a/Libraries/LibGfx/Path.cpp b/Libraries/LibGfx/Path.cpp
index aa514d4cd2e..b0c11cc38a6 100644
--- a/Libraries/LibGfx/Path.cpp
+++ b/Libraries/LibGfx/Path.cpp
@@ -401,7 +401,6 @@ public:
FFI::path_geometry_append_transformed_contours(path, outline, transform.data());
}
-private:
void draw_outline(u32 glyph_id, FFI::PathGeometry* outline) const
{
#ifdef AK_OS_MACOS
@@ -522,6 +521,7 @@ void* ladybird_gfx_path_stroke_to_fill(void const*, float thickness, i32 cap_sty
void ladybird_gfx_path_set_fill_type(void*, i32);
void* ladybird_gfx_path_create_from_glyph_runs(Gfx::FFI::FfiGlyphRun const* runs, size_t run_count);
void* ladybird_gfx_path_place_glyph_runs_along(void const* path, Gfx::FFI::FfiGlyphRun const* runs, size_t run_count, float offset);
+void ladybird_gfx_font_glyph_outlines(void const* font, Gfx::FFI::DrawGlyph const* glyphs, size_t glyph_count, void (*visit)(void*, size_t, Gfx::FFI::PathGeometryData const*), void* context);
}
extern "C" void ladybird_gfx_path_destroy(void* path)
@@ -670,6 +670,21 @@ extern "C" void* ladybird_gfx_path_place_glyph_runs_along(void const* path, Gfx:
return new Gfx::Path(static_cast<Gfx::Path const*>(path)->place_glyph_runs_along(adopt_glyph_runs(runs, run_count), offset));
}
+// Calls the callback with the outline of each glyph, in pixels with its origin on the baseline.
+extern "C" void ladybird_gfx_font_glyph_outlines(void const* font, Gfx::FFI::DrawGlyph const* glyphs, size_t glyph_count, void (*visit)(void*, size_t, Gfx::FFI::PathGeometryData const*), void* context)
+{
+ Gfx::GlyphOutliner outliner { *static_cast<Gfx::Font const*>(font) };
+ auto* outline = Gfx::FFI::path_geometry_create();
+ ScopeGuard destroy_outline = [&] { Gfx::FFI::path_geometry_destroy(outline); };
+ for (size_t index = 0; index < glyph_count; ++index) {
+ Gfx::FFI::path_geometry_clear(outline);
+ outliner.draw_outline(glyphs[index].glyph_id, outline);
+ Gfx::FFI::PathGeometryData data;
+ Gfx::FFI::path_geometry_data(outline, &data);
+ visit(context, index, &data);
+ }
+}
+
extern "C" void* ladybird_gfx_path_create_from_serialized_bytes(u8 const* bytes, size_t count)
{
return new Gfx::Path(Gfx::Path::from_serialized_bytes({ bytes, count }));
diff --git a/Libraries/LibGfx/Rust/src/path.rs b/Libraries/LibGfx/Rust/src/path.rs
index 5d089b2097b..bf90df18f8a 100644
--- a/Libraries/LibGfx/Rust/src/path.rs
+++ b/Libraries/LibGfx/Rust/src/path.rs
@@ -69,6 +69,13 @@ unsafe extern "C" {
run_count: usize,
offset: f32,
) -> *mut c_void;
+ fn ladybird_gfx_font_glyph_outlines(
+ font: *const c_void,
+ glyphs: *const crate::text_layout::DrawGlyph,
+ glyph_count: usize,
+ visit: unsafe extern "C" fn(*mut c_void, usize, *const ffi::PathGeometryData),
+ context: *mut c_void,
+ );
}
/// A shaped run of glyphs in one font, as the path operations below take one.
@@ -84,6 +91,50 @@ struct FfiGlyphRun {
glyph_count: usize,
}
+struct GlyphOutlineSink<'a> {
+ geometry: &'a mut PathGeometry,
+ transforms: &'a [AffineTransform],
+}
+
+unsafe extern "C" fn append_glyph_outline(
+ context: *mut c_void,
+ glyph_index: usize,
+ outline: *const ffi::PathGeometryData,
+) {
+ // SAFETY: `context` is the sink that append_glyph_outlines() passes, and the outline is a live path for this
+ // synchronous call.
+ unsafe {
+ let sink = &mut *context.cast::<GlyphOutlineSink<'_>>();
+ let transform = sink.transforms[glyph_index];
+ (*outline).append_mapped_to(sink.geometry, |point| transform.map_point(point));
+ }
+}
+
+/// Adds the outline of each glyph, in pixels with its origin on the baseline, mapped by the transform at the same
+/// index.
+fn append_glyph_outlines(
+ geometry: &mut PathGeometry,
+ font: &crate::font::FontHandle,
+ glyphs: &[crate::text_layout::DrawGlyph],
+ transforms: &[AffineTransform],
+) {
+ debug_assert_eq!(glyphs.len(), transforms.len());
+ if glyphs.is_empty() {
+ return;
+ }
+ let mut sink = GlyphOutlineSink { geometry, transforms };
+ // SAFETY: The handle keeps the font live, and the glyphs and the sink stay valid for the synchronous call.
+ unsafe {
+ ladybird_gfx_font_glyph_outlines(
+ font.as_raw(),
+ glyphs.as_ptr(),
+ glyphs.len(),
+ append_glyph_outline,
+ (&raw mut sink).cast(),
+ );
+ }
+}
+
fn to_ffi_glyph_runs(runs: &[GlyphRun]) -> Vec<FfiGlyphRun> {
runs.iter()
.map(|run| FfiGlyphRun {
@@ -507,6 +558,60 @@ impl Path {
Self::from_geometry(encoding::decode(bytes).unwrap_or_default())
}
+ /// The outlines of the given glyphs, as one path.
+ pub fn from_glyph_runs(runs: &[GlyphRun]) -> Path {
+ let mut geometry = PathGeometry::new();
+ for run in runs {
+ if run.font.is_invisible() {
+ continue;
+ }
+ let ascent = run.font.facts().ascent;
+ let transforms: Vec<_> = run
+ .glyphs
+ .iter()
+ .map(|glyph| AffineTransform {
+ values: [1.0, 0.0, 0.0, 1.0, glyph.x, glyph.y + ascent],
+ })
+ .collect();
+ append_glyph_outlines(&mut geometry, &run.font, &run.glyphs, &transforms);
+ }
+ geometry.update_state_from_contours();
+ Self::from_geometry(geometry)
+ }
+
+ /// The outlines of the given glyphs, laid along the first subpath of this path that has length. Each glyph has
+ /// its origin at the distance of its x position plus the offset, and its baseline along the direction of the
+ /// path there. The glyphs stop at the first one whose middle would go past the end.
+ pub fn place_glyph_runs_along(&self, runs: &[GlyphRun], offset: f32) -> Path {
+ let mut geometry = PathGeometry::new();
+ let Some(contour) = measure::ContourMeasure::contours_of(&self.geometry).next() else {
+ return Self::from_geometry(geometry);
+ };
+ let path_length = contour.length();
+ for run in runs {
+ if run.font.is_invisible() {
+ continue;
+ }
+ let mut transforms = Vec::with_capacity(run.glyphs.len());
+ for glyph in &run.glyphs {
+ let glyph_distance = offset + glyph.x;
+ if glyph_distance + (glyph.glyph_width / 2.0) > path_length {
+ break;
+ }
+ let (point, tangent) = contour.position_and_tangent(glyph_distance);
+ transforms.push(AffineTransform {
+ values: [tangent.x, tangent.y, -tangent.y, tangent.x, point.x, point.y],
+ });
+ }
+ append_glyph_outlines(&mut geometry, &run.font, &run.glyphs[..transforms.len()], &transforms);
+ if transforms.len() < run.glyphs.len() {
+ break;
+ }
+ }
+ geometry.update_state_from_contours();
+ Self::from_geometry(geometry)
+ }
+
/// The contours of the path, as C++ reads them, valid until the path changes or is dropped.
pub fn geometry_data(&self) -> ffi::PathGeometryData {
ffi::PathGeometryData::of(&self.geometry)
diff --git a/Libraries/LibGfx/Rust/src/path/ffi.rs b/Libraries/LibGfx/Rust/src/path/ffi.rs
index 3381822c59b..5af10c48792 100644
--- a/Libraries/LibGfx/Rust/src/path/ffi.rs
+++ b/Libraries/LibGfx/Rust/src/path/ffi.rs
@@ -10,7 +10,7 @@
//! libraries reaches them through the `ladybird_gfx_path_*` functions of LibGfx.
use super::encoding;
-use super::geometry::PathGeometry;
+use super::geometry::{PathGeometry, PathVerb};
use super::hit_test;
use super::measure::{self, ContourMeasure, PathVertex};
use super::stroke::{self, StrokeStyle};
@@ -521,6 +521,22 @@ impl PathGeometryData {
fill_rule: geometry.fill_rule() as u8,
}
}
+
+ /// Adds the contours to the path, with their points mapped.
+ ///
+ /// # Safety
+ /// The data must be that of a live path, as of() gives it.
+ pub(super) unsafe fn append_mapped_to(&self, geometry: &mut PathGeometry, map: impl Fn(FloatPoint) -> FloatPoint) {
+ // SAFETY: The caller keeps the arrays of the path live, and each of its verbs is a PathVerb.
+ let (verbs, points, conic_weights) = unsafe {
+ (
+ ak::slice_from_raw_parts(self.verbs.cast::<PathVerb>(), self.verb_count),
+ ak::slice_from_raw_parts(self.points, self.point_count),
+ ak::slice_from_raw_parts(self.conic_weights, self.conic_weight_count),
+ )
+ };
+ geometry.append_mapped_contours(verbs, points, conic_weights, map);
+ }
}
/// # Safety
diff --git a/Libraries/LibGfx/Rust/src/path/geometry.rs b/Libraries/LibGfx/Rust/src/path/geometry.rs
index e35e7d826ed..73ad4540f3b 100644
--- a/Libraries/LibGfx/Rust/src/path/geometry.rs
+++ b/Libraries/LibGfx/Rust/src/path/geometry.rs
@@ -373,7 +373,19 @@ impl PathGeometry {
/// Adds the contours of the other path with its points mapped, as for glyph outlines. The state of
/// Gfx::Path is left as it was; update_state_from_contours() sets it.
pub fn append_mapped(&mut self, other: &PathGeometry, map: impl Fn(FloatPoint) -> FloatPoint) {
- if other.verbs.is_empty() {
+ self.append_mapped_contours(&other.verbs, &other.points, &other.conic_weights, map);
+ }
+
+ /// Adds contours given as the verbs, points and conic weights of a path, with their points mapped, as
+ /// append_mapped() does.
+ pub fn append_mapped_contours(
+ &mut self,
+ verbs: &[PathVerb],
+ points: &[FloatPoint],
+ conic_weights: &[f32],
+ map: impl Fn(FloatPoint) -> FloatPoint,
+ ) {
+ if verbs.is_empty() {
return;
}
if self.verbs.last() == Some(&PathVerb::Move) {
@@ -381,9 +393,9 @@ impl PathGeometry {
self.pop_point();
}
let first_appended_point = self.points.len();
- self.verbs.extend_from_slice(&other.verbs);
- self.points.extend(other.points.iter().map(|point| map(*point)));
- self.conic_weights.extend_from_slice(&other.conic_weights);
+ self.verbs.extend_from_slice(verbs);
+ self.points.extend(points.iter().map(|point| map(*point)));
+ self.conic_weights.extend_from_slice(conic_weights);
self.has_only_finite_points &= self.points[first_appended_point..]
.iter()
.all(|point| is_finite_point(*point));