Skip to content

Commit

Permalink
Change --size and --margin of encode command
Browse files Browse the repository at this point in the history
Change these to optional arguments and remove the default values.
  • Loading branch information
sorairolake committed Dec 17, 2023
1 parent 2539aba commit 2f33684
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 312 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 14,13 @@ All notable changes to this project will be documented in this file.
The format is based on https://keepachangelog.com/[Keep a Changelog], and this
project adheres to https://semver.org/[Semantic Versioning].

== {compare-url}/v0.9.0\...HEAD[Unreleased]

=== Changed

* Change `--size` and `--margin` of `encode` command to optional arguments and
remove the default values ({pull-request-url}/311[#311])

== {compare-url}/v0.8.9\...v0.9.0[0.9.0] - 2023-12-16

=== Added
Expand Down
15 changes: 10 additions & 5 deletions 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-15
:docdate: 2023-12-17
:doctype: manpage
ifdef::revnumber[:mansource: qrtool {revnumber}]
:manmanual: General Commands Manual
Expand Down Expand Up @@ -46,7 46,9 @@ _STRING_::

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

The module size in pixels. Default is 8.
The module size in pixels. If this option is not specified, the module size
is 8 when the output format is PNG or SVG, and 1 when the output format is
UTF-8 string.

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

Expand Down Expand Up @@ -87,7 89,8 @@ _STRING_::

*-m*, *--margin* _NUMBER_::

The width of margin. Default is 4.
The width of margin. If this option is not specified, the margin will be 4
for normal QR code and 2 for Micro QR code.

*-t*, *--type* _FORMAT_::

Expand Down Expand Up @@ -147,11 150,13 @@ _STRING_::

*--foreground* _COLOR_::

Foreground color. _COLOR_ takes a CSS color string. Default is black.
Foreground color. _COLOR_ takes a CSS color string. Colored output is only
available when the output format is PNG or SVG. Default is black.

*--background* _COLOR_::

Background color. _COLOR_ takes a CSS color string. Default is white.
Background color. _COLOR_ takes a CSS color string. Colored output is only
available when the output format is PNG or SVG. Default is white.

*--verbose*::

Expand Down
12 changes: 8 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,7 @@
use std::{
fs,
io::{self, Cursor, Read, Write},
num::NonZeroU32,
str,
};

Expand Down Expand Up @@ -69,18 70,21 @@ pub fn run() -> anyhow::Result<()> {
eprintln!("Level: {:?}", metadata.error_correction_level());
}

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

if let Some(file) = arg.output {
Expand All @@ -94,7 98,7 @@ pub fn run() -> anyhow::Result<()> {
OutputFormat::Png => {
let image = encode::to_image(
&code,
arg.margin,
margin,
&(arg.foreground, arg.background),
module_size,
);
Expand Down
20 changes: 14 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 84,11 @@ 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,
///
/// If this option is not specified, the module size is 8 when the output
/// format is PNG or SVG, and 1 when the output format is UTF-8 string.
#[arg(short, long, value_name("NUMBER"))]
pub size: Option<NonZeroU32>,

/// Error correction level.
#[arg(
Expand Down Expand Up @@ -115,8 118,11 @@ pub struct Encode {
pub symbol_version: Option<i16>,

/// The width of margin.
#[arg(short, long, default_value("4"), value_name("NUMBER"))]
pub margin: u32,
///
/// If this option is not specified, the margin will be 4 for normal QR code
/// and 2 for Micro QR code.
#[arg(short, long, value_name("NUMBER"))]
pub margin: Option<u32>,

/// The format of the output.
#[arg(
Expand Down Expand Up @@ -154,13 160,15 @@ pub struct Encode {

/// Foreground color.
///
/// <COLOR> takes a CSS color string.
/// <COLOR> takes a CSS color string. Colored output is only available when
/// the output format is PNG or SVG.
#[arg(long, default_value("black"), value_name("COLOR"))]
pub foreground: Color,

/// Background color.
///
/// <COLOR> takes a CSS color string.
/// <COLOR> takes a CSS color string. Colored output is only available when
/// the output format is PNG or SVG.
#[arg(long, default_value("white"), value_name("COLOR"))]
pub background: Color,

Expand Down
50 changes: 34 additions & 16 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 52,53 @@ 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), 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()
pub fn to_svg(
code: &QrCode,
margin: u32,
colors: &(Color, Color),
module_size: Option<u32>,
) -> String {
let c = code.to_colors();
let mut renderer = Renderer::<svg::Color<'_>>::new(&c, code.width(), margin);
let (foreground, background) = (colors.0.to_hex_string(), colors.1.to_hex_string());
let mut renderer = renderer
.dark_color(svg::Color(&foreground))
.light_color(svg::Color(&background));
if let Some(size) = module_size {
renderer = renderer.module_dimensions(size, size);
}
renderer.build()
}

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

/// Renders the QR code into an image.
pub fn to_image(
code: &QrCode,
margin: u32,
colors: &(Color, Color),
module_size: u32,
module_size: Option<u32>,
) -> DynamicImage {
let image = Renderer::<Rgba<u8>>::new(&code.to_colors(), code.width(), margin)
let c = code.to_colors();
let mut renderer = Renderer::<Rgba<u8>>::new(&c, code.width(), margin);
let mut renderer = renderer
.dark_color(Rgba::from(colors.0.to_rgba8()))
.light_color(Rgba::from(colors.1.to_rgba8()))
.module_dimensions(module_size, module_size)
.build();
.light_color(Rgba::from(colors.1.to_rgba8()));
if let Some(size) = module_size {
renderer = renderer.module_dimensions(size, size);
}
let image = renderer.build();
DynamicImage::ImageRgba8(image)
}

Expand Down
Loading

0 comments on commit 2f33684

Please sign in to comment.