Skip to content

The headless browser for AI agents and web scraping

perf(render): optimize text rendering & feat(render): add border support

SGavrl6d33783ddff9
diff --git a/Cargo.lock b/Cargo.lock
index 31d35ed9..90fcb681 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2,6 +2,22 @@
# It is not intended for manual editing.
version = 4
+[[package]]
+name = "ab_glyph"
+version = "0.2.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "01c0457472c38ea5bd1c3b5ada5e368271cb550be7a4ca4a0b4634e9913f6cc2"
+dependencies = [
+ "ab_glyph_rasterizer",
+ "owned_ttf_parser",
+]
+
+[[package]]
+name = "ab_glyph_rasterizer"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "366ffbaa4442f4684d91e2cd7c5ea7c4ed8add41959a31447066e279e432b618"
+
[[package]]
name = "adler2"
version = "2.0.1"
@@ -1830,6 +1846,7 @@ dependencies = [
name = "obscura-render"
version = "0.1.0"
dependencies = [
+ "ab_glyph",
"obscura-dom",
"taffy",
"tiny-skia",
@@ -1870,6 +1887,15 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a80800c0488c3a21695ea981a54918fbb37abf04f4d0720c453632255e2ff0e"
+[[package]]
+name = "owned_ttf_parser"
+version = "0.25.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "36820e9051aca1014ddc75770aab4d68bc1e9e632f0f5627c4086bc216fb583b"
+dependencies = [
+ "ttf-parser",
+]
+
[[package]]
name = "parking_lot"
version = "0.12.5"
@@ -3138,6 +3164,12 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
+[[package]]
+name = "ttf-parser"
+version = "0.25.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31"
+
[[package]]
name = "tungstenite"
version = "0.26.2"
diff --git a/Cargo.toml b/Cargo.toml
index 0019a272..5ce079d9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -36,6 +36,8 @@ cssparser = "0.34"
taffy = "0.7"
# CPU-only pure-Rust 2D rasterizer for the render layer's paint step.
tiny-skia = "0.12"
+# Pure-Rust glyph rasterizer for the render layer's text step.
+ab_glyph = "0.2"
tokio = { version = "1", features = ["full"] }
tokio-tungstenite = "0.26"
# No "cookies" feature: obscura manages cookies through its own CookieJar
diff --git a/crates/obscura-render/Cargo.toml b/crates/obscura-render/Cargo.toml
index a3bc18f7..d7bdef50 100644
--- a/crates/obscura-render/Cargo.toml
+++ b/crates/obscura-render/Cargo.toml
@@ -8,14 +8,15 @@ description = "Optional scoped render layer for Obscura: layout (taffy) and, wit
[features]
default = []
-# Rasterize the laid-out tree to a pixmap (tiny-skia). Off by default; the
-# layout core (getBoundingClientRect etc.) works without it.
-paint = ["dep:tiny-skia"]
+# Rasterize the laid-out tree to a pixmap (tiny-skia) with text (ab_glyph).
+# Off by default; the layout core (getBoundingClientRect etc.) works without it.
+paint = ["dep:tiny-skia", "dep:ab_glyph"]
[dependencies]
taffy = { workspace = true }
obscura-dom = { workspace = true }
tiny-skia = { workspace = true, optional = true }
+ab_glyph = { workspace = true, optional = true }
[lib]
path = "src/lib.rs"
diff --git a/crates/obscura-render/src/lib.rs b/crates/obscura-render/src/lib.rs
index b59ed922..b5862d4f 100644
--- a/crates/obscura-render/src/lib.rs
+++ b/crates/obscura-render/src/lib.rs
@@ -65,6 +65,10 @@ pub struct LayoutStyle {
pub border: Edges,
/// RGBA for the paint step. Parsed always (cheap), used only with `paint`.
pub background_color: Option<[u8; 4]>,
+ /// Foreground (text) color for the paint step.
+ pub color: Option<[u8; 4]>,
+ /// Border color for the paint step.
+ pub border_color: Option<[u8; 4]>,
}
/// A node in the input layout tree. `text` is carried for the paint phase; it
diff --git a/crates/obscura-render/src/paint.rs b/crates/obscura-render/src/paint.rs
index f3c5b8f7..f8f4d3e0 100644
--- a/crates/obscura-render/src/paint.rs
+++ b/crates/obscura-render/src/paint.rs
@@ -11,7 +11,7 @@ use ab_glyph::{Font, FontRef, PxScale, ScaleFont};
static FONT_BYTES: &[u8] = include_bytes!("../assets/dejavu-sans.ttf");
-use crate::{compute_style, layout_dom};
+use crate::layout_dom;
/// Render `tree` at `viewport` (width, height) in CSS pixels to a Pixmap, or
/// None if the viewport is zero-sized.
@@ -33,8 +33,8 @@ pub fn paint_dom(tree: &DomTree, viewport: (f32, f32)) -> Option<Pixmap> {
continue;
}
- let bg = match bg_color_for(tree, nid) {
- Some(c) => c,
+ let name = match node.as_element() {
+ Some(name) => name,
None => continue,
};
let rect = match laid.rects.get(&nid) {
@@ -45,27 +45,55 @@ pub fn paint_dom(tree: &DomTree, viewport: (f32, f32)) -> Option<Pixmap> {
Some(r) => r,
None => continue,
};
- let mut path = PathBuilder::new();
- path.push_rect(box_rect);
- let path = match path.finish() {
- Some(p) => p,
- None => continue,
- };
- let mut paint = Paint::default();
- paint.set_color(Color::from_rgba8(bg[0], bg[1], bg[2], bg[3]));
- paint.anti_alias = false;
- pixmap.fill_path(&path, &paint, FillRule::Winding, Transform::identity(), None);
+
+ let style = crate::compute_style(name.local.as_ref(), node.get_attribute("style"));
+
+ if let Some(bg) = style.background_color {
+ let mut path = PathBuilder::new();
+ path.push_rect(box_rect);
+ if let Some(path) = path.finish() {
+ let mut paint = Paint::default();
+ paint.set_color(Color::from_rgba8(bg[0], bg[1], bg[2], bg[3]));
+ paint.anti_alias = false;
+ pixmap.fill_path(&path, &paint, FillRule::Winding, Transform::identity(), None);
+ }
+ }
+
+ if style.border.top > 0.0 || style.border.right > 0.0 || style.border.bottom > 0.0 || style.border.left > 0.0 {
+ let bc = style.border_color.or(style.color).unwrap_or([0, 0, 0, 255]);
+ let mut paint = Paint::default();
+ paint.set_color(Color::from_rgba8(bc[0], bc[1], bc[2], bc[3]));
+ paint.anti_alias = false;
+
+ let mut path = PathBuilder::new();
+ if style.border.top > 0.0 {
+ if let Some(r) = Rect::from_xywh(rect.x, rect.y, rect.width, style.border.top) {
+ path.push_rect(r);
+ }
+ }
+ if style.border.right > 0.0 {
+ if let Some(r) = Rect::from_xywh(rect.x + rect.width - style.border.right, rect.y, style.border.right, rect.height) {
+ path.push_rect(r);
+ }
+ }
+ if style.border.bottom > 0.0 {
+ if let Some(r) = Rect::from_xywh(rect.x, rect.y + rect.height - style.border.bottom, rect.width, style.border.bottom) {
+ path.push_rect(r);
+ }
+ }
+ if style.border.left > 0.0 {
+ if let Some(r) = Rect::from_xywh(rect.x, rect.y, style.border.left, rect.height) {
+ path.push_rect(r);
+ }
+ }
+ if let Some(path) = path.finish() {
+ pixmap.fill_path(&path, &paint, FillRule::Winding, Transform::identity(), None);
+ }
+ }
}
Some(pixmap)
}
-fn bg_color_for(tree: &DomTree, nid: obscura_dom::tree::NodeId) -> Option<[u8; 4]> {
- let node = tree.get_node(nid)?;
- let name = node.as_element()?;
- let style = compute_style(name.local.as_ref(), node.get_attribute("style"));
- style.background_color
-}
-
/// Render `tree` at `viewport` to PNG bytes (RGBA 8-bit). Returns None if the
/// viewport is zero-sized. Convenience over `paint_dom` + `encode_png`.
pub fn screenshot_png(tree: &DomTree, viewport: (f32, f32)) -> Option<Vec<u8>> {
@@ -105,6 +133,11 @@ fn draw_text(pixmap: &mut Pixmap, text: &str, x: f32, y: f32, color: [u8; 4]) {
let scaled_font = font.as_scaled(scale);
let mut caret = ab_glyph::point(x, y + scaled_font.ascent());
+ let width = pixmap.width() as i32;
+ let height = pixmap.height() as i32;
+ let pixels = pixmap.pixels_mut();
+ let (r, g, b, a_full) = (color[0], color[1], color[2], color[3]);
+
for c in text.chars() {
if c.is_control() {
continue;
@@ -117,10 +150,29 @@ fn draw_text(pixmap: &mut Pixmap, text: &str, x: f32, y: f32, color: [u8; 4]) {
outlined.draw(|gx, gy, c| {
let px = (bounds.min.x + gx as f32) as i32;
let py = (bounds.min.y + gy as f32) as i32;
- if px >= 0 && px < pixmap.width() as i32 && py >= 0 && py < pixmap.height() as i32 {
- let alpha = (color[3] as f32 * c) as u8;
+ if px >= 0 && px < width && py >= 0 && py < height {
+ let alpha = (a_full as f32 * c) as u8;
if alpha > 0 {
- blend_pixel(pixmap, px as u32, py as u32, color[0], color[1], color[2], alpha);
+ let idx = (py * width + px) as usize;
+ let dst = pixels[idx];
+
+ let src_a = alpha as u32;
+ let src_r = (r as u32 * src_a) / 255;
+ let src_g = (g as u32 * src_a) / 255;
+ let src_b = (b as u32 * src_a) / 255;
+
+ let dst_a = dst.alpha() as u32;
+ let out_a = src_a + (dst_a * (255 - src_a) / 255);
+
+ if out_a > 0 {
+ let out_r = src_r + (dst.red() as u32 * (255 - src_a) / 255);
+ let out_g = src_g + (dst.green() as u32 * (255 - src_a) / 255);
+ let out_b = src_b + (dst.blue() as u32 * (255 - src_a) / 255);
+
+ pixels[idx] = tiny_skia::PremultipliedColorU8::from_rgba(
+ out_r as u8, out_g as u8, out_b as u8, out_a as u8
+ ).unwrap_or_else(|| tiny_skia::PremultipliedColorU8::from_rgba(0,0,0,0).unwrap());
+ }
}
}
});
@@ -131,32 +183,6 @@ fn draw_text(pixmap: &mut Pixmap, text: &str, x: f32, y: f32, color: [u8; 4]) {
}
}
-fn blend_pixel(pixmap: &mut Pixmap, x: u32, y: u32, r: u8, g: u8, b: u8, a: u8) {
- let width = pixmap.width();
- let pixels = pixmap.pixels_mut();
- let idx = (y * width + x) as usize;
- let dst = pixels[idx];
-
- // source pre-multiplied
- let src_a = a as u32;
- let src_r = (r as u32 * src_a) / 255;
- let src_g = (g as u32 * src_a) / 255;
- let src_b = (b as u32 * src_a) / 255;
-
- let dst_a = dst.alpha() as u32;
-
- let out_a = src_a + (dst_a * (255 - src_a) / 255);
- if out_a > 0 {
- let out_r = src_r + (dst.red() as u32 * (255 - src_a) / 255);
- let out_g = src_g + (dst.green() as u32 * (255 - src_a) / 255);
- let out_b = src_b + (dst.blue() as u32 * (255 - src_a) / 255);
-
- pixels[idx] = tiny_skia::PremultipliedColorU8::from_rgba(
- out_r as u8, out_g as u8, out_b as u8, out_a as u8
- ).unwrap_or_else(|| tiny_skia::PremultipliedColorU8::from_rgba(0,0,0,0).unwrap());
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/crates/obscura-render/src/style.rs b/crates/obscura-render/src/style.rs
index bf46a3c9..d134ab93 100644
--- a/crates/obscura-render/src/style.rs
+++ b/crates/obscura-render/src/style.rs
@@ -73,6 +73,7 @@ fn apply_value(style: &mut LayoutStyle, name: &str, value: &str) {
"border-left-width" | "border-left" => set_edge(&mut style.border, Side::Left, px(value)),
"background-color" | "background" => style.background_color = parse_color(value),
"color" => style.color = parse_color(value),
+ "border-color" => style.border_color = parse_color(value),
_ => {}
}
}