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
impl Font
Sourcepub async fn load(url: &str) -> Result<Font, JsValue>
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(”.
Sourcepub async fn load_and_send(url: &str, sender: Sender<Result<Font, JsValue>>)
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.
Sourcepub fn times_new_roman() -> Font
pub fn times_new_roman() -> Font
Return the Times New Roman font.
Sourcepub fn courier_new() -> Font
pub fn courier_new() -> Font
Return the Courier New font.
Sourcepub fn comic_sans_ms() -> Font
pub fn comic_sans_ms() -> Font
Return the Comic Sans Ms font.
Sourcepub fn arial_black() -> Font
pub fn arial_black() -> Font
Return the Arial Black font.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more