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 fb631f9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 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
49 changes: 33 additions & 16 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,14 +122,31 @@ impl Generator {

let code = code?;

let min_height = self.height.or(self.min_height).unwrap_or_else(|| {
if matches!(self.format, Format::Unicode) {
20
} else {
360
}
});
let min_width = self.width.or(self.min_width).unwrap_or(min_height);
let max_height = self
.height
.or(self.max_height)
.unwrap_or_default()
.max(min_height);
let max_width = self
.width
.or(self.max_width)
.unwrap_or_default()
.max(min_width);

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_dimensions(min_width, min_height)
.max_dimensions(max_width, max_height)
.dark_color(svg::Color(
self.dark_color.as_deref().unwrap_or("#000"),
))
Expand All @@ -140,10 +163,8 @@ impl Generator {
Format::Png => {
let image = code
.render::<Luma<u8>>()
.min_dimensions(
self.min_width.unwrap_or(240),
self.min_height.unwrap_or(240),
)
.min_dimensions(min_width, min_height)
.max_dimensions(max_width, max_height)
.quiet_zone(self.quiet_zone.unwrap_or(true))
.build();
let bytes = image.as_bytes();
Expand All @@ -158,10 +179,8 @@ impl Generator {
Format::Jpeg => {
let image = code
.render::<Luma<u8>>()
.min_dimensions(
self.min_width.unwrap_or(240),
self.min_height.unwrap_or(240),
)
.min_dimensions(min_width, min_height)
.max_dimensions(max_width, max_height)
.quiet_zone(self.quiet_zone.unwrap_or(true))
.build();
let bytes = image.as_bytes();
Expand All @@ -187,10 +206,8 @@ impl Generator {
Format::Unicode => {
let mut bytes = code
.render::<unicode::Dense1x2>()
.min_dimensions(
self.min_width.unwrap_or(20),
self.min_height.unwrap_or(20),
)
.min_dimensions(min_width, min_height)
.max_dimensions(max_width, max_height)
.dark_color(unicode::Dense1x2::Dark)
.light_color(unicode::Dense1x2::Light)
.quiet_zone(self.quiet_zone.unwrap_or(true))
Expand Down

0 comments on commit fb631f9

Please sign in to comment.