Skip to content

Commit

Permalink
Add option to encode command
Browse files Browse the repository at this point in the history
Add `--size` option to set the module size in pixels.
  • Loading branch information
sorairolake committed Dec 13, 2023
1 parent 116aff2 commit c0003eb
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 20,8 @@ project adheres to https://semver.org/[Semantic Versioning].

* Add methods to optimize the output image to the documents
({pull-request-url}/301[#301])
* Add `--size` option to set the module size in pixels
({pull-request-url}/304[#304])

== {compare-url}/v0.8.8\...v0.8.9[0.8.9] - 2023-12-07

Expand Down
6 changes: 5 additions & 1 deletion docs/man/man1/qrtool-encode.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@

= qrtool-encode(1)
// Specify in UTC.
:docdate: 2023-12-04
:docdate: 2023-12-13
:doctype: manpage
ifdef::revnumber[:mansource: qrtool {revnumber}]
:manmanual: General Commands Manual
Expand Down Expand Up @@ -44,6 44,10 @@ _STRING_::

Read input data from a file. This option conflicts with _STRING_.

*-s*, *--size* _NUMBER_::

The module size in pixels. Default is 8.

*-l*, *--error-correction-level* _LEVEL_::

Error correction level.
Expand Down
18 changes: 14 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 65,18 @@ pub fn run() -> anyhow::Result<()> {
eprintln!("Level: {:?}", metadata.error_correction_level());
}

let module_size = arg.size.get();
match arg.output_format {
format @ (OutputFormat::Svg | OutputFormat::Terminal) => {
let string = if format == OutputFormat::Svg {
encode::to_svg(&code, arg.margin, &(arg.foreground, arg.background))
encode::to_svg(
&code,
arg.margin,
&(arg.foreground, arg.background),
module_size,
)
} else {
encode::to_terminal(&code, arg.margin)
encode::to_terminal(&code, arg.margin, module_size)
};

if let Some(file) = arg.output {
Expand All @@ -82,8 88,12 @@ pub fn run() -> anyhow::Result<()> {
}
}
OutputFormat::Png => {
let image =
encode::to_image(&code, arg.margin, &(arg.foreground, arg.background));
let image = encode::to_image(
&code,
arg.margin,
&(arg.foreground, arg.background),
module_size,
);

if let Some(file) = arg.output {
image
Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@

use std::{
io::{self, Write},
num::NonZeroU32,
path::PathBuf,
};

Expand Down Expand Up @@ -82,6 83,10 @@ pub struct Encode {
)]
pub read_from: Option<PathBuf>,

/// The module size in pixels.
#[arg(short, long, default_value("8"), value_name("NUMBER"))]
pub size: NonZeroU32,

/// Error correction level.
#[arg(
short('l'),
Expand Down
14 changes: 11 additions & 3 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 52,34 @@ pub fn push_data_for_selected_mode(
}

/// Renders the QR code into an image.
pub fn to_svg(code: &QrCode, margin: u32, colors: &(Color, Color)) -> String {
pub fn to_svg(code: &QrCode, margin: u32, colors: &(Color, Color), module_size: u32) -> String {
Renderer::<svg::Color<'_>>::new(&code.to_colors(), code.width(), margin)
.dark_color(svg::Color(&colors.0.to_hex_string()))
.light_color(svg::Color(&colors.1.to_hex_string()))
.module_dimensions(module_size, module_size)
.build()
}

/// Renders the QR code into the terminal as UTF-8 string.
pub fn to_terminal(code: &QrCode, margin: u32) -> String {
pub fn to_terminal(code: &QrCode, margin: u32, module_size: u32) -> String {
Renderer::<unicode::Dense1x2>::new(&code.to_colors(), code.width(), margin)
.dark_color(unicode::Dense1x2::Light)
.light_color(unicode::Dense1x2::Dark)
.module_dimensions(module_size, module_size)
.build()
}

/// Renders the QR code into an image.
pub fn to_image(code: &QrCode, margin: u32, colors: &(Color, Color)) -> DynamicImage {
pub fn to_image(
code: &QrCode,
margin: u32,
colors: &(Color, Color),
module_size: u32,
) -> DynamicImage {
let image = Renderer::<Rgba<u8>>::new(&code.to_colors(), code.width(), margin)
.dark_color(Rgba::from(colors.0.to_rgba8()))
.light_color(Rgba::from(colors.1.to_rgba8()))
.module_dimensions(module_size, module_size)
.build();
DynamicImage::ImageRgba8(image)
}
Expand Down
1 change: 1 addition & 0 deletions tests/data/colored/rgb.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/data/colored/rgb.svg.license
Original file line number Diff line number Diff line change
@@ -0,0 1,3 @@
SPDX-FileCopyrightText: 2023 Shun Sakai

SPDX-License-Identifier: Apache-2.0 OR MIT
1 change: 1 addition & 0 deletions tests/data/colored/rgba.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/data/colored/rgba.svg.license
Original file line number Diff line number Diff line change
@@ -0,0 1,3 @@
SPDX-FileCopyrightText: 2023 Shun Sakai

SPDX-License-Identifier: Apache-2.0 OR MIT
1 change: 1 addition & 0 deletions tests/data/encode/encode.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/data/encode/encode.svg.license
Original file line number Diff line number Diff line change
@@ -0,0 1,3 @@
SPDX-FileCopyrightText: 2023 Shun Sakai

SPDX-License-Identifier: Apache-2.0 OR MIT
Loading

0 comments on commit c0003eb

Please sign in to comment.