Skip to main content

A library for calculating the display width of Unicode strings.

Project description

Unicode Width Approximation

A library for calculating the display width of Unicode strings in terminal/monospace environments.

Installation

pip install unicode-width-approximation

Usage

from unicode_width_approximation import get_string_width, get_codepoint_width

# Get width of strings
print(get_string_width("hello"))      # 5
print(get_string_width("中文"))        # 4
print(get_string_width("👨‍👩‍👧‍👦"))  # 2

# Get width of single code points
print(get_codepoint_width(ord('A')))  # 1
print(get_codepoint_width(0x4E00))    # 2 (CJK)
print(get_codepoint_width(0x1F600))   # 2 (emoji)

API

get_string_width(s: str) -> int

Calculate the total display width of a UTF-8 encoded string.

get_codepoint_width(code: int) -> int

Get the display width of a single Unicode code point (0, 1, or 2).

is_wide_char(code: int) -> bool

Check if a code point is a wide character (East Asian Wide or Fullwidth).

is_zero_width(code: int) -> bool

Check if a code point is a zero-width character.

Width Rules

Character Type Width
Control characters (Cc) 0
Format characters (Cf) 0
Combining marks (Mn, Me) 0
East Asian Wide (W) 2
East Asian Fullwidth (F) 2
Emoji_Presentation 2
Other characters 1

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unicode_width_approximation-1.0.0.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file unicode_width_approximation-1.0.0.tar.gz.

File metadata

File hashes

Hashes for unicode_width_approximation-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9dfb7a8e922a14a8b631ab0efc99d1206f5d1b211122d5b593e56a9afe0a5030
MD5 d5b20c1345b1573cfac8d6a8c1cfedb8
BLAKE2b-256 bbfbd4373b8da9d409c7f8aa0c88a76267405c4d3bfd90814dde81088ce3f2c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page