mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-10-02 19:38:12 +08:00
New node: Flatten Raster
This commit is contained in:
@@ -156,6 +156,54 @@ pub trait IntoGraphicTable {
|
|||||||
flatten_table(&mut output, content);
|
flatten_table(&mut output, content);
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deeply flattens any raster content within a graphic table, discarding non-raster content, and returning a table of only raster elements.
|
||||||
|
fn into_flattened_raster_table(self) -> Table<Raster<CPU>>
|
||||||
|
where
|
||||||
|
Self: std::marker::Sized,
|
||||||
|
{
|
||||||
|
let content = self.into_graphic_table();
|
||||||
|
|
||||||
|
fn flatten_table(output_raster_table: &mut Table<Raster<CPU>>, current_graphic_table: Table<Graphic>) {
|
||||||
|
for current_graphic_row in current_graphic_table.iter() {
|
||||||
|
let current_graphic = current_graphic_row.element.clone();
|
||||||
|
let source_node_id = *current_graphic_row.source_node_id;
|
||||||
|
|
||||||
|
match current_graphic {
|
||||||
|
// If we're allowed to recurse, flatten any tables we encounter
|
||||||
|
Graphic::Graphic(mut current_graphic_table) => {
|
||||||
|
// Apply the parent graphic's transform to all child elements
|
||||||
|
for graphic in current_graphic_table.iter_mut() {
|
||||||
|
*graphic.transform = *current_graphic_row.transform * *graphic.transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
flatten_table(output_raster_table, current_graphic_table);
|
||||||
|
}
|
||||||
|
// Push any leaf RasterCPU elements we encounter
|
||||||
|
Graphic::RasterCPU(raster_table) => {
|
||||||
|
for current_raster_row in raster_table.iter() {
|
||||||
|
output_raster_table.push(TableRow {
|
||||||
|
element: current_raster_row.element.clone(),
|
||||||
|
transform: *current_graphic_row.transform * *current_raster_row.transform,
|
||||||
|
alpha_blending: AlphaBlending {
|
||||||
|
blend_mode: current_raster_row.alpha_blending.blend_mode,
|
||||||
|
opacity: current_graphic_row.alpha_blending.opacity * current_raster_row.alpha_blending.opacity,
|
||||||
|
fill: current_raster_row.alpha_blending.fill,
|
||||||
|
clip: current_raster_row.alpha_blending.clip,
|
||||||
|
},
|
||||||
|
source_node_id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut output = Table::new();
|
||||||
|
flatten_table(&mut output, content);
|
||||||
|
output
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoGraphicTable for Table<Graphic> {
|
impl IntoGraphicTable for Table<Graphic> {
|
||||||
|
|||||||
@@ -192,10 +192,16 @@ pub async fn flatten_graphic(_: impl Ctx, content: Table<Graphic>, fully_flatten
|
|||||||
|
|
||||||
/// Converts a graphic table into a vector table by deeply flattening any vector content it contains, and discarding any non-vector content.
|
/// Converts a graphic table into a vector table by deeply flattening any vector content it contains, and discarding any non-vector content.
|
||||||
#[node_macro::node(category("Vector"))]
|
#[node_macro::node(category("Vector"))]
|
||||||
pub async fn flatten_vector<I: IntoGraphicTable + 'n + Send + Clone>(_: impl Ctx, #[implementations(Table<Graphic>, Table<Vector>)] content: I) -> Table<Vector> {
|
pub async fn flatten_vector<T: IntoGraphicTable + 'n + Send + Clone>(_: impl Ctx, #[implementations(Table<Graphic>, Table<Vector>)] content: T) -> Table<Vector> {
|
||||||
content.into_flattened_vector_table()
|
content.into_flattened_vector_table()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts a graphic table into a vector table by deeply flattening any vector content it contains, and discarding any non-vector content.
|
||||||
|
#[node_macro::node(category("Vector"))]
|
||||||
|
pub async fn flatten_raster<T: IntoGraphicTable + 'n + Send + Clone>(_: impl Ctx, #[implementations(Table<Graphic>, Table<Raster<CPU>>)] content: T) -> Table<Raster<CPU>> {
|
||||||
|
content.into_flattened_raster_table()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the value at the specified index in the collection.
|
/// Returns the value at the specified index in the collection.
|
||||||
/// If no value exists at that index, the type's default value is returned.
|
/// If no value exists at that index, the type's default value is returned.
|
||||||
#[node_macro::node(category("General"))]
|
#[node_macro::node(category("General"))]
|
||||||
|
|||||||
Reference in New Issue
Block a user