Struct si_img::SiFont

source ·
pub struct SiFont { /* private fields */ }
Expand description

Represents a font used for text rendering.

Implementations§

source§

impl SiFont

source

pub fn new(vec: Vec<u8>) -> Self

Creates a new SiFont from a vector of font data.

source

pub fn from_vec(vec: Vec<u8>) -> SiFont

Creates a new SiFont from a vector of font data.

source

pub async fn from_network_async(url: &str) -> Result<SiFont, JsValue>

Creates a new SiFont from font data fetched from a network URL asynchronously.

Arguments
  • url - The URL from which to fetch the font data.
source

pub fn from_network(src: &str) -> SiFont

Creates a new SiFont from font data fetched from a network URL synchronously.

Arguments
  • src - The URL from which to fetch the font data.
Examples found in repository?
examples/main.rs (lines 5-7)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
fn main() {
    let font = SiFont::from_network(
        "https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
    );
    let preset = SiPreset::new(Box::new(|img: &mut SiImage, values: std::collections::HashMap<String, Box<dyn std::any::Any>>| {
        println!("Dimensions: {}, {}", img.width(), img.height());
        let font = match values.get("font") {
            Some(font) => {
                if font.type_id() == TypeId::of::<SiFont>() {
                    font.downcast_ref::<SiFont>().unwrap().clone()
                } else {
                    SiFont::from_network(
                        "https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
                    )
                }
            }
            None => SiFont::from_network(
                "https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
            )
        };
        let new_img = img.clone().render_text(
            "Hello from Preset!",
            32.0,
            480.00,
            480.00,
            Some("#00ffff".to_string()),
            &font,
        );
        new_img
    }));
    let img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/regraphic");

    let mut binding = img.render_text(
        "Hello, World!",
        64.0,
        480.00,
        254.00,
        Some("#00ffff".to_string()),
        &font.clone(),
    )
    .render_text(
        "Hello, Tagline!",
        48.0,
        480.0,
        320.0,
        None,
        &font.clone(),
    );
    let font = SiFont::from_network(
        "https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/arial.ttf",
    );
    let _font: Box<dyn std::any::Any> = Box::new(font.clone());
    let img = binding.load_preset(preset, std::collections::HashMap::from([("font".to_string(), _font)]));
    let mut file = File::create("output.png").expect("Could not create bytes");
    let _ = image::load_from_memory(&img.clone().to_bytes())
        .expect("Could not load image")
        .write_to(&mut file, image::ImageFormat::Png);
    println!("Created!")
}

Trait Implementations§

source§

impl Clone for SiFont

source§

fn clone(&self) -> SiFont

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl From<SiFont> for JsValue

source§

fn from(value: SiFont) -> Self

Converts to this type from the input type.
source§

impl FromWasmAbi for SiFont

§

type Abi = u32

The wasm ABI type that this converts from when coming back out from the ABI boundary.
source§

unsafe fn from_abi(js: u32) -> Self

Recover a Self from Self::Abi. Read more
source§

impl IntoWasmAbi for SiFont

§

type Abi = u32

The wasm ABI type that this converts into when crossing the ABI boundary.
source§

fn into_abi(self) -> u32

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
source§

impl LongRefFromWasmAbi for SiFont

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = Ref<'static, SiFont>

Same as RefFromWasmAbi::Anchor
source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
source§

impl OptionFromWasmAbi for SiFont

source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
source§

impl OptionIntoWasmAbi for SiFont

source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
source§

impl RefFromWasmAbi for SiFont

§

type Abi = u32

The wasm ABI type references to Self are recovered from.
§

type Anchor = Ref<'static, SiFont>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
source§

impl RefMutFromWasmAbi for SiFont

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = RefMut<'static, SiFont>

Same as RefFromWasmAbi::Anchor
source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
source§

impl TryFrom<JsValue> for SiFont

§

type Error = JsValue

The type returned in the event of a conversion error.
source§

fn try_from(value: JsValue) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl VectorFromWasmAbi for SiFont

§

type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi

source§

unsafe fn vector_from_abi(js: Self::Abi) -> Box<[SiFont]>

source§

impl VectorIntoWasmAbi for SiFont

§

type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi

source§

fn vector_into_abi(vector: Box<[SiFont]>) -> Self::Abi

source§

impl WasmDescribe for SiFont

source§

impl WasmDescribeVector for SiFont

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ReturnWasmAbi for T
where T: IntoWasmAbi,

§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more