feat: stroke_color options

This commit is contained in:
Compositr 2024-11-02 18:04:33 +11:00
parent aeed04688b
commit 4968a907dd

View file

@ -54,9 +54,11 @@ fn main() {
GET /icons/[icon].svg - Get an icon in SVG format (equivalent to fetching them directly from Lucide)
GET /icons/[icon].png - Get an icon rendered in PNG format
Query parameters:
-- note: Make sure to URL encode the query parameters: particularly special characters such as # (e.g. %23). The server only accepts a small subset of characters deemed necessary for the query parameters.
scale - integer - Scale the icon (default: 1, min: 1, max: 100)
padding - integer - Add padding (px) around the icon (default: 0, min: 0, max: 100)
background - hex - Set the background color in hex w/o the hashtag (e.g. FFFFFF) (default: transparent)
background - hex - Set the background color in hex WITHOUT the hashtag (e.g. FFFFFF) (default: transparent)
stroke_color - string - Set the stroke color for the SVG icon, any CSS color WITH the hashtag (default: currentColor)
discord_compatibility - boolean - Set padding to 8px for Discord compatibility. Typically for use in embed author icons as these have a circle clip applied by Discord. Overrides padding if set. (default: false)
cache_key - string - Technically this can have any name. The server does not interpret this at all. Useful for cache busting. (default: none)
Example:
@ -123,7 +125,7 @@ fn main() {
return Response::new(req, 404, Body::Static("Icon Not Found"));
}
let icon = match icons.get_icon(icon_name) {
let mut icon_string = match icons.get_icon_string(icon_name) {
Some(icon) => icon,
None => {
error("Failed to read icon");
@ -131,14 +133,6 @@ fn main() {
},
};
let tree = match usvg::Tree::from_data(&icon, &usvg::Options::default()) {
Ok(tree) => tree,
Err(e) => {
error(&format!("Failed to load icon into tree: {}", e));
return Response::new(req, 500, Body::Static("Failed to load icon into tree"))
}
};
// Options handling
let mut scale = 1;
match req.url.query.get("scale") {
@ -186,7 +180,7 @@ fn main() {
match u32::from_str_radix(&background_str, 16) {
Ok(background_val) => {
if background_val > 0xFFFFFF {
return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color 000000 - FFFFFF"));
return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color #000000 - #FFFFFF"));
}
let red = ((background_val >> 16) & 0xFF) as u8;
let green = ((background_val >> 8) & 0xFF) as u8;
@ -199,6 +193,24 @@ fn main() {
None => {}
}
let mut svg_stroke = "currentColor";
match req.url.query.get("stroke_color") {
Some(svg_stroke_str) => svg_stroke = svg_stroke_str,
None => {}
}
icon_string = icon_string.replace("currentColor", &svg_stroke);
// Rendering
let tree = match usvg::Tree::from_str(&icon_string, &usvg::Options::default()) {
Ok(tree) => tree,
Err(e) => {
error(&format!("Failed to load icon into tree: {}", e));
return Response::new(req, 500, Body::Static("Failed to load icon into tree"))
}
};
let length = 24 * scale + padding;
let mut pixmap = match Pixmap::new(length, length) {