Skip to content

Commit

Permalink
Add more dimension options
Browse files Browse the repository at this point in the history
Closes: #13
  • Loading branch information
sayanarijit committed Sep 25, 2021
1 parent 476a86a commit ee7ac3b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ PARAMETERS:
image/jpeg
Default: application/octet-stream

X-QR-Width Specify the default width

X-QR-Height Specify the default height

X-QR-Min-Width Specify the minimum width

X-QR-Min-Height Specify the minimun height

X-QR-Max-Width Specify the maximum width

X-QR-Max-Height Specify the maximum height

X-QR-Dark-Color Specify the dark color (hex)
Format: rrggbb

Expand Down
30 changes: 30 additions & 0 deletions axum-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ where
gen.format = Format::from(&val);
}

if let Some(val) =
get_first_header_value(req, HeaderName::from_static("x-qr-width"))
{
gen.width =
val.parse().map(Some).map_err(|_| StatusCode::BAD_REQUEST)?;
}

if let Some(val) =
get_first_header_value(req, HeaderName::from_static("x-qr-height"))
{
gen.height =
val.parse().map(Some).map_err(|_| StatusCode::BAD_REQUEST)?;
}

if let Some(val) = get_first_header_value(
req,
HeaderName::from_static("x-qr-min-width"),
Expand All @@ -94,6 +108,22 @@ where
val.parse().map(Some).map_err(|_| StatusCode::BAD_REQUEST)?;
}

if let Some(val) = get_first_header_value(
req,
HeaderName::from_static("x-qr-max-width"),
) {
gen.max_width =
val.parse().map(Some).map_err(|_| StatusCode::BAD_REQUEST)?;
}

if let Some(val) = get_first_header_value(
req,
HeaderName::from_static("x-qr-max-height"),
) {
gen.max_height =
val.parse().map(Some).map_err(|_| StatusCode::BAD_REQUEST)?;
}

if let Some(val) = get_first_header_value(
req,
HeaderName::from_static("x-qr-dark-color"),
Expand Down
16 changes: 16 additions & 0 deletions cf-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ fn generator_from_headers(headers: &Headers) -> Result<Generator> {
gen.format = Format::from(&val);
}

if let Some(val) = get_first_header_value(headers, "x-qr-width") {
gen.width = val.parse().map(Some).map_err(|_| "Bad Request")?;
}

if let Some(val) = get_first_header_value(headers, "x-qr-height") {
gen.height = val.parse().map(Some).map_err(|_| "Bad Request")?;
}

if let Some(val) = get_first_header_value(headers, "x-qr-min-width") {
gen.min_width = val.parse().map(Some).map_err(|_| "Bad Request")?;
}
Expand All @@ -36,6 +44,14 @@ fn generator_from_headers(headers: &Headers) -> Result<Generator> {
gen.min_height = val.parse().map(Some).map_err(|_| "Bad Request")?;
}

if let Some(val) = get_first_header_value(headers, "x-qr-max-width") {
gen.max_width = val.parse().map(Some).map_err(|_| "Bad Request")?;
}

if let Some(val) = get_first_header_value(headers, "x-qr-max-height") {
gen.max_height = val.parse().map(Some).map_err(|_| "Bad Request")?;
}

if let Some(val) = get_first_header_value(headers, "x-qr-dark-color") {
gen.dark_color = Some(format!("#{}", val));
}
Expand Down
50 changes: 42 additions & 8 deletions libs/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ impl From<&str> for VersionType {
pub struct Generator {
pub format: Format,

pub width: Option<u32>,
pub height: Option<u32>,

pub min_width: Option<u32>,
pub min_height: Option<u32>,

pub max_width: Option<u32>,
pub max_height: Option<u32>,

pub dark_color: Option<String>,
pub light_color: Option<String>,

Expand Down Expand Up @@ -116,13 +122,29 @@ impl Generator {

let code = code?;

let min_height = self.height.or(self.min_height);
let min_width = self.width.or(self.min_width);

let max_height = self.height.or(self.max_height);
let max_width = self.width.or(self.max_width);

let web_width = 240;
let web_height = 240;

let term_width = 20;
let term_height = 20;

let image = match self.format {
Format::Svg | Format::Html => {
let mut bytes = code
.render()
.min_dimensions(
self.min_width.unwrap_or(240),
self.min_height.unwrap_or(240),
min_width.unwrap_or(web_width),
min_height.unwrap_or(web_height),
)
.max_dimensions(
max_width.unwrap_or(web_width),
max_height.unwrap_or(web_height),
)
.dark_color(svg::Color(
self.dark_color.as_deref().unwrap_or("#000"),
Expand All @@ -141,8 +163,12 @@ impl Generator {
let image = code
.render::<Luma<u8>>()
.min_dimensions(
self.min_width.unwrap_or(240),
self.min_height.unwrap_or(240),
min_width.unwrap_or(web_width),
min_height.unwrap_or(web_height),
)
.max_dimensions(
max_width.unwrap_or(web_width),
max_height.unwrap_or(web_height),
)
.quiet_zone(self.quiet_zone.unwrap_or(true))
.build();
Expand All @@ -159,8 +185,12 @@ impl Generator {
let image = code
.render::<Luma<u8>>()
.min_dimensions(
self.min_width.unwrap_or(240),
self.min_height.unwrap_or(240),
min_width.unwrap_or(web_width),
min_height.unwrap_or(web_height),
)
.max_dimensions(
max_width.unwrap_or(web_width),
max_height.unwrap_or(web_height),
)
.quiet_zone(self.quiet_zone.unwrap_or(true))
.build();
Expand Down Expand Up @@ -188,8 +218,12 @@ impl Generator {
let mut bytes = code
.render::<unicode::Dense1x2>()
.min_dimensions(
self.min_width.unwrap_or(20),
self.min_height.unwrap_or(20),
min_width.unwrap_or(term_width),
min_height.unwrap_or(term_height),
)
.max_dimensions(
max_width.unwrap_or(term_width),
max_height.unwrap_or(term_height),
)
.dark_color(unicode::Dense1x2::Dark)
.light_color(unicode::Dense1x2::Light)
Expand Down

0 comments on commit ee7ac3b

Please sign in to comment.