Skip to content

Commit

Permalink
refactor(text): 💡 font DB store glyph as Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Jun 4, 2024
1 parent 3145f8d commit 82ced7d
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 166 deletions.
4 changes: 2 additions & 2 deletions core/src/animation/lerp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ impl Lerp for Transform {
impl Lerp for Pixel {
#[inline]
fn lerp(&self, to: &Self, factor: f32) -> Self {
let v = self.0.lerp(&to.0, factor);
Pixel(v.into())
let v = (**self).lerp(to, factor);
v.into()
}
}

Expand Down
60 changes: 30 additions & 30 deletions core/src/builtin_widgets/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,135 +183,135 @@ fn typography_theme(
TypographyTheme {
display_large: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(64.0.into()).into()),
line_height: Some(Em::from_pixel(64.0.into())),
font_size: FontSize::Pixel(57.0.into()),
letter_space: Some(Pixel(0.0.into())),
letter_space: Some(0.0.into()),
font_face: regular_face.clone(),
}),
decoration: decoration.clone(),
},
display_medium: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(52.0.into()).into()),
line_height: Some(Em::from_pixel(52.0.into())),
font_size: FontSize::Pixel(45.0.into()),
letter_space: Some(Pixel(0.0.into())),
letter_space: Some(0.0.into()),
font_face: regular_face.clone(),
}),
decoration: decoration.clone(),
},
display_small: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(44.0.into()).into()),
line_height: Some(Em::from_pixel(44.0.into())),
font_size: FontSize::Pixel(36.0.into()),
letter_space: Some(Pixel(0.0.into())),
letter_space: Some(0.0.into()),
font_face: regular_face.clone(),
}),
decoration: decoration.clone(),
},
headline_large: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(40.0.into()).into()),
line_height: Some(Em::from_pixel(40.0.into())),
font_size: FontSize::Pixel(32.0.into()),
letter_space: Some(Pixel(0.0.into())),
letter_space: Some(0.0.into()),
font_face: regular_face.clone(),
}),
decoration: decoration.clone(),
},
headline_medium: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(36.0.into()).into()),
line_height: Some(Em::from_pixel(36.0.into())),
font_size: FontSize::Pixel(28.0.into()),
letter_space: Some(Pixel(0.0.into())),
letter_space: Some(0.0.into()),
font_face: regular_face.clone(),
}),
decoration: decoration.clone(),
},
headline_small: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(32.0.into()).into()),
line_height: Some(Em::from_pixel(32.0.into())),
font_size: FontSize::Pixel(24.0.into()),
letter_space: Some(Pixel(0.0.into())),
letter_space: Some(0.0.into()),
font_face: regular_face.clone(),
}),
decoration: decoration.clone(),
},
title_large: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(28.0.into()).into()),
line_height: Some(Em::from_pixel(28.0.into())),
font_size: FontSize::Pixel(22.0.into()),
letter_space: Some(Pixel(0.0.into())),
letter_space: Some(0.0.into()),
font_face: medium_face.clone(),
}),
decoration: decoration.clone(),
},
title_medium: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(24.0.into()).into()),
line_height: Some(Em::from_pixel(24.0.into())),
font_size: FontSize::Pixel(16.0.into()),
letter_space: Some(Pixel(0.15.into())),
letter_space: Some(0.15.into()),
font_face: medium_face.clone(),
}),
decoration: decoration.clone(),
},
title_small: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(20.0.into()).into()),
line_height: Some(Em::from_pixel(20.0.into())),
font_size: FontSize::Pixel(14.0.into()),
letter_space: Some(Pixel(0.1.into())),
letter_space: Some(0.1.into()),
font_face: medium_face.clone(),
}),
decoration: decoration.clone(),
},
label_large: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(20.0.into()).into()),
line_height: Some(Em::from_pixel(20.0.into())),
font_size: FontSize::Pixel(14.0.into()),
letter_space: Some(Pixel(0.1.into())),
letter_space: Some(0.1.into()),
font_face: medium_face.clone(),
}),
decoration: decoration.clone(),
},
label_medium: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(16.0.into()).into()),
line_height: Some(Em::from_pixel(16.0.into())),
font_size: FontSize::Pixel(12.0.into()),
letter_space: Some(Pixel(0.5.into())),
letter_space: Some(0.5.into()),
font_face: medium_face.clone(),
}),
decoration: decoration.clone(),
},
label_small: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(16.0.into()).into()),
line_height: Some(Em::from_pixel(16.0.into())),
font_size: FontSize::Pixel(11.0.into()),
letter_space: Some(Pixel(0.5.into())),
letter_space: Some(0.5.into()),
font_face: medium_face,
}),
decoration: decoration.clone(),
},
body_large: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(24.0.into()).into()),
line_height: Some(Em::from_pixel(24.0.into())),
font_size: FontSize::Pixel(16.0.into()),
letter_space: Some(Pixel(0.5.into())),
letter_space: Some(0.5.into()),
font_face: regular_face.clone(),
}),
decoration: decoration.clone(),
},
body_medium: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(20.0.into()).into()),
line_height: Some(Em::from_pixel(20.0.into())),
font_size: FontSize::Pixel(14.0.into()),
letter_space: Some(Pixel(0.25.into())),
letter_space: Some(0.25.into()),
font_face: regular_face.clone(),
}),
decoration: decoration.clone(),
},
body_small: TextTheme {
text: CowArc::owned(TextStyle {
line_height: Some(Pixel(16.0.into()).into()),
line_height: Some(Em::from_pixel(16.0.into())),
font_size: FontSize::Pixel(12.0.into()),
letter_space: Some(Pixel(0.4.into())),
letter_space: Some(0.4.into()),
font_face: regular_face,
}),
decoration,
Expand Down
1 change: 1 addition & 0 deletions painter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde_json.workspace = true
tiny-skia-path = {workspace = true}
usvg.workspace = true
zerocopy = {workspace = true, optional = true, features = ["derive"]}
ordered-float = {workspace = true, features = ["serde"]}

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom.workspace = true
Expand Down
12 changes: 11 additions & 1 deletion painter/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,17 @@ impl Painter {
self
}

/// Fill a path with its style.
/// Outlines the current path with the current brush and `StrokeOptions`.
///
/// ## Note
///
/// Unlike `fill_path`, `stroke_path` accepts a `Path` instead of a
/// `PaintPath`. Therefore, the path will not be cached across `stroke_path`
/// calls, as the actual path depends on the current `StrokeOptions` of the
/// painter.
///
/// If you want to stroke a path using `Resource<Path>`, you should retain the
/// result of `Path::stroke` with `Resource<Path>` and pass it to `fill_path`.
pub fn stroke_path(&mut self, path: Path) -> &mut Self {
if let Some(stroke_path) = path.stroke(self.stroke_options(), Some(self.get_transform())) {
self.fill_path(stroke_path);
Expand Down
32 changes: 27 additions & 5 deletions painter/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub struct Path {
}

/// Describe how to paint path, fill or stroke.
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize, Default)]
pub enum PathPaintStyle {
#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize, Default)]
pub enum PathStyle {
/// Fill the path.
#[default]
Fill,
Expand All @@ -28,7 +28,7 @@ pub enum PathPaintStyle {
}

/// Stroke properties.
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct StrokeOptions {
/// A stroke thickness.
///
Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct StrokeOptions {
}

/// Draws at the beginning and end of an open path contour.
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Default)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Default, Hash)]
pub enum LineCap {
/// No stroke extension.
#[default]
Expand All @@ -67,7 +67,7 @@ pub enum LineCap {
Square,
}

#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Default)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Default, Hash)]
pub enum LineJoin {
/// Extends to miter limit.
#[default]
Expand Down Expand Up @@ -315,6 +315,28 @@ impl Default for StrokeOptions {
}
}

use ordered_float::OrderedFloat;

impl std::hash::Hash for StrokeOptions {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
OrderedFloat(self.width).hash(state);
OrderedFloat(self.miter_limit).hash(state);
self.line_cap.hash(state);
self.line_join.hash(state);
}
}

impl PartialEq for StrokeOptions {
fn eq(&self, other: &Self) -> bool {
OrderedFloat(self.width).eq(&OrderedFloat(other.width))
&& OrderedFloat(self.miter_limit).eq(&OrderedFloat(other.miter_limit))
&& self.line_cap.eq(&other.line_cap)
&& self.line_join.eq(&other.line_join)
}
}

impl Eq for StrokeOptions {}

#[cfg(feature = "tessellation")]
impl<Attr> Vertex<Attr> {
#[inline]
Expand Down
11 changes: 10 additions & 1 deletion painter/src/path_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,21 @@ impl PathBuilder {
stroke_path(&path, options, ts).map(Into::into)
}

/// Build a fill path, witch should fill with `style`
/// Construct a path from the current state of the builder.
#[inline]
pub fn build(self) -> Path {
// todo: we can store an anti-aliasing flag for the path.
self.lyon_builder.build().into()
}

/// Construct a path from the current state of the builder, and use the given
/// bounds as the bounds of the path.
///
/// Caller must ensure that the bounds are correct.
pub fn build_with_bounds(self, bounds: Rect) -> Path {
let path = self.lyon_builder.build();
Path { lyon_path: path, bounds }
}
}

pub(crate) fn stroke_path(
Expand Down
1 change: 0 additions & 1 deletion text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ version.workspace = true
derive_more.workspace = true
fontdb.workspace = true
log.workspace = true
lyon_path.workspace = true
ordered-float.workspace = true
ribir_algo = {path = "../algo", version = "0.3.0-alpha.5" }
ribir_geom = {path = "../geom", version = "0.3.0-alpha.5" }
Expand Down
Loading

0 comments on commit 82ced7d

Please sign in to comment.