Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: add support for fill in svg #797

Merged
merged 32 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c08c0d9
add support for `color` in `svg`
Aiving Jul 16, 2024
26279f7
fix svg color support & mocked engine
Aiving Jul 16, 2024
e6e8d39
fix mocked engine
Aiving Jul 16, 2024
92aeaef
format mocked engine using nightly fmt
Aiving Jul 16, 2024
b287b74
replace `color` attribute with `fill` in `svg`
Aiving Jul 16, 2024
0d3640e
make fill atributte optional
Aiving Jul 16, 2024
8f58b2a
i forgor change svg defintion (rip)
Aiving Jul 16, 2024
922d02c
format svg with nightly fmt
Aiving Jul 16, 2024
bc1d644
an attempt to fix svg rendering without `fill` attribute
Aiving Jul 16, 2024
50b450e
fixed svg rendering
Aiving Jul 16, 2024
04a161e
Merge branch 'main' into feat/support-color-in-svg
Aiving Jul 21, 2024
b89b6dc
Merge branch 'main' into feat/support-color-in-svg
Aiving Aug 4, 2024
0b8b288
chore(elements/svg): cover all `Fill` enum patterns
Aiving Aug 4, 2024
7ff5fb2
i forgor
Aiving Aug 4, 2024
dce691f
Update crates/elements/src/_docs/attributes/fill.md
Aiving Aug 4, 2024
4dbdc29
Merge branch 'main' into feat/support-color-in-svg
Aiving Aug 4, 2024
f2f290a
Merge branch 'main' into feat/support-color-in-svg
Aiving Oct 15, 2024
c5b2ac7
Update skia.rs
Aiving Oct 15, 2024
d55f7a2
Merge branch 'main' into feat/support-color-in-svg
Aiving Nov 23, 2024
577f6cb
fixes
Aiving Nov 23, 2024
d8e03e9
fmt changes
Aiving Nov 23, 2024
3c85e82
fix mocked engine
Aiving Nov 23, 2024
69414f2
Update crates/state/src/style.rs
Aiving Nov 23, 2024
567b3ae
Update crates/state/src/style.rs
Aiving Nov 23, 2024
a3aa054
Update crates/state/src/style.rs
Aiving Nov 23, 2024
677638c
Update crates/core/src/elements/svg.rs
Aiving Nov 23, 2024
c0c9bde
Update crates/core/src/elements/svg.rs
Aiving Nov 23, 2024
149fe9f
Update crates/state/src/style.rs
Aiving Nov 23, 2024
ad3545d
Merge branch 'main' into feat/support-color-in-svg
Aiving Nov 23, 2024
b942776
Merge branch 'main' into feat/support-color-in-svg
marc2332 Nov 24, 2024
fd56e33
chore: Add missing color attribute for svg, add attribute display for…
marc2332 Nov 24, 2024
1ace8c7
chore: fmt settings.svg
marc2332 Nov 24, 2024
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
39 changes: 36 additions & 3 deletions crates/core/src/elements/svg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use freya_engine::prelude::*;
use freya_native_core::real_dom::NodeImmutable;
use freya_node_state::StyleState;
use freya_node_state::{
Fill,
StyleState,
};
use torin::prelude::LayoutNode;

use super::utils::ElementUtils;
Expand All @@ -27,10 +30,40 @@ impl ElementUtils for SvgElement {
if let Some(svg_data) = &node_style.svg_data {
let svg_dom = svg::Dom::from_bytes(svg_data.as_slice(), font_manager);
if let Ok(mut svg_dom) = svg_dom {
canvas.save();
let (scale_x, scale_y) = (
area.width() / svg_dom.inner().fContainerSize.fWidth,
area.height() / svg_dom.inner().fContainerSize.fHeight,
);

canvas.save_layer(&SaveLayerRec::default());
canvas.translate((x, y));
svg_dom.set_container_size((area.width() as i32, area.height() as i32));

if scale_x.is_finite() && scale_y.is_finite() {
canvas.scale((scale_x, scale_y));
} else {
svg_dom.set_container_size((area.width(), area.height()));
}

svg_dom.render(canvas);

if let Some(fill) = node_style.fill.as_ref() {
let mut paint = Paint::default();

paint.set_anti_alias(true);
paint.set_blend_mode(BlendMode::SrcIn);

match fill {
Fill::Color(color) => {
paint.set_color(*color);
}
Fill::LinearGradient(gradient) => {
paint.set_shader(gradient.into_shader(area));
}
}

canvas.draw_paint(&paint);
}

canvas.restore();
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/elements/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ builder_constructors! {
/// }
/// ```
svg {
#[doc = include_str!("_docs/attributes/color.md")]
Aiving marked this conversation as resolved.
Show resolved Hide resolved
fill: String,
#[doc = include_str!("_docs/attributes/margin.md")]
margin: String,
#[doc = include_str!("_docs/attributes/width_height.md")]
Expand Down
62 changes: 61 additions & 1 deletion crates/engine/src/mocked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use std::ops::*;
use bitflags::bitflags;
use glutin::context::PossiblyCurrentContext;

#[derive(Default, Debug)]
pub struct SaveLayerRec;

#[derive(Clone, Debug, PartialEq, Copy, Eq)]
pub struct Color(u32);

Expand Down Expand Up @@ -589,6 +592,10 @@ impl Paint {
unimplemented!("This is mocked")
}

pub fn set_blend_mode(&mut self, _mode: BlendMode) -> &mut Self {
unimplemented!("This is mocked")
}

pub fn set_style(&mut self, _style: PaintStyle) -> &mut Self {
unimplemented!("This is mocked")
}
Expand Down Expand Up @@ -1018,6 +1025,10 @@ impl Canvas {
unimplemented!("This is mocked")
}

pub fn draw_paint(&self, _: &Paint) -> &Self {
unimplemented!("This is mocked")
}

pub fn draw_line(&self, _p1: impl Into<Point>, _p2: impl Into<Point>, _paint: &Paint) -> &Self {
unimplemented!("This is mocked")
}
Expand All @@ -1026,6 +1037,10 @@ impl Canvas {
unimplemented!("This is mocked")
}

pub fn save_layer(&self, layer_rec: &SaveLayerRec) -> usize {
unimplemented!("This is mocked")
}

pub fn save_layer_alpha_f(&self, bounds: impl Into<Option<Rect>>, alpha: f32) -> usize {
unimplemented!("This is mocked")
}
Expand Down Expand Up @@ -1327,6 +1342,40 @@ impl MaskFilter {
}
}

#[repr(i32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum BlendMode {
Clear = 0,
Src = 1,
Dst = 2,
SrcOver = 3,
DstOver = 4,
SrcIn = 5,
DstIn = 6,
SrcOut = 7,
DstOut = 8,
SrcATop = 9,
DstATop = 10,
Xor = 11,
Plus = 12,
Modulate = 13,
Screen = 14,
Overlay = 15,
Darken = 16,
Lighten = 17,
ColorDodge = 18,
ColorBurn = 19,
HardLight = 20,
SoftLight = 21,
Difference = 22,
Exclusion = 23,
Multiply = 24,
Hue = 25,
Saturation = 26,
Color = 27,
Luminosity = 28,
}

impl BlurStyle {
pub const LastEnum: BlurStyle = BlurStyle::Inner;
}
Expand All @@ -1346,7 +1395,14 @@ pub mod svg {
Size,
};

pub struct Dom;
pub struct Dom {
pub fContainerSize: SkSize,
}

pub struct SkSize {
pub fWidth: f32,
pub fHeight: f32,
}

impl Dom {
pub fn from_bytes(_bytes: &[u8], font_mgr: &FontMgr) -> Result<Self, ()> {
Expand All @@ -1360,6 +1416,10 @@ pub mod svg {
pub fn render(&self, _canvas: &Canvas) {
unimplemented!("This is mocked")
}

pub fn inner(&self) -> &Self {
unimplemented!("This is mocked")
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/engine/src/skia.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use skia_safe::{
canvas::SaveLayerRec,
font_style::{
Slant,
Weight,
Expand Down Expand Up @@ -55,6 +56,8 @@ pub use skia_safe::{
TextStyle,
TypefaceFontProvider,
},
wrapper::PointerWrapper,
BlendMode,
BlurStyle,
Canvas,
ClipOp,
Expand Down
2 changes: 2 additions & 0 deletions crates/native-core/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum AttributeName {
CornerRadius,
CornerSmoothing,
Color,
Fill,
FontSize,
FontFamily,
FontStyle,
Expand Down Expand Up @@ -88,6 +89,7 @@ impl FromStr for AttributeName {
"corner_radius" => Ok(AttributeName::CornerRadius),
"corner_smoothing" => Ok(AttributeName::CornerSmoothing),
"color" => Ok(AttributeName::Color),
"fill" => Ok(AttributeName::Fill),
"font_size" => Ok(AttributeName::FontSize),
"font_family" => Ok(AttributeName::FontFamily),
"font_style" => Ok(AttributeName::FontStyle),
Expand Down
10 changes: 10 additions & 0 deletions crates/state/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{
#[derive(Default, Debug, Clone, PartialEq, Component)]
pub struct StyleState {
pub background: Fill,
pub fill: Option<Fill>,
pub border: Border,
pub shadows: Vec<Shadow>,
pub corner_radius: CornerRadius,
Expand All @@ -54,6 +55,14 @@ impl ParseAttribute for StyleState {
self.background = Fill::parse(value)?;
}
}
AttributeName::Fill => {
if let Some(value) = attr.value.as_text() {
if value == "none" {
return Ok(());
}
self.fill = Some(Fill::parse(value)?);
}
}
AttributeName::Border => {
if let Some(value) = attr.value.as_text() {
let mut border = Border::parse(value)?;
Expand Down Expand Up @@ -137,6 +146,7 @@ impl State<CustomAttributeValues> for StyleState {
const NODE_MASK: NodeMaskBuilder<'static> =
NodeMaskBuilder::new().with_attrs(AttributeMaskBuilder::Some(&[
AttributeName::Background,
AttributeName::Fill,
Aiving marked this conversation as resolved.
Show resolved Hide resolved
AttributeName::Layer,
AttributeName::Border,
AttributeName::BorderAlign,
Expand Down