use emblema::{
Atlas, BackendPreference, BlendMode, Canvas, Color, Context, Coverage, Extent2D, GlyphKey,
GradientStop, Layer, Paint, PixelFormat, PositionedGlyph, Rect, Vec2,
};
const SIZE: Extent2D = Extent2D {
width: 512,
height: 512,
};
const FORMAT: PixelFormat = PixelFormat::Rgba8Unorm;
fn main() {
let path = std::env::args()
.nth(1)
.unwrap_or_else(|| "frame.ppm".into());
let mut ctx = match Context::new(BackendPreference::Auto) {
Ok(ctx) => ctx,
Err(e) => {
eprintln!("no usable rendering backend: {e}");
std::process::exit(1);
}
};
eprintln!(
"{:?} on {}",
ctx.backend(),
ctx.capabilities().device_name.clone()
);
let (atlas, glyphs) = build_atlas();
let mut image = ctx
.create_image(
Extent2D::new(atlas.size(), atlas.size()),
PixelFormat::R8Unorm,
)
.expect("atlas image");
ctx.write_image(&mut image, atlas.texels()).expect("upload");
let mut swatch = ctx
.create_image(Extent2D::new(2, 2), PixelFormat::Rgba8Unorm)
.expect("swatch image");
ctx.write_image(
&mut swatch,
&[
220, 90, 60, 255, 60, 140, 220, 255, 60, 200, 140, 255, 230, 200, 70, 255,
],
)
.expect("upload");
let recording = compose(&atlas, &glyphs);
let mut surface = ctx.create_surface(SIZE, FORMAT).expect("surface");
ctx.draw_with_images(&mut surface, &recording, &[&image, &swatch])
.expect("draw");
let pixels = ctx.read(&mut surface).expect("read");
ctx.destroy_surface(surface);
ctx.destroy_image(swatch);
ctx.destroy_image(image);
match write_ppm(&path, SIZE, &pixels) {
Ok(()) => eprintln!("wrote {path}"),
Err(e) => {
eprintln!("writing {path}: {e}");
std::process::exit(1);
}
}
}
fn compose(atlas: &Atlas, glyphs: &[PositionedGlyph]) -> emblema::Recording {
let mut canvas = Canvas::new(SIZE);
canvas.clear(Color::srgb(0.93, 0.94, 0.96, 1.0));
canvas
.draw_rect(
Rect::new(0.0, 0.0, 512.0, 96.0),
&Paint::linear_gradient(
Vec2::new(0.0, 0.0),
Vec2::new(512.0, 96.0),
vec![
GradientStop::new(Color::srgb(0.18, 0.22, 0.55, 1.0), 0.0),
GradientStop::new(Color::srgb(0.55, 0.20, 0.45, 1.0), 1.0),
],
),
)
.expect("header");
for (index, top) in [128.0f32, 248.0].into_iter().enumerate() {
let card = Rect::new(32.0, top, 480.0, top + 96.0);
let shadow = Rect::new(card.left, card.top + 8.0, card.right, card.bottom + 8.0);
canvas.save_layer_bounds(Layer::opacity(0.30).with_blur(9.0), shadow);
canvas
.draw_rrect(shadow, 18.0, &Paint::fill(Color::srgb(0.0, 0.0, 0.0, 1.0)))
.expect("shadow");
canvas.restore();
canvas
.draw_rrect(card, 18.0, &Paint::fill(Color::srgb(1.0, 1.0, 1.0, 1.0)))
.expect("card");
canvas
.draw_circle(
Vec2::new(card.left + 48.0, card.top + 48.0),
26.0,
&Paint::fill(if index == 0 {
Color::srgb(0.95, 0.55, 0.25, 1.0)
} else {
Color::srgb(0.30, 0.75, 0.60, 1.0)
}),
)
.expect("avatar");
let track = Rect::new(
card.left + 92.0,
card.top + 60.0,
card.right - 24.0,
card.top + 72.0,
);
canvas
.draw_rrect(track, 6.0, &Paint::fill(Color::srgb(0.89, 0.90, 0.93, 1.0)))
.expect("track");
canvas.save();
canvas
.clip_path(&track.to_rounded_path(6.0))
.expect("clip to the track");
let fraction = if index == 0 { 0.7 } else { 0.35 };
canvas
.draw_rect(
Rect::new(
track.left,
track.top,
track.left + track.width() * fraction,
track.bottom,
),
&Paint::linear_gradient(
Vec2::new(track.left, 0.0),
Vec2::new(track.right, 0.0),
vec![
GradientStop::new(Color::srgb(0.35, 0.65, 1.0, 1.0), 0.0),
GradientStop::new(Color::srgb(0.55, 0.35, 0.95, 1.0), 1.0),
],
),
)
.expect("progress");
canvas.restore();
}
let panel = Rect::new(32.0, 368.0, 236.0, 464.0);
canvas.save();
canvas
.clip_path(&panel.to_rounded_path(18.0))
.expect("clip to the panel");
canvas
.draw_rect(panel, &Paint::image(1, panel))
.expect("image");
canvas.restore();
canvas
.draw_circle(
Vec2::new(428.0, 60.0),
40.0,
&Paint::fill(Color::srgb(0.25, 0.18, 0.45, 1.0)).with_blend(BlendMode::Plus),
)
.expect("glow");
canvas
.draw_glyphs(
glyphs,
atlas,
0,
&Paint::fill(Color::srgb(0.25, 0.27, 0.33, 1.0)),
)
.expect("glyphs");
canvas.finish()
}
fn build_atlas() -> (Atlas, Vec<PositionedGlyph>) {
let mut atlas = Atlas::new(64);
let mut run = Vec::new();
for i in 0..8u16 {
let key = GlyphKey {
subpixel: 0,
font: 1,
glyph: i,
size: 24,
};
let (w, h) = (10u32, 18u32);
let mut texels = vec![0u8; (w * h) as usize];
for y in 0..h {
for x in 0..w {
let lit = x as f32 / w as f32 + y as f32 / h as f32 > i as f32 / 8.0;
texels[(y * w + x) as usize] = if lit { 255 } else { 40 };
}
}
let rect = atlas
.insert(
key,
&Coverage {
width: w,
height: h,
texels,
},
)
.expect("atlas insert");
run.push(PositionedGlyph::new(
key,
[364.0 + i as f32 * 14.0, 476.0],
rect,
));
}
(atlas, run)
}
fn write_ppm(path: &str, extent: Extent2D, pixels: &[u8]) -> std::io::Result<()> {
use std::io::Write as _;
let mut out = std::io::BufWriter::new(std::fs::File::create(path)?);
write!(out, "P6\n{} {}\n255\n", extent.width, extent.height)?;
let mut rgb = Vec::with_capacity(pixels.len() / 4 * 3);
for texel in pixels.chunks_exact(4) {
rgb.extend_from_slice(&texel[..3]);
}
out.write_all(&rgb)?;
out.flush()
}