ptouch-label/ptouch-print のオプション整理と trim-right 対応を反映

CLIヘルプ文言を簡潔化しつつ、右余白トリム機能と関連API・ドキュメント更新をまとめて取り込み、PNG/SVG/テンプレート経路での利用体験を揃える。

Made-with: Cursor
This commit is contained in:
knb
2026-04-16 19:09:37 +09:00
parent 32ab12f661
commit 094f183994
13 changed files with 272 additions and 24 deletions

View File

@@ -70,6 +70,7 @@ module Libptouch
opts_hash = default_cli_opts
build_cli_parser(opts_hash).parse!(argv)
return nil unless threshold_option_ok?(opts_hash)
return nil unless trim_right_option_ok?(opts_hash)
return nil unless usb_pid_option_ok?(opts_hash)
opts_hash.delete(:usb_pid_invalid)
@@ -85,6 +86,7 @@ module Libptouch
usb_pid: nil,
usb_pid_invalid: false,
dry_run: false,
trim_right: nil,
media_info: false,
status: false,
version: false,
@@ -114,6 +116,15 @@ module Libptouch
false
end
def trim_right_option_ok?(opts_hash)
v = opts_hash[:trim_right]
return true if v.nil? || v == :auto
return true if v.is_a?(Integer) && v >= 0
warn "--trim-right must be omitted, or >= 0"
false
end
def usb_pid_option_ok?(opts_hash)
return false if opts_hash[:usb_pid_invalid]
return true if opts_hash[:usb_pid].nil?
@@ -131,17 +142,21 @@ module Libptouch
p.on("--template PATH", "差込用 SVG テンプレート") { |v| opts_hash[:template] = v }
p.on("--data PATH", "差込データ JSON/YAML ファイル") { |v| opts_hash[:data] = v }
p.on("-t", "--threshold N", Integer,
"二値化しきい値 0255既定 #{Libptouch::PNG_DEFAULT_THRESHOLD}、PNG/SVG") do |v|
"しきい値 0255既定 #{Libptouch::PNG_DEFAULT_THRESHOLD}、PNG/SVG") do |v|
opts_hash[:threshold] = v
end
p.on("-p", "--pid PID", pid_option_description) do |v|
apply_usb_pid_option(opts_hash, v)
end
p.on("-n", "--dry-run", "読み込みと検証のみUSB なし)") { opts_hash[:dry_run] = true }
p.on("-M", "--media-info", "現在テープの幅(mm)・DPI・最小余白(mm)を JSON で表示して終了") do
p.on("--trim-right[=DOTS]", Integer,
"右側空白を削減。DOTS省略時は左余白失敗時 0") do |v|
opts_hash[:trim_right] = v.nil? ? :auto : v
end
p.on("-M", "--media-info", "現在テープ情報(幅/DPI/余白)を JSON で表示して終了") do
opts_hash[:media_info] = true
end
p.on("-S", "--status", "USB プリンタのステータスを JSON で表示して終了") do
p.on("-S", "--status", "ステータスを JSON で表示して終了") do
opts_hash[:status] = true
end
p.on("-V", "--version", "バージョンを表示して終了") { opts_hash[:version] = true }
@@ -153,15 +168,16 @@ module Libptouch
<<~BANNER
Usage: ptouch-label [options]
PNG/SVG -w/-H
--template/--data SVG
SVG USB
PNG/SVG 使-w/-H
--template/--data SVG
SVG USB
--trim-right[=DOTS]
--status / --media-info -f
BANNER
end
def warn_unused_file_options(opts)
return unless opts[:file] || opts[:template] || opts[:data] || opts[:dry_run] || !opts[:threshold].nil?
return unless opts[:file] || opts[:template] || opts[:data] || opts[:dry_run] || !opts[:trim_right].nil? || !opts[:threshold].nil?
warn "warning: options other than --status are ignored"
end
@@ -186,17 +202,21 @@ module Libptouch
parser.on("--template PATH", "差込用 SVG テンプレート") { |v| opts_hash[:template] = v }
parser.on("--data PATH", "差込データ JSON/YAML ファイル") { |v| opts_hash[:data] = v }
parser.on("-t", "--threshold N", Integer,
"二値化しきい値 0255既定 #{Libptouch::PNG_DEFAULT_THRESHOLD}、PNG/SVG") do |v|
"しきい値 0255既定 #{Libptouch::PNG_DEFAULT_THRESHOLD}、PNG/SVG") do |v|
opts_hash[:threshold] = v
end
parser.on("-p", "--pid PID", pid_option_description) do |v|
apply_usb_pid_option(opts_hash, v)
end
parser.on("-n", "--dry-run", "読み込みと検証のみUSB なし)") { opts_hash[:dry_run] = true }
parser.on("-M", "--media-info", "現在テープの幅(mm)・DPI・最小余白(mm)を JSON で表示して終了") do
parser.on("--trim-right[=DOTS]", Integer,
"右側空白を削減。DOTS省略時は左余白失敗時 0") do |v|
opts_hash[:trim_right] = v.nil? ? :auto : v
end
parser.on("-M", "--media-info", "現在テープ情報(幅/DPI/余白)を JSON で表示して終了") do
opts_hash[:media_info] = true
end
parser.on("-S", "--status", "USB プリンタのステータスを JSON で表示して終了") do
parser.on("-S", "--status", "ステータスを JSON で表示して終了") do
opts_hash[:status] = true
end
parser.on("-V", "--version", "バージョンを表示して終了") { opts_hash[:version] = true }
@@ -319,9 +339,11 @@ module Libptouch
ctx = nil
begin
ctx = Libptouch::Context.new
usb_opened = false
threshold = opts[:threshold]
data, width, height = if kind == :svg
open_usb_for_opts(ctx, opts)
usb_opened = true
if threshold.nil?
ctx.svg_file_to_raster_fit_current_tape(path)
else
@@ -333,6 +355,16 @@ module Libptouch
ctx.png_file_to_raster(path, threshold: threshold)
end
unless opts[:trim_right].nil?
trim_pad, usb_opened = resolve_trim_right_pad_dots(ctx, opts, usb_opened)
data, width, height = ctx.trim_right_blank_columns(
data,
width_dots: width,
height_dots: height,
right_padding_dots: trim_pad
)
end
ctx.check_raster(data, width_dots: width, height_dots: height)
if opts[:dry_run]
@@ -340,7 +372,7 @@ module Libptouch
return 0
end
open_usb_for_opts(ctx, opts) if kind == :png
open_usb_for_opts(ctx, opts) if kind == :png && !usb_opened
ctx.print_raster(data, width_dots: width, height_dots: height)
0
rescue Libptouch::Error => e
@@ -351,6 +383,22 @@ module Libptouch
end
end
def resolve_trim_right_pad_dots(ctx, opts, usb_opened)
trim = opts[:trim_right]
return [trim, usb_opened] if trim.is_a?(Integer)
begin
unless usb_opened
open_usb_for_opts(ctx, opts)
usb_opened = true
end
info = ctx.current_media_info
[Integer(info[:left_margin_dots] || 0), usb_opened]
rescue Libptouch::Error
[0, usb_opened]
end
end
def usage_error(msg)
warn msg
warn "(try ptouch-label --help)"