diff options
Diffstat (limited to 'Master')
-rwxr-xr-x | Master/texmf-dist/scripts/uptex/convbkmk.rb | 115 |
1 files changed, 77 insertions, 38 deletions
diff --git a/Master/texmf-dist/scripts/uptex/convbkmk.rb b/Master/texmf-dist/scripts/uptex/convbkmk.rb index 1d512fb10d1..f78583d546b 100755 --- a/Master/texmf-dist/scripts/uptex/convbkmk.rb +++ b/Master/texmf-dist/scripts/uptex/convbkmk.rb @@ -3,7 +3,7 @@ =begin -convbkmk Ver.0.04 +convbkmk Ver.0.07 = License @@ -42,10 +42,16 @@ THE SOFTWARE. Support sequences of end of line: '\' or other followed by "\n", "\r\n" or "\r" . Set file IO to binary mode. +2012.07.26 0.05 Add -o option to support conversion of OUT files + generated by dvipdfmx. +2012.08.07 0.06 Bug fix: Ver.0.05 does not work with Ruby1.9 +2012.09.17 0.07 Bug fix: An infinite loop occurs in Ver.0.05, 0.06 + with -g option in some cases. + Add reference for PDFDocEncoding. =end -Version = "0.04" +Version = "0.07" require "optparse" @@ -156,6 +162,8 @@ class TeXEncoding end enc = TeXEncoding.new +Opts = {} + OptionParser.new do |opt| opt.on('-e', '--euc-jp', 'set pTeX internal encoding to EUC-JP') {|v| @@ -183,8 +191,14 @@ OptionParser.new do |opt| 'set pTeX/upTeX internal encoding to ENC') {|v| enc.set_process_encoding(v) } + opt.on('-o', '--out', + 'treat OUT files') {|v| + Opts[:mode] = 'out' + require "fileutils" + } opt.banner += " file0.ps [file1.ps ...]\n" \ - + opt.banner.sub('Usage:',' ') + ' < in_file.ps > out_file.ps' + + opt.banner.sub('Usage:',' ') + " < in_file.ps > out_file.ps\n" \ + + opt.banner.sub('Usage:',' ') + ' -o file0.out [file1.out ...]' opt.parse! end @@ -193,11 +207,18 @@ end if enc.status == false enc.set_process_encoding('UTF-8') end - +if Opts[:mode] == 'out' + OpenP, CloseP, OpenPEsc, ClosePEsc = '{', '}', '\{', '\}' + FileSfx = 'out' +else + OpenP, CloseP, OpenPEsc, ClosePEsc = '(', ')', '\(', '\)' + FileSfx = 'ps' +end def try_guess_encoding(line, enc) return 'US-ASCII' if line.ascii_only? + enc.is_8bit = true valid_enc = false count = 0 enc.list.each { |e| @@ -229,12 +250,7 @@ def check_parentheses_balance(line, enc) tmp_rest = line.force_encoding(tmp_enc) else # ambiguous - enc.is_8bit = true - depth = 1 - count = 0 - tmp_prev = '' - tmp_rest = tmp_rest.force_encoding('ASCII-8BIT') - return depth, count, tmp_prev, tmp_rest + raise 'unexpected internal condition!' end else tmp_enc = enc.current @@ -257,8 +273,8 @@ def check_parentheses_balance(line, enc) end while tmp_rest.length>0 do - if (tmp_rest =~ /\A(\\\(|\\\)|[^()])*(\(|\))/) # parenthis - if $2 == '(' + if (tmp_rest =~ /\A(\\#{OpenPEsc}|\\#{ClosePEsc}|[^#{OpenP}#{CloseP}])*(#{OpenPEsc}|#{ClosePEsc})/o) # parenthis + if $2 == OpenP depth += 1 count += 1 else @@ -278,6 +294,9 @@ def check_parentheses_balance(line, enc) end # PDFDocEncoding -> UTF-16BE +# Ref. "PDF Reference, Sixth Edition, version 1.7", 2006, Adobe Systems Incorporated +# http://www.adobe.com/devnet/pdf/pdf_reference_archive.html +# http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf PDF2UNI = Array(0..255) PDF2UNI[0o030..0o037] = 0x02d8, 0x02c7, 0x02c6, 0x02d9, 0x02dd, 0x02db, 0x02da, 0x02dc PDF2UNI[0o200..0o207] = 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192, 0x2044 @@ -285,9 +304,10 @@ PDF2UNI[0o210..0o217] = 0x2039, 0x203a, 0x2212, 0x2030, 0x201e, 0x201c, 0x201d, PDF2UNI[0o220..0o227] = 0x2019, 0x201a, 0x2122, 0xfb01, 0xfb02, 0x0141, 0x0152, 0x0160 PDF2UNI[0o230..0o237] = 0x0178, 0x017d, 0x0131, 0x0142, 0x0153, 0x0161, 0x017e, 0xfffd PDF2UNI[0o240 ] = 0x20ac +PDF2UNI[0o255 ] = 0xfffd def conv_string_to_utf16be(line, enc) - if line !~ /(\()(.*)(\))/m + if line !~ /(#{OpenPEsc})(.*)(#{ClosePEsc})/mo raise 'illegal input!' end pre, tmp, post = $`, $2, $' @@ -322,11 +342,15 @@ def conv_string_to_utf16be(line, enc) tmp = $' end - buf = 'FEFF' # BOM for UTF-16BE - conv.utf8_to_utf16be.each_byte {|byte| - buf += '%02X' % byte + buf = '' + conv16be = "\xFE\xFF" # BOM U+FEFF + conv16be.force_encoding('UTF-16BE') if $RUBY_M17N + conv16be += conv.utf8_to_utf16be # UTF-16BE with BOM + conv16be.each_byte {|byte| + buf += (Opts[:mode] == 'out' ? '\%03o' : '%02X') % byte } - return pre + '<' + buf + '>' + post + buf = Opts[:mode] == 'out' ? '{' + buf + '}' : '<' + buf + '>' + return pre + buf + post end @@ -340,7 +364,8 @@ def file_treatment(ifile, ofile, enc) while l = ifile.gets do line.force_encoding('ASCII-8BIT') if $RUBY_M17N line += l - if (line !~ %r!(/Title|/Author|/Keywords|/Subject|/Creator|/Producer)(\s+\(|$)! ) + reg = Opts[:mode] == 'out' ? %r!(\{)! : %r!(/Title|/Author|/Keywords|/Subject|/Creator|/Producer)(\s+\(|$)! + if (line !~ reg ) ofile.print line line = '' next @@ -348,19 +373,36 @@ def file_treatment(ifile, ofile, enc) ofile.print $` line = $& + $' - depth, count, tmp_prev, tmp_rest \ - = check_parentheses_balance(line, enc) - if depth<0 - raise 'illegal input!' - elsif depth>0 - next - elsif count==0 - next - elsif count>0 - ofile.print conv_string_to_utf16be(tmp_prev, enc) - line = tmp_rest - else - raise 'illegal input!' + + if enc.status == 'guess' + if tmp_enc = try_guess_encoding(line, enc) + # succeeded in guess or ascii only + line.force_encoding(tmp_enc) + else + # ambiguous + next + end + end + + while line.length>0 do + depth, count, tmp_prev, tmp_rest \ + = check_parentheses_balance(line, enc) + if depth<0 + p depth, count, tmp_prev, tmp_rest + raise 'illegal input! (depth<0)' + elsif depth>0 + break + elsif count==0 + ofile.print line + line = '' + break + elsif count>0 + ofile.print conv_string_to_utf16be(tmp_prev, enc) + line = tmp_rest + else + p depth, count, tmp_prev, tmp_rest + raise 'illegal input! (count<0)' + end end end @@ -368,10 +410,6 @@ def file_treatment(ifile, ofile, enc) if enc.status == 'guess' && enc.is_8bit raise 'did not succeed in guess encoding!' end - - if line.length>0 - ofile.print line - end end @@ -382,15 +420,16 @@ if ARGV.size == 0 file_treatment(ifile, ofile, enc) else ARGV.each {|fin| - if (fin !~ /\.ps$/i) - raise 'input file does not seem PS file' + if (fin !~ /\.#{FileSfx}$/io) + raise 'input file does not seem ' + FileSfx.upcase + ' file' end - fout = fin.gsub(/\.ps$/i, "-convbkmk#{$&}") + fout = fin.gsub(/\.#{FileSfx}$/io, "-convbkmk#{$&}") open(fin, 'rb') {|ifile| open(fout, 'wb') {|ofile| file_treatment(ifile, ofile, enc) } } + FileUtils.mv(fout, fin) if Opts[:mode] == 'out' } end |