captionbob docs

Version 1.0.0
Contents ↓

Python: captionbob.emoji

captionbob.emoji.emoji_font

Emoji font: the PNG image of an emoji in a color emoji font.

A color emoji font, such as Noto-3D-128.ttf, holds one image for each emoji. The cmap table gives the glyph of one codepoint. An emoji of more than one codepoint, such as a profession, a skin tone or a flag, is a ligature of the GSUB table: the glyphs of its codepoints are replaced by the glyph of the emoji. The sbix table holds the image of each glyph as a PNG file, in one strike for each size. The reader takes the strike with the most pixels per em.

The reader reads the ligature lookups of the GSUB table, also when an extension lookup holds them, in the order of the lookup list. When two ligatures have the same glyphs, the first one wins, as when the font is shaped. The reader does not run the other lookups. A sequence is looked up as it is and then with no FE0F, the codepoint that asks for the emoji look of a mark, because a font can have the ligature in one form only.

The reader uses struct of the standard library, so it needs no package.

Source file: captionbob/emoji/emoji_font.py

codepoint_text

def codepoint_text(codepoints: Sequence[int]) -> str

Give the codepoints in hex with at least HEX_DIGITS digits, joined by _, such as 1f469_200d_2695_fe0f.

Args:

  • codepoints: The codepoints.

Returns:

The text of the codepoints.

EmojiFont

class EmojiFont
    def __init__(self, font_path: Path) -> None

Gives the PNG image of an emoji from a color emoji font.

Args:

  • font_path: The font file, with cmap, GSUB, sbix and maxp tables.

Raises:

  • FileNotFoundError: When the font is not a file.
  • ValueError: When the font has no such table, when its cmap has no subtable of format 12, when a coverage has another format, or when the file is cut off.

EmojiFont.strike_ppem

@property
def strike_ppem(self) -> int

The pixels per em of the strike whose images the font gives.

EmojiFont.glyph

def glyph(self, codepoints: Sequence[int]) -> int | None

Give the glyph of an emoji.

Args:

  • codepoints: The codepoints of the emoji.

Returns:

The glyph of the codepoints, or of the codepoints with no FE0F, from the cmap for one codepoint and from the ligatures for more. None when the font has neither.

EmojiFont.image

def image(self, codepoints: Sequence[int]) -> bytes

Give the PNG image of an emoji.

Args:

  • codepoints: The codepoints of the emoji.

Returns:

The PNG file of the glyph of the emoji in the largest strike.

Raises:

  • ValueError: When the font has no glyph for the codepoints, when the glyph has no image, or when the image is not a PNG.