Font

Struct Font 

Source
pub struct Font { /* private fields */ }
Expand description

This struct represent a font. It is useful when using the Text struct.

§Example

use wasm_game_lib::graphics::font::Font;
use wasm_game_lib::graphics::text::Text;
 
// use a default font
let arial = Font::arial();
 
// load a custom font
let trade_winds = Font::load("https://fonts.gstatic.com/s/tradewinds/v8/AYCPpXPpYNIIT7h8-QenM0Jt5vM.woff2").await.unwrap();
 
// use the fonts with the Text struct
let arial_text = Text::new_with_text_and_coords(&arial, String::from("This text is using Arial font."), (0,100));
let trade_winds_text = Text::new_with_text_and_coords(&trade_winds, String::from("This text is using Trade Winds font."), (0,200));

Implementations§

Source§

impl Font

Source

pub async fn load(url: &str) -> Result<Font, JsValue>

Load a custom font from an url. The load may fail so a Result is returned.

§Example
use wasm_game_lib::graphics::font::Font;
use wasm_game_lib::graphics::text::Text;
 
// load the font
let trade_winds = Font::load("https://fonts.gstatic.com/s/tradewinds/v8/AYCPpXPpYNIIT7h8-QenM0Jt5vM.woff2").await.unwrap();
 
// use the font with the Text struct
let trade_winds_text = Text::new_with_text_and_coords(&trade_winds, String::from("This text is using Trade Winds font."), (0,100));
§About Google Fonts

If you are using google fonts, you may have a link like “https://fonts.googleapis.com/css?family=Trade+Winds&display=swap”. To get the font url from this link, load this link in a web browser. You should get something like that:

/* latin */
@font-face {
    font-family: 'Trade Winds';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: local('Trade Winds'), local('TradeWinds'), url(https://fonts.gstatic.com/s/tradewinds/v8/AYCPpXPpYNIIT7h8-QenM0Jt5vM.woff2) format('woff2');
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

The url of the font is located at the seventh line, after “url(”.

Source

pub async fn load_and_send(url: &str, sender: Sender<Result<Font, JsValue>>)

Load a custom font from an url and send it trought a oneshot channel.

It works exactly like images so see that for an example.

Source

pub fn sherif() -> Font

Return the Sherif font.

Source

pub fn arial() -> Font

Return the Arial font.

Source

pub fn times_new_roman() -> Font

Return the Times New Roman font.

Source

pub fn times() -> Font

Return the Times font.

Source

pub fn courier_new() -> Font

Return the Courier New font.

Source

pub fn courier() -> Font

Return the Courier font.

Source

pub fn verdana() -> Font

Return the Verdana font.

Source

pub fn georgia() -> Font

Return the Georgia font.

Source

pub fn palatino() -> Font

Return the Palatino font.

Source

pub fn garamond() -> Font

Return the Garamond font.

Source

pub fn bookman() -> Font

Return the Bookman font.

Source

pub fn comic_sans_ms() -> Font

Return the Comic Sans Ms font.

Source

pub fn candara() -> Font

Return the Candara font.

Source

pub fn helvetica() -> Font

Return the Helvetica font.

Source

pub fn arial_black() -> Font

Return the Arial Black font.

Source

pub fn impact() -> Font

Return the Inpact font.

Trait Implementations§

Source§

impl Debug for Font

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Font

§

impl RefUnwindSafe for Font

§

impl Send for Font

§

impl Sync for Font

§

impl Unpin for Font

§

impl UnwindSafe for Font

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.

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.

Source§

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

Source§

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>,

Source§

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.