Something went wrong. Try again.
Exploring the idea of generated web rendering engine.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139//! The engine as a library: parse an HTML document, style it, lay it out against a//! viewport, and read back one border-box rect per element that generates a box.//!//! This is the whole stable surface; `dom` and `layout` stay free to change underneath//! it. The C interface (`webengine_layout`, `webengine_boxes_free`, declared in `include/webengine.h`) is//! the same call.//!//! Scope is the engine's: no text, no images, no script, no paint. A document is laid out//! once; there is no incremental update yet (`roadmap.md` -> "Substrate rework", item 4).
use std::ffi::{c_char, CStr};
use dom::Document;use layout::boxtree::BoxTree;use layout::fragment;
/// One element's border box in viewport coordinates, CSS px.#[repr(C)]#[derive(Debug, Clone, Copy, PartialEq)]pub struct WebengineBox { /// The element's position among the document's elements in document order, `<html>` /// first (`dom::Document::elements_in_document_order`). pub element: u32, pub x: f64, pub y: f64, pub width: f64, pub height: f64,}
/// Lays `html` out against a `viewport` of `(width, height)` CSS px and returns one box/// per element that generates one, in fragment-tree order (a parent before its/// children). An element with `display: none` returns none.pub fn lay_out(html: &str, viewport: (f64, f64)) -> Vec<WebengineBox> { let document = Document::parse_with_viewport(html, viewport); let tree = BoxTree::build(&document); let root = fragment::layout(&document, &tree, viewport); let order = document.elements_in_document_order(); let mut boxes = Vec::new(); root.walk_absolute(0.0, 0.0, &mut |node, x, y, width, height| { if let Some(element) = order.iter().position(|&e| e == node) { boxes.push(WebengineBox { element: element as u32, x, y, width, height }); } }); boxes}
/// C interface to [`lay_out`]. `html` is a NUL-terminated UTF-8 string. Returns an array/// of `*count` boxes, to be released with [`webengine_boxes_free`], or null with `*count` 0 when/// `html` is null or not UTF-8, or when layout panics.////// # Safety////// `html` must be null or point at a NUL-terminated string, and `count` must point at a/// writable `size_t`.#[no_mangle]pub unsafe extern "C" fn webengine_layout(html: *const c_char, width: f64, height: f64, count: *mut usize) -> *mut WebengineBox { *count = 0; if html.is_null() { return std::ptr::null_mut(); } let Ok(html) = CStr::from_ptr(html).to_str() else { return std::ptr::null_mut(); }; let Ok(boxes) = std::panic::catch_unwind(|| lay_out(html, (width, height))) else { return std::ptr::null_mut(); }; let boxes = boxes.into_boxed_slice(); *count = boxes.len(); Box::into_raw(boxes) as *mut WebengineBox}
/// Releases an array [`webengine_layout`] returned. Null is ignored.////// # Safety////// `boxes` and `count` must be exactly what one `webengine_layout` call returned, and the array/// must not be used afterwards.#[no_mangle]pub unsafe extern "C" fn webengine_boxes_free(boxes: *mut WebengineBox, count: usize) { if !boxes.is_null() { drop(Box::from_raw(std::ptr::slice_from_raw_parts_mut(boxes, count))); }}
/// Allocates `len` bytes in this library's memory, for a host with no allocator of its/// own there -- a WebAssembly host writing the HTML string (and `webengine_layout`'s `count`)/// into the module before calling it. Release with [`webengine_dealloc`].#[no_mangle]pub extern "C" fn webengine_alloc(len: usize) -> *mut u8 { let mut bytes = std::mem::ManuallyDrop::new(Vec::<u8>::with_capacity(len)); bytes.as_mut_ptr()}
/// Releases what [`webengine_alloc`] returned.////// # Safety////// `ptr` and `len` must be exactly one `webengine_alloc` call's pointer and argument.#[no_mangle]pub unsafe extern "C" fn webengine_dealloc(ptr: *mut u8, len: usize) { drop(Vec::from_raw_parts(ptr, 0, len));}
#[cfg(test)]mod tests { use super::*;
const HTML: &str = "<!doctype html><style>body{margin:0}.a{width:50vw;height:40px;margin-left:10px}</style>\ <div class=a></div>";
#[test] fn lays_out_one_document_at_two_viewports() { for width in [800.0, 400.0] { let boxes = lay_out(HTML, (width, 600.0)); // html, body, div; head and style generate no box. assert_eq!(boxes.iter().map(|b| b.element).collect::<Vec<_>>(), [0, 3, 4]); let div = boxes[2]; assert_eq!((div.x, div.y, div.width, div.height), (10.0, 0.0, width / 2.0, 40.0)); } }
#[test] fn c_interface_matches_the_rust_one() { let html = std::ffi::CString::new(HTML).unwrap(); let mut count = 0; let boxes = unsafe { webengine_layout(html.as_ptr(), 800.0, 600.0, &mut count) }; let from_c = unsafe { std::slice::from_raw_parts(boxes, count) }.to_vec(); unsafe { webengine_boxes_free(boxes, count) }; assert_eq!(from_c, lay_out(HTML, (800.0, 600.0))); }
#[test] fn c_interface_refuses_null() { let mut count = 7; assert!(unsafe { webengine_layout(std::ptr::null(), 800.0, 600.0, &mut count) }.is_null()); assert_eq!(count, 0); }}