Skip to content

Commit

Permalink
Fix width and height globally for the svg
Browse files Browse the repository at this point in the history
  • Loading branch information
AitorAstorga committed Feb 7, 2025
1 parent c834c36 commit c9980f2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ async fn svg_counter(

// Increment the counter
let count = counters.increment(name);

// Get width and height
let width = options.clone().unwrap_or_default().width.unwrap_or(150);
let height = options.clone().unwrap_or_default().height.unwrap_or(20);

// Generate the SVG
let svg = svg_generator::generate_svg(&label, count, &css);
let svg = svg_generator::generate_svg(&label, count, &css, width, height);

(ContentType::new("image", "svg+xml"), svg)
}
Expand Down
2 changes: 1 addition & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct CounterSetRequest {
}

/// Query parameters for the SVG endpoint. Derives `FromForm` so Rocket can parse query parameters into this struct.
#[derive(FromForm, Clone)]
#[derive(FromForm, Clone, Default)]
pub struct SvgOptions {
pub label: Option<String>,
pub style: Option<String>,
Expand Down
6 changes: 4 additions & 2 deletions src/svg_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::models::SvgOptions;
/// * `label` - The label to display on the left side.
/// * `count` - The counter value to display on the right side.
/// * `css` - The CSS to embed in the SVG.
pub fn generate_svg(label: &str, count: u64, css: &str) -> String {
pub fn generate_svg(label: &str, count: u64, css: &str, width: u32, height: u32) -> String {
format!(
r##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" class="svg-counter">
<svg width="{width}" height="{height}" xmlns="http://www.w3.org/2000/svg" class="svg-counter">
<style type="text/css"><![CDATA[
{css}
]]></style>
Expand All @@ -32,6 +32,8 @@ r##"<?xml version="1.0" encoding="UTF-8"?>
<text class="count">{count}</text>
</g>
</svg>"##,
width = width,
height = height,
css = css,
label = label,
count = count
Expand Down

0 comments on commit c9980f2

Please sign in to comment.