use crate::vector_font::PathCommand;
#[derive(Clone, Copy, Debug)]
pub struct Affine {
pub off_x: f64,
pub off_y: f64,
pub s: f64,
pub min_x: f64,
pub min_y: f64,
}
impl Affine {
fn mx(&self, x: f64) -> f64 {
self.off_x + (x - self.min_x) * self.s
}
fn my(&self, y: f64) -> f64 {
self.off_y + (y - self.min_y) * self.s
}
}
const FLATNESS: f64 = 1.0 / 64.0;
const MAX_DEPTH: u32 = 24;
pub fn rasterize(commands: &[PathCommand], affine: &Affine, size: u32) -> Vec<u8> {
let contours = flatten(commands, affine);
fill_analytic(&contours, size)
}
type Contour = Vec<(f64, f64)>;
fn flatten(commands: &[PathCommand], a: &Affine) -> Vec<Contour> {
let mut out: Vec<Contour> = Vec::new();
let mut cur: Contour = Vec::new();
let mut pos = (0.0, 0.0);
let flush = |cur: &mut Contour, out: &mut Vec<Contour>| {
if cur.len() > 1 {
out.push(std::mem::take(cur));
} else {
cur.clear();
}
};
for cmd in commands {
match *cmd {
PathCommand::MoveTo { x, y } => {
flush(&mut cur, &mut out);
pos = (a.mx(x), a.my(y));
cur.push(pos);
}
PathCommand::LineTo { x, y } => {
pos = (a.mx(x), a.my(y));
cur.push(pos);
}
PathCommand::QuadTo { x1, y1, x, y } => {
let c = (a.mx(x1), a.my(y1));
let p = (a.mx(x), a.my(y));
flatten_quad(pos, c, p, 0, &mut cur);
pos = p;
}
PathCommand::CurveTo {
x1,
y1,
x2,
y2,
x,
y,
} => {
let c1 = (a.mx(x1), a.my(y1));
let c2 = (a.mx(x2), a.my(y2));
let p = (a.mx(x), a.my(y));
flatten_cubic(pos, c1, c2, p, 0, &mut cur);
pos = p;
}
PathCommand::Close => {
flush(&mut cur, &mut out);
}
}
}
flush(&mut cur, &mut out);
out
}
fn mid(a: (f64, f64), b: (f64, f64)) -> (f64, f64) {
((a.0 + b.0) * 0.5, (a.1 + b.1) * 0.5)
}
fn flatten_quad(p0: (f64, f64), p1: (f64, f64), p2: (f64, f64), depth: u32, out: &mut Contour) {
let m = mid(p0, p2);
let dx = p1.0 - m.0;
let dy = p1.1 - m.1;
if depth >= MAX_DEPTH || (dx * dx + dy * dy) * 0.25 <= FLATNESS * FLATNESS {
out.push(p2);
return;
}
let a = mid(p0, p1);
let b = mid(p1, p2);
let c = mid(a, b);
flatten_quad(p0, a, c, depth + 1, out);
flatten_quad(c, b, p2, depth + 1, out);
}
fn flatten_cubic(
p0: (f64, f64),
p1: (f64, f64),
p2: (f64, f64),
p3: (f64, f64),
depth: u32,
out: &mut Contour,
) {
let d1 = (
p1.0 - (2.0 * p0.0 + p3.0) / 3.0,
p1.1 - (2.0 * p0.1 + p3.1) / 3.0,
);
let d2 = (
p2.0 - (p0.0 + 2.0 * p3.0) / 3.0,
p2.1 - (p0.1 + 2.0 * p3.1) / 3.0,
);
let err = (d1.0 * d1.0 + d1.1 * d1.1).max(d2.0 * d2.0 + d2.1 * d2.1);
if depth >= MAX_DEPTH || err <= FLATNESS * FLATNESS {
out.push(p3);
return;
}
let a = mid(p0, p1);
let b = mid(p1, p2);
let c = mid(p2, p3);
let d = mid(a, b);
let e = mid(b, c);
let f = mid(d, e);
flatten_cubic(p0, a, d, f, depth + 1, out);
flatten_cubic(f, e, c, p3, depth + 1, out);
}
pub(crate) fn fill_analytic(contours: &[Contour], size: u32) -> Vec<u8> {
let n = size as usize;
let mut out = vec![0u8; n * n];
if contours.is_empty() {
return out;
}
let mut cover = vec![0.0f64; n * n];
let mut area = vec![0.0f64; n * n];
for contour in contours {
for i in 0..contour.len() {
let a = contour[i];
let b = contour[(i + 1) % contour.len()];
add_edge(a, b, n, &mut cover, &mut area);
}
}
for y in 0..n {
let row = y * n;
let mut running = 0.0f64;
for x in (0..n).rev() {
let value = area[row + x] + running;
running += cover[row + x];
let c = value.abs().clamp(0.0, 1.0);
out[row + x] = (c * 255.0).round() as u8;
}
}
out
}
fn add_edge(a: (f64, f64), b: (f64, f64), n: usize, cover: &mut [f64], area: &mut [f64]) {
if a.1 == b.1 {
return; }
let ny = n as f64;
let (mut x0, mut y0, mut x1, mut y1) = (a.0, a.1, b.0, b.1);
let lo = y0.min(y1);
let hi = y0.max(y1);
if hi <= 0.0 || lo >= ny {
return;
}
let x_at = |y: f64, x0: f64, y0: f64, x1: f64, y1: f64| x0 + (x1 - x0) * (y - y0) / (y1 - y0);
if lo < 0.0 {
let x = x_at(0.0, x0, y0, x1, y1);
if y0 < y1 {
x0 = x;
y0 = 0.0;
} else {
x1 = x;
y1 = 0.0;
}
}
if hi > ny {
let x = x_at(ny, x0, y0, x1, y1);
if y0 < y1 {
x1 = x;
y1 = ny;
} else {
x0 = x;
y0 = ny;
}
}
if y0 == y1 {
return;
}
let down = y1 > y0;
let mut cy = (y0.min(y1)).floor() as isize;
let last = {
let end = y0.max(y1);
let e = end.ceil() as isize - 1;
e.max(cy)
};
while cy <= last {
if cy < 0 || cy as usize >= n {
cy += 1;
continue;
}
let band_top = cy as f64;
let band_bot = band_top + 1.0;
let (ys, ye) = if down {
(y0.max(band_top), y1.min(band_bot))
} else {
(y0.min(band_bot), y1.max(band_top))
};
if (ye - ys).abs() > 0.0 {
let xs = x0 + (x1 - x0) * (ys - y0) / (y1 - y0);
let xe = x0 + (x1 - x0) * (ye - y0) / (y1 - y0);
add_band(xs, ys, xe, ye, cy as usize, n, cover, area);
}
cy += 1;
}
}
#[allow(clippy::too_many_arguments)]
fn add_band(
xs: f64,
ys: f64,
xe: f64,
ye: f64,
cy: usize,
n: usize,
cover: &mut [f64],
area: &mut [f64],
) {
let nx = n as f64;
let row = cy * n;
let mut put = |col: usize, dy: f64, mean_fx: f64| {
cover[row + col] += dy;
area[row + col] += dy * mean_fx;
};
let dx = xe - xs;
let y_at = |x: f64| {
if dx == 0.0 {
ys
} else {
ys + (ye - ys) * (x - xs) / dx
}
};
let clip = |lo: f64, hi: f64| -> Option<(f64, f64, f64, f64)> {
let (a, b) = if dx >= 0.0 {
(xs.max(lo), xe.min(hi))
} else {
(xs.min(hi), xe.max(lo))
};
if (dx >= 0.0 && a >= b) || (dx < 0.0 && a <= b) {
return None;
}
Some((a, y_at(a), b, y_at(b)))
};
if xs < 0.0 || xe < 0.0 {
if let Some((_, ya, _, yb)) = clip(f64::NEG_INFINITY, 0.0) {
put(0, yb - ya, 0.0);
}
}
if xs > nx || xe > nx {
if let Some((_, ya, _, yb)) = clip(nx, f64::INFINITY) {
put(n - 1, yb - ya, 1.0);
}
}
let Some((cxs, cys, cxe, cye)) = clip(0.0, nx) else {
if dx == 0.0 {
let x = xs.clamp(0.0, nx);
let col = (x.floor() as usize).min(n - 1);
put(col, ye - ys, x - col as f64);
}
return;
};
if cxs == cxe {
let col = (cxs.floor() as usize).min(n - 1);
put(col, cye - cys, cxs - col as f64);
return;
}
let right = cxe > cxs;
let first = cxs.min(cxe).floor() as isize;
let last = ((cxs.max(cxe)).ceil() as isize - 1).max(first);
let dydx = (cye - cys) / (cxe - cxs);
let mut col = if right { first } else { last };
loop {
if col >= 0 && (col as usize) < n {
let cl = col as f64;
let cr = cl + 1.0;
let (pxs, pxe) = if right {
(cxs.max(cl), cxe.min(cr))
} else {
(cxs.min(cr), cxe.max(cl))
};
let pys = cys + (pxs - cxs) * dydx;
let pye = cys + (pxe - cxs) * dydx;
put(col as usize, pye - pys, ((pxs - cl) + (pxe - cl)) * 0.5);
}
if right {
if col >= last {
break;
}
col += 1;
} else {
if col <= first {
break;
}
col -= 1;
}
}
}