This commit is contained in:
ntr 2019-10-21 17:01:10 +11:00
parent 44df93b028
commit 68eb2fa1b4
2 changed files with 94 additions and 1 deletions

View File

@ -257,6 +257,94 @@ fn _hieroglyph() -> String {
return s;
}
pub fn smiley(id: Uuid) -> Result<Uuid, Error> {
let mut rng = thread_rng();
let mut svg = Vec::new();
// distribution for lightness
// bellcurve around 75%
let l_dist = Normal::new(50.0, 10.0);
let s_dist = Normal::new(50.0, 20.0);
// basic layout is 2000x1000 box w/ 50 padding
// 2:1 for each x,y
// left eye is top left
// x = 50 left padding + point offset
// y = 750 + 50 top padding + point offset
// right eye is top right
// x = 50 left padding + 1500 + point offset
// y = 50 top padding + point offset
// mouth is bottom centre
// x = 50 + 500 + xoffset
// y = 50 + 750 + yoffset
write!(&mut svg, "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='0 0 2100 1100' width='2000' height='2000' background=\"black\" ><g>")?;
let n_points = rng.gen_range(3, 5);
let left_eye_path: String = std::iter::repeat(())
.map(|()| {
let x = 50 + (rng.gen_range(0, 5) * 100);
let y = 50 + (rng.gen_range(0, 5) * 50);
format!("{} {}", x, y)
})
.take(n_points)
.collect::<Vec<String>>()
.join(",");
// left eye
write!(&mut svg,
"<polyline fill=\"none\" stroke=\"whitesmoke\" stroke-width=\"15px\" x=\"50\" y=\"50\" points=\"{:}\" />",
left_eye_path)?;
let n_points = rng.gen_range(3, 5);
let right_eye_path: String = std::iter::repeat(())
.map(|()| {
let x = 50 + 1500 + (rng.gen_range(0, 5) * 100);
let y = 50 + (rng.gen_range(0, 5) * 50);
format!("{} {}", x, y)
})
.take(n_points)
.collect::<Vec<String>>()
.join(",");
// right eye
write!(&mut svg,
"<polyline fill=\"none\" stroke=\"whitesmoke\" stroke-width=\"15px\" x=\"1550\" y=\"50\" points=\"{:}\" />",
right_eye_path)?;
let n_points = rng.gen_range(3, 5);
let mouth_path: String = std::iter::repeat(())
.map(|()| {
let x = 50 + 500 + (rng.gen_range(0, 10) * 100);
let y = 50 + 750 + (rng.gen_range(0, 5) * 50);
format!("{} {}", x, y)
})
.take(n_points)
.collect::<Vec<String>>()
.join(",");
// mouth
write!(&mut svg,
"<polyline fill=\"none\" stroke=\"whitesmoke\" stroke-width=\"15px\" x=\"550\" y=\"800\" points=\"{:}\" />",
mouth_path)?;
write!(&mut svg, "</g></svg>")?;
let dest = format!("/var/lib/mnml/public/imgs/{}.svg", id);
println!("/var/lib/mnml/public/imgs/{}.svg", id);
let mut file = File::create(dest)?;
file.write_all(&svg)?;
Ok(id)
}
pub fn exists(id: Uuid) -> bool {
std::path::Path::new(&format!("/var/lib/mnml/public/imgs/{}.svg", id)).exists()
}
@ -283,6 +371,11 @@ mod tests {
// shapes_write(Uuid::new_v4()).unwrap();
// }
// }
#[test]
fn smiley_test() {
smiley(Uuid::new_v4()).unwrap();
}
}

View File

@ -159,7 +159,7 @@ pub fn apply(tx: &mut Transaction, account: &Account, variant: MtxVariant, const
match mtx.variant {
MtxVariant::Invader => img::invader_write(construct.img)?,
MtxVariant::Molecular => img::molecular_write(construct.img)?,
MtxVariant::Shapes => img::shapes_write(construct.img)?,
MtxVariant::Shapes => img::smiley(construct.img)?,
_ => construct.img,
};