font_rs/
geom.rs

1// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Geometry primitive data structures and manipulations
16
17use std::fmt::{Debug, Formatter, Result};
18
19#[derive(Copy, Clone)]
20pub struct Point {
21    pub x: f32,
22    pub y: f32,
23}
24
25impl Point {
26    pub fn new<T>(x: T, y: T) -> Point
27    where
28        T: Into<f32>,
29    {
30        Point {
31            x: x.into(),
32            y: y.into(),
33        }
34    }
35
36    pub fn lerp(t: f32, p0: &Self, p1: &Self) -> Self {
37        Point {
38            x: p0.x + t * (p1.x - p0.x),
39            y: p0.y + t * (p1.y - p0.y),
40        }
41    }
42}
43
44impl Debug for Point {
45    fn fmt(&self, f: &mut Formatter) -> Result {
46        write!(f, "({}, {})", self.x, self.y)
47    }
48}
49
50#[derive(Debug)]
51pub struct Affine {
52    a: f32,
53    b: f32,
54    c: f32,
55    d: f32,
56    e: f32,
57    f: f32,
58}
59
60impl Affine {
61    /// Concatenate two affine transforms.
62    pub fn concat(t1: &Affine, t2: &Affine) -> Affine {
63        Affine {
64            a: t1.a * t2.a + t1.c * t2.b,
65            b: t1.b * t2.a + t1.d * t2.b,
66            c: t1.a * t2.c + t1.c * t2.d,
67            d: t1.b * t2.c + t1.d * t2.d,
68            e: t1.a * t2.e + t1.c * t2.f + t1.e,
69            f: t1.b * t2.e + t1.d * t2.f + t1.f,
70        }
71    }
72}
73
74pub fn affine_pt(z: &Affine, p: &Point) -> Point {
75    Point {
76        x: z.a * p.x + z.c * p.y + z.e,
77        y: z.b * p.x + z.d * p.y + z.f,
78    }
79}
80
81gen_new!(Affine, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32);