File: char_display_width.rb

package info (click to toggle)
ruby-unicode-utils 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,988 kB
  • sloc: ruby: 1,877; makefile: 4
file content (31 lines) | stat: -rw-r--r-- 900 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- encoding: utf-8 -*-

require "unicode_utils/display_width"

module UnicodeUtils

  # Get the width of +char+ when displayed with a fixed pitch font.
  #
  # Some code points (especially from east asian scripts) take the
  # width of two characters, while others have no width.
  #
  # Examples:
  #
  #   require "unicode_utils/char_display_width"
  #   UnicodeUtils.char_display_width("別")  # => 2
  #   UnicodeUtils.char_display_width(0x308) # => 0
  #   UnicodeUtils.char_display_width("a")   # => 1
  #
  # Performs the same logic as UnicodeUtils.display_width, but for a
  # single code point.
  def char_display_width(char)
    cp = char.ord
    # copied from display_width, keep in sync!
    case UnicodeUtils.east_asian_width(cp)
    when :Wide, :Fullwidth then 2
    else GENERAL_CATEGORY_BASIC_WIDTH_MAP[UnicodeUtils.gc(cp)]
    end
  end
  module_function :char_display_width

end