Skip to main content

A Python library for calculating visual width of text, especially for subtitle display

Project description

visual-width

A lightweight Python library for calculating visual width of text, especially designed for subtitle display.

Features

  • Accurate width calculation: Calculates visual width based on Unicode properties and character characteristics
  • Multi-language support: Supports CJK (Chinese, Japanese, Korean), Latin, Arabic, Hebrew, and many other scripts
  • Subtitle-optimized: Designed specifically for subtitle text width calculation
  • CJK comprehensive: Full support for all CJK character ranges including extensions A-I
  • Zero dependencies: Uses only Python standard library (unicodedata, math, functools)
  • Type safety: Full type hints for better IDE support
  • Performance: Uses LRU cache for efficient repeated calculations

Installation

pip install visual-width

Quick Start

Calculate visual width of text

from visual_width import VisualWidth

# Calculate width of English text
width = VisualWidth.calc("Hello, world!")
print(width)  # 13.0

# Calculate width of Chinese text
width = VisualWidth.calc("你好,世界")
print(width)  # 10.0 (5 characters × 2.0 width each)

# Calculate width of mixed text
width = VisualWidth.calc("Hello 世界")
print(width)  # 12.0 (5 + 1 + 6)

Character width mapping

The library uses the following width mapping:

  • CJK characters (Chinese, Japanese, Korean): 2.0
  • Latin lowercase letters: ~1.0 (varies by character)
  • Latin uppercase letters: ~1.15 (except I=0.4, M/W=1.3)
  • Numbers: 0.9
  • Space: 0.3
  • Very narrow characters (i, l, I, etc.): 0.4
  • Narrow characters (f, j, t, r, etc.): 0.6
  • Wide characters (m, w, M, W, etc.): 1.3

Return value

The calc() method returns a float value rounded up to one decimal place (always rounds up).

Examples:

  • 1.231.3
  • 1.211.3
  • 1.201.2

API Reference

VisualWidth

Main utility class for visual width calculation. All methods are static.

VisualWidth.calc(text)

Calculate the visual width of text for subtitle display.

This method calculates the visual width of text (in character units), where 1.0 represents the width of a standard Latin lowercase letter.

Parameters:

  • text (str): Input text string.

Returns:

  • float: Visual width in character units, rounded up to one decimal place.

Examples:

from visual_width import VisualWidth

# English text
width = VisualWidth.calc("Hello")
print(width)  # 5.0

# Chinese text
width = VisualWidth.calc("你好")
print(width)  # 4.0

# Mixed text
width = VisualWidth.calc("Hello 世界")
print(width)  # 12.0

# Text with numbers and punctuation
width = VisualWidth.calc("Price: $99.50")
print(width)  # 13.0

# Empty string
width = VisualWidth.calc("")
print(width)  # 0.0

Class Constants

The VisualWidth class provides the following constants for character width categories:

  • VisualWidth.VERY_NARROW_CHARS: Set of very narrow characters (width 0.4)
  • VisualWidth.NARROW_CHARS: Set of narrow characters (width 0.6)
  • VisualWidth.WIDE_CHARS: Set of wide characters (width 1.3)

These constants can be accessed directly if needed for custom logic.

Supported Character Sets

CJK Characters

  • CJK Unified Ideographs: 0x4E00-0x9FFF
  • CJK Extension A-I: All extension blocks (0x3400-0x323AF, 0x2EBF0-0x2EE5F)
  • CJK Compatibility Ideographs: 0xF900-0xFAFF, 0x2F800-0x2FA1F
  • Hiragana: 0x3040-0x309F
  • Katakana: 0x30A0-0x30FF
  • Hangul Syllables: 0xAC00-0xD7AF (plus extensions)
  • Bopomofo (Zhuyin): 0x3100-0x312F

Other Scripts

  • Latin: Standard and extended Latin characters
  • Greek: Greek letters (uppercase: 1.1, lowercase: 1.0)
  • Cyrillic: Cyrillic letters (uppercase: 1.15, lowercase: 1.0)
  • Arabic: Arabic letters (0.8 width)
  • Hebrew: Hebrew letters (0.9 width)
  • Thai: Thai characters (0.9 width)
  • Devanagari: Devanagari script (0.9 width)
  • Emoji: Emoji and symbols (2.0 width)

Requirements

  • Python >= 3.10

No external dependencies required. This package uses only Python standard library modules (unicodedata, math, functools).

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

visual_width-0.1.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

visual_width-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file visual_width-0.1.1.tar.gz.

File metadata

  • Download URL: visual_width-0.1.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for visual_width-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fc1afa78b6379f3a1d0d57f97fe1a687deb8748da80b13839c27c59ae1e474e1
MD5 e6c4ae9382a12e2e9c147ab98726e5e6
BLAKE2b-256 57f1af88c1e412e08b3fd507667f08ea40eeef082d09ba1452c5a80bc1a3fca7

See more details on using hashes here.

File details

Details for the file visual_width-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: visual_width-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for visual_width-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aab430912875b64cf7c4873754c7e3ea2c2aed79a3d7d6522c4299015001bb59
MD5 d8570a8f5732b588e23d858b4755ef72
BLAKE2b-256 3ac8de9020e8830ff42811bfa2ec8c64bb210d8894938d3e4c4df94d071ce5fd

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