Gate the Path item behind the 'std' feature

This commit is contained in:
Olivier Goffart 2021-11-25 13:41:01 +01:00 committed by Olivier Goffart
parent 5b4d6e8ece
commit 844c5cf7ec
9 changed files with 183 additions and 146 deletions

View file

@ -18,7 +18,7 @@ rtti = []
# Expose C ABI
ffi = []
# Use the standard library
std = []
std = ["euclid/std", "lyon_path", "lyon_algorithms", "lyon_geom"]
default = ["std"]
@ -27,9 +27,9 @@ default = ["std"]
const-field-offset = { version = "0.1", path = "../../helper_crates/const-field-offset" }
vtable = { version="0.1.1", path = "../../helper_crates/vtable" }
sixtyfps-corelib-macros = { version = "=0.1.6", path = "../corelib_macros" }
lyon_path = { version = "0.17.3" }
lyon_algorithms = { version = "0.17.1" }
lyon_geom = { version = "0.17.0" }
lyon_path = { version = "0.17.3" , optional = true }
lyon_algorithms = { version = "0.17.1" , optional = true }
lyon_geom = { version = "0.17.0", optional = true }
euclid = { version = "0.22.1", default-features = false }
triomphe = { version = "0.1.1", default-features = false }
once_cell = { version = "1.5", default-features = false }

View file

@ -139,6 +139,7 @@ pub fn current_tick() -> Instant {
pub fn easing_curve(curve: &EasingCurve, value: f32) -> f32 {
match curve {
EasingCurve::Linear => value,
#[cfg(feature = "std")]
EasingCurve::CubicBezier([a, b, c, d]) => {
if !(0.0..=1.0).contains(a) && !(0.0..=1.0).contains(c) {
return value;
@ -152,6 +153,10 @@ pub fn easing_curve(curve: &EasingCurve, value: f32) -> f32 {
let curve = curve.assume_monotonic();
curve.y(curve.solve_t_for_x(value, 0.0..1.0, 0.01))
}
#[cfg(not(feature = "std"))]
EasingCurve::CubicBezier(_) => {
todo!("bezier curve not implemented when no_std")
}
}
}
@ -189,6 +194,7 @@ fn easing_test() {
/// Update the global animation time to the current time
pub fn update_animations() {
CURRENT_ANIMATION_DRIVER.with(|driver| {
#[allow(unused_mut)]
let mut duration = instant::Instant::now() - driver.initial_instant;
#[cfg(feature = "std")]
if let Ok(val) = std::env::var("SIXTYFPS_SLOW_ANIMATIONS") {

View file

@ -17,11 +17,8 @@ LICENSE END */
created by the backend in a type-erased manner.
*/
extern crate alloc;
#[cfg(feature = "rtti")]
use crate::rtti::*;
use crate::SharedString;
use alloc::boxed::Box;
use auto_enums::auto_enum;
pub use euclid;
/// 2D Rectangle
@ -40,7 +37,9 @@ pub type Transform = euclid::default::Transform2D<f32>;
pub(crate) mod color;
pub use color::*;
#[cfg(feature = "std")]
mod path;
#[cfg(feature = "std")]
pub use path::*;
mod brush;

View file

@ -11,13 +11,13 @@ LICENSE END */
This module contains path related types and functions for the run-time library.
*/
use super::{Point, Rect, Size};
#[cfg(feature = "rtti")]
use crate::rtti::*;
use auto_enums::auto_enum;
use const_field_offset::FieldOffsets;
use sixtyfps_corelib_macros::*;
use super::*;
#[repr(C)]
#[derive(FieldOffsets, Default, SixtyFPSElement, Clone, Debug, PartialEq)]
#[pin]

View file

@ -145,6 +145,7 @@ pub trait ItemRenderer {
fn draw_clipped_image(&mut self, image: Pin<&ClippedImage>);
fn draw_text(&mut self, text: Pin<&Text>);
fn draw_text_input(&mut self, text_input: Pin<&TextInput>);
#[cfg(feature = "std")]
fn draw_path(&mut self, path: Pin<&Path>);
fn draw_box_shadow(&mut self, box_shadow: Pin<&BoxShadow>);
/// Clip the further call until restore_state.

View file

@ -25,8 +25,7 @@ When adding an item or a property, it needs to be kept in sync with different pl
#![allow(missing_docs)] // because documenting each property of items is redundant
use crate::component::ComponentVTable;
use crate::graphics::PathDataIterator;
use crate::graphics::{Brush, Color, PathData, Point, Rect};
use crate::graphics::{Brush, Color, Point, Rect};
use crate::input::{
FocusEvent, InputEventFilterResult, InputEventResult, KeyEvent, KeyEventResult, KeyEventType,
MouseEvent,
@ -48,6 +47,10 @@ mod text;
pub use text::*;
mod image;
pub use self::image::*;
#[cfg(feature = "std")]
mod path;
#[cfg(feature = "std")]
pub use path::*;
/// Alias for `&mut dyn ItemRenderer`. Required so cbindgen generates the ItemVTable
/// despite the presence of trait object
@ -800,134 +803,6 @@ declare_item_vtable! {
fn sixtyfps_get_RotateVTable() -> RotateVTable for Rotate
}
#[derive(Copy, Clone, Debug, PartialEq, strum::EnumString, strum::Display)]
#[repr(C)]
#[allow(non_camel_case_types)]
pub enum FillRule {
nonzero,
evenodd,
}
impl Default for FillRule {
fn default() -> Self {
Self::nonzero
}
}
/// The implementation of the `Path` element
#[repr(C)]
#[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin]
pub struct Path {
pub x: Property<f32>,
pub y: Property<f32>,
pub width: Property<f32>,
pub height: Property<f32>,
pub elements: Property<PathData>,
pub fill: Property<Brush>,
pub fill_rule: Property<FillRule>,
pub stroke: Property<Brush>,
pub stroke_width: Property<f32>,
pub viewbox_x: Property<f32>,
pub viewbox_y: Property<f32>,
pub viewbox_width: Property<f32>,
pub viewbox_height: Property<f32>,
pub clip: Property<bool>,
pub cached_rendering_data: CachedRenderingData,
}
impl Item for Path {
fn init(self: Pin<&Self>, _window: &WindowRc) {}
fn geometry(self: Pin<&Self>) -> Rect {
euclid::rect(self.x(), self.y(), self.width(), self.height())
}
fn layout_info(self: Pin<&Self>, _orientation: Orientation, _window: &WindowRc) -> LayoutInfo {
LayoutInfo::default()
}
fn input_event_filter_before_children(
self: Pin<&Self>,
event: MouseEvent,
_window: &WindowRc,
_self_rc: &ItemRc,
) -> InputEventFilterResult {
if let Some(pos) = event.pos() {
if self.clip()
&& (pos.x < 0. || pos.y < 0. || pos.x > self.width() || pos.y > self.height())
{
return InputEventFilterResult::Intercept;
}
}
InputEventFilterResult::ForwardAndIgnore
}
fn input_event(
self: Pin<&Self>,
_: MouseEvent,
_window: &WindowRc,
_self_rc: &ItemRc,
) -> InputEventResult {
InputEventResult::EventIgnored
}
fn key_event(self: Pin<&Self>, _: &KeyEvent, _window: &WindowRc) -> KeyEventResult {
KeyEventResult::EventIgnored
}
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &WindowRc) {}
fn render(self: Pin<&Self>, backend: &mut ItemRendererRef) {
let clip = self.clip();
if clip {
(*backend).save_state();
(*backend).combine_clip(self.geometry(), 0., 0.)
}
(*backend).draw_path(self);
if clip {
(*backend).restore_state();
}
}
}
impl Path {
/// Returns an iterator of the events of the path and an offset, so that the
/// shape fits into the width/height of the path while respecting the stroke
/// width.
pub fn fitted_path_events(
self: Pin<&Self>,
) -> (euclid::default::Vector2D<f32>, PathDataIterator) {
let stroke_width = self.stroke_width();
let bounds_width = (self.width() - stroke_width).max(0.);
let bounds_height = (self.height() - stroke_width).max(0.);
let offset = euclid::default::Vector2D::new(stroke_width / 2., stroke_width / 2.);
let viewbox_width = self.viewbox_width();
let viewbox_height = self.viewbox_height();
let mut elements_iter = self.elements().iter();
let maybe_viewbox = if viewbox_width > 0. && viewbox_height > 0. {
Some(euclid::rect(self.viewbox_x(), self.viewbox_y(), viewbox_width, viewbox_height))
} else {
None
};
elements_iter.fit(bounds_width, bounds_height, maybe_viewbox);
(offset, elements_iter)
}
}
impl ItemConsts for Path {
const cached_rendering_data_offset: const_field_offset::FieldOffset<Path, CachedRenderingData> =
Path::FIELD_OFFSETS.cached_rendering_data.as_unpinned_projection();
}
declare_item_vtable! {
fn sixtyfps_get_PathVTable() -> PathVTable for Path
}
/// The implementation of the `Flickable` element
#[repr(C)]
#[derive(FieldOffsets, Default, SixtyFPSElement)]
@ -1250,6 +1125,11 @@ declare_item_vtable! {
fn sixtyfps_get_ClippedImageVTable() -> ClippedImageVTable for ClippedImage
}
#[cfg(feature = "std")]
declare_item_vtable! {
fn sixtyfps_get_PathVTable() -> PathVTable for Path
}
#[derive(Copy, Clone, Debug, PartialEq, strum::EnumString, strum::Display)]
#[repr(C)]
#[allow(non_camel_case_types)]

View file

@ -0,0 +1,155 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2021 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2021 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
/*!
This module contains the builtin Path related items.
When adding an item or a property, it needs to be kept in sync with different place.
Lookup the [`crate::items`] module documentation.
*/
use super::{Item, ItemConsts, ItemRc, ItemRendererRef};
use crate::graphics::{Brush, PathData, PathDataIterator, Rect};
use crate::input::{
FocusEvent, InputEventFilterResult, InputEventResult, KeyEvent, KeyEventResult, MouseEvent,
};
use crate::item_rendering::CachedRenderingData;
use crate::layout::{LayoutInfo, Orientation};
#[cfg(feature = "rtti")]
use crate::rtti::*;
use crate::window::WindowRc;
use crate::Property;
use const_field_offset::FieldOffsets;
use core::pin::Pin;
use sixtyfps_corelib_macros::*;
#[derive(Copy, Clone, Debug, PartialEq, strum::EnumString, strum::Display)]
#[repr(C)]
#[allow(non_camel_case_types)]
pub enum FillRule {
nonzero,
evenodd,
}
impl Default for FillRule {
fn default() -> Self {
Self::nonzero
}
}
/// The implementation of the `Path` element
#[repr(C)]
#[derive(FieldOffsets, Default, SixtyFPSElement)]
#[pin]
pub struct Path {
pub x: Property<f32>,
pub y: Property<f32>,
pub width: Property<f32>,
pub height: Property<f32>,
pub elements: Property<PathData>,
pub fill: Property<Brush>,
pub fill_rule: Property<FillRule>,
pub stroke: Property<Brush>,
pub stroke_width: Property<f32>,
pub viewbox_x: Property<f32>,
pub viewbox_y: Property<f32>,
pub viewbox_width: Property<f32>,
pub viewbox_height: Property<f32>,
pub clip: Property<bool>,
pub cached_rendering_data: CachedRenderingData,
}
impl Item for Path {
fn init(self: Pin<&Self>, _window: &WindowRc) {}
fn geometry(self: Pin<&Self>) -> Rect {
euclid::rect(self.x(), self.y(), self.width(), self.height())
}
fn layout_info(self: Pin<&Self>, _orientation: Orientation, _window: &WindowRc) -> LayoutInfo {
LayoutInfo::default()
}
fn input_event_filter_before_children(
self: Pin<&Self>,
event: MouseEvent,
_window: &WindowRc,
_self_rc: &ItemRc,
) -> InputEventFilterResult {
if let Some(pos) = event.pos() {
if self.clip()
&& (pos.x < 0. || pos.y < 0. || pos.x > self.width() || pos.y > self.height())
{
return InputEventFilterResult::Intercept;
}
}
InputEventFilterResult::ForwardAndIgnore
}
fn input_event(
self: Pin<&Self>,
_: MouseEvent,
_window: &WindowRc,
_self_rc: &ItemRc,
) -> InputEventResult {
InputEventResult::EventIgnored
}
fn key_event(self: Pin<&Self>, _: &KeyEvent, _window: &WindowRc) -> KeyEventResult {
KeyEventResult::EventIgnored
}
fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &WindowRc) {}
fn render(self: Pin<&Self>, backend: &mut ItemRendererRef) {
let clip = self.clip();
if clip {
(*backend).save_state();
(*backend).combine_clip(self.geometry(), 0., 0.)
}
(*backend).draw_path(self);
if clip {
(*backend).restore_state();
}
}
}
impl Path {
/// Returns an iterator of the events of the path and an offset, so that the
/// shape fits into the width/height of the path while respecting the stroke
/// width.
pub fn fitted_path_events(
self: Pin<&Self>,
) -> (euclid::default::Vector2D<f32>, PathDataIterator) {
let stroke_width = self.stroke_width();
let bounds_width = (self.width() - stroke_width).max(0.);
let bounds_height = (self.height() - stroke_width).max(0.);
let offset = euclid::default::Vector2D::new(stroke_width / 2., stroke_width / 2.);
let viewbox_width = self.viewbox_width();
let viewbox_height = self.viewbox_height();
let mut elements_iter = self.elements().iter();
let maybe_viewbox = if viewbox_width > 0. && viewbox_height > 0. {
Some(euclid::rect(self.viewbox_x(), self.viewbox_y(), viewbox_width, viewbox_height))
} else {
None
};
elements_iter.fit(bounds_width, bounds_height, maybe_viewbox);
(offset, elements_iter)
}
}
impl ItemConsts for Path {
const cached_rendering_data_offset: const_field_offset::FieldOffset<Path, CachedRenderingData> =
Path::FIELD_OFFSETS.cached_rendering_data.as_unpinned_projection();
}

View file

@ -601,6 +601,7 @@ pub fn box_layout_info_ortho(cells: Slice<BoxLayoutCellData>, padding: &Padding)
fold
}
#[cfg(feature = "std")]
#[repr(C)]
pub struct PathLayoutData<'a> {
pub elements: &'a crate::graphics::PathData,
@ -612,13 +613,7 @@ pub struct PathLayoutData<'a> {
pub offset: f32,
}
#[repr(C)]
#[derive(Default)]
pub struct PathLayoutItemData {
pub width: Coord,
pub height: Coord,
}
#[cfg(feature = "std")]
pub fn solve_path_layout(data: &PathLayoutData, repeater_indexes: Slice<u32>) -> SharedVector<f32> {
use lyon_geom::*;
use lyon_path::iterator::PathIterator;

View file

@ -71,6 +71,7 @@ pub use graphics::Brush;
#[doc(inline)]
pub use graphics::RgbaColor;
#[cfg(feature = "std")]
#[doc(inline)]
pub use graphics::PathData;