Skip to content

Instantly share code, notes, and snippets.

@alxgsv
Last active May 15, 2023 07:43
Show Gist options
  • Save alxgsv/7ec8090943b28f229f0324897c17ef3a to your computer and use it in GitHub Desktop.
Save alxgsv/7ec8090943b28f229f0324897c17ef3a to your computer and use it in GitHub Desktop.
Get subset of .ttf emoji font and convert to .woff for web usage
import os
import re
import sys
from collections import Counter
import emoji
def is_emoji(s):
return s in UNICODE_EMOJI
def extract_emojis(s):
return emoji.distinct_emoji_list(s)
def get_files(dir_path):
for root, dirs, files in os.walk(dir_path):
for file in files:
try:
yield os.path.join(root, file)
except:
pass
def main(dir_path):
emoji_counter = Counter()
for file_path in get_files(dir_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
emojis = extract_emojis(content)
emoji_counter.update(emojis)
except:
pass
for emoji, count in emoji_counter.items():
print(f'"{emoji}", ', end="")
if __name__ == '__main__':
main(sys.argv[1])
  1. Install tools
pip install fonttools brotli zopfli emoji
  1. Get list of emojis used in the project
python3 search_emoji.py path/to/project
  1. Get codes of desired emojis (JS code)
emojis = ["❀️", "πŸ’©", "πŸ”₯", "πŸ‘‹", "🀣", "😑", "πŸ™", "πŸ‘‰", "πŸŽ‰", "πŸͺ…", "πŸ₯³", "🎊", "πŸ₯Ή", "🫢", "🫠", "πŸͺΏ", "πŸŽ“"]
emojis.map((emoji) => "U+" + emoji.codePointAt(0).toString(16)).join(", ")
  1. Get subset in woff2
pyftsubset NotoColorEmoji.ttf \
           --output-file="EmojiColor.woff2" \
           --flavor=woff2 \
           --unicodes="U+2764, U+1f4a9, U+1f525, U+1f44b, U+1f923, U+1f621, U+1f64f, U+1f449, U+1f389, U+1fa85, U+1f973, U+1f38a, U+1f979, U+1faf6, U+1fae0, U+1fabf, U+1f393"
  1. Get subset in woff
pyftsubset NotoColorEmoji.ttf \
          --output-file="EmojiColor.woff" \
          --flavor=woff \
          --unicodes="U+2764, U+1f4a9, U+1f525, U+1f44b, U+1f923, U+1f621, U+1f64f, U+1f449, U+1f389, U+1fa85, U+1f973, U+1f38a, U+1f979, U+1faf6, U+1fae0, U+1fabf, U+1f393"
  1. Include in CSS
@font-face {
  font-family: "Font";
  src: url("EmojiColor.woff2") format("woff2"), url("EmojiColor.woff") format("woff");
  font-weight: normal;
  font-style: normal;
}

Credits: https://markoskon.com/creating-font-subsets/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment