2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Novell code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Novell Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* robert@ocallahan.org
|
2008-12-18 12:12:53 -08:00
|
|
|
* Ehsan Akhgari <ehsan.akhgari@gmail.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsTextFrameUtils.h"
|
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIWordBreaker.h"
|
|
|
|
#include "gfxFont.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
2007-07-02 16:36:59 -07:00
|
|
|
#include "nsBidiUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX TODO implement transform of backslash to yen that nsTextTransform does
|
|
|
|
// when requested by PresContext->LanguageSpecificTransformType(). Do it with
|
|
|
|
// a new factory type that just munges the input stream. But first, check
|
|
|
|
// that we really still need this, it's only enabled via a hidden pref
|
|
|
|
// which defaults false...
|
|
|
|
|
2007-03-22 16:13:12 -07:00
|
|
|
#define UNICODE_ZWSP 0x200B
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool IsDiscardable(PRUnichar ch, PRUint32* aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Unlike IS_DISCARDABLE, we don't discard \r. \r will be ignored by gfxTextRun
|
|
|
|
// and discarding it would force us to copy text in many cases of preformatted
|
|
|
|
// text containing \r\n.
|
|
|
|
if (ch == CH_SHY) {
|
|
|
|
*aFlags |= nsTextFrameUtils::TEXT_HAS_SHY;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if ((ch & 0xFF00) != 0x2000) {
|
|
|
|
// Not a Bidi control character
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-07-02 16:36:59 -07:00
|
|
|
return IS_BIDI_CONTROL_CHAR(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool IsDiscardable(PRUint8 ch, PRUint32* aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (ch == CH_SHY) {
|
|
|
|
*aFlags |= nsTextFrameUtils::TEXT_HAS_SHY;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUnichar*
|
|
|
|
nsTextFrameUtils::TransformText(const PRUnichar* aText, PRUint32 aLength,
|
|
|
|
PRUnichar* aOutput,
|
2008-08-12 01:31:56 -07:00
|
|
|
CompressionMode aCompression,
|
2008-12-18 12:12:53 -08:00
|
|
|
PRUint8* aIncomingFlags,
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxSkipCharsBuilder* aSkipChars,
|
|
|
|
PRUint32* aAnalysisFlags)
|
|
|
|
{
|
2007-05-09 15:04:56 -07:00
|
|
|
PRUint32 flags = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUnichar* outputStart = aOutput;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool lastCharArabic = false;
|
2008-12-18 12:12:53 -08:00
|
|
|
|
2008-08-12 01:31:56 -07:00
|
|
|
if (aCompression == COMPRESS_NONE) {
|
2007-05-22 16:45:47 -07:00
|
|
|
// Skip discardables.
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < aLength; ++i) {
|
|
|
|
PRUnichar ch = *aText++;
|
2007-05-22 16:45:47 -07:00
|
|
|
if (IsDiscardable(ch, &flags)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aSkipChars->SkipChar();
|
|
|
|
} else {
|
|
|
|
aSkipChars->KeepChar();
|
2007-05-30 15:27:27 -07:00
|
|
|
if (ch == '\t') {
|
2007-05-22 16:45:47 -07:00
|
|
|
flags |= TEXT_HAS_TAB;
|
2008-12-18 12:12:53 -08:00
|
|
|
} else if (ch != ' ' && ch != '\n') {
|
|
|
|
// we already know it's not a tab from the previous check
|
|
|
|
lastCharArabic = IS_ARABIC_CHAR(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
*aOutput++ = ch;
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 12:12:53 -08:00
|
|
|
if (lastCharArabic) {
|
|
|
|
*aIncomingFlags |= INCOMING_ARABICCHAR;
|
|
|
|
} else {
|
|
|
|
*aIncomingFlags &= ~INCOMING_ARABICCHAR;
|
|
|
|
}
|
|
|
|
*aIncomingFlags &= ~INCOMING_WHITESPACE;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool inWhitespace = (*aIncomingFlags & INCOMING_WHITESPACE) != 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < aLength; ++i) {
|
|
|
|
PRUnichar ch = *aText++;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nowInWhitespace;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (ch == ' ' &&
|
|
|
|
(i + 1 >= aLength ||
|
2007-03-22 16:13:12 -07:00
|
|
|
!IsSpaceCombiningSequenceTail(aText, aLength - (i + 1)))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
nowInWhitespace = true;
|
2008-08-12 01:31:56 -07:00
|
|
|
} else if (ch == '\n' && aCompression == COMPRESS_WHITESPACE_NEWLINE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (i > 0 && IS_CJ_CHAR(aText[-1]) &&
|
|
|
|
i + 1 < aLength && IS_CJ_CHAR(aText[1])) {
|
|
|
|
// Discard newlines between CJK chars.
|
|
|
|
// XXX this really requires more context to get right!
|
|
|
|
aSkipChars->SkipChar();
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
nowInWhitespace = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2007-03-24 04:07:36 -07:00
|
|
|
nowInWhitespace = ch == '\t';
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!nowInWhitespace) {
|
|
|
|
if (IsDiscardable(ch, &flags)) {
|
|
|
|
aSkipChars->SkipChar();
|
|
|
|
nowInWhitespace = inWhitespace;
|
|
|
|
} else {
|
|
|
|
*aOutput++ = ch;
|
|
|
|
aSkipChars->KeepChar();
|
2008-12-18 12:12:53 -08:00
|
|
|
lastCharArabic = IS_ARABIC_CHAR(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (inWhitespace) {
|
|
|
|
aSkipChars->SkipChar();
|
|
|
|
} else {
|
|
|
|
if (ch != ' ') {
|
|
|
|
flags |= TEXT_WAS_TRANSFORMED;
|
|
|
|
}
|
|
|
|
*aOutput++ = ' ';
|
|
|
|
aSkipChars->KeepChar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inWhitespace = nowInWhitespace;
|
|
|
|
}
|
2008-12-18 12:12:53 -08:00
|
|
|
if (lastCharArabic) {
|
|
|
|
*aIncomingFlags |= INCOMING_ARABICCHAR;
|
|
|
|
} else {
|
|
|
|
*aIncomingFlags &= ~INCOMING_ARABICCHAR;
|
|
|
|
}
|
|
|
|
if (inWhitespace) {
|
|
|
|
*aIncomingFlags |= INCOMING_WHITESPACE;
|
|
|
|
} else {
|
|
|
|
*aIncomingFlags &= ~INCOMING_WHITESPACE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outputStart + aLength != aOutput) {
|
|
|
|
flags |= TEXT_WAS_TRANSFORMED;
|
|
|
|
}
|
|
|
|
*aAnalysisFlags = flags;
|
|
|
|
return aOutput;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint8*
|
|
|
|
nsTextFrameUtils::TransformText(const PRUint8* aText, PRUint32 aLength,
|
|
|
|
PRUint8* aOutput,
|
2008-08-12 01:31:56 -07:00
|
|
|
CompressionMode aCompression,
|
2008-12-18 12:12:53 -08:00
|
|
|
PRUint8* aIncomingFlags,
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxSkipCharsBuilder* aSkipChars,
|
|
|
|
PRUint32* aAnalysisFlags)
|
|
|
|
{
|
|
|
|
PRUint32 flags = 0;
|
|
|
|
PRUint8* outputStart = aOutput;
|
|
|
|
|
2008-08-12 01:31:56 -07:00
|
|
|
if (aCompression == COMPRESS_NONE) {
|
2007-05-22 16:45:47 -07:00
|
|
|
// Skip discardables.
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < aLength; ++i) {
|
|
|
|
PRUint8 ch = *aText++;
|
2007-05-22 16:45:47 -07:00
|
|
|
if (IsDiscardable(ch, &flags)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aSkipChars->SkipChar();
|
|
|
|
} else {
|
|
|
|
aSkipChars->KeepChar();
|
2007-05-30 15:27:27 -07:00
|
|
|
if (ch == '\t') {
|
2007-05-22 16:45:47 -07:00
|
|
|
flags |= TEXT_HAS_TAB;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
*aOutput++ = ch;
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 12:12:53 -08:00
|
|
|
*aIncomingFlags &= ~(INCOMING_ARABICCHAR | INCOMING_WHITESPACE);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool inWhitespace = (*aIncomingFlags & INCOMING_WHITESPACE) != 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < aLength; ++i) {
|
|
|
|
PRUint8 ch = *aText++;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nowInWhitespace = ch == ' ' || ch == '\t' ||
|
2008-08-12 01:31:56 -07:00
|
|
|
(ch == '\n' && aCompression == COMPRESS_WHITESPACE_NEWLINE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!nowInWhitespace) {
|
|
|
|
if (IsDiscardable(ch, &flags)) {
|
|
|
|
aSkipChars->SkipChar();
|
|
|
|
nowInWhitespace = inWhitespace;
|
|
|
|
} else {
|
|
|
|
*aOutput++ = ch;
|
|
|
|
aSkipChars->KeepChar();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (inWhitespace) {
|
|
|
|
aSkipChars->SkipChar();
|
|
|
|
} else {
|
|
|
|
if (ch != ' ') {
|
|
|
|
flags |= TEXT_WAS_TRANSFORMED;
|
|
|
|
}
|
|
|
|
*aOutput++ = ' ';
|
|
|
|
aSkipChars->KeepChar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inWhitespace = nowInWhitespace;
|
|
|
|
}
|
2008-12-18 12:12:53 -08:00
|
|
|
*aIncomingFlags &= ~INCOMING_ARABICCHAR;
|
|
|
|
if (inWhitespace) {
|
|
|
|
*aIncomingFlags |= INCOMING_WHITESPACE;
|
|
|
|
} else {
|
|
|
|
*aIncomingFlags &= ~INCOMING_WHITESPACE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outputStart + aLength != aOutput) {
|
|
|
|
flags |= TEXT_WAS_TRANSFORMED;
|
|
|
|
}
|
|
|
|
*aAnalysisFlags = flags;
|
|
|
|
return aOutput;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsSkipCharsRunIterator::NextRun() {
|
2007-03-22 10:30:00 -07:00
|
|
|
do {
|
|
|
|
if (mRunLength) {
|
|
|
|
mIterator.AdvanceOriginal(mRunLength);
|
|
|
|
NS_ASSERTION(mRunLength > 0, "No characters in run (initial length too large?)");
|
|
|
|
if (!mSkipped || mLengthIncludesSkipped) {
|
|
|
|
mRemainingLength -= mRunLength;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 16:13:12 -07:00
|
|
|
if (!mRemainingLength)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 length;
|
|
|
|
mSkipped = mIterator.IsOriginalCharSkipped(&length);
|
2009-09-16 08:01:36 -07:00
|
|
|
mRunLength = NS_MIN(length, mRemainingLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
} while (!mVisitSkipped && mSkipped);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|