Skip to content

Commit

Permalink
feat: reimplement vello text rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Stumblinbear committed Feb 16, 2024
1 parent a18d685 commit 4dc90be
Show file tree
Hide file tree
Showing 21 changed files with 486 additions and 318 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ lyon = "1.0"
raw-window-handle = "0.5"

winit = { version = "0.28" }
vello = { git = "https://github.com/linebender/vello.git", version = "0.0.1", rev = "bd235d7977772440618eebb3d304bc85be143e65" }
vello = { git = "https://github.com/linebender/vello.git", version = "0.0.1", rev = "d902830c2e92c5d1341530e3404c4c981530aed6" }

[features]
default = ["primitives", "widgets", "macros"]
Expand Down
4 changes: 4 additions & 0 deletions crates/agui_core/src/element/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ pub trait ContextRenderObject {
}

pub trait ContextDirtyRenderObject {
/// Marks the render object as needing layout.
///
/// This will cause a layout pass from its nearest ancestor layout boundary.
fn mark_needs_layout(&mut self);

/// Marks the render object as needing paint.
fn mark_needs_paint(&mut self);
}

Expand Down
50 changes: 25 additions & 25 deletions crates/agui_core/src/unit/font.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
use std::{borrow::Cow, sync::Arc};

use url::Url;

use super::TextStyle;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Font {
inner: Arc<FontData>,
data: FontData,
}

impl Font {
pub fn new(data: FontData) -> Self {
Self {
inner: Arc::new(data),
}
Self { data }
}

pub fn from_bytes(bytes: Vec<u8>) -> Self {
Self::new(FontData::Bytes(bytes))
Self::new(FontData::Bytes(Arc::new(bytes)))
}

pub fn from_family(family: impl Into<Cow<'static, str>>) -> Self {
Self::new(FontData::Family(family.into()))
}
}

pub fn from_url(url: Url) -> Self {
Self::new(FontData::Url(url))
impl AsRef<FontData> for Font {
fn as_ref(&self) -> &FontData {
&self.data
}
}

pub fn parse_url(url: &str) -> Result<Self, url::ParseError> {
Ok(Self::from_url(Url::parse(url)?))
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FontData {
Bytes(Arc<Vec<u8>>),
Family(Cow<'static, str>),
}

pub fn styled(&self) -> TextStyle {
TextStyle {
font: Some(self.clone()),
impl std::fmt::Debug for FontData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
struct DebugNonExhaustive;

..TextStyle::default()
impl std::fmt::Debug for DebugNonExhaustive {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("..")
}
}
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FontData {
Bytes(Vec<u8>),
Family(Cow<'static, str>),
Url(Url),
match self {
Self::Bytes(_) => f.debug_tuple("Bytes").field(&DebugNonExhaustive).finish(),
Self::Family(family) => f.debug_tuple("Family").field(family).finish(),
}
}
}
35 changes: 34 additions & 1 deletion crates/agui_core/src/unit/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use lyon::{

use crate::unit::Rect;

#[derive(Debug, Default, Clone)]
#[derive(Default, Clone)]
pub enum Shape {
#[default]
Rect,
Expand Down Expand Up @@ -115,3 +115,36 @@ impl Shape {
}
}
}

impl std::fmt::Debug for Shape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
struct DebugNonExhaustive;

impl std::fmt::Debug for DebugNonExhaustive {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("..")
}
}

match self {
Shape::Rect => f.write_str("Rect"),

Shape::RoundedRect {
top_left,
top_right,
bottom_right,
bottom_left,
} => f
.debug_struct("RoundedRect")
.field("top_left", &top_left)
.field("top_right", &top_right)
.field("bottom_right", &bottom_right)
.field("bottom_left", &bottom_left)
.finish(),

Shape::Circle => f.write_str("Circle"),

Shape::Path(_) => f.debug_tuple("Path").field(&DebugNonExhaustive).finish(),
}
}
}
33 changes: 5 additions & 28 deletions crates/agui_core/src/unit/text_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{Color, Font};

#[derive(Debug, Clone, PartialEq)]
pub struct TextStyle {
pub font: Option<Font>,
pub font: Font,

pub size: f32,
pub color: Color,
Expand All @@ -11,32 +11,9 @@ pub struct TextStyle {
pub v_align: VerticalAlign,
}

impl Default for TextStyle {
fn default() -> Self {
Self {
font: None,

size: 16.0,
color: Color {
red: 0.0,
green: 0.0,
blue: 0.0,
alpha: 1.0,
},

h_align: HorizontalAlign::Left,
v_align: VerticalAlign::Top,
}
}
}

impl TextStyle {
pub fn new() -> Self {
Self::default()
}

pub fn font(mut self, font: Font) -> Self {
self.font = Some(font);
self.font = font;
self
}

Expand All @@ -63,14 +40,14 @@ impl TextStyle {

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HorizontalAlign {
Left,
Start,
Center,
Right,
End,
}

impl Default for HorizontalAlign {
fn default() -> Self {
Self::Left
Self::Start
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/agui_executor/src/shared/update_render_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ where

if needs_layout {
self.needs_layout.insert(*ctx.render_object_id);
} else if needs_paint {
}

if needs_paint {
self.needs_paint.insert(*ctx.render_object_id);
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/agui_primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ agui_macros.workspace = true

agui_elements.workspace = true

tracing.workspace = true

rustc-hash.workspace = true
im-rc.workspace = true

Expand Down
1 change: 0 additions & 1 deletion crates/agui_primitives/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub enum TextBaseline {

#[derive(RenderObjectWidget, Debug)]
pub struct Text {
#[prop(default)]
pub style: TextStyle,

pub text: Cow<'static, str>,
Expand Down
21 changes: 21 additions & 0 deletions crates/agui_primitives/src/text/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ mod tests {
use agui_core::{
engine::elements::{strategies::mocks::MockInflateElements, ElementTree},
query::by_widget::ExactWidgetIterator,
unit::{Color, Font, HorizontalAlign, TextStyle, VerticalAlign},
};
use agui_macros::build;

Expand All @@ -85,9 +86,29 @@ mod tests {
<Column> {
children: [
<Text> {
style: TextStyle {
font: Font::from_family("Arial"),

size: 16.0,
color: Color::from_rgb((0.0, 0.0, 0.0)),

h_align: HorizontalAlign::default(),
v_align: VerticalAlign::default(),
},

text: "foo".into(),
}.into(),
<Text> {
style: TextStyle {
font: Font::from_family("Arial"),

size: 16.0,
color: Color::from_rgb((0.0, 0.0, 0.0)),

h_align: HorizontalAlign::default(),
v_align: VerticalAlign::default(),
},

text: "bar".into(),
}.into(),
],
Expand Down
18 changes: 11 additions & 7 deletions crates/agui_primitives/src/text/render_paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl RenderParagraph {

self.style = style;
ctx.mark_needs_layout();
ctx.mark_needs_paint();
}

pub fn update_text(&mut self, ctx: &mut RenderObjectUpdateContext, text: Cow<'static, str>) {
Expand All @@ -32,6 +33,7 @@ impl RenderParagraph {

self.text = text;
ctx.mark_needs_layout();
ctx.mark_needs_paint();
}
}

Expand All @@ -48,17 +50,19 @@ impl RenderObjectImpl for RenderParagraph {
}

fn layout(&self, ctx: &mut RenderObjectLayoutContext, constraints: Constraints) -> Size {
let size = if let Some(text_layout) = ctx.text_layout() {
assert_eq!(
ctx.child_count(),
0,
"RenderParagraph should have no children"
);

if let Some(text_layout) = ctx.text_layout() {
text_layout.compute_size(&self.style, &self.text, constraints)
} else {
constraints.smallest()
};
tracing::warn!("No text layout delegate available");

if let Some(mut child) = ctx.iter_children_mut().next() {
child.layout(Constraints::tight(size));
constraints.smallest()
}

constraints.smallest()
}

fn does_paint(&self) -> bool {
Expand Down
69 changes: 0 additions & 69 deletions crates/agui_vello/src/old/old_handle.rs

This file was deleted.

Loading

0 comments on commit 4dc90be

Please sign in to comment.