diff options
| author | 2023-02-04 11:12:15 +0100 | |
|---|---|---|
| committer | 2023-02-24 13:29:11 +0100 | |
| commit | 238154af4ac8dda7f12dd90aa7be106e933bcb30 (patch) | |
| tree | 4c8560b3464f0c900ddf31a3c063e925394923c6 /native | |
| parent | b29de28d1f0f608f8029c93d154cfd1b0f8b8cbb (diff) | |
| download | iced-238154af4ac8dda7f12dd90aa7be106e933bcb30.tar.gz iced-238154af4ac8dda7f12dd90aa7be106e933bcb30.tar.bz2 iced-238154af4ac8dda7f12dd90aa7be106e933bcb30.zip  | |
Implement `font::load` command in `iced_native`
Diffstat (limited to '')
| -rw-r--r-- | native/src/command/action.rs | 16 | ||||
| -rw-r--r-- | native/src/font.rs | 19 | ||||
| -rw-r--r-- | native/src/lib.rs | 6 | ||||
| -rw-r--r-- | native/src/program.rs | 3 | ||||
| -rw-r--r-- | native/src/renderer/null.rs | 4 | ||||
| -rw-r--r-- | native/src/text.rs | 7 | 
6 files changed, 51 insertions, 4 deletions
diff --git a/native/src/command/action.rs b/native/src/command/action.rs index a51b8c21..d1589c05 100644 --- a/native/src/command/action.rs +++ b/native/src/command/action.rs @@ -1,10 +1,12 @@  use crate::clipboard; +use crate::font;  use crate::system;  use crate::widget;  use crate::window;  use iced_futures::MaybeSend; +use std::borrow::Cow;  use std::fmt;  /// An action that a [`Command`] can perform. @@ -27,6 +29,15 @@ pub enum Action<T> {      /// Run a widget action.      Widget(widget::Action<T>), + +    /// Load a font from its bytes. +    LoadFont { +        /// The bytes of the font to load. +        bytes: Cow<'static, [u8]>, + +        /// The message to produce when the font has been loaded. +        tagger: Box<dyn Fn(Result<(), font::Error>) -> T>, +    },  }  impl<T> Action<T> { @@ -49,6 +60,10 @@ impl<T> Action<T> {              Self::Window(window) => Action::Window(window.map(f)),              Self::System(system) => Action::System(system.map(f)),              Self::Widget(widget) => Action::Widget(widget.map(f)), +            Self::LoadFont { bytes, tagger } => Action::LoadFont { +                bytes, +                tagger: Box::new(move |result| f(tagger(result))), +            },          }      }  } @@ -63,6 +78,7 @@ impl<T> fmt::Debug for Action<T> {              Self::Window(action) => write!(f, "Action::Window({action:?})"),              Self::System(action) => write!(f, "Action::System({action:?})"),              Self::Widget(_action) => write!(f, "Action::Widget"), +            Self::LoadFont { .. } => write!(f, "Action::LoadFont"),          }      }  } diff --git a/native/src/font.rs b/native/src/font.rs new file mode 100644 index 00000000..6840a25f --- /dev/null +++ b/native/src/font.rs @@ -0,0 +1,19 @@ +//! Load and use fonts. +pub use iced_core::Font; + +use crate::command::{self, Command}; +use std::borrow::Cow; + +/// An error while loading a font. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Error {} + +/// Load a font from its bytes. +pub fn load( +    bytes: impl Into<Cow<'static, [u8]>>, +) -> Command<Result<(), Error>> { +    Command::single(command::Action::LoadFont { +        bytes: bytes.into(), +        tagger: Box::new(std::convert::identity), +    }) +} diff --git a/native/src/lib.rs b/native/src/lib.rs index ebdc8490..27b6fc0d 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -47,6 +47,7 @@  pub mod clipboard;  pub mod command;  pub mod event; +pub mod font;  pub mod image;  pub mod keyboard;  pub mod layout; @@ -80,8 +81,8 @@ mod debug;  pub use iced_core::alignment;  pub use iced_core::time;  pub use iced_core::{ -    color, Alignment, Background, Color, ContentFit, Font, Length, Padding, -    Pixels, Point, Rectangle, Size, Vector, +    color, Alignment, Background, Color, ContentFit, Length, Padding, Pixels, +    Point, Rectangle, Size, Vector,  };  pub use iced_futures::{executor, futures};  pub use iced_style::application; @@ -95,6 +96,7 @@ pub use command::Command;  pub use debug::Debug;  pub use element::Element;  pub use event::Event; +pub use font::Font;  pub use hasher::Hasher;  pub use layout::Layout;  pub use overlay::Overlay; diff --git a/native/src/program.rs b/native/src/program.rs index c71c237f..25cab332 100644 --- a/native/src/program.rs +++ b/native/src/program.rs @@ -1,4 +1,5 @@  //! Build interactive programs using The Elm Architecture. +use crate::text;  use crate::{Command, Element, Renderer};  mod state; @@ -8,7 +9,7 @@ pub use state::State;  /// The core of a user interface application following The Elm Architecture.  pub trait Program: Sized {      /// The graphics backend to use to draw the [`Program`]. -    type Renderer: Renderer; +    type Renderer: Renderer + text::Renderer;      /// The type of __messages__ your [`Program`] will produce.      type Message: std::fmt::Debug + Send; diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 50d7d6d6..150ee786 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -2,6 +2,8 @@ use crate::renderer::{self, Renderer};  use crate::text::{self, Text};  use crate::{Background, Font, Point, Rectangle, Size, Theme, Vector}; +use std::borrow::Cow; +  /// A renderer that does nothing.  ///  /// It can be useful if you are writing tests! @@ -52,6 +54,8 @@ impl text::Renderer for Null {          16.0      } +    fn load_font(&mut self, _font: Cow<'static, [u8]>) {} +      fn measure(          &self,          _content: &str, diff --git a/native/src/text.rs b/native/src/text.rs index b7915a55..1bbd36cc 100644 --- a/native/src/text.rs +++ b/native/src/text.rs @@ -2,6 +2,8 @@  use crate::alignment;  use crate::{Color, Point, Rectangle, Size, Vector}; +use std::borrow::Cow; +  /// A paragraph.  #[derive(Debug, Clone, Copy)]  pub struct Text<'a, Font> { @@ -72,7 +74,7 @@ pub trait Renderer: crate::Renderer {      /// [`ICON_FONT`]: Self::ICON_FONT      const ARROW_DOWN_ICON: char; -    /// Returns the default [`Font`]. +    /// Returns the default [`Self::Font`].      fn default_font(&self) -> Self::Font;      /// Returns the default size of [`Text`]. @@ -112,6 +114,9 @@ pub trait Renderer: crate::Renderer {          nearest_only: bool,      ) -> Option<Hit>; +    /// Loads a [`Self::Font`] from its bytes. +    fn load_font(&mut self, font: Cow<'static, [u8]>); +      /// Draws the given [`Text`].      fn fill_text(&mut self, text: Text<'_, Self::Font>);  }  | 
