question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Check if font supports multi code point emoji

See original GitHub issue

I’m trying to check if a font has a glyph for a multi codepoint emoji like β€œπŸ‘±πŸΌβ€β™‚οΈβ€, β€œπŸ±β€πŸ‰β€ or β€œπŸ–πŸΌβ€ in Python 3.x.

For single codepoint emoji like β€œπŸ˜„β€ or β€œπŸ˜‰β€ I’m able to validate their support via the following code:

from fontTools.ttLib import TTFont

def __isEmojiSupportedByFont(emoji: str) -> bool:
    font = TTFont(r"C:\Windows\Fonts\seguiemj.ttf")
    emojiCodepoint = ord(str) # Only works for single codepoint emoji
    for table in font['cmap'].tables:
        for char_code, glyph_name in table.cmap.items():
            if char_code == emojiCodepoint:
                return True
    return False

How do I do this for multi codepoint emoji (is there a method for this), since cmp only has single codepoint emoji in it?

Link to my stackoverflow question for this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15

github_iconTop GitHub Comments

1reaction
COM8commented, Apr 7, 2019

That’s it! Thank you very much for your help!

And again as reference, my code:

from uharfbuzz import Face, Font, Buffer, ot_font_set_funcs, shape

def __isEmojiSupportedByFont(self, emoji: str) -> bool:
    # Load font:
    with open(r"C:\Windows\Fonts\seguiemj.ttf", 'rb') as fontfile:
        self.fontdata = fontfile.read()

    # Load font (has to be done for call):
    face = Face(self.fontdata)
    font = Font(face)
    upem = face.upem
    font.scale = (upem, upem)
    ot_font_set_funcs(font)

    # Create text buffer:
    buf = Buffer()
    buf.add_str(emoji)
    buf.guess_segment_properties()

    # Shape text:
    features = {"kern": True, "liga": True}
    shape(font, buf, features)
    
    # Remove all variant selectors:
    while len(infos) > 0 and infos[-1].codepoint == 3:
        infos = infos[:-1]

    # Filter empty:
    if len(infos) <= 0:
        return False

    # Remove uncombined ending with skin tone like "πŸ‘­πŸΏ":
    lastCp = infos[-1].codepoint
    if lastCp == 1076 or lastCp == 1079 or lastCp == 1082 or lastCp == 1085 or lastCp == 1088:
        return False
            
    # If there is a code point 0 or 3 => Emoji not fully supported by font:
    return all(info.codepoint != 0 and info.codepoint != 3 for info in infos)
0reactions
khaledhosnycommented, Apr 6, 2019

Or len(buf.glyph_infos) == 1 and buf.glyph_infos[0].codepoint since the individual characters might not be supported but not the ligature (depending on your definition of supported, the presence of the ligature might be significant or not).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python fonttools: Check if font supports multi codepoint emoji
An easier way to check if an Emoji is supported by a font in python is to use HarfBuzz or more exact harfpy....
Read more >
Python fonttools: Check if font supports multi codepoint emoji
I'm trying to check if a font has a glyph for a multi codepoint emoji like "πŸ‘±πŸΌβ€β™‚οΈ", "πŸ±β€πŸ‰" or "πŸ–πŸΌ" in Python 3.x....
Read more >
Multi-codepoint emojis Β· Issue #39 Β· jquast/wcwidth - GitHub
Hi,. Can wcwidth help me with multi-codepoint emojis? For instance, here I want to get the cell width for a "woman_mechanic_dark_skin_tone"Β ...
Read more >
Everything You Need To Know About Emoji
Units of a coded character set are known as code points. A code point value represents the position of a character in the...
Read more >
font-variant-emoji - CSS: Cascading Style Sheets | MDN
The font-variant-emoji CSS property specifies the default presentation style for displaying emojis.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found