diff options
Diffstat (limited to 'tiny_skia/src')
| -rw-r--r-- | tiny_skia/src/backend.rs | 18 | ||||
| -rw-r--r-- | tiny_skia/src/geometry.rs | 131 | ||||
| -rw-r--r-- | tiny_skia/src/primitive.rs | 4 | 
3 files changed, 96 insertions, 57 deletions
| diff --git a/tiny_skia/src/backend.rs b/tiny_skia/src/backend.rs index 706db40e..d1393b4d 100644 --- a/tiny_skia/src/backend.rs +++ b/tiny_skia/src/backend.rs @@ -543,7 +543,6 @@ impl Backend {                  path,                  paint,                  rule, -                transform,              }) => {                  let bounds = path.bounds(); @@ -566,9 +565,11 @@ impl Backend {                      path,                      paint,                      *rule, -                    transform -                        .post_translate(translation.x, translation.y) -                        .post_scale(scale_factor, scale_factor), +                    tiny_skia::Transform::from_translate( +                        translation.x, +                        translation.y, +                    ) +                    .post_scale(scale_factor, scale_factor),                      clip_mask,                  );              } @@ -576,7 +577,6 @@ impl Backend {                  path,                  paint,                  stroke, -                transform,              }) => {                  let bounds = path.bounds(); @@ -599,9 +599,11 @@ impl Backend {                      path,                      paint,                      stroke, -                    transform -                        .post_translate(translation.x, translation.y) -                        .post_scale(scale_factor, scale_factor), +                    tiny_skia::Transform::from_translate( +                        translation.x, +                        translation.y, +                    ) +                    .post_scale(scale_factor, scale_factor),                      clip_mask,                  );              } diff --git a/tiny_skia/src/geometry.rs b/tiny_skia/src/geometry.rs index 5f28b737..74a08d38 100644 --- a/tiny_skia/src/geometry.rs +++ b/tiny_skia/src/geometry.rs @@ -1,4 +1,5 @@ -use crate::core::{Point, Rectangle, Size, Vector}; +use crate::core::text::LineHeight; +use crate::core::{Pixels, Point, Rectangle, Size, Vector};  use crate::graphics::geometry::fill::{self, Fill};  use crate::graphics::geometry::stroke::{self, Stroke};  use crate::graphics::geometry::{Path, Style, Text}; @@ -39,17 +40,22 @@ impl Frame {      }      pub fn fill(&mut self, path: &Path, fill: impl Into<Fill>) { -        let Some(path) = convert_path(path) else { +        let Some(path) = +            convert_path(path).and_then(|path| path.transform(self.transform)) +        else {              return;          }; +          let fill = fill.into(); +        let mut paint = into_paint(fill.style); +        paint.shader.transform(self.transform); +          self.primitives              .push(Primitive::Custom(primitive::Custom::Fill {                  path, -                paint: into_paint(fill.style), +                paint,                  rule: into_fill_rule(fill.rule), -                transform: self.transform,              }));      } @@ -59,76 +65,111 @@ impl Frame {          size: Size,          fill: impl Into<Fill>,      ) { -        let Some(path) = convert_path(&Path::rectangle(top_left, size)) else { +        let Some(path) = convert_path(&Path::rectangle(top_left, size)) +            .and_then(|path| path.transform(self.transform)) +        else {              return;          }; +          let fill = fill.into(); +        let mut paint = tiny_skia::Paint { +            anti_alias: false, +            ..into_paint(fill.style) +        }; +        paint.shader.transform(self.transform); +          self.primitives              .push(Primitive::Custom(primitive::Custom::Fill {                  path, -                paint: tiny_skia::Paint { -                    anti_alias: false, -                    ..into_paint(fill.style) -                }, +                paint,                  rule: into_fill_rule(fill.rule), -                transform: self.transform,              }));      }      pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>) { -        let Some(path) = convert_path(path) else { +        let Some(path) = +            convert_path(path).and_then(|path| path.transform(self.transform)) +        else {              return;          };          let stroke = stroke.into();          let skia_stroke = into_stroke(&stroke); +        let mut paint = into_paint(stroke.style); +        paint.shader.transform(self.transform); +          self.primitives              .push(Primitive::Custom(primitive::Custom::Stroke {                  path, -                paint: into_paint(stroke.style), +                paint,                  stroke: skia_stroke, -                transform: self.transform,              }));      }      pub fn fill_text(&mut self, text: impl Into<Text>) {          let text = text.into(); -        let position = if self.transform.is_identity() { -            text.position +        let (scale_x, scale_y) = self.transform.get_scale(); + +        if self.transform.is_scale_translate() +            && scale_x == scale_y +            && scale_x > 0.0 +            && scale_y > 0.0 +        { +            let (position, size, line_height) = if self.transform.is_identity() +            { +                (text.position, text.size, text.line_height) +            } else { +                let mut position = [tiny_skia::Point { +                    x: text.position.x, +                    y: text.position.y, +                }]; + +                self.transform.map_points(&mut position); + +                let size = text.size.0 * scale_y; + +                let line_height = match text.line_height { +                    LineHeight::Absolute(size) => { +                        LineHeight::Absolute(Pixels(size.0 * scale_y)) +                    } +                    LineHeight::Relative(factor) => { +                        LineHeight::Relative(factor) +                    } +                }; + +                ( +                    Point::new(position[0].x, position[0].y), +                    size.into(), +                    line_height, +                ) +            }; + +            let bounds = Rectangle { +                x: position.x, +                y: position.y, +                width: f32::INFINITY, +                height: f32::INFINITY, +            }; + +            // TODO: Honor layering! +            self.primitives.push(Primitive::Text { +                content: text.content, +                bounds, +                color: text.color, +                size, +                line_height, +                font: text.font, +                horizontal_alignment: text.horizontal_alignment, +                vertical_alignment: text.vertical_alignment, +                shaping: text.shaping, +                clip_bounds: Rectangle::with_size(Size::INFINITY), +            });          } else { -            let mut transformed = [tiny_skia::Point { -                x: text.position.x, -                y: text.position.y, -            }]; - -            self.transform.map_points(&mut transformed); - -            Point::new(transformed[0].x, transformed[0].y) -        }; - -        let bounds = Rectangle { -            x: position.x, -            y: position.y, -            width: f32::INFINITY, -            height: f32::INFINITY, -        }; - -        // TODO: Use vectorial text instead of primitive -        self.primitives.push(Primitive::Text { -            content: text.content, -            bounds, -            color: text.color, -            size: text.size, -            line_height: text.line_height, -            font: text.font, -            horizontal_alignment: text.horizontal_alignment, -            vertical_alignment: text.vertical_alignment, -            shaping: text.shaping, -            clip_bounds: Rectangle::with_size(Size::INFINITY), -        }); +            text.draw_with(|path, color| self.fill(&path, color)); +        }      }      pub fn push_transform(&mut self) { diff --git a/tiny_skia/src/primitive.rs b/tiny_skia/src/primitive.rs index 0ed24969..7718d542 100644 --- a/tiny_skia/src/primitive.rs +++ b/tiny_skia/src/primitive.rs @@ -13,8 +13,6 @@ pub enum Custom {          paint: tiny_skia::Paint<'static>,          /// The fill rule to follow.          rule: tiny_skia::FillRule, -        /// The transform to apply to the path. -        transform: tiny_skia::Transform,      },      /// A path stroked with some paint.      Stroke { @@ -24,8 +22,6 @@ pub enum Custom {          paint: tiny_skia::Paint<'static>,          /// The stroke settings.          stroke: tiny_skia::Stroke, -        /// The transform to apply to the path. -        transform: tiny_skia::Transform,      },  } | 
