印刷の任意倍率を自動計算します。
イレギュラーな印刷範囲から、A3・A4用紙に収まる倍率を算出し、クリップボードにコピーします。
(AIで作成)
使い方
1. 外変を実行
2. 基準点1(左上)をクリック(任意の点)
3. 基準点2(右下)をクリック(任意の点)
4. 用紙サイズを入力(A3=3、A4=4)
5. 倍率を印刷ダイアログの任意倍率に貼り付けて使用
印刷倍率.bat
@echo off
goto do1
REM #jww
REM #cd
REM #hc 基準点1(左上)
REM #1
REM #hc 基準点2(右下)
REM #2
REM #c 用紙サイズを入力(A3=3、A4=4):/4/V
REM #e
:do1
setlocal
cd /d "%~dp0"
if exist "印刷倍率_clip.txt" del "印刷倍率_clip.txt"
set RUBY=
if exist C:\Ruby34-x64\bin\ruby.exe set RUBY=C:\Ruby34-x64\bin\ruby.exe
if "%RUBY%"=="" if exist C:\Ruby33-x64\bin\ruby.exe set RUBY=C:\Ruby33-x64\bin\ruby.exe
if "%RUBY%"=="" if exist C:\Ruby32-x64\bin\ruby.exe set RUBY=C:\Ruby32-x64\bin\ruby.exe
if "%RUBY%"=="" if exist C:\Ruby31-x64\bin\ruby.exe set RUBY=C:\Ruby31-x64\bin\ruby.exe
if "%RUBY%"=="" set RUBY=ruby
set PAPER=%1
set PAPER=%PAPER: =%
if "%PAPER%"=="3" goto run
if "%PAPER%"=="4" goto run
echo 用紙サイズは 3 または 4 を入力してください。
exit /b 1
:run
"%RUBY%" "%~dp0印刷倍率.rb" %PAPER%
if errorlevel 1 exit /b 1
if exist "印刷倍率_clip.txt" (
powershell -NoProfile -Command "Set-Clipboard -Value ((Get-Content -LiteralPath '印刷倍率_clip.txt' -Raw).TrimEnd())"
del "印刷倍率_clip.txt"
)
endlocal
+++++++++++++++++++++++++++++++++++++++++++++++
印刷倍率.rb
# encoding: cp932
# 印刷倍率.rb
# ARGV[0]: "3" or "4"
MARGIN = 20.0
PAPER = {
"4" => { w: 297.0, h: 210.0, name: "A4" },
"3" => { w: 420.0, h: 297.0, name: "A3" }
}
paper_key = ARGV[0].to_s.strip
unless PAPER.key?(paper_key)
STDERR.puts "invalid paper: #{paper_key}"
exit 1
end
paper = PAPER[paper_key]
jwtemp = "jwc_temp.txt"
unless File.exist?(jwtemp)
STDERR.puts "jwc_temp.txt not found"
exit 1
end
lines = File.open(jwtemp, "r:cp932") { |f| f.readlines }
hp1 = lines.find { |l| l =~ /^hp1\s/ }
hp2 = lines.find { |l| l =~ /^hp2\s/ }
unless hp1 && hp2
STDERR.puts "hp1/hp2 not found"
exit 1
end
x1, y1 = hp1.split[1..2].map(&:to_f)
x2, y2 = hp2.split[1..2].map(&:to_f)
width_zu = (x2 - x1).abs
height_zu = (y1 - y2).abs
hs_vals = []
lines.each do |l|
if l.start_with?("hs ")
hs_vals = l.split[1..].map(&:to_f)
break
end
end
current_lg = 0
lines.each do |l|
if l =~ /^lg([0-9a-fA-F])/i
current_lg = $1.to_i(16)
break
end
end
scale_denom = 1.0
if hs_vals.length > current_lg && hs_vals[current_lg] > 0
scale_denom = hs_vals[current_lg]
end
# 実寸(mm)
width_mm = width_zu / scale_denom
height_mm = height_zu / scale_denom
if width_mm <= 0 || height_mm <= 0
STDERR.puts "width or height <= 0"
exit 1
end
effective_w = paper[:w] - MARGIN
effective_h = paper[:h] - MARGIN
if effective_w <= 0 || effective_h <= 0
STDERR.puts "effective paper size <= 0"
exit 1
end
# 長辺・短辺判定
long_mm = [width_mm, height_mm].max
short_mm = [width_mm, height_mm].min
ratio_by_long = effective_w / long_mm * 100.0
ratio_by_short = effective_h / short_mm * 100.0
ratio_raw = [ratio_by_long, ratio_by_short].min
# ★小数点2位で切り捨て
ratio = (ratio_raw * 100).floor / 100.0
if ratio <= 0
STDERR.puts "ratio <= 0"
exit 1
end
# 文字列化(不要な .0 を消す)
ratio_str = if ratio % 1 == 0
ratio.to_i.to_s
else
('%.2f' % ratio)
end
File.open("印刷倍率_clip.txt", "wb") { |f| f.write(ratio_str) }
new_lines = lines.reject { |l| l.chomp == "hq" }
File.open(jwtemp, "w:cp932") { |f| f.write(new_lines.join) }