Class: Unicode::DisplayWidth

Inherits:
Object
  • Object
show all
Defined in:
lib/unicode/display_width.rb,
lib/unicode/display_width/index.rb,
lib/unicode/display_width/constants.rb

Constant Summary collapse

INITIAL_DEPTH =
0x10000
ASCII_NON_ZERO_REGEX =
/[\0\x05\a\b\n\v\f\r\x0E\x0F]/
FIRST_4096 =
decompress_index(INDEX[0][0], 1)
VERSION =
"2.5.0"
UNICODE_VERSION =
"15.1.0"
DATA_DIRECTORY =
File.expand_path(File.dirname(__FILE__) + "/../../../data/")
INDEX_FILENAME =
DATA_DIRECTORY + "/display_width.marshal.gz"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ambiguous: 1, overwrite: {}, emoji: false) ⇒ DisplayWidth

Returns a new instance of DisplayWidth.



104
105
106
107
108
# File 'lib/unicode/display_width.rb', line 104

def initialize(ambiguous: 1, overwrite: {}, emoji: false)
  @ambiguous = ambiguous
  @overwrite = overwrite
  @emoji     = emoji
end

Class Method Details

.decompress_index(index, level) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/unicode/display_width/index.rb', line 14

def self.decompress_index(index, level)
  index.flat_map{ |value|
    if level > 0
      if value.instance_of?(Array)
        value[15] ||= nil
        decompress_index(value, level - 1)
      else
        decompress_index([value] * 16, level - 1)
      end
    else
      if value.instance_of?(Array)
        value[15] ||= nil
        value
      else
        [value] * 16
      end
    end
  }
end

.emoji_extra_width_of(string, ambiguous = 1, overwrite = {}, _ = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/unicode/display_width.rb', line 86

def self.emoji_extra_width_of(string, ambiguous = 1, overwrite = {}, _ = {})
  require "unicode/emoji"

  extra_width = 0
  modifier_regex = /[#{ Unicode::Emoji::EMOJI_MODIFIERS.pack("U*") }]/
  zwj_regex = /(?<=#{ [Unicode::Emoji::ZWJ].pack("U") })./

  string.scan(Unicode::Emoji::REGEX){ |emoji|
    extra_width += 2 * emoji.scan(modifier_regex).size

    emoji.scan(zwj_regex){ |zwj_succ|
      extra_width += self.of(zwj_succ, ambiguous, overwrite)
    }
  }

  extra_width
end

.of(string, ambiguous = 1, overwrite = {}, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/unicode/display_width.rb', line 12

def self.of(string, ambiguous = 1, overwrite = {}, options = {})
  if overwrite.empty?
    # Optimization for ASCII-only strings without certain control symbols
    if string.ascii_only?
      if string.match?(ASCII_NON_ZERO_REGEX)
        res = string.gsub(ASCII_NON_ZERO_REGEX, "").size - string.count("\b")
        res < 0 ? 0 : res
      else
        string.size
      end
    else
      width_no_overwrite(string, ambiguous, options)
    end
  else
    width_all_features(string, ambiguous, overwrite, options)
  end
end

.width_all_features(string, ambiguous, overwrite, options) ⇒ Object

Same as .width_no_overwrite - but with applying overwrites for each char



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
82
83
# File 'lib/unicode/display_width.rb', line 57

def self.width_all_features(string, ambiguous, overwrite, options)
  # Sum of all chars widths
  res = string.codepoints.sum{ |codepoint|
    next overwrite[codepoint] if overwrite[codepoint]

    if codepoint > 15 && codepoint < 161 # very common
      next 1
    elsif codepoint < 0x1001
      width = FIRST_4096[codepoint]
    else
      width = INDEX
      depth = INITIAL_DEPTH
      while (width = width[codepoint / depth]).instance_of? Array
        codepoint %= depth
        depth /= 16
      end
    end

    width == :A ? ambiguous : (width || 1)
  }

  # Substract emoji error
  res -= emoji_extra_width_of(string, ambiguous, overwrite) if options[:emoji]

  # Return result + prevent negative lengths
  res < 0 ? 0 : res
end

.width_no_overwrite(string, ambiguous, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unicode/display_width.rb', line 30

def self.width_no_overwrite(string, ambiguous, options = {})
  # Sum of all chars widths
  res = string.codepoints.sum{ |codepoint|
    if codepoint > 15 && codepoint < 161 # very common
      next 1
    elsif codepoint < 0x1001
      width = FIRST_4096[codepoint]
    else
      width = INDEX
      depth = INITIAL_DEPTH
      while (width = width[codepoint / depth]).instance_of? Array
        codepoint %= depth
        depth /= 16
      end
    end

    width == :A ? ambiguous : (width || 1)
  }

  # Substract emoji error
  res -= emoji_extra_width_of(string, ambiguous) if options[:emoji]

  # Return result + prevent negative lengths
  res < 0 ? 0 : res
end

Instance Method Details

#get_config(**kwargs) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/unicode/display_width.rb', line 110

def get_config(**kwargs)
  [
    kwargs[:ambiguous] || @ambiguous,
    kwargs[:overwrite] || @overwrite,
    { emoji: kwargs[:emoji] || @emoji },
  ]
end

#of(string, **kwargs) ⇒ Object



118
119
120
# File 'lib/unicode/display_width.rb', line 118

def of(string, **kwargs)
  self.class.of(string, *get_config(**kwargs))
end