Skip to content

Commit

Permalink
Merge pull request #89 from linebender/text
Browse files Browse the repository at this point in the history
Start text rendering
  • Loading branch information
raphlinus authored Jun 23, 2021
2 parents 090c99e + 951f3aa commit 379fb1c
Show file tree
Hide file tree
Showing 8 changed files with 562 additions and 104 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions piet-gpu-derive/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn gen_derive_def(name: &str, size: usize, def: &LayoutTypeDef) -> proc_macro2::
encode_fields.extend(gen_encode_field(field_name, *offset, &ty.ty));
}
quote! {
#[derive(Clone)]
pub struct #name_id {
#gen_fields
}
Expand Down Expand Up @@ -104,6 +105,7 @@ fn gen_derive_def(name: &str, size: usize, def: &LayoutTypeDef) -> proc_macro2::
cases.extend(case);
}
quote! {
#[derive(Clone)]
pub enum #name_id {
#gen_variants
}
Expand Down
1 change: 1 addition & 0 deletions piet-gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rand = "0.7.3"
roxmltree = "0.13"
winit = "0.23"
clap = "2.33"
ttf-parser = "0.12"

[target.'cfg(target_os = "android")'.dependencies]
ndk = "0.3"
Expand Down
21 changes: 19 additions & 2 deletions piet-gpu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
mod pico_svg;
mod render_ctx;
mod text;

use std::convert::TryInto;

pub use render_ctx::PietGpuRenderContext;

use rand::{Rng, RngCore};

use piet::kurbo::{BezPath, Circle, Point, Shape, Vec2};
use piet::{Color, ImageFormat, RenderContext};
use piet::kurbo::{Affine, BezPath, Circle, Point, Shape, Vec2};
use piet::{Color, ImageFormat, RenderContext, Text, TextAttribute, TextLayoutBuilder};

use piet_gpu_types::encoder::Encode;

Expand Down Expand Up @@ -77,6 +78,7 @@ pub fn render_scene(rc: &mut impl RenderContext) {
//render_cardioid(rc);
render_clip_test(rc);
render_alpha_test(rc);
render_text_test(rc);
//render_tiger(rc);
}

Expand Down Expand Up @@ -158,6 +160,21 @@ fn diamond(origin: Point) -> impl Shape {
return path;
}

#[allow(unused)]
fn render_text_test(rc: &mut impl RenderContext) {
rc.save();
//rc.transform(Affine::new([0.2, 0.0, 0.0, -0.2, 200.0, 800.0]));
let layout = rc
.text()
.new_text_layout("hello piet-gpu text!")
.default_attribute(TextAttribute::FontSize(100.0))
.build()
.unwrap();
rc.draw_text(&layout, Point::new(110.0, 600.0));
rc.draw_text(&layout, Point::new(110.0, 700.0));
rc.restore();
}

#[allow(unused)]
fn render_tiger(rc: &mut impl RenderContext) {
let xml_str = std::str::from_utf8(include_bytes!("../Ghostscript_Tiger.svg")).unwrap();
Expand Down
134 changes: 33 additions & 101 deletions piet-gpu/src/render_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ use piet_gpu_types::scene::{
Clip, CubicSeg, Element, FillColor, LineSeg, QuadSeg, SetFillMode, SetLineWidth, Transform,
};

pub struct PietGpuImage;

#[derive(Clone)]
pub struct PietGpuTextLayout;
use crate::text::Font;
pub use crate::text::{PathEncoder, PietGpuText, PietGpuTextLayout, PietGpuTextLayoutBuilder};

pub struct PietGpuTextLayoutBuilder;

#[derive(Clone)]
pub struct PietGpuText;
pub struct PietGpuImage;

pub struct PietGpuRenderContext {
encoder: Encoder,
Expand Down Expand Up @@ -69,7 +64,7 @@ struct ClipElement {
}

#[derive(Clone, Copy, PartialEq)]
enum FillMode {
pub(crate) enum FillMode {
// Fill path according to the non-zero winding rule.
Nonzero = 0,
// Fill stroked path.
Expand All @@ -82,7 +77,8 @@ impl PietGpuRenderContext {
pub fn new() -> PietGpuRenderContext {
let encoder = Encoder::new();
let elements = Vec::new();
let inner_text = PietGpuText;
let font = Font::new();
let inner_text = PietGpuText::new(font);
let stroke_width = 0.0;
PietGpuRenderContext {
encoder,
Expand Down Expand Up @@ -115,14 +111,14 @@ impl PietGpuRenderContext {
pub fn trans_count(&self) -> usize {
self.trans_count
}
}

fn set_fill_mode(ctx: &mut PietGpuRenderContext, fill_mode: FillMode) {
if ctx.fill_mode != fill_mode {
ctx.elements.push(Element::SetFillMode(SetFillMode {
fill_mode: fill_mode as u32,
}));
ctx.fill_mode = fill_mode;
pub(crate) fn set_fill_mode(&mut self, fill_mode: FillMode) {
if self.fill_mode != fill_mode {
self.elements.push(Element::SetFillMode(SetFillMode {
fill_mode: fill_mode as u32,
}));
self.fill_mode = fill_mode;
}
}
}

Expand Down Expand Up @@ -165,7 +161,7 @@ impl RenderContext for PietGpuRenderContext {
.push(Element::SetLineWidth(SetLineWidth { width: width_f32 }));
self.stroke_width = width_f32;
}
set_fill_mode(self, FillMode::Stroke);
self.set_fill_mode(FillMode::Stroke);
let brush = brush.make_brush(self, || shape.bounding_box()).into_owned();
match brush {
PietGpuBrush::Solid(rgba_color) => {
Expand Down Expand Up @@ -197,7 +193,7 @@ impl RenderContext for PietGpuRenderContext {
// Perhaps that should be added to kurbo.
self.accumulate_bbox(|| shape.bounding_box());
let path = shape.path_elements(TOLERANCE);
set_fill_mode(self, FillMode::Nonzero);
self.set_fill_mode(FillMode::Nonzero);
self.encode_path(path, true);
let fill = FillColor { rgba_color };
self.elements.push(Element::FillColor(fill));
Expand All @@ -208,7 +204,7 @@ impl RenderContext for PietGpuRenderContext {
fn fill_even_odd(&mut self, _shape: impl Shape, _brush: &impl IntoBrush<Self>) {}

fn clip(&mut self, shape: impl Shape) {
set_fill_mode(self, FillMode::Nonzero);
self.set_fill_mode(FillMode::Nonzero);
let path = shape.path_elements(TOLERANCE);
self.encode_path(path, true);
let begin_ix = self.elements.len();
Expand All @@ -229,7 +225,9 @@ impl RenderContext for PietGpuRenderContext {
&mut self.inner_text
}

fn draw_text(&mut self, _layout: &Self::TextLayout, _pos: impl Into<Point>) {}
fn draw_text(&mut self, layout: &Self::TextLayout, pos: impl Into<Point>) {
layout.draw_text(self, pos.into());
}

fn save(&mut self) -> Result<(), Error> {
self.state_stack.push(State {
Expand All @@ -244,9 +242,7 @@ impl RenderContext for PietGpuRenderContext {
if let Some(state) = self.state_stack.pop() {
if state.rel_transform != Affine::default() {
let a_inv = state.rel_transform.inverse();
self.elements
.push(Element::Transform(to_scene_transform(a_inv)));
self.trans_count += 1;
self.encode_transform(to_scene_transform(a_inv));
}
self.cur_transform = state.transform;
for _ in 0..state.n_clip {
Expand All @@ -266,9 +262,7 @@ impl RenderContext for PietGpuRenderContext {
}

fn transform(&mut self, transform: Affine) {
self.elements
.push(Element::Transform(to_scene_transform(transform)));
self.trans_count += 1;
self.encode_transform(to_scene_transform(transform));
if let Some(tos) = self.state_stack.last_mut() {
tos.rel_transform *= transform;
}
Expand Down Expand Up @@ -486,84 +480,22 @@ impl PietGpuRenderContext {
};
}
}
}

impl Text for PietGpuText {
type TextLayout = PietGpuTextLayout;
type TextLayoutBuilder = PietGpuTextLayoutBuilder;

fn load_font(&mut self, _data: &[u8]) -> Result<FontFamily, Error> {
Ok(FontFamily::default())
}

fn new_text_layout(&mut self, _text: impl TextStorage) -> Self::TextLayoutBuilder {
PietGpuTextLayoutBuilder
}

fn font_family(&mut self, _family_name: &str) -> Option<FontFamily> {
Some(FontFamily::default())
}
}

impl TextLayoutBuilder for PietGpuTextLayoutBuilder {
type Out = PietGpuTextLayout;

fn max_width(self, _width: f64) -> Self {
self
}

fn alignment(self, _alignment: piet::TextAlignment) -> Self {
self
}

fn default_attribute(self, _attribute: impl Into<TextAttribute>) -> Self {
self
}

fn range_attribute(
self,
_range: impl RangeBounds<usize>,
_attribute: impl Into<TextAttribute>,
) -> Self {
self
pub(crate) fn append_path_encoder(&mut self, path: &PathEncoder) {
let elements = path.elements();
self.elements.extend(elements.iter().cloned());
self.pathseg_count += elements.len();
}

fn build(self) -> Result<Self::Out, Error> {
Ok(PietGpuTextLayout)
}
}

impl TextLayout for PietGpuTextLayout {
fn size(&self) -> Size {
Size::ZERO
}

fn image_bounds(&self) -> Rect {
Rect::ZERO
}

fn line_text(&self, _line_number: usize) -> Option<&str> {
None
}

fn line_metric(&self, _line_number: usize) -> Option<LineMetric> {
None
}

fn line_count(&self) -> usize {
0
}

fn hit_test_point(&self, _point: Point) -> HitTestPoint {
HitTestPoint::default()
}

fn hit_test_text_position(&self, _text_position: usize) -> HitTestPosition {
HitTestPosition::default()
pub(crate) fn fill_glyph(&mut self, rgba_color: u32) {
let fill = FillColor { rgba_color };
self.elements.push(Element::FillColor(fill));
self.path_count += 1;
}

fn text(&self) -> &str {
""
pub(crate) fn encode_transform(&mut self, transform: Transform) {
self.elements.push(Element::Transform(transform));
self.trans_count += 1;
}
}

Expand All @@ -577,7 +509,7 @@ impl IntoBrush<PietGpuRenderContext> for PietGpuBrush {
}
}

fn to_f32_2(point: Point) -> [f32; 2] {
pub(crate) fn to_f32_2(point: Point) -> [f32; 2] {
[point.x as f32, point.y as f32]
}

Expand Down
Loading

0 comments on commit 379fb1c

Please sign in to comment.