Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions crates/rustmotion-components/src/avatar_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use skia_safe::{Canvas, Paint, PaintStyle, RRect, Rect};
use rustmotion_core::engine::animator::AnimatedProperties;
use rustmotion_core::engine::layout_pass::BoxLayout;
use rustmotion_core::engine::renderer::{
asset_cache, draw_text_with_fallback, emoji_typeface, font_mgr, measure_text_with_fallback,
paint_from_hex,
asset_cache, draw_text_with_fallback, emoji_typeface, measure_text_with_fallback,
paint_from_hex, typeface_with_fallback,
};
use rustmotion_core::error::RustmotionError;
use rustmotion_core::schema::TimelineStep;
Expand Down Expand Up @@ -164,13 +164,10 @@ impl AvatarGroup {
// Text
let text = format!("+{}", overflow);
let font_size = s * 0.35;
let fm = font_mgr();
let font_style = skia_safe::FontStyle::bold();
let typeface = fm
.match_family_style("Inter", font_style)
.or_else(|| fm.match_family_style("Helvetica", font_style))
.or_else(|| fm.match_family_style("Arial", font_style))
.unwrap_or_else(|| fm.legacy_make_typeface(None, font_style).unwrap());
let Ok(typeface) = typeface_with_fallback("Inter", font_style) else {
return Ok(());
};
let font = skia_safe::Font::from_typeface(typeface, font_size);
let emoji_font =
emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, font_size));
Expand Down
40 changes: 15 additions & 25 deletions crates/rustmotion-components/src/badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use rustmotion_core::css::CssStyle;
use rustmotion_core::engine::animator::AnimatedProperties;
use rustmotion_core::engine::layout_pass::BoxLayout;
use rustmotion_core::engine::renderer::{
asset_cache, draw_text_with_fallback, emoji_typeface, fetch_icon_svg, font_mgr,
measure_text_with_fallback, paint_from_hex,
asset_cache, draw_text_with_fallback, emoji_typeface, fetch_icon_svg,
measure_text_with_fallback, paint_from_hex, typeface_with_fallback,
};
use rustmotion_core::error::RustmotionError;
use rustmotion_core::schema::TimelineStep;
use rustmotion_core::traits::{PaintCtx, Painter, TimingConfig};

Expand Down Expand Up @@ -95,20 +94,14 @@ impl Badge {
(h_pad * ratio, v_pad * ratio, icon_size * ratio)
}

fn make_font(&self) -> skia_safe::Font {
let fm = font_mgr();
fn make_font(&self) -> Option<skia_safe::Font> {
let font_style = skia_safe::FontStyle::normal();
let family = self.style.font_family.as_deref().unwrap_or("Inter");

let typeface = fm
.match_family_style(family, font_style)
.or_else(|| fm.match_family_style("Helvetica", font_style))
.or_else(|| fm.match_family_style("Arial", font_style))
.or_else(|| fm.match_family_style("sans-serif", font_style))
.or_else(|| fm.legacy_make_typeface(None, font_style))
.unwrap_or_else(|| panic!("{}", RustmotionError::FontNotFound.to_string()));

skia_safe::Font::from_typeface(typeface, self.resolved_font_size())
let typeface = typeface_with_fallback(family, font_style).ok()?;
Some(skia_safe::Font::from_typeface(
typeface,
self.resolved_font_size(),
))
}
}

Expand Down Expand Up @@ -209,7 +202,9 @@ impl Badge {
} else {
color
};
let font = self.make_font();
let Some(font) = self.make_font() else {
return;
};
let font_size = self.resolved_font_size();
let emoji_font = emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, font_size));
let mut text_paint = paint_from_hex(text_color);
Expand Down Expand Up @@ -270,15 +265,10 @@ impl Badge {
};

let count_fs = font_size * 0.65;
let fm = font_mgr();
let count_typeface = fm
.match_family_style("Inter", skia_safe::FontStyle::bold())
.or_else(|| fm.match_family_style("Helvetica", skia_safe::FontStyle::bold()))
.or_else(|| fm.match_family_style("Arial", skia_safe::FontStyle::bold()))
.unwrap_or_else(|| {
fm.legacy_make_typeface(None, skia_safe::FontStyle::bold())
.unwrap()
});
let Ok(count_typeface) = typeface_with_fallback("Inter", skia_safe::FontStyle::bold())
else {
return;
};
let count_font = skia_safe::Font::from_typeface(count_typeface, count_fs);
let count_emoji =
emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, count_fs));
Expand Down
10 changes: 2 additions & 8 deletions crates/rustmotion-components/src/callout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use skia_safe::{Canvas, PaintStyle, Path, RRect, Rect};

use rustmotion_core::engine::animator::AnimatedProperties;
use rustmotion_core::engine::layout_pass::BoxLayout;
use rustmotion_core::engine::renderer::{font_mgr, paint_from_hex, wrap_text};
use rustmotion_core::error::RustmotionError;
use rustmotion_core::engine::renderer::{paint_from_hex, typeface_with_fallback, wrap_text};
use rustmotion_core::schema::TimelineStep;
use rustmotion_core::traits::{PaintCtx, Painter, TimingConfig};

Expand Down Expand Up @@ -147,14 +146,9 @@ impl Callout {
canvas.draw_path(&arrow, &bg_paint);

// Draw text
let fm = font_mgr();
let font_style = skia_safe::FontStyle::normal();
let family = self.style.font_family.as_deref().unwrap_or("Inter");
let typeface = fm
.match_family_style(family, font_style)
.or_else(|| fm.match_family_style("Helvetica", font_style))
.or_else(|| fm.match_family_style("Arial", font_style))
.ok_or(RustmotionError::FontNotFound)?;
let typeface = typeface_with_fallback(family, font_style)?;

let font = skia_safe::Font::from_typeface(typeface, font_size);
let (_, metrics) = font.metrics();
Expand Down
16 changes: 5 additions & 11 deletions crates/rustmotion-components/src/caption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use rustmotion_core::css::CssStyle;
use rustmotion_core::engine::animator::AnimatedProperties;
use rustmotion_core::engine::layout_pass::BoxLayout;
use rustmotion_core::engine::renderer::{
draw_text_with_fallback, emoji_typeface, font_mgr, measure_text_with_fallback, paint_from_hex,
draw_text_with_fallback, emoji_typeface, measure_text_with_fallback, paint_from_hex,
typeface_with_fallback,
};
use rustmotion_core::schema::{CaptionStyle, CaptionWord, TimelineStep};
use rustmotion_core::traits::{PaintCtx, Painter, TimingConfig};
Expand Down Expand Up @@ -42,16 +43,9 @@ impl Caption {
let color = self.style.color_str_or("#FFFFFF");
let font_family = self.style.font_family_or("Inter");

let fm = font_mgr();
let typeface = fm
.match_family_style(font_family, FontStyle::bold())
.or_else(|| fm.match_family_style("Helvetica", FontStyle::bold()))
.or_else(|| fm.match_family_style("Arial", FontStyle::bold()))
.or_else(|| fm.match_family_style("sans-serif", FontStyle::bold()))
.unwrap_or_else(|| {
fm.legacy_make_typeface(None, FontStyle::bold())
.expect("No fallback font")
});
let Ok(typeface) = typeface_with_fallback(font_family, FontStyle::bold()) else {
return;
};

let font = Font::from_typeface(typeface, font_size);
let emoji_font = emoji_typeface().map(|tf| Font::from_typeface(tf, font_size));
Expand Down
4 changes: 3 additions & 1 deletion crates/rustmotion-components/src/chart/axes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ impl Chart {
x_labels: &[String],
categorical: bool,
) {
let font = self.make_label_font();
let Some(font) = self.make_label_font() else {
return;
};
let emoji_font =
emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, self.label_font_size));
let (_, metrics) = font.metrics();
Expand Down
4 changes: 3 additions & 1 deletion crates/rustmotion-components/src/chart/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ impl Chart {
let gap = 8.0;
let bar_h = (chart_h - gap * (n + 1) as f32) / n as f32;

let font = self.make_label_font();
let Some(font) = self.make_label_font() else {
return Ok(());
};
let emoji_font =
emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, self.label_font_size));
let (_, metrics) = font.metrics();
Expand Down
8 changes: 6 additions & 2 deletions crates/rustmotion-components/src/chart/funnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ impl Chart {
let total_gap = gap * (n.saturating_sub(1)) as f32;
let seg_h = (h - total_gap) / n as f32;

let font = self.make_label_font();
let Some(font) = self.make_label_font() else {
return Ok(());
};
let emoji_font =
emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, self.label_font_size));
let (_, metrics) = font.metrics();
Expand Down Expand Up @@ -120,7 +122,9 @@ impl Chart {
let total_gap = gap * (n.saturating_sub(1)) as f32;
let seg_w = (w - total_gap) / n as f32;

let font = self.make_label_font();
let Some(font) = self.make_label_font() else {
return Ok(());
};
let emoji_font =
emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, self.label_font_size));
let (_, metrics) = font.metrics();
Expand Down
17 changes: 7 additions & 10 deletions crates/rustmotion-components/src/chart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use skia_safe::Canvas;
use rustmotion_core::css::CssStyle;
use rustmotion_core::engine::animator::AnimatedProperties;
use rustmotion_core::engine::layout_pass::BoxLayout;
use rustmotion_core::engine::renderer::font_mgr;
use rustmotion_core::engine::renderer::typeface_with_fallback;
use rustmotion_core::schema::TimelineStep;
use rustmotion_core::traits::{PaintCtx, Painter, TimingConfig};

Expand Down Expand Up @@ -286,16 +286,13 @@ impl Chart {
(8.0, 8.0, bottom + 8.0, left + 8.0)
}

pub(super) fn make_label_font(&self) -> skia_safe::Font {
let fm = font_mgr();
pub(super) fn make_label_font(&self) -> Option<skia_safe::Font> {
let font_style = skia_safe::FontStyle::normal();
let typeface = fm
.match_family_style("Inter", font_style)
.or_else(|| fm.match_family_style("Helvetica", font_style))
.or_else(|| fm.match_family_style("Arial", font_style))
.or_else(|| fm.match_family_style("sans-serif", font_style))
.unwrap_or_else(|| fm.legacy_make_typeface(None, font_style).unwrap());
skia_safe::Font::from_typeface(typeface, self.label_font_size)
let typeface = typeface_with_fallback("Inter", font_style).ok()?;
Some(skia_safe::Font::from_typeface(
typeface,
self.label_font_size,
))
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/rustmotion-components/src/chart/radar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ impl Chart {
}

// Draw axis labels
let font = self.make_label_font();
let Some(font) = self.make_label_font() else {
return Ok(());
};
let emoji_font =
emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, self.label_font_size));
let (_, metrics) = font.metrics();
Expand Down
15 changes: 5 additions & 10 deletions crates/rustmotion-components/src/codeblock/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use skia_safe::{Canvas, Font, FontStyle, Rect};

use super::Codeblock;
use rustmotion_core::engine::renderer::{
draw_text_with_fallback, emoji_typeface, font_mgr, measure_text_with_fallback, paint_from_hex,
draw_text_with_fallback, emoji_typeface, measure_text_with_fallback, paint_from_hex,
typeface_with_fallback,
};

pub(super) fn draw_chrome(
Expand Down Expand Up @@ -39,15 +40,9 @@ pub(super) fn draw_chrome(
}

if let Some(ref title) = chrome.title {
let fm = font_mgr();
let typeface = fm
.match_family_style("Inter", FontStyle::normal())
.or_else(|| fm.match_family_style("Helvetica", FontStyle::normal()))
.or_else(|| fm.match_family_style("Arial", FontStyle::normal()))
.unwrap_or_else(|| {
fm.match_family_style("sans-serif", FontStyle::normal())
.unwrap()
});
let Ok(typeface) = typeface_with_fallback("Inter", FontStyle::normal()) else {
return;
};
let title_font = Font::from_typeface(typeface, 13.0);
let emoji_font = emoji_typeface().map(|tf| Font::from_typeface(tf, 13.0));
let title_width = measure_text_with_fallback(title, &title_font, &emoji_font, 0.0);
Expand Down
16 changes: 5 additions & 11 deletions crates/rustmotion-components/src/codeblock/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ pub(super) fn highlight_code(code: &str, language: &str, theme: &Theme) -> Vec<H
result
}

pub(super) fn resolve_monospace_font(family: &str, size: f32, weight: FontWeight) -> Font {
pub(super) fn resolve_monospace_font(family: &str, size: f32, weight: FontWeight) -> Option<Font> {
let font_mgr = rustmotion_core::engine::renderer::font_mgr();
let w: i32 = match weight {
FontWeight::Normal => 400,
Expand All @@ -551,14 +551,8 @@ pub(super) fn resolve_monospace_font(family: &str, size: f32, weight: FontWeight
.iter()
.filter_map(|name| font_mgr.match_family_style(name, style))
.next()
.unwrap_or_else(|| {
if font_mgr.count_families() > 0 {
font_mgr
.match_family_style(font_mgr.family_name(0), style)
.unwrap()
} else {
panic!("No fonts available on this system");
}
});
Font::from_typeface(typeface, size)
.or_else(|| {
rustmotion_core::engine::renderer::typeface_with_fallback(family, style).ok()
})?;
Some(Font::from_typeface(typeface, size))
}
4 changes: 3 additions & 1 deletion crates/rustmotion-components/src/codeblock/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ pub(super) fn render_codeblock(
_ => FontWeight::Normal,
};
let actual_line_height = layer.style.line_height_for(font_size);
let font = resolve_monospace_font(font_family, font_size, font_weight);
let Some(font) = resolve_monospace_font(font_family, font_size, font_weight) else {
return;
};
let padding = {
let (t, r, b, l) = layer.style.padding_px();
if t == 0.0 && r == 0.0 && b == 0.0 && l == 0.0 {
Expand Down
14 changes: 7 additions & 7 deletions crates/rustmotion-components/src/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rustmotion_core::css::CssStyle;
use rustmotion_core::engine::animator::AnimatedProperties;
use rustmotion_core::engine::layout_pass::BoxLayout;
use rustmotion_core::engine::renderer::{
draw_text_with_fallback, emoji_typeface, font_mgr, measure_text_with_fallback, paint_from_hex,
parse_hex_color,
draw_text_with_fallback, emoji_typeface, measure_text_with_fallback, paint_from_hex,
parse_hex_color, typeface_with_fallback,
};
use rustmotion_core::schema::TimelineStep;
use rustmotion_core::traits::{PaintCtx, Painter, TimingConfig};
Expand Down Expand Up @@ -127,12 +127,12 @@ impl Comparison {
canvas.draw_rect(right_rect, &right_paint);

let font_size = (h * 0.08).clamp(16.0, 36.0);
let fm = font_mgr();
let font_style = skia_safe::FontStyle::bold();
let typeface = fm
.match_family_style("Inter", font_style)
.or_else(|| fm.match_family_style("Helvetica", font_style))
.unwrap_or_else(|| fm.legacy_make_typeface(None, font_style).unwrap());
let Ok(typeface) = typeface_with_fallback("Inter", font_style) else {
// Balance the canvas.save() above before bailing out.
canvas.restore();
return;
};
let font = skia_safe::Font::from_typeface(typeface, font_size);
let emoji_font = emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, font_size));

Expand Down
12 changes: 5 additions & 7 deletions crates/rustmotion-components/src/countdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rustmotion_core::css::CssStyle;
use rustmotion_core::engine::animator::AnimatedProperties;
use rustmotion_core::engine::layout_pass::BoxLayout;
use rustmotion_core::engine::renderer::{
draw_text_with_fallback, emoji_typeface, font_mgr, measure_text_with_fallback, paint_from_hex,
parse_hex_color,
draw_text_with_fallback, emoji_typeface, measure_text_with_fallback, paint_from_hex,
parse_hex_color, typeface_with_fallback,
};
use rustmotion_core::schema::TimelineStep;
use rustmotion_core::traits::{PaintCtx, Painter, TimingConfig};
Expand Down Expand Up @@ -178,12 +178,10 @@ impl Countdown {
let secs = total_secs % 60;

let font_size = self.digit_size * 0.6;
let fm = font_mgr();
let font_style = skia_safe::FontStyle::bold();
let typeface = fm
.match_family_style("Inter", font_style)
.or_else(|| fm.match_family_style("Helvetica", font_style))
.unwrap_or_else(|| fm.legacy_make_typeface(None, font_style).unwrap());
let Ok(typeface) = typeface_with_fallback("Inter", font_style) else {
return;
};
let font = skia_safe::Font::from_typeface(typeface, font_size);
let emoji_font = emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, font_size));

Expand Down
Loading
Loading