Feat: add new logo
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Dorian Zedler 2023-04-20 17:08:55 +02:00
parent 5ca0021533
commit 43b0b1d7a7
Signed by: dozedler
GPG key ID: 989DE36109AFA354
7 changed files with 139 additions and 43 deletions

View file

@ -1,3 +1,5 @@
use std::default;
use axum::{extract::Query, routing::get, Router};
use cairo::{Context, Format, ImageSurface};
use rust_embed::RustEmbed;
@ -25,50 +27,86 @@ where
}
}
#[derive(Deserialize)]
#[derive(Deserialize, Default)]
#[allow(non_camel_case_types)]
enum LogoVariant {
NoText,
#[default]
DarkText,
LightText,
}
#[derive(Deserialize, Default)]
enum LogoOrientation {
#[default]
Landscape,
Portrait,
}
#[derive(Deserialize, Default)]
struct ImageProperties {
#[serde(default = "default_as_false")]
#[serde(deserialize_with = "deserialize_bool")]
dark_mode: bool,
#[serde(default = "default_as_false")]
#[serde(deserialize_with = "deserialize_bool")]
text: bool,
background_color: Option<Color>,
#[serde(default)]
variant: LogoVariant,
#[serde(default)]
orientation: LogoOrientation,
}
#[derive(RustEmbed)]
#[folder = "images"]
#[folder = "artwork/logo"]
#[include = "*_T.png"]
struct ImageFiles;
fn get_surface_and_logo_coordiates(properties: &ImageProperties) -> (ImageSurface, (f64, f64)) {
if properties.text {
let image = ImageFiles::get("Makerlab.png").unwrap();
(
ImageSurface::create_from_png(&mut image.data.as_ref()).unwrap(),
(604.0, 432.0),
)
} else {
(
fn get_surface_and_logo_coordiates(
properties: &ImageProperties,
) -> (ImageSurface, (f64, f64), f64, f64) {
if let LogoVariant::NoText = properties.variant {
return (
ImageSurface::create(Format::ARgb32, 400, 400).unwrap(),
(200.0, 200.0),
)
148.0,
67.0,
);
}
let background_image_path = match (&properties.variant, &properties.orientation) {
(LogoVariant::DarkText, LogoOrientation::Landscape) => "landscape/4C_T.png",
(LogoVariant::LightText, LogoOrientation::Landscape) => "landscape/W_T.png",
(LogoVariant::DarkText, LogoOrientation::Portrait) => "portrait/4C_T.png",
(LogoVariant::LightText, LogoOrientation::Portrait) => "portrait/W_T.png",
_ => unreachable!(),
};
let (coordinates, outer_radius, inner_radius) = match &properties.orientation {
LogoOrientation::Landscape => ((412.0, 299.0), 209.0, 99.0),
LogoOrientation::Portrait => ((828.0, 563.0), 253.0, 118.0),
};
let image = ImageFiles::get(background_image_path).unwrap();
(
ImageSurface::create_from_png(&mut image.data.as_ref()).unwrap(),
coordinates,
outer_radius,
inner_radius,
)
}
async fn handler(Query(properties): Query<ImageProperties>) -> impl axum::response::IntoResponse {
let (surface, logo_coordinates) = get_surface_and_logo_coordiates(&properties);
let (surface, logo_coordinates, logo_outer_radius, logo_inner_radius) =
get_surface_and_logo_coordiates(&properties);
let context = Context::new(&surface).unwrap();
if let Some(c) = properties.background_color {
context.set_source_rgb(c.r as f64 / 255.0, c.g as f64 / 255.0, c.b as f64 / 255.0);
} else {
context.set_source_rgba(0.0, 0.0, 0.0, 0.0);
}
context.set_source_rgba(0.0, 0.0, 0.0, 0.0);
context.paint().unwrap();
polygon::draw_polygon_of_segmented_polygons(logo_coordinates, 148.0, 67.0, 6, &context)
.unwrap();
polygon::draw_polygon_of_segmented_polygons(
logo_coordinates,
logo_outer_radius,
logo_inner_radius,
6,
&context,
)
.unwrap();
let mut data: Vec<u8> = Vec::new();
surface.write_to_png(&mut data).unwrap();