2019-01-05 19:14:16 +00:00
|
|
|
const ANTI_ALIAS: bool = true;
|
2019-01-19 16:10:28 +00:00
|
|
|
const AA_SIZE: f32 = 0.5; // TODO: 1.0 / pixels_per_point
|
2019-01-05 19:14:16 +00:00
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
/// Outputs render info in a format suitable for e.g. OpenGL.
|
|
|
|
use crate::{
|
2019-01-12 23:55:56 +00:00
|
|
|
fonts::Fonts,
|
2019-01-05 15:23:40 +00:00
|
|
|
math::{remap, vec2, Vec2, TAU},
|
2019-01-04 13:14:32 +00:00
|
|
|
types::{Color, PaintCmd},
|
|
|
|
};
|
|
|
|
|
2019-01-17 17:03:39 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Serialize)]
|
2019-01-04 13:14:32 +00:00
|
|
|
pub struct Vertex {
|
2019-01-05 15:23:40 +00:00
|
|
|
/// Pixel coordinates
|
|
|
|
pub pos: Vec2,
|
2019-01-04 13:14:32 +00:00
|
|
|
/// Texel indices into the texture
|
2019-01-05 15:23:40 +00:00
|
|
|
pub uv: (u16, u16),
|
2019-01-04 13:14:32 +00:00
|
|
|
/// sRGBA
|
|
|
|
pub color: Color,
|
|
|
|
}
|
|
|
|
|
2019-01-17 17:03:39 +00:00
|
|
|
#[derive(Clone, Debug, Default, Serialize)]
|
2019-01-04 13:14:32 +00:00
|
|
|
pub struct Frame {
|
2019-01-05 14:28:07 +00:00
|
|
|
/// Draw as triangles (i.e. the length is a multiple of three)
|
2019-01-04 13:14:32 +00:00
|
|
|
pub indices: Vec<u32>,
|
|
|
|
pub vertices: Vec<Vertex>,
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:23:40 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
pub enum PathType {
|
|
|
|
Open,
|
|
|
|
Closed,
|
|
|
|
}
|
|
|
|
use self::PathType::*;
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
impl Frame {
|
2019-01-17 17:03:39 +00:00
|
|
|
pub fn append(&mut self, frame: &Frame) {
|
|
|
|
let index_offset = self.vertices.len() as u32;
|
|
|
|
for index in &frame.indices {
|
|
|
|
self.indices.push(index_offset + index);
|
|
|
|
}
|
|
|
|
self.vertices.extend(frame.vertices.iter());
|
|
|
|
}
|
|
|
|
|
2019-01-05 19:14:16 +00:00
|
|
|
fn triangle(&mut self, a: u32, b: u32, c: u32) {
|
|
|
|
self.indices.push(a);
|
|
|
|
self.indices.push(b);
|
|
|
|
self.indices.push(c);
|
|
|
|
}
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
/// Uniformly colored rectangle
|
|
|
|
pub fn add_rect(&mut self, top_left: Vertex, bottom_right: Vertex) {
|
|
|
|
let idx = self.vertices.len() as u32;
|
2019-01-05 19:14:16 +00:00
|
|
|
self.triangle(idx + 0, idx + 1, idx + 2);
|
|
|
|
self.triangle(idx + 2, idx + 1, idx + 3);
|
2019-01-05 14:28:07 +00:00
|
|
|
|
|
|
|
let top_right = Vertex {
|
2019-01-05 15:23:40 +00:00
|
|
|
pos: vec2(bottom_right.pos.x, top_left.pos.y),
|
|
|
|
uv: (bottom_right.uv.0, top_left.uv.1),
|
2019-01-05 14:28:07 +00:00
|
|
|
color: top_left.color,
|
|
|
|
};
|
|
|
|
let botom_left = Vertex {
|
2019-01-05 15:23:40 +00:00
|
|
|
pos: vec2(top_left.pos.x, bottom_right.pos.y),
|
|
|
|
uv: (top_left.uv.0, bottom_right.uv.1),
|
2019-01-05 14:28:07 +00:00
|
|
|
color: top_left.color,
|
|
|
|
};
|
|
|
|
self.vertices.push(top_left);
|
|
|
|
self.vertices.push(top_right);
|
|
|
|
self.vertices.push(botom_left);
|
|
|
|
self.vertices.push(bottom_right);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fill_closed_path(&mut self, points: &[Vec2], normals: &[Vec2], color: Color) {
|
|
|
|
assert_eq!(points.len(), normals.len());
|
|
|
|
let n = points.len() as u32;
|
2019-01-05 19:14:16 +00:00
|
|
|
let vert = |pos, color| Vertex {
|
2019-01-05 15:23:40 +00:00
|
|
|
pos,
|
|
|
|
uv: (0, 0),
|
|
|
|
color,
|
2019-01-05 19:14:16 +00:00
|
|
|
};
|
|
|
|
if ANTI_ALIAS {
|
|
|
|
let color_outer = color.transparent();
|
|
|
|
let idx_inner = self.vertices.len() as u32;
|
|
|
|
let idx_outer = idx_inner + 1;
|
|
|
|
for i in 2..n {
|
|
|
|
self.triangle(idx_inner + 2 * (i - 1), idx_inner, idx_inner + 2 * i);
|
|
|
|
}
|
|
|
|
let mut i0 = n - 1;
|
|
|
|
for i1 in 0..n {
|
|
|
|
let dm = normals[i1 as usize] * AA_SIZE * 0.5;
|
|
|
|
self.vertices.push(vert(points[i1 as usize] - dm, color));
|
|
|
|
self.vertices
|
|
|
|
.push(vert(points[i1 as usize] + dm, color_outer));
|
|
|
|
self.triangle(idx_inner + i1 * 2, idx_inner + i0 * 2, idx_outer + 2 * i0);
|
|
|
|
self.triangle(idx_outer + i0 * 2, idx_outer + i1 * 2, idx_inner + 2 * i1);
|
|
|
|
i0 = i1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let idx = self.vertices.len() as u32;
|
|
|
|
self.vertices
|
|
|
|
.extend(points.iter().map(|&pos| vert(pos, color)));
|
|
|
|
for i in 2..n {
|
|
|
|
self.triangle(idx, idx + i - 1, idx + i);
|
|
|
|
}
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:23:40 +00:00
|
|
|
pub fn paint_path(
|
2019-01-05 14:28:07 +00:00
|
|
|
&mut self,
|
2019-01-05 15:23:40 +00:00
|
|
|
path_type: PathType,
|
2019-01-05 14:28:07 +00:00
|
|
|
points: &[Vec2],
|
|
|
|
normals: &[Vec2],
|
|
|
|
color: Color,
|
2019-01-05 15:23:40 +00:00
|
|
|
width: f32,
|
2019-01-05 14:28:07 +00:00
|
|
|
) {
|
|
|
|
assert_eq!(points.len(), normals.len());
|
|
|
|
let n = points.len() as u32;
|
|
|
|
let hw = width / 2.0;
|
|
|
|
let idx = self.vertices.len() as u32;
|
|
|
|
|
2019-01-05 19:14:16 +00:00
|
|
|
let vert = |pos, color| Vertex {
|
|
|
|
pos,
|
|
|
|
uv: (0, 0),
|
|
|
|
color,
|
|
|
|
};
|
|
|
|
|
|
|
|
if ANTI_ALIAS {
|
|
|
|
let color_outer = color.transparent();
|
|
|
|
let thin_line = width <= 1.0;
|
|
|
|
let mut color_inner = color;
|
|
|
|
if thin_line {
|
|
|
|
// Fade out as it gets thinner:
|
|
|
|
color_inner.a = (color_inner.a as f32 * width).round() as u8;
|
|
|
|
}
|
|
|
|
// TODO: line caps ?
|
|
|
|
let mut i0 = n - 1;
|
|
|
|
for i1 in 0..n {
|
|
|
|
let connect_with_previous = path_type == PathType::Closed || i1 > 0;
|
|
|
|
if thin_line {
|
|
|
|
let p = points[i1 as usize];
|
|
|
|
let n = normals[i1 as usize];
|
|
|
|
self.vertices.push(vert(p + n * AA_SIZE, color_outer));
|
|
|
|
self.vertices.push(vert(p, color_inner));
|
|
|
|
self.vertices.push(vert(p - n * AA_SIZE, color_outer));
|
|
|
|
|
|
|
|
if connect_with_previous {
|
|
|
|
self.triangle(idx + 3 * i0 + 0, idx + 3 * i0 + 1, idx + 3 * i1 + 0);
|
|
|
|
self.triangle(idx + 3 * i0 + 1, idx + 3 * i1 + 0, idx + 3 * i1 + 1);
|
|
|
|
|
|
|
|
self.triangle(idx + 3 * i0 + 1, idx + 3 * i0 + 2, idx + 3 * i1 + 1);
|
|
|
|
self.triangle(idx + 3 * i0 + 2, idx + 3 * i1 + 1, idx + 3 * i1 + 2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let hw = (width - AA_SIZE) * 0.5;
|
|
|
|
let p = points[i1 as usize];
|
|
|
|
let n = normals[i1 as usize];
|
|
|
|
self.vertices
|
|
|
|
.push(vert(p + n * (hw + AA_SIZE), color_outer));
|
|
|
|
self.vertices.push(vert(p + n * (hw + 0.0), color_inner));
|
|
|
|
self.vertices.push(vert(p - n * (hw + 0.0), color_inner));
|
|
|
|
self.vertices
|
|
|
|
.push(vert(p - n * (hw + AA_SIZE), color_outer));
|
|
|
|
|
|
|
|
if connect_with_previous {
|
|
|
|
self.triangle(idx + 4 * i0 + 0, idx + 4 * i0 + 1, idx + 4 * i1 + 0);
|
|
|
|
self.triangle(idx + 4 * i0 + 1, idx + 4 * i1 + 0, idx + 4 * i1 + 1);
|
|
|
|
|
|
|
|
self.triangle(idx + 4 * i0 + 1, idx + 4 * i0 + 2, idx + 4 * i1 + 1);
|
|
|
|
self.triangle(idx + 4 * i0 + 2, idx + 4 * i1 + 1, idx + 4 * i1 + 2);
|
|
|
|
|
|
|
|
self.triangle(idx + 4 * i0 + 2, idx + 4 * i0 + 3, idx + 4 * i1 + 2);
|
|
|
|
self.triangle(idx + 4 * i0 + 3, idx + 4 * i1 + 2, idx + 4 * i1 + 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i0 = i1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let last_index = if path_type == Closed { n } else { n - 1 };
|
|
|
|
for i in 0..last_index {
|
|
|
|
self.triangle(
|
|
|
|
idx + (2 * i + 0) % (2 * n),
|
|
|
|
idx + (2 * i + 1) % (2 * n),
|
|
|
|
idx + (2 * i + 2) % (2 * n),
|
|
|
|
);
|
|
|
|
self.triangle(
|
|
|
|
idx + (2 * i + 2) % (2 * n),
|
|
|
|
idx + (2 * i + 1) % (2 * n),
|
|
|
|
idx + (2 * i + 3) % (2 * n),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (&p, &n) in points.iter().zip(normals) {
|
|
|
|
self.vertices.push(vert(p + hw * n, color));
|
|
|
|
self.vertices.push(vert(p - hw * n, color));
|
|
|
|
}
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-04 13:14:32 +00:00
|
|
|
|
2019-01-16 15:28:43 +00:00
|
|
|
pub fn paint(fonts: &Fonts, commands: &[PaintCmd]) -> Frame {
|
2019-01-05 15:23:40 +00:00
|
|
|
let mut path_points = Vec::new();
|
|
|
|
let mut path_normals = Vec::new();
|
2019-01-05 14:28:07 +00:00
|
|
|
|
2019-01-04 13:14:32 +00:00
|
|
|
let mut frame = Frame::default();
|
|
|
|
for cmd in commands {
|
|
|
|
match cmd {
|
2019-01-05 14:28:07 +00:00
|
|
|
PaintCmd::Circle {
|
|
|
|
center,
|
|
|
|
fill_color,
|
|
|
|
outline,
|
|
|
|
radius,
|
|
|
|
} => {
|
2019-01-05 15:23:40 +00:00
|
|
|
path_points.clear();
|
|
|
|
path_normals.clear();
|
2019-01-05 14:28:07 +00:00
|
|
|
|
2019-01-05 15:23:40 +00:00
|
|
|
let n = 32; // TODO: parameter
|
|
|
|
for i in 0..n {
|
|
|
|
let angle = remap(i as f32, 0.0, n as f32, 0.0, TAU);
|
|
|
|
let normal = vec2(angle.cos(), angle.sin());
|
|
|
|
path_normals.push(normal);
|
|
|
|
path_points.push(*center + *radius * normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(color) = fill_color {
|
|
|
|
frame.fill_closed_path(&path_points, &path_normals, *color);
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
2019-01-05 15:23:40 +00:00
|
|
|
if let Some(outline) = outline {
|
|
|
|
frame.paint_path(
|
|
|
|
Closed,
|
|
|
|
&path_points,
|
|
|
|
&path_normals,
|
|
|
|
outline.color,
|
|
|
|
outline.width,
|
|
|
|
);
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-17 17:03:39 +00:00
|
|
|
PaintCmd::Frame(cmd_frame) => {
|
|
|
|
frame.append(cmd_frame);
|
|
|
|
}
|
2019-01-05 15:23:40 +00:00
|
|
|
PaintCmd::Line {
|
|
|
|
points,
|
|
|
|
color,
|
|
|
|
width,
|
|
|
|
} => {
|
|
|
|
let n = points.len();
|
|
|
|
if n >= 2 {
|
|
|
|
path_points = points.clone();
|
|
|
|
path_normals.clear();
|
|
|
|
|
|
|
|
path_normals.push((path_points[1] - path_points[0]).normalized().rot90());
|
|
|
|
for i in 1..n - 1 {
|
|
|
|
let n0 = (path_points[i] - path_points[i - 1]).normalized().rot90();
|
|
|
|
let n1 = (path_points[i + 1] - path_points[i]).normalized().rot90();
|
|
|
|
let v = (n0 + n1) / 2.0;
|
|
|
|
let normal = v / v.length_sq();
|
|
|
|
path_normals.push(normal); // TODO: handle VERY sharp turns better
|
|
|
|
}
|
|
|
|
path_normals.push(
|
|
|
|
(path_points[n - 1] - path_points[n - 2])
|
|
|
|
.normalized()
|
|
|
|
.rot90(),
|
|
|
|
);
|
|
|
|
|
|
|
|
frame.paint_path(Open, &path_points, &path_normals, *color, *width);
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 13:14:32 +00:00
|
|
|
PaintCmd::Rect {
|
2019-01-05 19:14:16 +00:00
|
|
|
corner_radius,
|
2019-01-05 14:28:07 +00:00
|
|
|
fill_color,
|
|
|
|
outline,
|
2019-01-14 13:26:02 +00:00
|
|
|
rect,
|
2019-01-04 13:14:32 +00:00
|
|
|
} => {
|
2019-01-05 15:23:40 +00:00
|
|
|
path_points.clear();
|
|
|
|
path_normals.clear();
|
|
|
|
|
2019-01-14 13:26:02 +00:00
|
|
|
let min = rect.min();
|
|
|
|
let max = rect.max();
|
2019-01-05 15:23:40 +00:00
|
|
|
|
2019-01-14 13:26:02 +00:00
|
|
|
let cr = corner_radius.min(rect.size.x * 0.5).min(rect.size.y * 0.5);
|
2019-01-05 19:14:16 +00:00
|
|
|
|
2019-01-06 23:03:29 +00:00
|
|
|
if cr <= 0.0 {
|
2019-01-05 19:14:16 +00:00
|
|
|
path_points.push(vec2(min.x, min.y));
|
|
|
|
path_normals.push(vec2(-1.0, -1.0));
|
|
|
|
path_points.push(vec2(max.x, min.y));
|
|
|
|
path_normals.push(vec2(1.0, -1.0));
|
|
|
|
path_points.push(vec2(max.x, max.y));
|
|
|
|
path_normals.push(vec2(1.0, 1.0));
|
|
|
|
path_points.push(vec2(min.x, max.y));
|
|
|
|
path_normals.push(vec2(-1.0, 1.0));
|
|
|
|
} else {
|
|
|
|
let n = 8;
|
|
|
|
|
|
|
|
let mut add_arc = |c, quadrant| {
|
|
|
|
let quadrant = quadrant as f32;
|
|
|
|
|
|
|
|
const RIGHT_ANGLE: f32 = TAU / 4.0;
|
|
|
|
for i in 0..=n {
|
|
|
|
let angle = remap(
|
|
|
|
i as f32,
|
|
|
|
0.0,
|
|
|
|
n as f32,
|
|
|
|
quadrant * RIGHT_ANGLE,
|
|
|
|
(quadrant + 1.0) * RIGHT_ANGLE,
|
|
|
|
);
|
|
|
|
let normal = vec2(angle.cos(), angle.sin());
|
|
|
|
path_points.push(c + cr * normal);
|
|
|
|
path_normals.push(normal);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
add_arc(vec2(max.x - cr, max.y - cr), 0);
|
|
|
|
add_arc(vec2(min.x + cr, max.y - cr), 1);
|
|
|
|
add_arc(vec2(min.x + cr, min.y + cr), 2);
|
|
|
|
add_arc(vec2(max.x - cr, min.y + cr), 3);
|
|
|
|
}
|
2019-01-05 15:23:40 +00:00
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
if let Some(color) = fill_color {
|
2019-01-05 15:23:40 +00:00
|
|
|
frame.fill_closed_path(&path_points, &path_normals, *color);
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
|
|
|
if let Some(outline) = outline {
|
2019-01-05 15:23:40 +00:00
|
|
|
frame.paint_path(
|
|
|
|
Closed,
|
|
|
|
&path_points,
|
|
|
|
&path_normals,
|
|
|
|
outline.color,
|
|
|
|
outline.width,
|
|
|
|
);
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PaintCmd::Text {
|
|
|
|
color,
|
|
|
|
pos,
|
|
|
|
text,
|
2019-01-12 23:55:56 +00:00
|
|
|
text_style,
|
2019-01-05 14:28:07 +00:00
|
|
|
x_offsets,
|
|
|
|
} => {
|
2019-01-16 15:28:43 +00:00
|
|
|
let font = &fonts[*text_style];
|
2019-01-05 14:28:07 +00:00
|
|
|
for (c, x_offset) in text.chars().zip(x_offsets.iter()) {
|
2019-01-12 23:55:56 +00:00
|
|
|
if let Some(glyph) = font.uv_rect(c) {
|
2019-01-05 20:23:53 +00:00
|
|
|
let mut top_left = Vertex {
|
2019-01-19 16:10:28 +00:00
|
|
|
pos: *pos + glyph.offset + vec2(*x_offset, 0.0),
|
|
|
|
uv: glyph.min,
|
2019-01-05 14:28:07 +00:00
|
|
|
color: *color,
|
|
|
|
};
|
2019-01-19 16:10:28 +00:00
|
|
|
top_left.pos.x = font.round_to_pixel(top_left.pos.x); // Pixel-perfection.
|
|
|
|
top_left.pos.y = font.round_to_pixel(top_left.pos.y); // Pixel-perfection.
|
2019-01-05 14:28:07 +00:00
|
|
|
let bottom_right = Vertex {
|
2019-01-19 16:10:28 +00:00
|
|
|
pos: top_left.pos + glyph.size,
|
|
|
|
uv: glyph.max,
|
2019-01-05 14:28:07 +00:00
|
|
|
color: *color,
|
|
|
|
};
|
|
|
|
frame.add_rect(top_left, bottom_right);
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 13:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frame
|
|
|
|
}
|
|
|
|
}
|