| 1 |
#!/usr/bin/env ruby |
| 2 |
# frozen_string_literal: true |
| 3 |
|
| 4 |
# ase2def.rb |
| 5 |
# A script to extract CMYK spot colors from Adobe Swatch Exchange (.ase) files |
| 6 |
# and generate a .def file for the spotxcolor LaTeX package. |
| 7 |
|
| 8 |
if ARGV.empty? |
| 9 |
puts "Usage: ruby ase2def.rb <input.ase> [output.def]" |
| 10 |
exit 1 |
| 11 |
end |
| 12 |
|
| 13 |
input_file = ARGV[0] |
| 14 |
output_file = ARGV[1] || input_file.sub(/\.ase$/i, '.def') |
| 15 |
|
| 16 |
# UTF-16BE (Big Endian) の文字列を読み込むヘルパー関数 |
| 17 |
def read_ase_string(f) |
| 18 |
# 文字列の長さ(UTF-16の文字数、null終端を含む) |
| 19 |
len = f.read(2).unpack1('n') |
| 20 |
return "" if len == 0 |
| 21 |
|
| 22 |
# バイト数は文字数の2倍 |
| 23 |
bytes = f.read(len * 2) |
| 24 |
# UTF-16BEからUTF-8に変換し、末尾のnull文字を削除 |
| 25 |
bytes.force_encoding('UTF-16BE').encode('UTF-8').chomp("\x00") |
| 26 |
end |
| 27 |
|
| 28 |
begin |
| 29 |
File.open(input_file, 'rb') do |f| |
| 30 |
# 1. Header Check |
| 31 |
signature = f.read(4) |
| 32 |
unless signature == 'ASEF' |
| 33 |
raise "Invalid signature '#{signature}'. Not a valid ASE file." |
| 34 |
end |
| 35 |
|
| 36 |
version_major, version_minor = f.read(4).unpack('n2') |
| 37 |
block_count = f.read(4).unpack1('N') |
| 38 |
|
| 39 |
puts "ASE Version : #{version_major}.#{version_minor}" |
| 40 |
puts "Total Blocks: #{block_count}" |
| 41 |
|
| 42 |
colors = [] |
| 43 |
|
| 44 |
# 2. Parse Blocks |
| 45 |
block_count.times do |
| 46 |
block_type = f.read(2).unpack1('n') # 0xC001: Group Start, 0xC002: Group End, 0x0001: Color |
| 47 |
block_len = f.read(4).unpack1('N') |
| 48 |
|
| 49 |
case block_type |
| 50 |
when 0xC001 # Group Start |
| 51 |
# 修正箇所: read_acb_string -> read_ase_string |
| 52 |
group_name = read_ase_string(f) |
| 53 |
when 0xC002 # Group End |
| 54 |
# Do nothing |
| 55 |
when 0x0001 # Color Block |
| 56 |
# Read the raw block data |
| 57 |
block_data = f.read(block_len) |
| 58 |
|
| 59 |
# We process the block data manually since we know its length |
| 60 |
require 'stringio' |
| 61 |
b = StringIO.new(block_data) |
| 62 |
|
| 63 |
color_name = read_ase_string(b) |
| 64 |
|
| 65 |
# Color Model (4 chars: "CMYK", "RGB ", "LAB ", "Gray") |
| 66 |
color_model = b.read(4) |
| 67 |
|
| 68 |
# The channel values are stored as 32-bit single-precision floats (Big Endian) |
| 69 |
case color_model |
| 70 |
when "CMYK" |
| 71 |
c, m, y, k = b.read(16).unpack('g4') # 'g' is big-endian single-precision float |
| 72 |
# Format floats to 4 decimal places |
| 73 |
val_str = [c, m, y, k].map { |v| v.round(4) }.join(", ") |
| 74 |
when "RGB " |
| 75 |
r, g, b_val = b.read(12).unpack('g3') |
| 76 |
val_str = [r, g, b_val].map { |v| v.round(4) }.join(", ") |
| 77 |
when "LAB " |
| 78 |
l, a, b_val = b.read(12).unpack('g3') |
| 79 |
val_str = [l, a, b_val].map { |v| v.round(4) }.join(", ") |
| 80 |
when "Gray" |
| 81 |
gray = b.read(4).unpack1('g') |
| 82 |
val_str = gray.round(4).to_s |
| 83 |
else |
| 84 |
# Skip unknown |
| 85 |
next |
| 86 |
end |
| 87 |
|
| 88 |
# Color Type (0=Global, 1=Spot, 2=Normal) |
| 89 |
color_type = b.read(2).unpack1('n') |
| 90 |
|
| 91 |
# Create a safe macro name (remove spaces, hyphens, and asterisks) |
| 92 |
macro_name = color_name.gsub(/[\s\-\*\/]/, '') |
| 93 |
|
| 94 |
colors << { |
| 95 |
macro_name: macro_name, |
| 96 |
pdf_name: color_name, |
| 97 |
model: color_model, |
| 98 |
values: val_str, |
| 99 |
is_spot: color_type == 1 |
| 100 |
} |
| 101 |
else |
| 102 |
# Skip unknown block types |
| 103 |
f.read(block_len) |
| 104 |
end |
| 105 |
end |
| 106 |
|
| 107 |
# 3. Write to .def |
| 108 |
File.open(output_file, 'w') do |out| |
| 109 |
base_name = File.basename(output_file, '.*') |
| 110 |
out.puts "% spotxcolor definition file: #{base_name}" |
| 111 |
out.puts "% Auto-generated from .ase" |
| 112 |
out.puts "" |
| 113 |
out.puts "\\ProvidesFile{#{File.basename(output_file)}}[Spot color definitions]" |
| 114 |
out.puts "" |
| 115 |
|
| 116 |
cmyk_count = 0 |
| 117 |
other_count = 0 |
| 118 |
|
| 119 |
colors.each do |c| |
| 120 |
if c[:model] == "CMYK" |
| 121 |
out.puts "\\definespotcolor{#{c[:macro_name]}}{#{c[:pdf_name]}}{#{c[:values]}}" |
| 122 |
cmyk_count += 1 |
| 123 |
else |
| 124 |
# CMYK以外の場合はコメントアウト |
| 125 |
out.puts "% \\definespotcolor{#{c[:macro_name]}}{#{c[:pdf_name]}}{#{c[:values]}} % Need CMYK conversion (#{c[:model].strip})" |
| 126 |
other_count += 1 |
| 127 |
end |
| 128 |
end |
| 129 |
out.puts "\\endinput" |
| 130 |
|
| 131 |
puts "Extraction Complete!" |
| 132 |
puts " - CMYK Colors extracted: #{cmyk_count}" |
| 133 |
if other_count > 0 |
| 134 |
puts " - Non-CMYK Colors skipped (commented out): #{other_count}" |
| 135 |
end |
| 136 |
end |
| 137 |
|
| 138 |
end |
| 139 |
rescue => e |
| 140 |
puts "Error processing file: #{e.class} - #{e.message}" |
| 141 |
puts e.backtrace.join("\n") |
| 142 |
end |