use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Rect {
pub left: i32,
pub top: i32,
pub width: i32,
pub height: i32,
}
impl Rect {
#[must_use]
pub fn scaled(&self, scale: f32) -> Rect {
let at = |v: i32| (v as f32 * scale).round() as i32;
let (left, top) = (at(self.left), at(self.top));
Rect::new(left, top, at(self.right()) - left, at(self.bottom()) - top)
}
pub const EMPTY: Rect = Rect {
left: 0,
top: 0,
width: 0,
height: 0,
};
pub const fn new(left: i32, top: i32, width: i32, height: i32) -> Rect {
Rect {
left,
top,
width,
height,
}
}
pub fn right(&self) -> i32 {
self.left.wrapping_add(self.width)
}
pub fn bottom(&self) -> i32 {
self.top.wrapping_add(self.height)
}
pub fn contains(&self, x: i32, y: i32) -> bool {
x >= self.left && x < self.right() && y >= self.top && y < self.bottom()
}
pub fn overlaps(&self, r: &Rect) -> bool {
r.left < self.right()
&& self.left < r.right()
&& r.top < self.bottom()
&& self.top < r.bottom()
}
#[must_use]
pub fn cut_to(&self, c: &Rect) -> Rect {
let l = self.left.max(c.left);
let t = self.top.max(c.top);
Rect::new(
l,
t,
self.right().min(c.right()) - l,
self.bottom().min(c.bottom()) - t,
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BitmapDef {
pub id: Rc<str>,
pub file: Rc<str>,
pub x: i32,
pub y: i32,
pub w: Option<i32>,
pub h: Option<i32>,
pub gamma_group: Option<Rc<str>>,
}
impl BitmapDef {
pub fn new(
id: &str,
file: &str,
x: i32,
y: i32,
w: Option<i32>,
h: Option<i32>,
gamma_group: Option<&str>,
) -> Self {
BitmapDef {
id: Rc::from(id),
file: Rc::from(file),
x,
y,
w,
h,
gamma_group: gamma_group.map(Rc::from),
}
}
#[must_use]
pub fn part(&self, x: i32, y: i32, w: Option<i32>, h: Option<i32>) -> Self {
BitmapDef {
id: self.id.clone(),
file: self.file.clone(),
x,
y,
w,
h,
gamma_group: self.gamma_group.clone(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Gamma {
pub r: i32,
pub g: i32,
pub b: i32,
pub boost: i32,
pub gray: i32,
}
impl Gamma {
pub fn is_identity(&self) -> bool {
self.r == 0 && self.g == 0 && self.b == 0 && self.gray == 0 && self.boost == 0
}
pub fn apply(&self, argb: u32) -> u32 {
let mut red = ((argb >> 16) & 0xff) as i32;
let mut green = ((argb >> 8) & 0xff) as i32;
let mut blue = (argb & 0xff) as i32;
if self.gray != 0 {
red = if self.gray == 2 {
(red + green + blue) / 3
} else {
red.max(green).max(blue)
};
green = red;
blue = red;
}
let lifted = |c: i32| -> f64 {
match self.boost {
1 => c as f64 / 2.0 + 128.0,
2 => (c + 32) as f64 * 4.0,
_ => c as f64,
}
};
let ch = |c: i32, f: i32| -> u32 {
((lifted(c) * (1.0 + f as f64 / 4096.0)) as i32).clamp(0, 255) as u32
};
(argb & 0xff00_0000) | (ch(red, self.r) << 16) | (ch(green, self.g) << 8) | ch(blue, self.b)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BitmapFontDef {
pub id: Rc<str>,
pub file: Rc<str>,
pub char_width: i32,
pub char_height: i32,
pub h_spacing: i32,
pub v_spacing: i32,
pub ox: i32,
pub oy: i32,
pub gamma_group: Option<Rc<str>>,
}
const FONT_ROWS: [&str; 2] = [
"abcdefghijklmnopqrstuvwxyz\"@ ",
"0123456789….:()-'!_+\\/[]^&%,=$#\nâöä?*",
];
impl BitmapFontDef {
pub fn cell(&self, c: char) -> Option<(i32, i32)> {
if c == ' ' {
return None;
}
let lower = c.to_lowercase().next().unwrap_or(c);
for (row, chars) in FONT_ROWS.iter().enumerate() {
if let Some(col) = chars.chars().position(|x| x == lower) {
return Some((col as i32, row as i32));
}
}
None
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FontDef {
pub id: Rc<str>,
pub file: Rc<str>,
}
impl FontDef {
pub fn new(id: &str, file: &str) -> Self {
FontDef {
id: Rc::from(id),
file: Rc::from(file),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Rgb(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Align {
Left,
Center,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VAlign {
Top,
Center,
Bottom,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextCmd {
pub text: String,
pub font: Option<FontDef>,
pub size: i32,
pub color: Rgb,
pub rect: Rect,
pub align: Align,
pub scroll: i32,
pub alpha: i32,
pub valign: VAlign,
pub bold: bool,
pub shadow: Option<Rgb>,
pub shadow_dx: i32,
pub shadow_dy: i32,
pub smooth: bool,
pub x: f32,
pub baseline: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct BitmapTextCmd {
pub text: String,
pub font: Rc<BitmapFontDef>,
pub rect: Rect,
pub align: Align,
pub alpha: i32,
pub valign: VAlign,
pub colon_width: Option<i32>,
pub scroll: i32,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DrawCmd {
Image {
bitmap: BitmapDef,
dst: Rect,
alpha: i32,
gamma: Option<Gamma>,
smooth: bool,
},
Fill {
rect: Rect,
color: Rgb,
alpha: i32,
},
WindowStart(Region),
WindowEnd,
Text(Box<TextCmd>),
BitmapText(Box<BitmapTextCmd>),
Clip(Rect),
Unclip,
Pixels {
rect: Rect,
w: i32,
h: i32,
argb: Rc<[u32]>,
alpha: i32,
stable: bool,
},
}
impl DrawCmd {
#[must_use]
pub fn scaled(&self, scale: f32) -> DrawCmd {
let mut cmd = self.clone();
match &mut cmd {
DrawCmd::Image { dst, .. } => *dst = dst.scaled(scale),
DrawCmd::Fill { rect, .. } | DrawCmd::Clip(rect) | DrawCmd::Pixels { rect, .. } => {
*rect = rect.scaled(scale);
}
DrawCmd::WindowStart(region) => *region = region.scaled(scale),
DrawCmd::Text(t) => {
t.rect = t.rect.scaled(scale);
t.size = (t.size as f32 * scale).round() as i32;
t.x *= scale;
t.baseline *= scale;
t.scroll = (t.scroll as f32 * scale).round() as i32;
t.shadow_dx = (t.shadow_dx as f32 * scale).round() as i32;
t.shadow_dy = (t.shadow_dy as f32 * scale).round() as i32;
}
DrawCmd::BitmapText(t) => t.rect = t.rect.scaled(scale),
DrawCmd::WindowEnd | DrawCmd::Unclip => {}
}
cmd
}
pub fn draws_same_as(&self, other: &DrawCmd) -> bool {
match (self, other) {
(DrawCmd::BitmapText(a), DrawCmd::BitmapText(b)) => {
a.text == b.text && a.rect == b.rect
}
(
DrawCmd::Pixels {
rect,
w,
h,
argb,
alpha,
stable,
},
DrawCmd::Pixels {
rect: r2,
w: w2,
h: h2,
argb: a2,
alpha: al2,
stable: s2,
},
) => {
rect == r2
&& w == w2
&& h == h2
&& alpha == al2
&& stable == s2
&& (Rc::ptr_eq(argb, a2) || argb == a2)
}
_ => self == other,
}
}
pub fn image(bitmap: BitmapDef, dst: Rect, alpha: i32) -> DrawCmd {
DrawCmd::Image {
bitmap,
dst,
alpha,
gamma: None,
smooth: false,
}
}
pub fn is_cut_pixel(argb: u32) -> bool {
(argb >> 24) >= 128
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RegionImage {
pub bitmap: BitmapDef,
pub dst: Rect,
pub alpha: i32,
pub gamma: Option<Gamma>,
pub smooth: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Region {
Pic(RegionImage),
Box(Rect),
Op {
base: Option<Box<Region>>,
add: Vec<Region>,
keep: Vec<Region>,
cut: Vec<Region>,
},
}
impl Region {
#[must_use]
pub fn scaled(&self, scale: f32) -> Region {
match self {
Region::Pic(img) => Region::Pic(RegionImage {
dst: img.dst.scaled(scale),
..img.clone()
}),
Region::Box(r) => Region::Box(r.scaled(scale)),
Region::Op {
base,
add,
keep,
cut,
} => {
let all = |v: &[Region]| v.iter().map(|r| r.scaled(scale)).collect();
Region::Op {
base: base.as_ref().map(|b| Box::new(b.scaled(scale))),
add: all(add),
keep: all(keep),
cut: all(cut),
}
}
}
}
pub fn map_pics(
&self,
f: &mut dyn FnMut(&RegionImage) -> Option<RegionImage>,
) -> Option<Region> {
match self {
Region::Pic(img) => f(img).map(Region::Pic),
Region::Box(_) => Some(self.clone()),
Region::Op {
base,
add,
keep,
cut,
} => Some(Region::Op {
base: base.as_ref().and_then(|b| b.map_pics(f)).map(Box::new),
add: add.iter().filter_map(|r| r.map_pics(f)).collect(),
keep: keep.iter().filter_map(|r| r.map_pics(f)).collect(),
cut: cut.iter().filter_map(|r| r.map_pics(f)).collect(),
}),
}
}
pub fn pics_mut(&mut self, f: &mut dyn FnMut(&mut RegionImage)) {
match self {
Region::Pic(img) => f(img),
Region::Box(_) => {}
Region::Op {
base,
add,
keep,
cut,
} => {
if let Some(b) = base {
b.pics_mut(f);
}
for r in add.iter_mut().chain(keep.iter_mut()).chain(cut.iter_mut()) {
r.pics_mut(f);
}
}
}
}
pub fn bounds(&self) -> Option<Rect> {
match self {
Region::Pic(img) => Some(img.dst),
Region::Box(r) => Some(*r),
Region::Op { base, add, .. } => {
let mut all = base
.as_ref()
.and_then(|b| b.bounds())
.into_iter()
.collect::<Vec<_>>();
all.extend(add.iter().filter_map(|r| r.bounds()));
all.into_iter().reduce(|a, b| {
let l = a.left.min(b.left);
let t = a.top.min(b.top);
Rect::new(
l,
t,
a.right().max(b.right()) - l,
a.bottom().max(b.bottom()) - t,
)
})
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_tint_lifts_and_scales() {
let g = Gamma {
r: 0,
g: 0,
b: 0,
boost: 1,
gray: 0,
};
assert_eq!(g.apply(0xff00_0000), 0xff80_8080);
let g = Gamma {
r: 4096,
g: 0,
b: -4096,
boost: 0,
gray: 0,
};
assert_eq!(g.apply(0xff40_4040), 0xff80_4000);
}
#[test]
fn bitmap_font_cells() {
let f = BitmapFontDef {
id: Rc::from("f"),
file: Rc::from("f.png"),
char_width: 5,
char_height: 6,
h_spacing: 0,
v_spacing: 0,
ox: 0,
oy: 0,
gamma_group: None,
};
assert_eq!(f.cell('A'), Some((0, 0)));
assert_eq!(f.cell(':'), Some((12, 1)));
assert_eq!(f.cell(' '), None);
}
}