Debian Codegraph @

texmf-dist/doc/latex/spotxcolor/tools/acbl2def.rb

raw
1 #!/usr/bin/env ruby
2 # frozen_string_literal: true
3
4 # acbl2def.rb
5 # A script to extract CMYK spot colors from Adobe Color Book Legacy (.acbl) XML files
6 # and generate a .def file for the spotxcolor LaTeX package.
7
8 if ARGV.empty?
9 puts "Usage: ruby acbl2def.rb <input.acbl> [output.def]"
10 exit 1
11 end
12
13 input_file = ARGV[0]
14 output_file = ARGV[1] || input_file.sub(/\.acbl$/i, '.def')
15
16 begin
17 xml_content = File.read(input_file, encoding: 'UTF-8')
18
19 # プレフィックスの抽出 (例: <PrefixPostfixPair Prefix="DIC " Postfix=""/>)
20 prefix = ""
21 if xml_content =~ /<PrefixPostfixPair Prefix="([^"]*)"/
22 prefix = $1
23 end
24
25 colors = []
26
27 # <Sp> タグをすべて抽出
28 # 例: <Sp N="1s"><A N="1p"/><C>0 0.16 0.17 0</C></Sp>
29 xml_content.scan(/<Sp N="([^"]+)".*?<C>([^<]+)<\/C><\/Sp>/m) do |name, cmyk_vals|
30
31 full_pdf_name = "#{prefix}#{name}"
32
33 # LaTeXマクロ名として安全な形に整形 (空白、ハイフン、アスタリスク等を除去)
34 macro_name = full_pdf_name.gsub(/[\s\-\*\/]/, '')
35
36 # CMYK値のスペース区切りをカンマ区切りに変換
37 cmyk_csv = cmyk_vals.strip.split(/\s+/).map { |v| v.to_f.round(4).to_s }.join(", ")
38
39 colors << {
40 macro: macro_name,
41 pdf_name: full_pdf_name,
42 cmyk: cmyk_csv
43 }
44 end
45
46 # .def ファイルへの書き出し
47 File.open(output_file, 'w', encoding: 'UTF-8') do |out|
48 base_name = File.basename(output_file, '.*')
49 out.puts "% spotxcolor definition file: #{base_name}"
50 out.puts "% Auto-generated natively from Adobe .acbl (CMYK exact values)"
51 out.puts ""
52 out.puts "\\ProvidesFile{#{File.basename(output_file)}}[Spot color definitions]"
53 out.puts ""
54
55 colors.each do |c|
56 out.puts "\\definespotcolor{#{c[:macro]}}{#{c[:pdf_name]}}{#{c[:cmyk]}}"
57 end
58 out.puts "\\endinput"
59 end
60
61 puts "Successfully extracted #{colors.size} CMYK colors natively from ACBL."
62 puts "Output saved to: #{output_file}" # <-- 修正箇所
63
64 rescue => e
65 puts "Error: #{e.message}"
66 end