2012-05-29 19:52:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/*
|
2010-06-11 23:14:38 +04:00
|
|
|
* This file is based on usc_impl.c from ICU 4.2.0.1, slightly adapted
|
|
|
|
* for use within Mozilla Gecko, separate from a standard ICU build.
|
|
|
|
*
|
|
|
|
* The original ICU license of the code follows:
|
|
|
|
*
|
|
|
|
* ICU License - ICU 1.8.1 and later
|
|
|
|
*
|
|
|
|
* COPYRIGHT AND PERMISSION NOTICE
|
|
|
|
*
|
|
|
|
* Copyright (c) 1995-2009 International Business Machines Corporation and
|
|
|
|
* others
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, provided that the above copyright notice(s) and this
|
|
|
|
* permission notice appear in all copies of the Software and that both the
|
|
|
|
* above copyright notice(s) and this permission notice appear in supporting
|
|
|
|
* documentation.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
|
|
|
|
* BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
|
|
|
|
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
|
|
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
|
|
|
* Except as contained in this notice, the name of a copyright holder shall
|
|
|
|
* not be used in advertising or otherwise to promote the sale, use or other
|
|
|
|
* dealings in this Software without prior written authorization of the
|
|
|
|
* copyright holder.
|
|
|
|
*
|
|
|
|
* All trademarks and registered trademarks mentioned herein are the property
|
|
|
|
* of their respective owners.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gfxScriptItemizer.h"
|
2012-02-24 14:15:46 +04:00
|
|
|
#include "nsUnicodeProperties.h"
|
2010-06-11 23:14:38 +04:00
|
|
|
#include "nsCharTraits.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "harfbuzz/hb.h"
|
2010-06-11 23:14:38 +04:00
|
|
|
|
2017-02-23 05:29:46 +03:00
|
|
|
using namespace mozilla::unicode;
|
|
|
|
|
2010-06-11 23:14:38 +04:00
|
|
|
#define MOD(sp) ((sp) % PAREN_STACK_DEPTH)
|
|
|
|
#define LIMIT_INC(sp) (((sp) < PAREN_STACK_DEPTH)? (sp) + 1 : PAREN_STACK_DEPTH)
|
|
|
|
#define INC(sp,count) (MOD((sp) + (count)))
|
|
|
|
#define INC1(sp) (INC(sp, 1))
|
|
|
|
#define DEC(sp,count) (MOD((sp) + PAREN_STACK_DEPTH - (count)))
|
|
|
|
#define DEC1(sp) (DEC(sp, 1))
|
|
|
|
#define STACK_IS_EMPTY() (pushCount <= 0)
|
|
|
|
#define STACK_IS_NOT_EMPTY() (! STACK_IS_EMPTY())
|
|
|
|
#define TOP() (parenStack[parenSP])
|
|
|
|
#define SYNC_FIXUP() (fixupCount = 0)
|
|
|
|
|
|
|
|
void
|
2016-04-21 20:58:59 +03:00
|
|
|
gfxScriptItemizer::push(uint32_t endPairChar, Script newScriptCode)
|
2010-06-11 23:14:38 +04:00
|
|
|
{
|
|
|
|
pushCount = LIMIT_INC(pushCount);
|
|
|
|
fixupCount = LIMIT_INC(fixupCount);
|
|
|
|
|
|
|
|
parenSP = INC1(parenSP);
|
2012-06-20 23:58:18 +04:00
|
|
|
parenStack[parenSP].endPairChar = endPairChar;
|
2015-09-12 06:30:14 +03:00
|
|
|
parenStack[parenSP].scriptCode = newScriptCode;
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxScriptItemizer::pop()
|
|
|
|
{
|
|
|
|
if (STACK_IS_EMPTY()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixupCount > 0) {
|
|
|
|
fixupCount -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pushCount -= 1;
|
|
|
|
parenSP = DEC1(parenSP);
|
|
|
|
|
|
|
|
/* If the stack is now empty, reset the stack
|
|
|
|
pointers to their initial values.
|
|
|
|
*/
|
|
|
|
if (STACK_IS_EMPTY()) {
|
|
|
|
parenSP = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-21 20:58:59 +03:00
|
|
|
gfxScriptItemizer::fixup(Script newScriptCode)
|
2010-06-11 23:14:38 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t fixupSP = DEC(parenSP, fixupCount);
|
2010-06-11 23:14:38 +04:00
|
|
|
|
|
|
|
while (fixupCount-- > 0) {
|
|
|
|
fixupSP = INC1(fixupSP);
|
2015-09-12 06:30:14 +03:00
|
|
|
parenStack[fixupSP].scriptCode = newScriptCode;
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 23:38:05 +03:00
|
|
|
// We regard the current char as having the same script as the in-progress run
|
|
|
|
// if either script code is Common or Inherited, or if the run script appears
|
|
|
|
// in the character's ScriptExtensions, or if the char is a cluster extender.
|
2012-06-20 23:58:18 +04:00
|
|
|
static inline bool
|
2017-01-18 23:38:05 +03:00
|
|
|
SameScript(Script runScript, Script currCharScript, uint32_t aCurrCh)
|
2010-06-11 23:14:38 +04:00
|
|
|
{
|
2016-04-21 20:58:59 +03:00
|
|
|
return runScript <= Script::INHERITED ||
|
|
|
|
currCharScript <= Script::INHERITED ||
|
2017-01-18 23:38:05 +03:00
|
|
|
currCharScript == runScript ||
|
|
|
|
IsClusterExtender(aCurrCh) ||
|
|
|
|
HasScript(aCurrCh, runScript);
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
gfxScriptItemizer::gfxScriptItemizer(const char16_t *src, uint32_t length)
|
2010-06-11 23:14:38 +04:00
|
|
|
: textPtr(src), textLength(length)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-04 19:02:17 +04:00
|
|
|
gfxScriptItemizer::SetText(const char16_t *src, uint32_t length)
|
2010-06-11 23:14:38 +04:00
|
|
|
{
|
|
|
|
textPtr = src;
|
|
|
|
textLength = length;
|
|
|
|
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxScriptItemizer::Next(uint32_t& aRunStart, uint32_t& aRunLimit,
|
2016-04-21 20:58:59 +03:00
|
|
|
Script& aRunScript)
|
2010-06-11 23:14:38 +04:00
|
|
|
{
|
|
|
|
/* if we've fallen off the end of the text, we're done */
|
|
|
|
if (scriptLimit >= textLength) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SYNC_FIXUP();
|
2016-04-21 20:58:59 +03:00
|
|
|
scriptCode = Script::COMMON;
|
2010-06-11 23:14:38 +04:00
|
|
|
|
|
|
|
for (scriptStart = scriptLimit; scriptLimit < textLength; scriptLimit += 1) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ch;
|
2016-04-21 20:58:59 +03:00
|
|
|
Script sc;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t startOfChar = scriptLimit;
|
2010-06-11 23:14:38 +04:00
|
|
|
|
|
|
|
ch = textPtr[scriptLimit];
|
|
|
|
|
2012-06-20 23:58:18 +04:00
|
|
|
/* decode UTF-16 (may be surrogate pair) */
|
|
|
|
if (NS_IS_HIGH_SURROGATE(ch) && scriptLimit < textLength - 1) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t low = textPtr[scriptLimit + 1];
|
2012-06-20 23:58:18 +04:00
|
|
|
if (NS_IS_LOW_SURROGATE(low)) {
|
|
|
|
ch = SURROGATE_TO_UCS4(ch, low);
|
|
|
|
scriptLimit += 1;
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|
2012-06-20 23:58:18 +04:00
|
|
|
}
|
2010-06-11 23:14:38 +04:00
|
|
|
|
2012-06-20 23:58:18 +04:00
|
|
|
// Initialize gc to UNASSIGNED; we'll only set it to the true GC
|
|
|
|
// if the character has script=COMMON, otherwise we don't care.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t gc = HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
|
2010-06-11 23:14:38 +04:00
|
|
|
|
2016-01-13 18:45:22 +03:00
|
|
|
sc = GetScriptCode(ch);
|
2016-04-21 20:58:59 +03:00
|
|
|
if (sc == Script::COMMON) {
|
2010-06-11 23:14:38 +04:00
|
|
|
/*
|
|
|
|
* Paired character handling:
|
|
|
|
*
|
|
|
|
* if it's an open character, push it onto the stack.
|
|
|
|
* if it's a close character, find the matching open on the
|
|
|
|
* stack, and use that script code. Any non-matching open
|
2012-06-20 23:58:18 +04:00
|
|
|
* characters above it on the stack will be popped.
|
|
|
|
*
|
|
|
|
* We only do this if the script is COMMON; for chars with
|
|
|
|
* specific script assignments, we just use them as-is.
|
2010-06-11 23:14:38 +04:00
|
|
|
*/
|
2016-01-20 18:17:14 +03:00
|
|
|
gc = GetGeneralCategory(ch);
|
2012-06-20 23:58:18 +04:00
|
|
|
if (gc == HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t endPairChar = mozilla::unicode::GetMirroredChar(ch);
|
2012-06-20 23:58:18 +04:00
|
|
|
if (endPairChar != ch) {
|
|
|
|
push(endPairChar, scriptCode);
|
|
|
|
}
|
|
|
|
} else if (gc == HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION &&
|
|
|
|
HasMirroredChar(ch))
|
|
|
|
{
|
|
|
|
while (STACK_IS_NOT_EMPTY() && TOP().endPairChar != ch) {
|
|
|
|
pop();
|
|
|
|
}
|
2010-06-11 23:14:38 +04:00
|
|
|
|
2012-06-20 23:58:18 +04:00
|
|
|
if (STACK_IS_NOT_EMPTY()) {
|
|
|
|
sc = TOP().scriptCode;
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 23:38:05 +03:00
|
|
|
if (SameScript(scriptCode, sc, ch)) {
|
2016-04-21 20:58:59 +03:00
|
|
|
if (scriptCode <= Script::INHERITED &&
|
|
|
|
sc > Script::INHERITED)
|
2012-02-14 12:03:01 +04:00
|
|
|
{
|
2010-06-11 23:14:38 +04:00
|
|
|
scriptCode = sc;
|
|
|
|
fixup(scriptCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if this character is a close paired character,
|
|
|
|
* pop the matching open character from the stack
|
|
|
|
*/
|
2012-06-20 23:58:18 +04:00
|
|
|
if (gc == HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION &&
|
|
|
|
HasMirroredChar(ch)) {
|
2010-06-11 23:14:38 +04:00
|
|
|
pop();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* reset scriptLimit in case it was advanced during reading a
|
|
|
|
* multiple-code-unit character
|
|
|
|
*/
|
|
|
|
scriptLimit = startOfChar;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aRunStart = scriptStart;
|
|
|
|
aRunLimit = scriptLimit;
|
|
|
|
aRunScript = scriptCode;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|