module

CRT::Ansi::DisplayWidth

Constants

TABLES = begin tables = {"COMBINING" => [] of ::Tuple(Int32, Int32), "DOUBLEWIDTH" => [] of ::Tuple(Int32, Int32), "AMBIGUOUS" => [] of ::Tuple(Int32, Int32), "NARROW" => [] of ::Tuple(Int32, Int32), "EMOJI" => [] of ::Tuple(Int32, Int32)} current = nil ({{ read_file("/tmp/tmp.hNaJjc/src/src/crt/ansi/data/widths.txt") }}).each_line do |line| line = line.strip if line.empty? || (line.starts_with?('#')) next end if tables.has_key?(line) current = tables[line] elsif target = current parts = line.split if parts.size == 2 target << {parts[0].to_i(16), parts[1].to_i(16)} end end end tables end

Width tables parsed from data/widths.txt at compile time. The file is generated by scripts/generate_width_tables.cr from Unicode consortium EastAsianWidth.txt and emoji-data.txt.

ZERO_WIDTH_JOINER = 8205
ZERO_WIDTH_NON_JOINER = 8204

Instance methods

grapheme_at_column(text : String, column : Int32) : Int32

Returns the grapheme index at or before the given absolute display column.

Source
max_width(texts : Indexable(String)) : Int32

Returns the maximum display width across an array of strings.

Source
of(grapheme : String) : Int32

Returns the display width of a single grapheme cluster.

Source
width(text : String) : Int32

Returns the display width of a string by summing grapheme cluster widths.

Source
width_to(text : String, grapheme_index : Int32) : Int32

Returns the display width of the first grapheme_index graphemes.

Source