2017-11-08 19:40:43 +01:00
|
|
|
# This file contains various helper functions used by `build_emoji` tool.
|
|
|
|
# See docs/subsystems/emoji.md for details on how this system works.
|
2017-01-26 08:35:23 +01:00
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
2017-11-08 17:14:52 +01:00
|
|
|
from typing import Any, Dict, List
|
2017-01-26 08:35:23 +01:00
|
|
|
|
2017-04-01 17:20:32 +02:00
|
|
|
# Emojisets that we currently support.
|
|
|
|
EMOJISETS = ['apple', 'emojione', 'google', 'twitter']
|
|
|
|
|
2017-11-22 22:03:24 +01:00
|
|
|
def emoji_names_for_picker(emoji_name_maps: Dict[str, Dict[str, Any]]) -> List[str]:
|
2017-11-08 19:40:43 +01:00
|
|
|
emoji_names = [] # type: List[str]
|
|
|
|
for emoji_code, name_info in emoji_name_maps.items():
|
|
|
|
emoji_names.append(name_info["canonical_name"])
|
|
|
|
emoji_names.extend(name_info["aliases"])
|
2017-01-26 08:35:23 +01:00
|
|
|
|
2017-11-08 19:40:43 +01:00
|
|
|
return sorted(emoji_names)
|
2017-03-19 09:41:24 +01:00
|
|
|
|
|
|
|
# Returns a dict from categories to list of codepoints. The list of
|
|
|
|
# codepoints are sorted according to the `sort_order` as defined in
|
|
|
|
# `emoji_data`.
|
2017-11-22 22:03:24 +01:00
|
|
|
def generate_emoji_catalog(emoji_data: List[Dict[str, Any]],
|
|
|
|
emoji_name_maps: Dict[str, Dict[str, Any]]) -> Dict[str, List[str]]:
|
2017-05-31 23:35:57 +02:00
|
|
|
sort_order = {} # type: Dict[str, int]
|
2017-11-08 19:40:43 +01:00
|
|
|
emoji_catalog = defaultdict(list) # type: Dict[str, List[str]]
|
|
|
|
|
|
|
|
for emoji_dict in emoji_data:
|
|
|
|
emoji_code = emoji_dict["unified"].lower()
|
|
|
|
if not emoji_is_universal(emoji_dict) or emoji_code not in emoji_name_maps:
|
2017-03-21 04:58:21 +01:00
|
|
|
continue
|
2017-11-08 19:40:43 +01:00
|
|
|
category = emoji_dict["category"]
|
|
|
|
sort_order[emoji_code] = emoji_dict["sort_order"]
|
|
|
|
emoji_catalog[category].append(emoji_code)
|
|
|
|
|
|
|
|
# Sort the emojis according to iamcal's sort order. This sorting determines the
|
|
|
|
# order in which emojis will be displayed in emoji picker.
|
2017-03-19 09:41:24 +01:00
|
|
|
for category in emoji_catalog:
|
2017-11-08 19:40:43 +01:00
|
|
|
emoji_catalog[category].sort(key=lambda emoji_code: sort_order[emoji_code])
|
|
|
|
|
|
|
|
return dict(emoji_catalog)
|
2017-03-21 04:58:21 +01:00
|
|
|
|
|
|
|
# Use only those names for which images are present in all
|
|
|
|
# the emoji sets so that we can switch emoji sets seemlessly.
|
2017-11-22 22:03:24 +01:00
|
|
|
def emoji_is_universal(emoji_dict: Dict[str, Any]) -> bool:
|
2017-04-01 17:20:32 +02:00
|
|
|
for emoji_set in EMOJISETS:
|
2017-03-21 04:58:21 +01:00
|
|
|
if not emoji_dict['has_img_' + emoji_set]:
|
|
|
|
return False
|
|
|
|
return True
|
2017-05-23 17:15:26 +02:00
|
|
|
|
2017-11-22 22:03:24 +01:00
|
|
|
def generate_codepoint_to_name_map(emoji_name_maps: Dict[str, Dict[str, Any]]) -> Dict[str, str]:
|
2017-05-23 17:15:26 +02:00
|
|
|
codepoint_to_name = {} # type: Dict[str, str]
|
2017-11-08 19:40:43 +01:00
|
|
|
for emoji_code, name_info in emoji_name_maps.items():
|
|
|
|
codepoint_to_name[emoji_code] = name_info["canonical_name"]
|
2017-05-23 17:15:26 +02:00
|
|
|
return codepoint_to_name
|
2017-10-01 15:19:58 +02:00
|
|
|
|
2017-11-22 22:03:24 +01:00
|
|
|
def generate_name_to_codepoint_map(emoji_name_maps: Dict[str, Dict[str, Any]]) -> Dict[str, str]:
|
2017-11-08 19:40:43 +01:00
|
|
|
name_to_codepoint = {}
|
|
|
|
for emoji_code, name_info in emoji_name_maps.items():
|
|
|
|
canonical_name = name_info["canonical_name"]
|
|
|
|
aliases = name_info["aliases"]
|
|
|
|
name_to_codepoint[canonical_name] = emoji_code
|
|
|
|
for alias in aliases:
|
|
|
|
name_to_codepoint[alias] = emoji_code
|
|
|
|
return name_to_codepoint
|