1/*
2 * Copyright (C) 2015 Apple, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <unicode/uchar.h>
29
30namespace WebCore {
31
32#define ICU_HEADERS_UNDERSTAND_SUPPLEMENTAL_SYMBOLS_AND_PICTOGRAPHS (U_ICU_VERSION_MAJOR_NUM > 56 || (U_ICU_VERSION_MAJOR_NUM == 56 && U_ICU_VERSION_MINOR_NUM >= 1))
33
34#if ICU_HEADERS_UNDERSTAND_SUPPLEMENTAL_SYMBOLS_AND_PICTOGRAPHS
35static bool icuLibraryUnderstandsSupplementalSymbolsAndPictographs()
36{
37 UVersionInfo versionInfo;
38 u_getVersion(versionInfo);
39 static_assert(U_MAX_VERSION_LENGTH >= 2, "Cannot run ICU version check");
40 return versionInfo[0] > 56 || (versionInfo[0] == 56 && versionInfo[1] >= 1);
41}
42#endif
43
44static inline bool isEmojiGroupCandidate(UChar32 character)
45{
46 auto unicodeBlock = ublock_getCode(character);
47
48 if (unicodeBlock == UBLOCK_MISCELLANEOUS_SYMBOLS
49 || unicodeBlock == UBLOCK_DINGBATS
50 || unicodeBlock == UBLOCK_MISCELLANEOUS_SYMBOLS_AND_PICTOGRAPHS
51 || unicodeBlock == UBLOCK_EMOTICONS
52 || unicodeBlock == UBLOCK_TRANSPORT_AND_MAP_SYMBOLS)
53 return true;
54
55#if ICU_HEADERS_UNDERSTAND_SUPPLEMENTAL_SYMBOLS_AND_PICTOGRAPHS
56 static bool useSupplementalSymbolsAndPictographs = icuLibraryUnderstandsSupplementalSymbolsAndPictographs();
57 if (useSupplementalSymbolsAndPictographs)
58 return unicodeBlock == UBLOCK_SUPPLEMENTAL_SYMBOLS_AND_PICTOGRAPHS;
59#endif
60 return character >= 0x1F900 && character <= 0x1F9FF;
61}
62
63static inline bool isEmojiFitzpatrickModifier(UChar32 character)
64{
65 // U+1F3FB - EMOJI MODIFIER FITZPATRICK TYPE-1-2
66 // U+1F3FC - EMOJI MODIFIER FITZPATRICK TYPE-3
67 // U+1F3FD - EMOJI MODIFIER FITZPATRICK TYPE-4
68 // U+1F3FE - EMOJI MODIFIER FITZPATRICK TYPE-5
69 // U+1F3FF - EMOJI MODIFIER FITZPATRICK TYPE-6
70
71 return character >= 0x1F3FB && character <= 0x1F3FF;
72}
73
74inline bool isVariationSelector(UChar32 character)
75{
76 return character >= 0xFE00 && character <= 0xFE0F;
77}
78
79inline bool isEmojiKeycapBase(UChar32 character)
80{
81 return (character >= '0' && character <= '9') || character == '#' || character == '*';
82}
83
84inline bool isEmojiRegionalIndicator(UChar32 character)
85{
86 return character >= 0x1F1E6 && character <= 0x1F1FF;
87}
88
89inline bool isEmojiWithPresentationByDefault(UChar32 character)
90{
91#if U_ICU_VERSION_MAJOR_NUM >= 57
92 return u_hasBinaryProperty(character, UCHAR_EMOJI_PRESENTATION);
93#else
94 UNUSED_PARAM(character);
95 return false;
96#endif
97}
98
99inline bool isEmojiModifierBase(UChar32 character)
100{
101#if U_ICU_VERSION_MAJOR_NUM >= 57
102 return u_hasBinaryProperty(character, UCHAR_EMOJI_MODIFIER_BASE);
103#else
104 UNUSED_PARAM(character);
105 return false;
106#endif
107}
108
109inline bool isDefaultIgnorableCodePoint(UChar32 character)
110{
111 return u_hasBinaryProperty(character, UCHAR_DEFAULT_IGNORABLE_CODE_POINT);
112}
113
114inline bool isControlCharacter(UChar32 character)
115{
116 return u_getIntPropertyValue(character, UCHAR_GENERAL_CATEGORY) == U_CONTROL_CHAR;
117}
118
119}
120