2 * Copyright (C) 2003, 2006, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Holger Hans Peter Freyther
4 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "SurrogatePairAwareTextIterator.h"
26 #include <unicode/unorm.h>
30 SurrogatePairAwareTextIterator::SurrogatePairAwareTextIterator(const UChar* characters, unsigned currentIndex, unsigned lastIndex, unsigned endIndex)
31 : m_characters(characters)
32 , m_currentIndex(currentIndex)
33 , m_lastIndex(lastIndex)
34 , m_endIndex(endIndex)
38 bool SurrogatePairAwareTextIterator::consumeSlowCase(UChar32& character, unsigned& clusterLength)
40 if (character <= 0x30FE) {
41 // Deal with Hiragana and Katakana voiced and semi-voiced syllables.
42 // Normalize into composed form, and then look for glyph with base + combined mark.
43 // Check above for character range to minimize performance impact.
44 if (UChar32 normalized = normalizeVoicingMarks()) {
45 character = normalized;
51 if (!U16_IS_SURROGATE(character))
54 // If we have a surrogate pair, make sure it starts with the high part.
55 if (!U16_IS_SURROGATE_LEAD(character))
58 // Do we have a surrogate pair? If so, determine the full Unicode (32 bit) code point before glyph lookup.
59 // Make sure we have another character and it's a low surrogate.
60 if (m_currentIndex + 1 >= m_endIndex)
63 UChar low = m_characters[1];
64 if (!U16_IS_TRAIL(low))
67 character = U16_GET_SUPPLEMENTARY(character, low);
72 UChar32 SurrogatePairAwareTextIterator::normalizeVoicingMarks()
74 // According to http://www.unicode.org/Public/UNIDATA/UCD.html#Canonical_Combining_Class_Values
75 static const uint8_t hiraganaKatakanaVoicingMarksCombiningClass = 8;
77 if (m_currentIndex + 1 >= m_endIndex)
80 if (u_getCombiningClass(m_characters[1]) == hiraganaKatakanaVoicingMarksCombiningClass) {
81 // Normalize into composed form using 3.2 rules.
82 UChar normalizedCharacters[2] = { 0, 0 };
83 UErrorCode uStatus = U_ZERO_ERROR;
84 int32_t resultLength = unorm_normalize(m_characters, 2, UNORM_NFC, UNORM_UNICODE_3_2, &normalizedCharacters[0], 2, &uStatus);
85 if (resultLength == 1 && !uStatus)
86 return normalizedCharacters[0];