2007-01-16 23:51:52 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04: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-01-16 23:51:52 +03:00
|
|
|
|
|
|
|
#ifndef GFX_SKIP_CHARS_H
|
|
|
|
#define GFX_SKIP_CHARS_H
|
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "gfxTypes.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gfxSkipChars is a data structure representing a list of characters that
|
|
|
|
* have been skipped. The initial string is called the "original string"
|
|
|
|
* and after skipping some characters, the result is called the "skipped string".
|
|
|
|
* gfxSkipChars provides efficient ways to translate between offsets in the
|
|
|
|
* original string and the skipped string. It is used by textrun code to keep
|
|
|
|
* track of offsets before and after text transformations such as whitespace
|
|
|
|
* compression and control code deletion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gfxSkipCharsBuilder is a helper class that accumulates a list of (skip, keep)
|
|
|
|
* commands and can eventually be used to construct a real gfxSkipChars.
|
|
|
|
* gfxSkipCharsBuilder objects are quite large so don't keep these around.
|
|
|
|
* On the positive side, the Skip/KeepChar(s) methods are very efficient,
|
|
|
|
* especially when you have runs of all-kept or all-skipped characters.
|
|
|
|
*
|
|
|
|
* mBuffer is an array of bytes; even numbered bytes represent characters kept,
|
|
|
|
* odd numbered bytes represent characters skipped. After those characters
|
|
|
|
* are accounted for, we have mRunCharCount characters which are kept or
|
|
|
|
* skipped depending on the value of mRunSkipped.
|
|
|
|
*
|
|
|
|
* mCharCount is the sum of counts of all skipped and kept characters, i.e.,
|
|
|
|
* the length of the original string.
|
|
|
|
*/
|
|
|
|
class THEBES_API gfxSkipCharsBuilder {
|
|
|
|
public:
|
|
|
|
gfxSkipCharsBuilder() :
|
2011-10-17 18:59:28 +04:00
|
|
|
mCharCount(0), mRunCharCount(0), mRunSkipped(false), mInErrorState(false)
|
2007-01-16 23:51:52 +03:00
|
|
|
{}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SkipChars(uint32_t aChars) {
|
2011-10-17 18:59:28 +04:00
|
|
|
DoChars(aChars, true);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
void KeepChars(uint32_t aChars) {
|
2011-10-17 18:59:28 +04:00
|
|
|
DoChars(aChars, false);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
|
|
|
void SkipChar() {
|
|
|
|
SkipChars(1);
|
|
|
|
}
|
|
|
|
void KeepChar() {
|
|
|
|
KeepChars(1);
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
void DoChars(uint32_t aChars, bool aSkipped) {
|
2007-01-16 23:51:52 +03:00
|
|
|
if (aSkipped != mRunSkipped && aChars > 0) {
|
|
|
|
FlushRun();
|
|
|
|
}
|
|
|
|
NS_ASSERTION(mRunCharCount + aChars > mRunCharCount,
|
|
|
|
"Character count overflow");
|
|
|
|
mRunCharCount += aChars;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsOK() { return !mInErrorState; }
|
2007-01-16 23:51:52 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t GetCharCount() { return mCharCount + mRunCharCount; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GetAllCharsKept() { return mBuffer.Length() == 0; }
|
2007-01-16 23:51:52 +03:00
|
|
|
|
|
|
|
friend class gfxSkipChars;
|
|
|
|
|
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef AutoFallibleTArray<uint8_t,256> Buffer;
|
2007-01-16 23:51:52 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves mRunCharCount/mRunSkipped to the buffer (updating mCharCount),
|
|
|
|
* sets mRunCharCount to zero and toggles mRunSkipped.
|
|
|
|
*/
|
|
|
|
void FlushRun();
|
|
|
|
|
|
|
|
Buffer mBuffer;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCharCount;
|
|
|
|
uint32_t mRunCharCount;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mRunSkipped; // == mBuffer.Length()&1
|
|
|
|
bool mInErrorState;
|
2007-01-16 23:51:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The gfxSkipChars list is represented as a list of bytes of the form
|
|
|
|
* [chars to keep, chars to skip, chars to keep, chars to skip, ...]
|
|
|
|
* In the special case where all chars are to be kept, the list is length
|
|
|
|
* zero.
|
|
|
|
*
|
|
|
|
* A freshly-created gfxSkipChars means "all chars kept".
|
|
|
|
*/
|
|
|
|
class THEBES_API gfxSkipChars {
|
|
|
|
public:
|
|
|
|
gfxSkipChars() : mListLength(0), mCharCount(0) {}
|
|
|
|
|
|
|
|
void TakeFrom(gfxSkipChars* aSkipChars) {
|
|
|
|
mList = aSkipChars->mList.forget();
|
|
|
|
mListLength = aSkipChars->mListLength;
|
|
|
|
mCharCount = aSkipChars->mCharCount;
|
|
|
|
aSkipChars->mCharCount = 0;
|
|
|
|
aSkipChars->mListLength = 0;
|
|
|
|
BuildShortcuts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TakeFrom(gfxSkipCharsBuilder* aSkipCharsBuilder) {
|
|
|
|
if (!aSkipCharsBuilder->mBuffer.Length()) {
|
|
|
|
NS_ASSERTION(!aSkipCharsBuilder->mRunSkipped, "out of sync");
|
|
|
|
// all characters kept
|
|
|
|
mCharCount = aSkipCharsBuilder->mRunCharCount;
|
2012-07-30 18:20:58 +04:00
|
|
|
mList = nullptr;
|
2007-01-16 23:51:52 +03:00
|
|
|
mListLength = 0;
|
|
|
|
} else {
|
|
|
|
aSkipCharsBuilder->FlushRun();
|
|
|
|
mCharCount = aSkipCharsBuilder->mCharCount;
|
2012-08-22 19:56:38 +04:00
|
|
|
mList = new uint8_t[aSkipCharsBuilder->mBuffer.Length()];
|
2007-01-16 23:51:52 +03:00
|
|
|
if (!mList) {
|
|
|
|
mListLength = 0;
|
|
|
|
} else {
|
|
|
|
mListLength = aSkipCharsBuilder->mBuffer.Length();
|
|
|
|
memcpy(mList, aSkipCharsBuilder->mBuffer.Elements(), mListLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aSkipCharsBuilder->mBuffer.Clear();
|
|
|
|
aSkipCharsBuilder->mCharCount = 0;
|
|
|
|
aSkipCharsBuilder->mRunCharCount = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
aSkipCharsBuilder->mRunSkipped = false;
|
2007-01-16 23:51:52 +03:00
|
|
|
BuildShortcuts();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetAllKeep(uint32_t aLength) {
|
2007-01-16 23:51:52 +03:00
|
|
|
mCharCount = aLength;
|
2012-07-30 18:20:58 +04:00
|
|
|
mList = nullptr;
|
2007-01-16 23:51:52 +03:00
|
|
|
mListLength = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetOriginalCharCount() const { return mCharCount; }
|
2007-01-16 23:51:52 +03:00
|
|
|
|
2007-05-31 02:20:11 +04:00
|
|
|
friend class gfxSkipCharsIterator;
|
2007-01-16 23:51:52 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Shortcut {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mListPrefixLength;
|
|
|
|
uint32_t mListPrefixCharCount;
|
|
|
|
uint32_t mListPrefixKeepCharCount;
|
2007-01-16 23:51:52 +03:00
|
|
|
|
|
|
|
Shortcut() {}
|
2012-08-22 19:56:38 +04:00
|
|
|
Shortcut(uint32_t aListPrefixLength, uint32_t aListPrefixCharCount,
|
|
|
|
uint32_t aListPrefixKeepCharCount) :
|
2007-01-16 23:51:52 +03:00
|
|
|
mListPrefixLength(aListPrefixLength),
|
|
|
|
mListPrefixCharCount(aListPrefixCharCount),
|
|
|
|
mListPrefixKeepCharCount(aListPrefixKeepCharCount) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
void BuildShortcuts();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAutoArrayPtr<uint8_t> mList;
|
2007-01-16 23:51:52 +03:00
|
|
|
nsAutoArrayPtr<Shortcut> mShortcuts;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mListLength;
|
|
|
|
uint32_t mCharCount;
|
2007-01-16 23:51:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A gfxSkipCharsIterator represents a position in the original string. It lets you
|
|
|
|
* map efficiently to and from positions in the string after skipped characters
|
|
|
|
* have been removed. You can also specify an offset that is added to all
|
|
|
|
* incoming original string offsets and subtracted from all outgoing original
|
|
|
|
* string offsets --- useful when the gfxSkipChars corresponds to something
|
|
|
|
* offset from the original DOM coordinates, which it often does for gfxTextRuns.
|
|
|
|
*
|
|
|
|
* The current positions (in both the original and skipped strings) are
|
|
|
|
* always constrained to be >= 0 and <= the string length. When the position
|
|
|
|
* is equal to the string length, it is at the end of the string. The current
|
|
|
|
* positions do not include any aOriginalStringToSkipCharsOffset.
|
2007-05-31 02:20:11 +04:00
|
|
|
*
|
|
|
|
* When the position in the original string corresponds to a skipped character,
|
|
|
|
* the skipped-characters offset is the offset of the next unskipped character,
|
|
|
|
* or the skipped-characters string length if there is no next unskipped character.
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
|
|
|
class THEBES_API gfxSkipCharsIterator {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aOriginalStringToSkipCharsOffset add this to all incoming and
|
|
|
|
* outgoing original string offsets
|
|
|
|
*/
|
|
|
|
gfxSkipCharsIterator(const gfxSkipChars& aSkipChars,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aOriginalStringToSkipCharsOffset,
|
|
|
|
int32_t aOriginalStringOffset)
|
2007-01-16 23:51:52 +03:00
|
|
|
: mSkipChars(&aSkipChars),
|
|
|
|
mOriginalStringToSkipCharsOffset(aOriginalStringToSkipCharsOffset),
|
|
|
|
mListPrefixLength(0), mListPrefixCharCount(0), mListPrefixKeepCharCount(0) {
|
|
|
|
SetOriginalOffset(aOriginalStringOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxSkipCharsIterator(const gfxSkipChars& aSkipChars,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aOriginalStringToSkipCharsOffset = 0)
|
2007-01-16 23:51:52 +03:00
|
|
|
: mSkipChars(&aSkipChars),
|
|
|
|
mOriginalStringOffset(0), mSkippedStringOffset(0),
|
|
|
|
mOriginalStringToSkipCharsOffset(aOriginalStringToSkipCharsOffset),
|
|
|
|
mListPrefixLength(0), mListPrefixCharCount(0), mListPrefixKeepCharCount(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxSkipCharsIterator(const gfxSkipCharsIterator& aIterator)
|
|
|
|
: mSkipChars(aIterator.mSkipChars),
|
|
|
|
mOriginalStringOffset(aIterator.mOriginalStringOffset),
|
|
|
|
mSkippedStringOffset(aIterator.mSkippedStringOffset),
|
|
|
|
mOriginalStringToSkipCharsOffset(aIterator.mOriginalStringToSkipCharsOffset),
|
|
|
|
mListPrefixLength(aIterator.mListPrefixLength),
|
|
|
|
mListPrefixCharCount(aIterator.mListPrefixCharCount),
|
|
|
|
mListPrefixKeepCharCount(aIterator.mListPrefixKeepCharCount)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The empty constructor creates an object that is useless until it is assigned.
|
|
|
|
*/
|
2012-07-30 18:20:58 +04:00
|
|
|
gfxSkipCharsIterator() : mSkipChars(nullptr) {}
|
2007-02-09 07:20:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if this iterator is properly initialized and usable.
|
|
|
|
*/
|
2012-07-30 18:20:58 +04:00
|
|
|
bool IsInitialized() { return mSkipChars != nullptr; }
|
2007-02-09 07:20:17 +03:00
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
|
|
|
* Set the iterator to aOriginalStringOffset in the original string.
|
|
|
|
* This can efficiently move forward or backward from the current position.
|
|
|
|
* aOriginalStringOffset is clamped to [0,originalStringLength].
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetOriginalOffset(int32_t aOriginalStringOffset) {
|
2011-10-17 18:59:28 +04:00
|
|
|
SetOffsets(aOriginalStringOffset + mOriginalStringToSkipCharsOffset, true);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the iterator to aSkippedStringOffset in the skipped string.
|
|
|
|
* This can efficiently move forward or backward from the current position.
|
|
|
|
* aSkippedStringOffset is clamped to [0,skippedStringLength].
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetSkippedOffset(uint32_t aSkippedStringOffset) {
|
2011-10-17 18:59:28 +04:00
|
|
|
SetOffsets(aSkippedStringOffset, false);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ConvertOriginalToSkipped(int32_t aOriginalStringOffset) {
|
2007-01-16 23:51:52 +03:00
|
|
|
SetOriginalOffset(aOriginalStringOffset);
|
|
|
|
return GetSkippedOffset();
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ConvertSkippedToOriginal(int32_t aSkippedStringOffset) {
|
2007-01-16 23:51:52 +03:00
|
|
|
SetSkippedOffset(aSkippedStringOffset);
|
|
|
|
return GetOriginalOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the character at the current position in the original string
|
|
|
|
* is skipped or not. If aRunLength is non-null, then *aRunLength is set
|
|
|
|
* to a number of characters all of which are either skipped or not, starting
|
|
|
|
* at this character. When the current position is at the end of the original
|
2011-10-17 18:59:28 +04:00
|
|
|
* string, we return true and *aRunLength is set to zero.
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
bool IsOriginalCharSkipped(int32_t* aRunLength = nullptr) const;
|
2007-01-16 23:51:52 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void AdvanceOriginal(int32_t aDelta) {
|
2011-10-17 18:59:28 +04:00
|
|
|
SetOffsets(mOriginalStringOffset + aDelta, true);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
void AdvanceSkipped(int32_t aDelta) {
|
2011-10-17 18:59:28 +04:00
|
|
|
SetOffsets(mSkippedStringOffset + aDelta, false);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the offset within the original string
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetOriginalOffset() const {
|
2007-01-16 23:51:52 +03:00
|
|
|
return mOriginalStringOffset - mOriginalStringToSkipCharsOffset;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return the offset within the skipped string corresponding to the
|
|
|
|
* current position in the original string. If the current position
|
|
|
|
* in the original string is a character that is skipped, then we return
|
|
|
|
* the position corresponding to the first non-skipped character in the
|
|
|
|
* original string after the current position, or the length of the skipped
|
|
|
|
* string if there is no such character.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t GetSkippedOffset() const { return mSkippedStringOffset; }
|
2008-12-17 05:14:33 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetOriginalEnd() const {
|
2008-12-17 05:14:33 +03:00
|
|
|
return mSkipChars->GetOriginalCharCount() -
|
|
|
|
mOriginalStringToSkipCharsOffset;
|
|
|
|
}
|
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetOffsets(uint32_t aOffset, bool aInOriginalString);
|
2007-01-16 23:51:52 +03:00
|
|
|
|
|
|
|
const gfxSkipChars* mSkipChars;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mOriginalStringOffset;
|
|
|
|
uint32_t mSkippedStringOffset;
|
2008-12-17 05:14:33 +03:00
|
|
|
|
|
|
|
// This offset is added to map from "skipped+unskipped characters in
|
|
|
|
// the original DOM string" character space to "skipped+unskipped
|
|
|
|
// characters in the textrun's gfxSkipChars" character space
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mOriginalStringToSkipCharsOffset;
|
2008-12-17 05:14:33 +03:00
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
/*
|
|
|
|
* This is used to speed up cursor-style traversal. The invariant is that
|
|
|
|
* the first mListPrefixLength bytes of mSkipChars.mList sum to
|
|
|
|
* mListPrefixCharCount, and the even-indexed bytes in that prefix sum to
|
|
|
|
* mListPrefixKeepCharCount.
|
|
|
|
* Also, 0 <= mListPrefixLength < mSkipChars.mListLength, or else
|
|
|
|
* mSkipChars.mListLength is zero.
|
|
|
|
* Also, mListPrefixCharCount <= mOriginalStringOffset (and therefore
|
|
|
|
* mListPrefixKeepCharCount < mSkippedStringOffset).
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mListPrefixLength;
|
|
|
|
uint32_t mListPrefixCharCount;
|
|
|
|
uint32_t mListPrefixKeepCharCount;
|
2007-01-16 23:51:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /*GFX_SKIP_CHARS_H*/
|