Class: Unicoder::Builder::Scripts

Inherits:
Object
  • Object
show all
Includes:
Unicoder::Builder, MultiDimensionalArrayBuilder
Defined in:
lib/unicoder/builders/scripts.rb

Instance Attribute Summary

Attributes included from Unicoder::Builder

#formats, #index, #option

Instance Method Summary collapse

Methods included from MultiDimensionalArrayBuilder

#assign_codepoint, #compress!, #remove_trailing_nils!

Methods included from Unicoder::Builder

#assign, #assign_codepoint, build, #export, #initialize, #meta, #parse_file

Instance Method Details

#assign_classic(sub_index_name, codepoint, value) ⇒ Object

TODO refactor how multiple indexes are organized



31
32
33
34
35
36
37
38
39
# File 'lib/unicoder/builders/scripts.rb', line 31

def assign_classic(sub_index_name, codepoint, value)
  idx = @index[sub_index_name]

  if option =~ /charkeys/
    idx[[codepoint].pack("U*")] = value
  else
    idx[codepoint] = value
  end
end

#initialize_indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/unicoder/builders/scripts.rb', line 7

def initialize_index
  @index = {
    SCRIPTS: [],
    SCRIPT_EXTENSIONS: {},
    SCRIPT_ALIASES: {},
    SCRIPT_NAMES: [],
    OFFSETS: [
      0x10000,
      0x1000,
      0x100,
      0x10
    ],
  }
  @reverse_script_names = {}
  @reverse_script_extension_names = {}
end

#lookup_extension_names(extension_scripts_string) ⇒ Object



24
25
26
27
28
# File 'lib/unicoder/builders/scripts.rb', line 24

def lookup_extension_names(extension_scripts_string)
  extension_scripts_string.split(" ").map{ |extension_script|
    @reverse_script_extension_names[extension_script]
  }
end

#parse!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/unicoder/builders/scripts.rb', line 41

def parse!
  parse_file :property_value_aliases, :line, regex: /^sc ; (?<short>\S+?)\s*; (?<long>\S+?)(?:\s*; (?<short2>\S+))?$/ do |line|
    @index[:SCRIPT_NAMES] << line["long"]
    script_number = @reverse_script_names.size
    @reverse_script_names[line["long"]] = script_number

    @index[:SCRIPT_ALIASES][line["short" ]] = script_number
    @index[:SCRIPT_ALIASES][line["short2"]] = script_number if line["short2"]
    @reverse_script_extension_names[line["short"]] = script_number
  end

  parse_file :scripts, :line, regex: /^(?<from>\S+?)(\.\.(?<to>\S+))?\s+; (?<script>\S+) #.*$/ do |line|
    if line["to"]
      (line["from"].to_i(16)..line["to"].to_i(16)).each{ |codepoint|
        assign_codepoint codepoint, @reverse_script_names[line["script"]], @index[:SCRIPTS]
      }
    else
      assign_codepoint line["from"].to_i(16), @reverse_script_names[line["script"]], @index[:SCRIPTS]
    end
  end

  4.times{ compress! @index[:SCRIPTS] }

  parse_file :script_extensions, :line, regex: /^(?<from>\S+?)(\.\.(?<to>\S+))?\s+; (?<scripts>.+?) #.*$/ do |line|
    if line["to"]
      (line["from"].to_i(16)..line["to"].to_i(16)).each{ |codepoint|
        assign_classic :SCRIPT_EXTENSIONS, codepoint, lookup_extension_names(line["scripts"])
      }
    else
      assign_classic :SCRIPT_EXTENSIONS, line["from"].to_i(16), lookup_extension_names(line["scripts"])
    end
  end
end