1 hosho_width
liquibyte edited this page 2026-11-19 16:23:30 -06:00

hosho_width.h

Column Width of a Character.

Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.

A terminal is a grid of cells. Most characters fill one cell, East Asian wide characters and most emoji fill two, and combining marks fill none: they join the character in front of them.

Features:

  • The number of cells (0, 1 or 2) a Unicode code point fills
  • Grapheme clusters: whether a character joins the one before it
  • Tables made from the Unicode data files, not from the C library's locale, so the answer is the same on every system

Internal design notes:

  • East_Asian_Width W and F are two cells; A (ambiguous) is one cell, as in a terminal that is not set to an East Asian locale
  • General categories Mn, Me and Cf are no cell, and so are the Hangul Jamo that join a syllable (U+1160..U+11FF)
  • The tables are in hosho_width_tables.h, made from the Unicode data files of the version hosho_width_unicode_version() gives
  • Grapheme clusters (UAX #29): the class of a character and whether it continues a cluster, so a terminal can keep a flag, an emoji with a skin tone or a family in one cell
#include "hosho_width.h"

Types: HoshoGraphemeClass

Functions: hosho_width, hosho_width_emoji, hosho_grapheme_class, hosho_grapheme_joins, hosho_width_unicode_version

Types

HoshoGraphemeClass

A character's class for grapheme clusters (UAX #29, Grapheme_Cluster_Break); the classes a terminal needs.

Name Value Description
HOSHO_GRAPHEME_OTHER no class of the ones below: a cluster starts with it
HOSHO_GRAPHEME_EXTEND a mark, a variation selector, an emoji's skin tone (U+1F3FB..U+1F3FF): joins the cluster before it
HOSHO_GRAPHEME_ZWJ U+200D, the zero width joiner.
HOSHO_GRAPHEME_REGIONAL_INDICATOR a letter of a flag (U+1F1E6..U+1F1FF): two of them are one flag
HOSHO_GRAPHEME_PREPEND a character that joins the one after it
HOSHO_GRAPHEME_SPACING_MARK a mark with a width of its own (a vowel sign of Indic scripts): joins the cluster before it
HOSHO_GRAPHEME_L a Hangul leading consonant
HOSHO_GRAPHEME_V a Hangul vowel
HOSHO_GRAPHEME_T a Hangul trailing consonant
HOSHO_GRAPHEME_LV a Hangul syllable with no trailing consonant
HOSHO_GRAPHEME_LVT a Hangul syllable with a trailing consonant

Functions

hosho_width

int hosho_width(uint32_t codepoint);

The number of cells a character fills.

Parameter Description
codepoint A Unicode code point

Returns: 2 for a wide character, 0 for a combining mark, a format character or a control character, 1 for every other character

hosho_width_emoji

int hosho_width_emoji(uint32_t codepoint);

Say if a character of one cell becomes two cells wide when U+FE0F (the emoji variation selector) follows it.

Parameter Description
codepoint A Unicode code point

Returns: 1 for a character with the Emoji property that is one cell wide by itself (for example U+2764, the heart), 0 for all others

hosho_grapheme_class

HoshoGraphemeClass hosho_grapheme_class(uint32_t codepoint);

A character's grapheme cluster class.

Parameter Description
codepoint A Unicode code point

Returns: Its class; HOSHO_GRAPHEME_OTHER for all others, control characters too

hosho_grapheme_joins

int hosho_grapheme_joins(const uint32_t *cluster, size_t count, uint32_t next);

Say whether a character continues a grapheme cluster: what a reader sees as one character.

The rules of UAX #29 for extended grapheme clusters, GB6 to GB13: Hangul syllables, marks and joiners (GB9, GB9a), a Prepend character (GB9b), emoji joined with U+200D (GB11) and the two letters of a flag (GB12, GB13). Not GB9c (conjuncts of Indic scripts): those stay clusters of their own.

Parameter Description
cluster The code points of the cluster so far, in order
count How many; 0 for none
next The code point after them

Returns: 1 when next belongs to the same cluster, 0 when a new cluster starts with it

hosho_width_unicode_version

const char *hosho_width_unicode_version(void);

The Unicode version the width tables come from.

Returns: The version as text, for example "18.0.0"