🎉 Swap default font for source sans (wasm)

This commit is contained in:
Belén Albeza 2025-04-08 14:46:36 +02:00
parent 2470c1788e
commit 90cb0357c6
6 changed files with 34 additions and 15 deletions

View file

@ -253,7 +253,6 @@
(defn get-variant (defn get-variant
[{:keys [variants] :as font} font-variant-id] [{:keys [variants] :as font} font-variant-id]
(prn "get-variant" font-variant-id fonts)
(or (d/seek #(= (:id %) font-variant-id) variants) (or (d/seek #(= (:id %) font-variant-id) variants)
(get-default-variant font))) (get-default-variant font)))

Binary file not shown.

View file

@ -1,12 +1,22 @@
use skia_safe::{self as skia, textlayout, Font, FontMgr}; use skia_safe::{self as skia, textlayout, Font, FontMgr};
use crate::shapes::FontFamily; use crate::shapes::{FontFamily, FontStyle};
use crate::uuid::Uuid;
const DEFAULT_FONT_BYTES: &[u8] = include_bytes!("../fonts/RobotoMono-Regular.ttf");
const EMOJI_FONT_BYTES: &[u8] = include_bytes!("../fonts/NotoColorEmoji-Regular.ttf"); const EMOJI_FONT_BYTES: &[u8] = include_bytes!("../fonts/NotoColorEmoji-Regular.ttf");
pub static DEFAULT_FONT: &'static str = "robotomono-regular";
pub static DEFAULT_EMOJI_FONT: &'static str = "noto-color-emoji"; pub static DEFAULT_EMOJI_FONT: &'static str = "noto-color-emoji";
const DEFAULT_FONT_BYTES: &[u8] = include_bytes!("../fonts/sourcesanspro-regular.ttf");
pub fn default_font() -> String {
let family = FontFamily::new(default_font_uuid(), 400, FontStyle::Normal);
format!("{}", family)
}
fn default_font_uuid() -> Uuid {
Uuid::nil()
}
pub struct FontStore { pub struct FontStore {
font_mgr: FontMgr, font_mgr: FontMgr,
font_provider: textlayout::TypefaceFontProvider, font_provider: textlayout::TypefaceFontProvider,
@ -18,25 +28,19 @@ impl FontStore {
pub fn new() -> Self { pub fn new() -> Self {
let font_mgr = FontMgr::new(); let font_mgr = FontMgr::new();
let mut font_provider = skia::textlayout::TypefaceFontProvider::new(); let mut font_provider = load_default_provider(&font_mgr);
let default_font = font_mgr
.new_from_data(DEFAULT_FONT_BYTES, None)
.expect("Failed to load font");
font_provider.register_typeface(default_font, DEFAULT_FONT);
// TODO: Load emoji font lazily
let emoji_font = font_mgr let emoji_font = font_mgr
.new_from_data(EMOJI_FONT_BYTES, None) .new_from_data(EMOJI_FONT_BYTES, None)
.expect("Failed to load font"); .expect("Failed to load font");
font_provider.register_typeface(emoji_font, DEFAULT_EMOJI_FONT); font_provider.register_typeface(emoji_font, DEFAULT_EMOJI_FONT);
let mut font_collection = skia::textlayout::FontCollection::new(); let mut font_collection = skia::textlayout::FontCollection::new();
font_collection.set_default_font_manager(FontMgr::from(font_provider.clone()), None); font_collection.set_default_font_manager(FontMgr::from(font_provider.clone()), None);
let debug_typeface = font_provider let debug_typeface = font_provider
.match_family_style("robotomono-regular", skia::FontStyle::default()) .match_family_style(default_font().as_str(), skia::FontStyle::default())
.unwrap(); .unwrap();
let debug_font = skia::Font::new(debug_typeface, 10.0); let debug_font = skia::Font::new(debug_typeface, 10.0);
@ -85,3 +89,15 @@ impl FontStore {
self.font_provider.family_names().any(|x| x == serialized) self.font_provider.family_names().any(|x| x == serialized)
} }
} }
fn load_default_provider(font_mgr: &FontMgr) -> skia::textlayout::TypefaceFontProvider {
let mut font_provider = skia::textlayout::TypefaceFontProvider::new();
let family = FontFamily::new(default_font_uuid(), 400, FontStyle::Normal);
let font = font_mgr
.new_from_data(DEFAULT_FONT_BYTES, None)
.expect("Failed to load font");
font_provider.register_typeface(font, family.alias().as_str());
font_provider
}

View file

@ -39,6 +39,10 @@ impl FontFamily {
pub fn new(id: Uuid, weight: u32, style: FontStyle) -> Self { pub fn new(id: Uuid, weight: u32, style: FontStyle) -> Self {
Self { id, style, weight } Self { id, style, weight }
} }
pub fn alias(&self) -> String {
format!("{}", self)
}
} }
impl fmt::Display for FontFamily { impl fmt::Display for FontFamily {

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
math::Rect, math::Rect,
render::{DEFAULT_EMOJI_FONT, DEFAULT_FONT}, render::{default_font, DEFAULT_EMOJI_FONT},
}; };
use skia_safe::{ use skia_safe::{
self as skia, self as skia,
@ -133,7 +133,7 @@ impl TextLeaf {
style.set_font_size(self.font_size); style.set_font_size(self.font_size);
style.set_font_families(&[ style.set_font_families(&[
self.serialized_font_family(), self.serialized_font_family(),
DEFAULT_FONT.to_string(), default_font(),
DEFAULT_EMOJI_FONT.to_string(), DEFAULT_EMOJI_FONT.to_string(),
]); ]);
style style