class TwitterCldr::Resources::BidiTestImporter

This class should be used with JRuby in 1.9 mode

Constants

BIDI_TEST_FILE
DIRECTIONS
OUT_FILE

Public Instance Methods

execute() click to toggle source
# File lib/twitter_cldr/resources/bidi_test_importer.rb, line 19
def execute
  generate_test
end

Private Instance Methods

before_prepare() click to toggle source
# File lib/twitter_cldr/resources/bidi_test_importer.rb, line 25
def before_prepare
  require 'java'
  java_import 'java.lang.Character'
  java_import 'classpath.Bidi'
end
expand_bitset_str(bitset) click to toggle source
# File lib/twitter_cldr/resources/bidi_test_importer.rb, line 80
def expand_bitset_str(bitset)
  bitset.to_i.to_s(2).rjust(3, '0').chars.to_a.map { |i| i == '1' }.reverse
end
generate_test() click to toggle source
# File lib/twitter_cldr/resources/bidi_test_importer.rb, line 39
def generate_test
  run_hash = {}

  File.open(source_file, 'r').each_line do |ln|
    cur_line = ln.strip

    case cur_line[0]
      when '#', '@'
        next
      else
        input, bitset = cur_line.split('; ')

        expand_bitset_str(bitset).each_with_index do |check, index|
          if check
            types = input.split(" ")
            direction = get_java_direction(DIRECTIONS[index])
            bidi = Java::Classpath::Bidi.new(types_to_string(types), direction)
            levels = types.each_with_index.map { |_, idx| bidi.getLevelAt(idx) }
            reorder_arr = Java::Classpath::Bidi.reorderVisually(levels.dup, 0, (0...types.size).to_a, 0, types.size).to_a

            key = "#{levels.join(" ")} | #{reorder_arr.join(" ")}"
            run_hash[key] ||= {}
            run_hash[key][input] ||= 0
            run_hash[key][input] |= (2 ** index)
          end
        end
    end
  end

  File.open(output_file, 'w+') do |out|
    run_hash.each_pair do |levels_and_reorders, inputs|
      levels, reorders = levels_and_reorders.split(' | ')
      out.write("@Levels: #{levels}\n")
      out.write("@Reorder: #{reorders}\n")
      inputs.each_pair do |input, bitset|
        out.write("#{input}; #{bitset}\n")
      end
    end
  end
end
get_java_direction(dir) click to toggle source
# File lib/twitter_cldr/resources/bidi_test_importer.rb, line 84
def get_java_direction(dir)
  case dir
    when :RTL
      Java::Classpath::Bidi::DIRECTION_RIGHT_TO_LEFT
    when :LTR
      Java::Classpath::Bidi::DIRECTION_LEFT_TO_RIGHT
    else
      Java::Classpath::Bidi::DIRECTION_DEFAULT_LEFT_TO_RIGHT
  end
end
output_file() click to toggle source
# File lib/twitter_cldr/resources/bidi_test_importer.rb, line 35
def output_file
  File.join(params.fetch(:output_path), File.basename(BIDI_TEST_FILE))
end
source_file() click to toggle source
# File lib/twitter_cldr/resources/bidi_test_importer.rb, line 31
def source_file
  requirements[:unicode].source_path_for(BIDI_TEST_FILE)
end
types_to_string(types) click to toggle source
# File lib/twitter_cldr/resources/bidi_test_importer.rb, line 95
def types_to_string(types)
  @utf_map ||= {
    'L'   => "\u0041",
    'LRE' => "\u202a",
    'LRO' => "\u202d",
    'R'   => "\u05be",
    'AL'  => "\u0626",
    'RLE' => "\u202b",
    'RLO' => "\u202e",
    'PDF' => "\u202c",
    'EN'  => "\u0030",
    'ET'  => "\u0023",
    'AN'  => "\u0667",
    'CS'  => "\u002c",
    'NSM' => "\u0300",
    'BN'  => "\u0000",
    'B'   => "\u0085",
    'S'   => "\u0009",
    'WS'  => "\u000c",
    'ON'  => "\u0021"
  }

  # java 1.6 and 1.7 report different representative characters for the "ES" bidi class
  @utf_map['ES'] = (Character.getDirectionality(0x002b) == Character::DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR) ? "\u002b" : "\u002f"
  types.inject('') { |ret, type| ret << @utf_map[type]; ret }
end