Skip to content
Merged
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
28 changes: 28 additions & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ fn main() {
#[derive(Debug, Clone)]
struct ChainedFeature {
pub tag: String,
pub name: String,
pub glyphs: Vec<String>,
pub source_table: String,
}

fn build_glyph_map(face: &ttf_parser::Face) -> std::collections::HashMap<u16, char> {
Expand Down Expand Up @@ -150,7 +148,7 @@ fn extract_features_via_chained_sequences(
}

fn extract_gsub_features_simplified(
face: &ttf_parser::Face,
_face: &ttf_parser::Face,
gsub_table: ttf_parser::opentype_layout::LayoutTable,
glyph_map: &std::collections::HashMap<u16, char>,
) -> Vec<ChainedFeature> {
Expand All @@ -160,7 +158,6 @@ fn extract_gsub_features_simplified(
for i in 0..gsub_table.features.len() {
if let Some(feature) = gsub_table.features.get(i as u16) {
let tag = feature.tag.to_string();
let name = get_feature_name_from_font(face, &tag);

// Extract glyphs from all lookups in this feature
let mut all_glyphs = std::collections::HashSet::new();
Expand Down Expand Up @@ -224,9 +221,7 @@ fn extract_gsub_features_simplified(

features.push(ChainedFeature {
tag,
name,
glyphs: glyph_chars,
source_table: "GSUB".to_string(),
});
}
}
Expand All @@ -235,7 +230,7 @@ fn extract_gsub_features_simplified(
}

fn extract_gpos_features_simplified(
face: &ttf_parser::Face,
_face: &ttf_parser::Face,
gpos_table: ttf_parser::opentype_layout::LayoutTable,
glyph_map: &std::collections::HashMap<u16, char>,
) -> Vec<ChainedFeature> {
Expand All @@ -245,7 +240,6 @@ fn extract_gpos_features_simplified(
for i in 0..gpos_table.features.len() {
if let Some(feature) = gpos_table.features.get(i as u16) {
let tag = feature.tag.to_string();
let name = get_feature_name_from_font(face, &tag);

// Extract glyphs from all lookups in this feature
let mut all_glyphs = std::collections::HashSet::new();
Expand Down Expand Up @@ -301,9 +295,7 @@ fn extract_gpos_features_simplified(

features.push(ChainedFeature {
tag,
name,
glyphs: glyph_chars,
source_table: "GPOS".to_string(),
});
}
}
Expand Down Expand Up @@ -337,31 +329,3 @@ fn extract_coverage_glyphs(coverage: &ttf_parser::opentype_layout::Coverage) ->

glyphs
}

fn get_feature_name_from_font(_face: &ttf_parser::Face, tag: &str) -> String {
// Simplified feature name mapping
match tag {
"kern" => "Kerning".to_string(),
"liga" => "Ligatures".to_string(),
"ss01" => "Stylistic Set 1".to_string(),
"cv01" => "Character Variant 1".to_string(),
"locl" => "Localized Forms".to_string(),
"zero" => "Slashed Zero".to_string(),
"sinf" => "Scientific Inferiors".to_string(),
"aalt" => "Access All Alternates".to_string(),
"numr" => "Numerators".to_string(),
"ordn" => "Ordinals".to_string(),
"case" => "Case-Sensitive Forms".to_string(),
"pnum" => "Proportional Numbers".to_string(),
"ccmp" => "Glyph Composition/Decomposition".to_string(),
"dlig" => "Discretionary Ligatures".to_string(),
"sups" => "Superscript".to_string(),
"tnum" => "Tabular Numbers".to_string(),
"subs" => "Subscript".to_string(),
"salt" => "Stylistic Alternates".to_string(),
"dnom" => "Denominators".to_string(),
"frac" => "Fractions".to_string(),
"calt" => "Contextual Alternates".to_string(),
_ => format!("Feature {}", tag),
}
}
3 changes: 2 additions & 1 deletion crates/grida-canvas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ futures = "0.3.31"
gl = "0.14.0"
figma-api = { version = "0.31.3", optional = true }
seahash = "4.1.0"
taffy = "0.9.1"


[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down Expand Up @@ -50,4 +51,4 @@ harness = false

[[example]]
name = "app_figma"
required-features = ["figma"]
required-features = ["figma"]
88 changes: 42 additions & 46 deletions crates/grida-canvas/benches/bench_rectangles.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,70 @@
use cg::cg::types::*;
use cg::node::repository::NodeRepository;
use cg::node::scene_graph::{Parent, SceneGraph};
use cg::node::schema::*;
use cg::runtime::camera::Camera2D;
use cg::runtime::scene::{Backend, Renderer};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use math2::transform::AffineTransform;

fn create_rectangles(count: usize, with_effects: bool) -> Scene {
let mut repository = NodeRepository::new();
let mut ids = Vec::new();
let mut graph = SceneGraph::new();

// Create rectangles
for i in 0..count {
let id = format!("rect-{}", i);
ids.push(id.clone());

let rect = RectangleNodeRec {
id: id.clone(),
name: None,
active: true,
opacity: 1.0,
blend_mode: LayerBlendMode::default(),
mask: None,
transform: AffineTransform::identity(),
size: Size {
width: 100.0,
height: 100.0,
},
corner_radius: RectangularCornerRadius::zero(),
fills: Paints::new([Paint::from(CGColor(255, 0, 0, 255))]),
strokes: Paints::default(),
stroke_width: 1.0,
stroke_align: StrokeAlign::Inside,
stroke_dash_array: None,
effects: if with_effects {
LayerEffects::from_array(vec![FilterEffect::DropShadow(FeShadow {
dx: 2.0,
dy: 2.0,
blur: 4.0,
spread: 0.0,
color: CGColor(0, 0, 0, 128),
})])
} else {
LayerEffects::default()
},
};

repository.insert(Node::Rectangle(rect));
}
let rectangles: Vec<Node> = (0..count)
.map(|i| {
let id = format!("rect-{}", i);

Node::Rectangle(RectangleNodeRec {
id: id.clone(),
name: None,
active: true,
opacity: 1.0,
blend_mode: LayerBlendMode::default(),
mask: None,
transform: AffineTransform::identity(),
size: Size {
width: 100.0,
height: 100.0,
},
corner_radius: RectangularCornerRadius::zero(),
fills: Paints::new([Paint::from(CGColor(255, 0, 0, 255))]),
strokes: Paints::default(),
stroke_width: 1.0,
stroke_align: StrokeAlign::Inside,
stroke_dash_array: None,
effects: if with_effects {
LayerEffects::from_array(vec![FilterEffect::DropShadow(FeShadow {
dx: 2.0,
dy: 2.0,
blur: 4.0,
spread: 0.0,
color: CGColor(0, 0, 0, 128),
})])
} else {
LayerEffects::default()
},
})
})
.collect();

// Create root group
let root_group = GroupNodeRec {
id: "root".to_string(),
name: Some("Root Group".to_string()),
active: true,
transform: None,
children: ids.clone(),
opacity: 1.0,
blend_mode: LayerBlendMode::default(),
mask: None,
};

repository.insert(Node::Group(root_group));
let root_id = graph.append_child(Node::Group(root_group), Parent::Root);
graph.append_children(rectangles, Parent::NodeId(root_id));

Scene {
id: "scene".to_string(),
name: "Test Scene".to_string(),
children: vec!["root".to_string()],
nodes: repository,
name: "Test Scene".into(),
background_color: None,
graph,
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/grida-canvas/examples/app_figma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,8 @@ async fn main() {
.expect("Failed to load scene");

println!("Rendering scene: {}", scene.name);
println!("Scene ID: {}", scene.id);
println!("Number of children: {}", scene.children.len());
println!("Total nodes in repository: {}", scene.nodes.len());
println!("Number of roots: {}", scene.graph.roots().len());
println!("Total nodes in graph: {}", scene.graph.node_count());

// Load webfonts metadata and find matching font files
let webfonts_metadata = load_webfonts_metadata()
Expand Down
49 changes: 25 additions & 24 deletions crates/grida-canvas/examples/app_grida.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cg::cg::types::*;
use cg::io::io_grida::parse;
use cg::node::scene_graph::SceneGraph;
use cg::node::schema::*;
use cg::window;
use clap::Parser;
Expand Down Expand Up @@ -40,37 +41,37 @@ async fn load_scene_from_file(file_path: &str) -> Scene {
.and_then(|c| c.clone())
.unwrap_or_default();

// Convert nodes to repository, filtering out scene nodes and populating children from links
let mut node_repo = cg::node::repository::NodeRepository::new();
for (node_id, json_node) in canvas_file.document.nodes {
// Skip scene nodes - they're handled separately
if matches!(json_node, cg::io::io_grida::JSONNode::Scene(_)) {
continue;
}
// Build scene graph from nodes and links using snapshot
let scene_node_ids: std::collections::HashSet<String> = canvas_file
.document
.nodes
.iter()
.filter(|(_, json_node)| matches!(json_node, cg::io::io_grida::JSONNode::Scene(_)))
.map(|(id, _)| id.clone())
.collect();

let mut node: cg::node::schema::Node = json_node.into();
// Convert all nodes (skip scene nodes)
let nodes: Vec<cg::node::schema::Node> = canvas_file
.document
.nodes
.into_iter()
.filter(|(_, json_node)| !matches!(json_node, cg::io::io_grida::JSONNode::Scene(_)))
.map(|(_, json_node)| json_node.into())
.collect();

// Populate children from links
if let Some(children_opt) = links.get(&node_id) {
if let Some(children) = children_opt {
match &mut node {
cg::node::schema::Node::Container(n) => n.children = children.clone(),
cg::node::schema::Node::Group(n) => n.children = children.clone(),
cg::node::schema::Node::BooleanOperation(n) => n.children = children.clone(),
_ => {} // Other nodes don't have children
}
}
}
// Filter links (skip scene nodes as parents)
let filtered_links: std::collections::HashMap<String, Vec<String>> = links
.into_iter()
.filter(|(parent_id, _)| !scene_node_ids.contains(parent_id))
.filter_map(|(parent_id, children_opt)| children_opt.map(|children| (parent_id, children)))
.collect();

node_repo.insert(node);
}
let graph = SceneGraph::new_from_snapshot(nodes, filtered_links, scene_children);

Scene {
nodes: node_repo,
id: scene_id,
name: scene_name,
children: scene_children,
background_color: Some(CGColor(230, 230, 230, 255)),
graph,
}
}

Expand Down
Loading