Module: Unicode::Scripts
- Defined in:
- lib/unicode/scripts.rb,
lib/unicode/scripts/index.rb,
lib/unicode/scripts/constants.rb
Constant Summary collapse
- VERSION =
"1.12.0"- UNICODE_VERSION =
"17.0.0"- DATA_DIRECTORY =
File.(File.dirname(__FILE__) + "/../../../data/").freeze
- INDEX_FILENAME =
(DATA_DIRECTORY + "/scripts.marshal.gz").freeze
- AUGMENTED_SCRIPT_CODES =
["Hanb", "Jpan", "Kore"]
Class Method Summary collapse
- .augmented_scripts(string) ⇒ Object
- .mixed?(string) ⇒ Boolean
-
.names(format: :long, augmented: :exclude) ⇒ Object
Lists scripts.
- .resolved_scripts(string) ⇒ Object
- .script(char, format: :long) ⇒ Object
- .script_extensions(string, format: :long) ⇒ Object
- .scripts(string, **options) ⇒ Object (also: of)
- .single?(string) ⇒ Boolean
Class Method Details
.augmented_scripts(string) ⇒ Object
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 74 75 76 77 78 79 80 81 |
# File 'lib/unicode/scripts.rb', line 49 def self.augmented_scripts(string) require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX augmented = string.each_codepoint.inject([]){ |res, codepoint| if new_scripts = INDEX[:SCRIPT_EXTENSIONS][codepoint] script_extension_names = new_scripts.map{ |new_script| INDEX[:SCRIPT_ALIASES].key(new_script) } else script_extension_names = scripts([codepoint].pack("U"), format: :short) end res | script_extension_names } if augmented.include? "Hani" augmented |= ["Hanb", "Jpan", "Kore"] end if augmented.include?("Hira") || augmented.include?("Kana") augmented |= ["Jpan"] end if augmented.include? "Hang" augmented |= ["Kore"] end if augmented.include? "Bopo" augmented |= ["Hanb"] end if augmented.include?("Zyyy") || augmented.include?("Zinh") augmented |= names(format: :short, augmented: :include ) end augmented.sort end |
.mixed?(string) ⇒ Boolean
91 92 93 |
# File 'lib/unicode/scripts.rb', line 91 def self.mixed?(string) resolved_scripts(string).empty? end |
.names(format: :long, augmented: :exclude) ⇒ Object
Lists scripts. Options:
-
format - :long, :short
-
augmented - :include, :exclude, :only
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/unicode/scripts.rb', line 102 def self.names(format: :long, augmented: :exclude) if format == :long && augmented != :exclude raise ArgumentError, "only short four-letter script codes (ISO 15924) supported when listing augmented scripts" end if augmented == :only return AUGMENTED_SCRIPT_CODES end require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX if format == :long INDEX[:SCRIPT_NAMES].sort elsif augmented == :exclude INDEX[:SCRIPT_ALIASES].keys.sort else (INDEX[:SCRIPT_ALIASES].keys + AUGMENTED_SCRIPT_CODES).sort end end |
.resolved_scripts(string) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/unicode/scripts.rb', line 83 def self.resolved_scripts(string) string.chars.reduce( Unicode::Scripts.names(format: :short, augmented: :include) ){ |acc, char| acc & augmented_scripts(char) } end |
.script(char, format: :long) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/unicode/scripts.rb', line 15 def self.script(char, format: :long) require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX codepoint_depth_offset = char.unpack("U")[0] or raise(ArgumentError, "Unicode::Scripts.script must be given a valid char") index_or_value = INDEX[:SCRIPTS] [0x10000, 0x1000, 0x100, 0x10].each{ |depth| index_or_value = index_or_value[codepoint_depth_offset / depth] codepoint_depth_offset = codepoint_depth_offset % depth unless index_or_value.is_a? Array res = index_or_value || INDEX[:SCRIPT_ALIASES]["Zzzz"] return format == :long ? INDEX[:SCRIPT_NAMES][res] : INDEX[:SCRIPT_ALIASES].key(res) end } res = index_or_value[codepoint_depth_offset] || INDEX[:SCRIPT_ALIASES]["Zzzz"] format == :long ? INDEX[:SCRIPT_NAMES][res] : INDEX[:SCRIPT_ALIASES].key(res) end |
.script_extensions(string, format: :long) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/unicode/scripts.rb', line 33 def self.script_extensions(string, format: :long) require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX string.each_codepoint.inject([]){ |res, codepoint| if new_scripts = INDEX[:SCRIPT_EXTENSIONS][codepoint] script_extension_names = new_scripts.map{ |new_script| format == :long ? INDEX[:SCRIPT_NAMES][new_script] : INDEX[:SCRIPT_ALIASES].key(new_script) } else script_extension_names = scripts([codepoint].pack("U"), format: format) end res | script_extension_names }.sort end |
.scripts(string, **options) ⇒ Object Also known as: of
5 6 7 8 9 10 11 12 |
# File 'lib/unicode/scripts.rb', line 5 def self.scripts(string, **) res = [] string.each_char{ |char| script_name = script(char, **) res << script_name unless res.include?(script_name) } res.sort end |
.single?(string) ⇒ Boolean
95 96 97 |
# File 'lib/unicode/scripts.rb', line 95 def self.single?(string) !resolved_scripts(string).empty? end |