Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Sep 18, 2024
1 parent 992d5bf commit 2747e8d
Show file tree
Hide file tree
Showing 49 changed files with 333 additions and 680 deletions.
703 changes: 209 additions & 494 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ gosub_shared = { path = "./crates/gosub_shared", features = [] }
gosub_config = { path = "./crates/gosub_config", features = [] }
gosub_html5 = { path = "./crates/gosub_html5", features = [] }
gosub_css3 = { path = "./crates/gosub_css3", features = [] }
gosub_styling = { path = "./crates/gosub_styling", features = [] }
gosub_jsapi = { path = "./crates/gosub_jsapi", features = [] }
gosub_testing = { path = "./crates/gosub_testing", features = [] }
gosub_rendering = { path = "crates/gosub_render_utils", features = [] }
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod wrapper;

use gosub_html5::parser::document::{Document, DocumentBuilder};
use gosub_html5::parser::Html5Parser;
use gosub_rendering::render_tree::{Node, NodeType, RenderTree, TreeIterator};
use gosub_rendering::macos_render_tree::{Node, NodeType, RenderTree, TreeIterator};
use gosub_shared::byte_stream::{ByteStream, Encoding};
use wrapper::node::CNode;

Expand Down
4 changes: 2 additions & 2 deletions crates/gosub_bindings/src/wrapper/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use gosub_rendering::render_tree::properties::{Position, Rectangle};
use gosub_rendering::render_tree::{text::TextNode, Node};
use gosub_rendering::macos_render_tree::properties::{Position, Rectangle};
use gosub_rendering::macos_render_tree::{text::TextNode, Node};

use crate::wrapper::{text::CTextNode, CNodeType};

Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_bindings/src/wrapper/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use gosub_rendering::render_tree::text::TextNode;
use gosub_rendering::macos_render_tree::text::TextNode;
use std::ffi::c_char;
use std::ffi::CString;

Expand Down
4 changes: 2 additions & 2 deletions crates/gosub_css3/src/matcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod property_definitions;
mod shorthands;
pub mod property_definitions;
pub mod shorthands;
pub mod styling;
mod syntax;
mod syntax_matcher;
Expand Down
10 changes: 5 additions & 5 deletions crates/gosub_css3/src/matcher/styling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::matcher::property_definitions::get_css_definitions;
use crate::stylesheet::{Combinator, CssSelector, CssSelectorPart, CssValue, MatcherType, Specificity};

// Matches a complete selector (all parts) against the given node(id)
pub(crate) fn match_selector<D: Document<S>, S: CssSystem>(
document: DocumentHandle<D, S>,
pub(crate) fn match_selector<D: Document<C>, C: CssSystem>(
document: DocumentHandle<D, C>,
node_id: NodeId,
selector: &CssSelector,
) -> (bool, Specificity) {
Expand All @@ -39,8 +39,8 @@ fn consume<'a, T>(this: &mut &'a [T]) -> Option<&'a T> {
}

/// Returns true when the given node matches the part(s)
fn match_selector_parts<D: Document<S>, S: CssSystem>(
handle: DocumentHandle<D, S>,
fn match_selector_parts<D: Document<C>, C: CssSystem>(
handle: DocumentHandle<D, C>,
node_id: NodeId,
mut parts: &[CssSelectorPart],
) -> bool {
Expand Down Expand Up @@ -78,7 +78,7 @@ fn match_selector_parts<D: Document<S>, S: CssSystem>(
true
}

fn match_selector_part<'a, D: Document<S>, S: CssSystem>(
fn match_selector_part<'a, D: Document<C>, C: CssSystem>(
part: &CssSelectorPart,
current_node: &D::Node,
doc: &'a D,
Expand Down
10 changes: 9 additions & 1 deletion crates/gosub_css3/src/stylesheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ pub struct CssStylesheet {
pub parse_log: Vec<CssLog>
}

impl gosub_shared::traits::css3::CssStylesheet for CssStylesheet {}
impl gosub_shared::traits::css3::CssStylesheet for CssStylesheet {
fn origin(&self) -> CssOrigin {
self.origin
}

fn location(&self) -> &str {
&self.location
}
}

/// A CSS rule, which contains a list of selectors and a list of declarations
#[derive(Debug, PartialEq, Clone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (

const (
exportType = Both
ResourcePath = "crates/gosub_styling/resources/definitions"
ResourcePath = "crates/gosub_css3/resources/definitions"
SingleFilePath = ResourcePath + "/definitions.json"
MultiFileDir = ResourcePath
MultiFilePrefix = "definitions_"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const (
REPO = "w3c/webref"
LOCATION = "ed/css"
PATCH_LOCATION = "ed/csspatches"
CACHE_DIR = "crates/gosub_styling/resources/cache"
CACHE_DIR = "crates/gosub_css3/resources/cache"
CACHE_INDEX_FILE = CACHE_DIR + "/index/cache_index.json"
CUSTOM_PATCH_DIR = "crates/gosub_styling/resources/patches"
CUSTOM_PATCH_DIR = "crates/gosub_css3/resources/patches"
BRANCH = "curated"
)

Expand Down
18 changes: 13 additions & 5 deletions crates/gosub_html5/src/document/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ impl<C: CssSystem> Document<C> for DocumentImpl<C> {
self.arena.node_mut(node_id)
}

fn stylesheets(&self) -> &Vec<C::Stylesheet> {
&self.stylesheets
}

fn add_stylesheet(&mut self, stylesheet: C::Stylesheet) {
self.stylesheets.push(stylesheet);
}
Expand Down Expand Up @@ -259,6 +263,10 @@ impl<C: CssSystem> Document<C> for DocumentImpl<C> {
None
}

fn node_count(&self) -> usize {
self.arena.node_count()
}

/// Register a node
fn register_node(&mut self, _node: Self::Node) -> NodeId {
todo!("register_node() not implemented");
Expand Down Expand Up @@ -461,15 +469,15 @@ fn internal_visit<C: CssSystem>(
/// WARNING: mutations in the document would be reflected
/// in the iterator. It's advised to consume the entire iterator
/// before mutating the document again.
pub struct TreeIterator<D: Clone + Document<S>, S: CssSystem> {
pub struct TreeIterator<D: Clone + Document<C>, C: CssSystem> {
current_node_id: Option<NodeId>,
node_stack: Vec<NodeId>,
document: DocumentHandle<D, S>,
document: DocumentHandle<D, C>,
}

impl<D: Document<S> + Clone, S: CssSystem> TreeIterator<D, S> {
impl<D: Document<C> + Clone, C: CssSystem> TreeIterator<D, C> {
#[must_use]
pub fn new(doc: DocumentHandle<D, S>) -> Self {
pub fn new(doc: DocumentHandle<D, C>) -> Self {
Self {
current_node_id: None,
document: doc.clone(),
Expand All @@ -478,7 +486,7 @@ impl<D: Document<S> + Clone, S: CssSystem> TreeIterator<D, S> {
}
}

impl<D: Document<S> + Clone, S: CssSystem> Iterator for TreeIterator<D, S> {
impl<D: Document<C> + Clone, C: CssSystem> Iterator for TreeIterator<D, C> {
type Item = NodeId;

fn next(&mut self) -> Option<NodeId> {
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_html5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub mod tokenizer;
pub mod writer;

/// Parses the given HTML string and returns a handle to the resulting DOM tree.
pub fn html_compile<S: CssSystem>(html: &str) -> DocumentHandle<DocumentImpl<S>, S> {
pub fn html_compile<C: CssSystem>(html: &str) -> DocumentHandle<DocumentImpl<C>, C> {
let mut stream = ByteStream::new(Encoding::UTF8, None);
stream.read_from_str(html, Some(Encoding::UTF8));
stream.close();
Expand Down
6 changes: 6 additions & 0 deletions crates/gosub_html5/src/node/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub struct NodeArena<N: Node<C>, C: CssSystem> {
_marker: PhantomData<C>
}

impl<C: CssSystem, N: Node<C>>NodeArena<N, C> {
pub fn node_count(&self) -> usize {
self.nodes.len()
}
}

impl<C: CssSystem, N: Node<C>> PartialEq for NodeArena<N, C> {
fn eq(&self, other: &Self) -> bool {
if self.next_id != other.next_id {
Expand Down
3 changes: 3 additions & 0 deletions crates/gosub_render_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ license = "MIT"
[dependencies]
gosub_shared = { path = "../gosub_shared" }
gosub_html5 = { path = "../gosub_html5" }
gosub_css3 = { path = "../gosub_css3" }
gosub_typeface = { path = "../gosub_typeface" }
smallvec = "1.13.2"
image = "0.25.2"
raw-window-handle = "0.6.2"
log = "0.4.14"
anyhow = "1.0.89"

1 change: 1 addition & 0 deletions crates/gosub_render_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use smallvec::SmallVec;
pub mod geo;
pub mod layout;
pub mod svg;
mod render_tree;

pub trait WindowHandle: HasDisplayHandle + HasWindowHandle + Send + Sync + Clone {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ use std::fmt::{Debug, Formatter};

use log::warn;

use gosub_css3::matcher::property_definitions::get_css_definitions;
use gosub_css3::matcher::shorthands::FixList;
use gosub_css3::matcher::styling::{CssProperty, CssProperties, DeclarationProperty, prop_is_inherit};
use gosub_css3::stylesheet::{CssDeclaration, CssStylesheet, CssValue, Specificity};
use gosub_css3::stylesheet::{CssDeclaration, CssValue, Specificity};
use gosub_html5::document::document::TreeIterator;
use gosub_html5::node::data::element::ElementData;
use gosub_render_backend::geo::Size;
use gosub_render_backend::layout::{HasTextLayout, Layout, LayoutTree, Layouter, Node, TextLayout};
use crate::geo::Size;
use crate::layout::{HasTextLayout, Layout, LayoutTree, Layouter, Node, TextLayout};
use gosub_shared::document::DocumentHandle;
use gosub_shared::node::NodeId;
use gosub_shared::traits::css3::CssSystem;
use gosub_shared::traits::document::Document;
use gosub_shared::traits::node::Node as DocumentNode;
use gosub_shared::traits::node::NodeData;
use gosub_shared::types::Result;
use gosub_shared::traits::css3::CssStylesheet;

mod desc;

Expand Down Expand Up @@ -233,36 +236,36 @@ impl<L: Layouter> RenderTree<L> {
}

/// Generate a render tree from the given document
pub fn from_document<D: Document<L::CssSystem>>(document: DocumentHandle<D, L::CssSystem>) -> Self {
let mut render_tree = RenderTree::with_capacity(document.get().count_nodes());
pub fn from_document<D: Document<L::CssSystem> + Clone>(document: DocumentHandle<D, L::CssSystem>) -> Self {
let mut render_tree = RenderTree::with_capacity(document.get().node_count());

render_tree.generate_from(document);

render_tree
}

fn generate_from<D: Document<L::CssSystem>>(&mut self, handle: DocumentHandle<D, L::CssSystem>) {
fn generate_from<D: Document<L::CssSystem> + Clone>(&mut self, handle: DocumentHandle<D, L::CssSystem>) {
// Iterate the complete document tree
let tree_iterator = TreeIterator::new(handle.clone());

{
let doc = handle.get();
println!("Stylesheets: {:?}", doc.get_stylesheets().len());
println!("Stylesheets: {:?}", doc.stylesheets().len());

for stylesheet in &doc.get_stylesheets() {
println!(" {:?}", stylesheet.location);
println!(" {:?}", stylesheet.origin);
for &stylesheet in doc.stylesheets() {
println!(" {:?}", stylesheet.location());
println!(" {:?}", stylesheet.origin());
}
}

for current_node_id in tree_iterator {
let mut css_map_entry = CssProperties::new();

let doc = handle.get();
let node = doc.get_node_by_id(current_node_id).expect("node not found");
let node = doc.node_by_id(current_node_id).expect("node not found");

if node_is_unrenderable(node) {
if let Some(parent) = node.parent {
if let Some(parent) = node.parent_id() {
if let Some(parent) = self.get_node_mut(parent) {
parent.children.retain(|id| *id != current_node_id);
// continue
Expand All @@ -283,7 +286,7 @@ impl<L: Layouter> RenderTree<L> {
let mut fix_list = FixList::new();

for sheet in handle.get().stylesheets().iter() {
for rule in sheet.rules.iter() {
for rule in sheet.rules().iter() {
for selector in rule.selectors().iter() {
let (matched, specificity) = match_selector(
handle.clone(),
Expand All @@ -308,7 +311,7 @@ impl<L: Layouter> RenderTree<L> {
continue;
};

let value = resolve_functions(&declaration.value, node, &doc);
let value = resolve_functions(&declaration.value, node, handle.clone());

// Check if the declaration matches the definition and return the "expanded" order
let res = definition.matches_and_shorthands(&value, &mut fix_list);
Expand Down Expand Up @@ -341,9 +344,9 @@ impl<L: Layouter> RenderTree<L> {
fix_list.apply(&mut css_map_entry);

let binding = handle.get();
let current_node = binding.get_node_by_id(current_node_id).unwrap();
let current_node = binding.node_by_id(current_node_id).unwrap();

let data = || RenderNodeData::from_node_data(current_node.data.clone());
let data = || RenderNodeData::from_node_data(current_node.data().clone());

let data = match data() {
ControlFlow::Ok(data) => data,
Expand All @@ -357,10 +360,10 @@ impl<L: Layouter> RenderTree<L> {
let render_tree_node = RenderTreeNode {
id: current_node_id,
properties: css_map_entry,
children: current_node.children.clone(),
parent: node.parent,
name: node.name.clone(), // We might be able to move node into render_tree_node
namespace: node.namespace.clone(),
children: current_node.children().clone(),
parent: node.parent_id(),
name: node.name().clone(), // We might be able to move node into render_tree_node
namespace: node.namespace().clone(),
data,
css_dirty: true,
cache: L::Cache::default(),
Expand Down Expand Up @@ -557,7 +560,7 @@ impl<L: Layouter> RenderTree<L> {
// Generates a declaration property and adds it to the css_map_entry
pub fn add_property_to_map(
css_map_entry: &mut CssProperties,
sheet: &CssStylesheet,
sheet: &gosub_css3::stylesheet::CssStylesheet,
specificity: Specificity,
declaration: &CssDeclaration,
) {
Expand Down Expand Up @@ -798,7 +801,7 @@ impl<L: Layouter> Node for RenderTreeNode<L> {

pub struct Value(pub CssValue);

impl gosub_render_backend::layout::CssProperty for CssProperty {
impl crate::layout::CssProperty for CssProperty {
type Value = Value;

fn compute_value(&mut self) {
Expand Down Expand Up @@ -867,7 +870,7 @@ impl gosub_render_backend::layout::CssProperty for CssProperty {
}
}

impl gosub_render_backend::layout::CssValue for Value {
impl crate::layout::CssValue for Value {
fn unit_to_px(&self) -> f32 {
self.0.unit_to_px()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::render_tree::{RenderNodeData, RenderTree};
use gosub_render_backend::layout::{Layout, Layouter};
use gosub_render_backend::{NodeDesc, Point, Size};
use crate::layout::{Layout, Layouter};
use crate::{NodeDesc, Point, Size};
use gosub_shared::node::NodeId;
use gosub_shared::traits::document::Document;

impl<L: Layouter> RenderTree<L> {
pub fn desc(&self) -> NodeDesc {
Expand Down
3 changes: 2 additions & 1 deletion crates/gosub_render_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ license = "MIT"

[dependencies]
gosub_html5 = { path = "../gosub_html5" }
gosub_styling = { path = "../gosub_styling" }
gosub_css3 = { path = "../gosub_css3" }
gosub_shared = { path = "../gosub_shared" }
gosub_render_backend = { path = "../gosub_render_backend" }
anyhow = "1.0.87"
regex = "1.10.6"
rstar = "0.12.0"
log = "0.4.14"
2 changes: 1 addition & 1 deletion crates/gosub_render_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
//!

pub mod position;
pub mod render_tree;
pub mod macos_render_tree;
pub mod text;
Loading

0 comments on commit 2747e8d

Please sign in to comment.