ptouch-label/ptouch-print のオプション整理と trim-right 対応を反映
CLIヘルプ文言を簡潔化しつつ、右余白トリム機能と関連API・ドキュメント更新をまとめて取り込み、PNG/SVG/テンプレート経路での利用体験を揃える。 Made-with: Cursor
This commit is contained in:
@@ -5,5 +5,5 @@ source "https://rubygems.org"
|
||||
gemspec
|
||||
|
||||
group :development do
|
||||
gem "rubocop", "~> 1.69", require: false
|
||||
gem "rubocop", "~> 1.86", require: false
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@ PATH
|
||||
specs:
|
||||
libptouch (1.0.0)
|
||||
ffi (~> 1.15)
|
||||
rexml
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
@@ -30,6 +31,7 @@ GEM
|
||||
racc (1.8.1)
|
||||
rainbow (3.1.1)
|
||||
regexp_parser (2.12.0)
|
||||
rexml (3.4.4)
|
||||
rubocop (1.86.1)
|
||||
json (~> 2.3)
|
||||
language_server-protocol (~> 3.17.0.2)
|
||||
@@ -64,7 +66,7 @@ PLATFORMS
|
||||
|
||||
DEPENDENCIES
|
||||
libptouch!
|
||||
rubocop (~> 1.69)
|
||||
rubocop (~> 1.86)
|
||||
|
||||
CHECKSUMS
|
||||
ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383
|
||||
@@ -89,6 +91,7 @@ CHECKSUMS
|
||||
racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f
|
||||
rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
|
||||
regexp_parser (2.12.0) sha256=35a916a1d63190ab5c9009457136ae5f3c0c7512d60291d0d1378ba18ce08ebb
|
||||
rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142
|
||||
rubocop (1.86.1) sha256=44415f3f01d01a21e01132248d2fd0867572475b566ca188a0a42133a08d4531
|
||||
rubocop-ast (1.49.1) sha256=4412f3ee70f6fe4546cc489548e0f6fcf76cafcfa80fa03af67098ffed755035
|
||||
ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33
|
||||
|
||||
@@ -39,6 +39,7 @@ SVG は現在装着テープの印字可能幅に合わせて自動拡大・縮
|
||||
|
||||
オプションは C 側に合わせ、**`-p` / `--pid`** で USB 製品 ID(16 進可)を指定できます。省略時は PT-P900W(`Libptouch::USB_PID_PTP900W` = `0x2085`)。例: PT-P750W `0x2062`、PT-P710BT `0x20af`(`libptouch.h` / `Libptouch` 定数と同じ)。
|
||||
また、`--template`(SVG)と `--data`(JSON/YAML)を使うと `data-field` 属性をキーにした差込印刷が可能です。
|
||||
`--trim-right[=DOTS]` を付けると、libptouch 側の共通処理でラベル右側の空白ドット列を削減します。`DOTS` 省略時は左余白ドット数を使い、取得失敗時は `0` にフォールバックします。
|
||||
|
||||
開発ツリーからそのまま試す例:
|
||||
|
||||
@@ -46,6 +47,8 @@ SVG は現在装着テープの印字可能幅に合わせて自動拡大・縮
|
||||
bundle exec ruby -I lib exe/ptouch-label --help
|
||||
bundle exec ruby -I lib exe/ptouch-label -n -f ../samples/your.png
|
||||
bundle exec ruby -I lib exe/ptouch-label -n -f ../samples/your.svg
|
||||
bundle exec ruby -I lib exe/ptouch-label -n -f ../samples/your.svg --trim-right
|
||||
bundle exec ruby -I lib exe/ptouch-label -n -f ../samples/your.svg --trim-right=20
|
||||
bundle exec ruby -I lib exe/ptouch-label -n --template ../samples/your_template.svg --data ../samples/your_data.yml
|
||||
bundle exec ruby -I lib exe/ptouch-label --media-info
|
||||
bundle exec ruby -I lib exe/ptouch-label --status -p 0x2062
|
||||
@@ -96,7 +99,7 @@ ctx.dispose
|
||||
|
||||
## API の範囲
|
||||
|
||||
- 実行ファイル `ptouch-label`(互換: `ptouch-print-png`)… PNG/SVG(`-f`, `-t`, `-p`, `-n`, `-M`, `-S`, `-V`, `-h`, `--template`, `--data`)。`-M` は現在テープ情報(幅 mm・DPI 等)を JSON 出力、`-S` はステータス JSON 出力
|
||||
- 実行ファイル `ptouch-label`(互換: `ptouch-print-png`)… PNG/SVG(`-f`, `-t`, `-p`, `-n`, `-M`, `-S`, `-V`, `-h`, `--template`, `--data`, `--trim-right[=DOTS]`)。`-M` は現在テープ情報(幅 mm・DPI 等)を JSON 出力、`-S` はステータス JSON 出力、`--trim-right` は右側空白列を削減
|
||||
- `Libptouch::Context` … `open_usb` / `open_usb_vid_pid` / `close` / `dispose`
|
||||
- `check_raster` / `print_raster` / `png_file_to_raster` / `svg_file_to_raster_fit_current_tape` / `status_bytes` / `status_hash` / `current_media_info`
|
||||
- `current_media_info` には `print_dpi` / `feed_dpi` / `tape_width_mm` / `printable_height_dots` / `min_feed_mm` などを含む(テープ幅方向は `printable_height_dots`)
|
||||
|
||||
0
ruby/exe/ptouch-label
Normal file → Executable file
0
ruby/exe/ptouch-label
Normal file → Executable file
@@ -59,6 +59,8 @@ module Libptouch
|
||||
attach_function :libptouch_close, [:pointer], :void
|
||||
attach_function :libptouch_check_raster, %i[pointer pointer size_t pointer], :int
|
||||
attach_function :libptouch_get_current_media_info, %i[pointer pointer], :int
|
||||
attach_function :libptouch_trim_right_blank_columns,
|
||||
%i[pointer pointer size_t pointer uint16 pointer pointer pointer], :int
|
||||
attach_function :libptouch_print_raster, %i[pointer pointer size_t pointer], :int
|
||||
attach_function :libptouch_png_file_to_raster,
|
||||
%i[pointer string pointer pointer pointer pointer], :int
|
||||
|
||||
@@ -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,
|
||||
"二値化しきい値 0–255(既定 #{Libptouch::PNG_DEFAULT_THRESHOLD}、PNG/SVG)") do |v|
|
||||
"しきい値 0–255(既定 #{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,
|
||||
"二値化しきい値 0–255(既定 #{Libptouch::PNG_DEFAULT_THRESHOLD}、PNG/SVG)") do |v|
|
||||
"しきい値 0–255(既定 #{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)"
|
||||
|
||||
@@ -115,6 +115,29 @@ module Libptouch
|
||||
[bytes, out_params[:width_dots], out_params[:height_dots]]
|
||||
end
|
||||
|
||||
def trim_right_blank_columns(data, width_dots:, height_dots:, margin_mm: 0, right_padding_dots: 0)
|
||||
in_params = Binding::RasterParams.new
|
||||
in_params[:width_dots] = width_dots
|
||||
in_params[:height_dots] = height_dots
|
||||
in_params[:margin_mm] = margin_mm
|
||||
in_buf = FFI::MemoryPointer.new(:uint8, data.bytesize)
|
||||
in_buf.put_bytes(0, data)
|
||||
out_pp = FFI::MemoryPointer.new(:pointer)
|
||||
out_len = FFI::MemoryPointer.new(:size_t)
|
||||
out_params = Binding::RasterParams.new
|
||||
raise_on_error(Binding.libptouch_trim_right_blank_columns(
|
||||
@native, in_buf, data.bytesize, in_params.pointer,
|
||||
right_padding_dots, out_pp, out_len, out_params.pointer
|
||||
))
|
||||
raw = out_pp.read_pointer
|
||||
raise Libptouch::Error.new(OK, "null raster from trim_right_blank_columns") if raw.null?
|
||||
|
||||
len = out_len.get(:size_t, 0)
|
||||
bytes = raw.read_bytes(len)
|
||||
Binding.libptouch_free_raster(raw)
|
||||
[bytes, out_params[:width_dots], out_params[:height_dots]]
|
||||
end
|
||||
|
||||
def status_bytes
|
||||
buf = FFI::MemoryPointer.new(:uint8, STATUS_LENGTH)
|
||||
raise_on_error(Binding.libptouch_get_status(@native, buf))
|
||||
|
||||
@@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
|
||||
spec.require_paths = ["lib"]
|
||||
|
||||
spec.add_dependency "ffi", "~> 1.15"
|
||||
spec.add_dependency "rexml"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user