Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for piet-scene changes #5

Merged
merged 2 commits into from
Nov 23, 2022
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
36 changes: 22 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ wayland = ["glazier/wayland"]
glazier = { git = "https://github.com/linebender/glazier", default-features = false }
piet-gpu-hal = { git = "https://github.com/linebender/piet-gpu" }
piet-gpu = { git = "https://github.com/linebender/piet-gpu" }
piet-scene = { git = "https://github.com/linebender/piet-gpu", features = ["kurbo"] }
piet-scene = { git = "https://github.com/linebender/piet-gpu" }
raw-window-handle = "0.5"
png = "0.16.2"
rand = "0.7.3"
Expand Down
36 changes: 9 additions & 27 deletions src/test_scenes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::text::*;
use parley::FontContext;
use piet_scene::kurbo::{Affine, Rect};
use piet_scene::*;

pub fn render(fcx: &mut FontContext, scene: &mut Scene, which: usize, arg: u64) {
Expand All @@ -9,38 +10,19 @@ pub fn render(fcx: &mut FontContext, scene: &mut Scene, which: usize, arg: u64)
}

fn basic_scene(fcx: &mut FontContext, scene: &mut Scene, arg: u64) {
let transform = Affine::translate(400.0, 400.0) * Affine::rotate((arg as f64 * 0.01) as f32);
let transform = Affine::translate((400.0, 400.0)) * Affine::rotate(arg as f64 * 0.01);
let mut builder = SceneBuilder::for_scene(scene);
let stops = &[
GradientStop {
offset: 0.0,
color: Color::rgb8(128, 0, 0),
},
GradientStop {
offset: 0.5,
color: Color::rgb8(0, 128, 0),
},
GradientStop {
offset: 1.0,
color: Color::rgb8(0, 0, 128),
},
][..];
let gradient = Brush::LinearGradient(LinearGradient {
start: Point::new(0.0, 0.0),
end: Point::new(0.0, 400.0),
extend: ExtendMode::Pad,
stops: stops.iter().copied().collect(),
});
let gradient = LinearGradient::new((0.0, 0.0), (0.0, 400.0)).stops([
Color::rgb8(128, 0, 0),
Color::rgb8(0, 128, 0),
Color::rgb8(0, 0, 128),
]);
builder.fill(
Fill::NonZero,
transform,
&gradient,
None,
Rect {
min: Point::new(0.0, 0.0),
max: Point::new(600.0, 400.0),
}
.elements(),
&Rect::new(0.0, 0.0, 600.0, 400.0),
);
let scale = (arg as f64 * 0.01).sin() * 0.5 + 1.5;
let mut lcx = parley::LayoutContext::new();
Expand All @@ -57,6 +39,6 @@ fn basic_scene(fcx: &mut FontContext, scene: &mut Scene, arg: u64) {
)));
let mut layout = layout_builder.build();
layout.break_all_lines(None, parley::layout::Alignment::Start);
render_text(&mut builder, Affine::translate(100.0, 400.0), &layout);
render_text(&mut builder, Affine::translate((100.0, 400.0)), &layout);
builder.finish();
}
12 changes: 4 additions & 8 deletions src/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use parley::Layout;
use piet_scene::kurbo::Affine;
use piet_scene::{
glyph::{
pinot::{types::Tag, FontRef},
Expand All @@ -7,7 +8,7 @@ use piet_scene::{
*,
};

#[derive(Clone, Debug)]
#[derive(Clone, PartialEq, Debug)]
pub struct ParleyBrush(pub Brush);

impl Default for ParleyBrush {
Expand All @@ -16,12 +17,6 @@ impl Default for ParleyBrush {
}
}

impl PartialEq<ParleyBrush> for ParleyBrush {
fn eq(&self, _other: &ParleyBrush) -> bool {
true // FIXME
}
}

impl parley::style::Brush for ParleyBrush {}

pub fn render_text(builder: &mut SceneBuilder, transform: Affine, layout: &Layout<ParleyBrush>) {
Expand All @@ -44,7 +39,8 @@ pub fn render_text(builder: &mut SceneBuilder, transform: Affine, layout: &Layou
if let Some(fragment) = gp.get(glyph.id, Some(&style.brush.0)) {
let gx = x + glyph.x;
let gy = y - glyph.y;
let xform = Affine::translate(gx, gy) * Affine::scale(1.0, -1.0);
let xform = Affine::translate((gx as f64, gy as f64))
* Affine::scale_non_uniform(1.0, -1.0);
builder.append(&fragment, Some(transform * xform));
}
x += glyph.advance;
Expand Down
32 changes: 6 additions & 26 deletions src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use glazier::kurbo::{Insets, Size};
use glazier::kurbo::{Affine, Insets, Size};
use parley::Layout;
use piet_scene::{Affine, Brush, Color, GradientStop, GradientStops, SceneBuilder, SceneFragment};
use piet_scene::{Brush, Color, SceneBuilder, SceneFragment, Stroke};

use crate::{event::Event, id::IdPath, text::ParleyBrush, VertAlignment};

Expand Down Expand Up @@ -137,29 +137,9 @@ impl Widget for Button {
Color::rgb8(0x3a, 0x3a, 0x3a)
};
let bg_stops = if is_active {
[
GradientStop {
offset: 0.0,
color: Color::rgb8(0x3a, 0x3a, 0x3a),
},
GradientStop {
offset: 1.0,
color: Color::rgb8(0xa1, 0xa1, 0xa1),
},
][..]
.into()
[Color::rgb8(0x3a, 0x3a, 0x3a), Color::rgb8(0xa1, 0xa1, 0xa1)]
} else {
[
GradientStop {
offset: 0.0,
color: Color::rgb8(0xa1, 0xa1, 0xa1),
},
GradientStop {
offset: 1.0,
color: Color::rgb8(0x3a, 0x3a, 0x3a),
},
][..]
.into()
[Color::rgb8(0xa1, 0xa1, 0xa1), Color::rgb8(0x3a, 0x3a, 0x3a)]
};
/*
let bg_gradient = if is_active {
Expand All @@ -181,7 +161,7 @@ impl Widget for Button {
piet_scene_helpers::stroke(
&mut builder,
&rounded_rect,
&Brush::Solid(border_color),
border_color,
button_border_width,
);
piet_scene_helpers::fill_lin_gradient(
Expand All @@ -195,7 +175,7 @@ impl Widget for Button {
if let Some(layout) = &self.layout {
let size = Size::new(layout.width() as f64, layout.height() as f64);
let offset = (cx.size().to_vec2() - size.to_vec2()) * 0.5;
let transform = Affine::translate(offset.x as f32, offset.y as f32);
let transform = Affine::translate(offset);
crate::text::render_text(&mut builder, transform, &layout);
}
Rendered(fragment)
Expand Down
59 changes: 18 additions & 41 deletions src/widget/piet_scene_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
use glazier::kurbo::{self, Rect, Shape};
use piet_scene::{
Affine, Brush, Cap, ExtendMode, Fill, GradientStops, Join, LinearGradient, PathElement, Point,
SceneBuilder, Stroke,
};
use glazier::kurbo::{self, Affine, Rect, Shape};
use piet_scene::{BrushRef, ColorStopsSource, Fill, LinearGradient, SceneBuilder, Stroke};

#[derive(Debug, Clone, Copy)]
pub struct UnitPoint {
u: f64,
v: f64,
}

pub fn stroke(builder: &mut SceneBuilder, path: &impl Shape, brush: &Brush, stroke_width: f64) {
let style = Stroke {
width: stroke_width as f32,
join: Join::Round,
miter_limit: 1.0,
start_cap: Cap::Round,
end_cap: Cap::Round,
dash_pattern: [],
dash_offset: 0.0,
scale: false,
};
// TODO: figure out how to avoid allocation
// (Just removing the collect should work in theory, but running into a clone bound)
let elements = path
.path_elements(1e-3)
.map(PathElement::from_kurbo)
.collect::<Vec<_>>();
builder.stroke(&style, Affine::IDENTITY, brush, None, &elements)
pub fn stroke<'b>(
builder: &mut SceneBuilder,
path: &impl Shape,
brush: impl Into<BrushRef<'b>>,
stroke_width: f64,
) {
builder.stroke(
&Stroke::new(stroke_width as f32),
Affine::IDENTITY,
brush,
None,
path,
)
}

// Note: copied from piet
Expand Down Expand Up @@ -71,26 +63,11 @@ impl UnitPoint {
pub fn fill_lin_gradient(
builder: &mut SceneBuilder,
path: &impl Shape,
stops: GradientStops,
stops: impl ColorStopsSource,
start: UnitPoint,
end: UnitPoint,
) {
let rect = path.bounding_box();
let lin_grad = LinearGradient {
start: Point::from_kurbo(start.resolve(rect)),
end: Point::from_kurbo(end.resolve(rect)),
stops,
extend: ExtendMode::Pad,
};
let elements = path
.path_elements(1e-3)
.map(PathElement::from_kurbo)
.collect::<Vec<_>>();
builder.fill(
Fill::NonZero,
Affine::IDENTITY,
&Brush::LinearGradient(lin_grad),
None,
elements,
);
let brush = LinearGradient::new(start.resolve(rect), end.resolve(rect)).stops(stops);
builder.fill(Fill::NonZero, Affine::IDENTITY, &brush, None, path);
}
6 changes: 3 additions & 3 deletions src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use glazier::kurbo::{Point, Size};
use glazier::kurbo::{Affine, Point, Size};
use parley::Layout;
use piet_scene::{Affine, Brush, Color, SceneBuilder, SceneFragment};
use piet_scene::{Brush, Color, SceneBuilder, SceneFragment};

use crate::text::ParleyBrush;

Expand Down Expand Up @@ -81,7 +81,7 @@ impl Widget for TextWidget {
let mut fragment = SceneFragment::default();
let mut builder = SceneBuilder::for_fragment(&mut fragment);
if let Some(layout) = &self.layout {
let transform = Affine::translate(40.0, 40.0);
let transform = Affine::translate((40.0, 40.0));
crate::text::render_text(&mut builder, transform, &layout);
}
Rendered(fragment)
Expand Down