1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use crate::{Point, Vector};
use ab_glyph_rasterizer::{point as ab_point, Point as AbPoint, Rasterizer};
use owned_ttf_parser::OutlineBuilder;

pub(crate) struct OutlineScaler<'b, T: ?Sized> {
    inner: &'b mut T,
    scale: Vector<f32>,
}

impl<'b, T: ?Sized> OutlineScaler<'b, T> {
    pub(crate) fn new(inner: &'b mut T, scale: Vector<f32>) -> Self {
        Self { inner, scale }
    }
}

impl<T: OutlineBuilder + ?Sized> OutlineBuilder for OutlineScaler<'_, T> {
    fn move_to(&mut self, x: f32, y: f32) {
        self.inner.move_to(x * self.scale.x, y * self.scale.y)
    }

    fn line_to(&mut self, x1: f32, y1: f32) {
        self.inner.line_to(x1 * self.scale.x, y1 * self.scale.y)
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32) {
        self.inner.quad_to(
            x1 * self.scale.x,
            y1 * self.scale.y,
            x2 * self.scale.x,
            y2 * self.scale.y,
        )
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32) {
        self.inner.curve_to(
            x1 * self.scale.x,
            y1 * self.scale.y,
            x2 * self.scale.x,
            y2 * self.scale.y,
            x3 * self.scale.x,
            y3 * self.scale.y,
        )
    }

    fn close(&mut self) {
        self.inner.close()
    }
}

pub(crate) struct OutlineTranslator<'b, T: ?Sized> {
    inner: &'b mut T,
    translation: Point<f32>,
}

impl<'b, T: ?Sized> OutlineTranslator<'b, T> {
    pub(crate) fn new(inner: &'b mut T, translation: Point<f32>) -> Self {
        Self { inner, translation }
    }
}

impl<T: OutlineBuilder + ?Sized> OutlineBuilder for OutlineTranslator<'_, T> {
    fn move_to(&mut self, x: f32, y: f32) {
        self.inner
            .move_to(x + self.translation.x, y + self.translation.y)
    }

    fn line_to(&mut self, x1: f32, y1: f32) {
        self.inner
            .line_to(x1 + self.translation.x, y1 + self.translation.y)
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32) {
        self.inner.quad_to(
            x1 + self.translation.x,
            y1 + self.translation.y,
            x2 + self.translation.x,
            y2 + self.translation.y,
        )
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32) {
        self.inner.curve_to(
            x1 + self.translation.x,
            y1 + self.translation.y,
            x2 + self.translation.x,
            y2 + self.translation.y,
            x3 + self.translation.x,
            y3 + self.translation.y,
        )
    }

    fn close(&mut self) {
        self.inner.close()
    }
}

pub(crate) struct OutlineRasterizer {
    pub(crate) rasterizer: Rasterizer,
    last: AbPoint,
    last_move: Option<AbPoint>,
}

impl OutlineRasterizer {
    pub(crate) fn new(width: usize, height: usize) -> Self {
        Self {
            rasterizer: Rasterizer::new(width, height),
            last: ab_point(0.0, 0.0),
            last_move: None,
        }
    }
}

impl OutlineBuilder for OutlineRasterizer {
    fn move_to(&mut self, x: f32, y: f32) {
        self.last = AbPoint { x, y };
        self.last_move = Some(self.last);
    }

    fn line_to(&mut self, x1: f32, y1: f32) {
        let p1 = AbPoint { x: x1, y: y1 };

        self.rasterizer.draw_line(self.last, p1);
        self.last = p1;
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32) {
        let p1 = AbPoint { x: x1, y: y1 };
        let p2 = AbPoint { x: x2, y: y2 };

        self.rasterizer.draw_quad(self.last, p1, p2);
        self.last = p2;
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32) {
        let p1 = AbPoint { x: x1, y: y1 };
        let p2 = AbPoint { x: x2, y: y2 };
        let p3 = AbPoint { x: x3, y: y3 };

        self.rasterizer.draw_cubic(self.last, p1, p2, p3);
        self.last = p3;
    }

    fn close(&mut self) {
        if let Some(m) = self.last_move {
            self.rasterizer.draw_line(self.last, m);
        }
    }
}