2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#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"
|
2012-04-16 15:32:12 -07:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsStyleStruct.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
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
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static bool IsDiscardable(PRUnichar ch, uint32_t* 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
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static bool IsDiscardable(uint8_t ch, uint32_t* 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*
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTextFrameUtils::TransformText(const PRUnichar* aText, uint32_t aLength,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUnichar* aOutput,
|
2008-08-12 01:31:56 -07:00
|
|
|
CompressionMode aCompression,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t* aIncomingFlags,
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxSkipCharsBuilder* aSkipChars,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t* aAnalysisFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t 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
|
|
|
|
2012-09-06 22:42:19 -07:00
|
|
|
if (aCompression == COMPRESS_NONE ||
|
|
|
|
aCompression == DISCARD_NEWLINE) {
|
2007-05-22 16:45:47 -07:00
|
|
|
// Skip discardables.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < aLength; ++i) {
|
|
|
|
PRUnichar ch = *aText++;
|
2012-09-06 22:42:19 -07:00
|
|
|
if (IsDiscardable(ch, &flags) ||
|
|
|
|
(ch == '\n' && aCompression == DISCARD_NEWLINE)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
aSkipChars->SkipChar();
|
|
|
|
} else {
|
|
|
|
aSkipChars->KeepChar();
|
2012-06-20 12:58:19 -07:00
|
|
|
if (ch > ' ') {
|
2008-12-18 12:12:53 -08:00
|
|
|
lastCharArabic = IS_ARABIC_CHAR(ch);
|
2012-06-20 12:58:19 -07:00
|
|
|
} else if (ch == '\t') {
|
|
|
|
flags |= TEXT_HAS_TAB;
|
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;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t*
|
|
|
|
nsTextFrameUtils::TransformText(const uint8_t* aText, uint32_t aLength,
|
|
|
|
uint8_t* aOutput,
|
2008-08-12 01:31:56 -07:00
|
|
|
CompressionMode aCompression,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t* aIncomingFlags,
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxSkipCharsBuilder* aSkipChars,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t* aAnalysisFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags = 0;
|
|
|
|
uint8_t* outputStart = aOutput;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-06 22:42:19 -07:00
|
|
|
if (aCompression == COMPRESS_NONE ||
|
|
|
|
aCompression == DISCARD_NEWLINE) {
|
2007-05-22 16:45:47 -07:00
|
|
|
// Skip discardables.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < aLength; ++i) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t ch = *aText++;
|
2012-10-09 22:00:05 -07:00
|
|
|
if (IsDiscardable(ch, &flags) ||
|
|
|
|
(ch == '\n' && aCompression == DISCARD_NEWLINE)) {
|
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;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < aLength; ++i) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t 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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2012-04-16 15:32:12 -07:00
|
|
|
nsTextFrameUtils::ComputeApproximateLengthWithWhitespaceCompression(
|
|
|
|
nsIContent *aContent, const nsStyleText *aStyleText)
|
|
|
|
{
|
|
|
|
const nsTextFragment *frag = aContent->GetText();
|
|
|
|
// This is an approximation so we don't really need anything
|
|
|
|
// too fancy here.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2012-04-16 15:32:12 -07:00
|
|
|
if (aStyleText->WhiteSpaceIsSignificant()) {
|
|
|
|
len = frag->GetLength();
|
|
|
|
} else {
|
|
|
|
bool is2b = frag->Is2b();
|
|
|
|
union {
|
|
|
|
const char *s1b;
|
|
|
|
const PRUnichar *s2b;
|
|
|
|
} u;
|
|
|
|
if (is2b) {
|
|
|
|
u.s2b = frag->Get2b();
|
|
|
|
} else {
|
|
|
|
u.s1b = frag->Get1b();
|
|
|
|
}
|
|
|
|
bool prevWS = true; // more important to ignore blocks with
|
|
|
|
// only whitespace than get inline boundaries
|
|
|
|
// exactly right
|
|
|
|
len = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0, i_end = frag->GetLength(); i < i_end; ++i) {
|
2012-04-16 15:32:12 -07:00
|
|
|
PRUnichar c = is2b ? u.s2b[i] : u.s1b[i];
|
|
|
|
if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {
|
|
|
|
if (!prevWS) {
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
prevWS = true;
|
|
|
|
} else {
|
|
|
|
++len;
|
|
|
|
prevWS = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
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;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t length;
|
2007-03-22 10:30:00 -07:00
|
|
|
mSkipped = mIterator.IsOriginalCharSkipped(&length);
|
2013-01-15 04:22:03 -08:00
|
|
|
mRunLength = std::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
|
|
|
}
|