2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
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 "nsTArray.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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-01-10 13:42:05 +04:00
|
|
|
* The gfxSkipChars is represented as a sorted array of skipped ranges.
|
|
|
|
*
|
|
|
|
* A freshly-created gfxSkipChars means "all chars kept".
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
2014-01-10 13:42:05 +04:00
|
|
|
class gfxSkipChars {
|
2014-09-17 17:46:24 +04:00
|
|
|
friend struct SkippedRangeStartComparator;
|
|
|
|
friend struct SkippedRangeOffsetComparator;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
private:
|
|
|
|
class SkippedRange {
|
|
|
|
public:
|
|
|
|
SkippedRange(uint32_t aOffset, uint32_t aLength, uint32_t aDelta)
|
|
|
|
: mOffset(aOffset), mLength(aLength), mDelta(aDelta) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t Start() const { return mOffset; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t End() const { return mOffset + mLength; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t Length() const { return mLength; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t SkippedOffset() const { return mOffset - mDelta; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t Delta() const { return mDelta; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t NextDelta() const { return mDelta + mLength; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
void Extend(uint32_t aChars) { mLength += aChars; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
private:
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t mOffset; // original-string offset at which we want to skip
|
|
|
|
uint32_t mLength; // number of skipped chars at this offset
|
|
|
|
uint32_t mDelta; // sum of lengths of preceding skipped-ranges
|
|
|
|
};
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2008-12-17 05:14:33 +03:00
|
|
|
gfxSkipChars() : mCharCount(0) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
void SkipChars(uint32_t aChars) {
|
|
|
|
NS_ASSERTION(mCharCount + aChars > mCharCount, "Character count overflow");
|
|
|
|
uint32_t rangeCount = mRanges.Length();
|
|
|
|
uint32_t delta = 0;
|
|
|
|
if (rangeCount > 0) {
|
|
|
|
SkippedRange& lastRange = mRanges[rangeCount - 1];
|
|
|
|
if (lastRange.End() == mCharCount) {
|
|
|
|
lastRange.Extend(aChars);
|
|
|
|
mCharCount += aChars;
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-01-10 13:42:05 +04:00
|
|
|
delta = lastRange.NextDelta();
|
|
|
|
}
|
|
|
|
mRanges.AppendElement(SkippedRange(mCharCount, aChars, delta));
|
|
|
|
mCharCount += aChars;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-01-10 13:42:05 +04:00
|
|
|
|
|
|
|
void KeepChars(uint32_t aChars) {
|
|
|
|
NS_ASSERTION(mCharCount + aChars > mCharCount, "Character count overflow");
|
|
|
|
mCharCount += aChars;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkipChar() { SkipChars(1); }
|
|
|
|
|
|
|
|
void KeepChar() { KeepChars(1); }
|
|
|
|
|
|
|
|
void TakeFrom(gfxSkipChars* aSkipChars) {
|
|
|
|
mRanges.SwapElements(aSkipChars->mRanges);
|
|
|
|
mCharCount = aSkipChars->mCharCount;
|
2007-01-16 23:51:52 +03:00
|
|
|
aSkipChars->mCharCount = 0;
|
|
|
|
}
|
2014-01-10 13:42:05 +04:00
|
|
|
|
2008-12-17 05:14:33 +03:00
|
|
|
int32_t GetOriginalCharCount() const { return mCharCount; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
const SkippedRange& LastRange() const {
|
|
|
|
// this is only valid if mRanges is non-empty; no assertion here
|
|
|
|
// because nsTArray will already assert if we abuse it
|
|
|
|
return mRanges[mRanges.Length() - 1];
|
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:
|
2014-01-10 13:42:05 +04:00
|
|
|
nsTArray<SkippedRange> mRanges;
|
|
|
|
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.
|
2014-01-10 13:42:05 +04:00
|
|
|
*
|
2007-01-16 23:51:52 +03:00
|
|
|
* 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.
|
2014-01-10 13:42:05 +04:00
|
|
|
*
|
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
|
|
|
*/
|
2014-01-10 13:42:05 +04:00
|
|
|
class gfxSkipCharsIterator {
|
2007-01-16 23:51:52 +03:00
|
|
|
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),
|
2014-01-10 13:42:05 +04:00
|
|
|
mOriginalStringOffset(0),
|
|
|
|
mSkippedStringOffset(0),
|
|
|
|
mCurrentRangeIndex(-1),
|
|
|
|
mOriginalStringToSkipCharsOffset(aOriginalStringToSkipCharsOffset) {
|
2015-04-07 11:26:49 +03:00
|
|
|
SetOriginalOffset(aOriginalStringOffset);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-08-06 01:58:40 +04:00
|
|
|
explicit gfxSkipCharsIterator(const gfxSkipChars& aSkipChars,
|
|
|
|
int32_t aOriginalStringToSkipCharsOffset = 0)
|
2007-01-16 23:51:52 +03:00
|
|
|
: mSkipChars(&aSkipChars),
|
2014-01-10 13:42:05 +04:00
|
|
|
mOriginalStringOffset(0),
|
|
|
|
mSkippedStringOffset(0),
|
|
|
|
mOriginalStringToSkipCharsOffset(aOriginalStringToSkipCharsOffset) {
|
2015-04-07 11:26:49 +03:00
|
|
|
mCurrentRangeIndex =
|
|
|
|
mSkipChars->mRanges.IsEmpty() || mSkipChars->mRanges[0].Start() > 0 ? -1
|
|
|
|
: 0;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-03-04 18:39:20 +03:00
|
|
|
gfxSkipCharsIterator(const gfxSkipCharsIterator& aIterator) = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
|
|
|
* The empty constructor creates an object that is useless until it is
|
|
|
|
* assigned.
|
|
|
|
*/
|
2014-01-10 13:42:05 +04:00
|
|
|
gfxSkipCharsIterator()
|
2018-06-16 17:42:33 +03:00
|
|
|
: mSkipChars(nullptr),
|
|
|
|
mOriginalStringOffset(0),
|
|
|
|
mSkippedStringOffset(0),
|
|
|
|
mCurrentRangeIndex(0),
|
|
|
|
mOriginalStringToSkipCharsOffset(0) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-02-09 07:20:17 +03:00
|
|
|
/**
|
|
|
|
* Return true if this iterator is properly initialized and usable.
|
2014-01-10 13:42:05 +04:00
|
|
|
*/
|
2020-04-06 11:36:30 +03:00
|
|
|
bool IsInitialized() const { return mSkipChars != nullptr; }
|
2018-11-30 13:46:48 +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].
|
|
|
|
*/
|
2014-01-10 13:42:05 +04:00
|
|
|
void SetOriginalOffset(int32_t aOriginalStringOffset);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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].
|
|
|
|
*/
|
2014-01-10 13:42:05 +04:00
|
|
|
void SetSkippedOffset(uint32_t aSkippedStringOffset);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t ConvertOriginalToSkipped(int32_t aOriginalStringOffset) {
|
2007-01-16 23:51:52 +03:00
|
|
|
SetOriginalOffset(aOriginalStringOffset);
|
|
|
|
return GetSkippedOffset();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-30 09:23:10 +03:00
|
|
|
int32_t ConvertSkippedToOriginal(uint32_t aSkippedStringOffset) {
|
2007-01-16 23:51:52 +03:00
|
|
|
SetSkippedOffset(aSkippedStringOffset);
|
|
|
|
return GetOriginalOffset();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
void AdvanceOriginal(int32_t aDelta) {
|
|
|
|
SetOriginalOffset(GetOriginalOffset() + aDelta);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
void AdvanceSkipped(int32_t aDelta) {
|
|
|
|
SetSkippedOffset(GetSkippedOffset() + aDelta);
|
2007-01-16 23:51:52 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
|
|
|
* @return the offset within the original string
|
|
|
|
*/
|
2014-01-10 13:42:05 +04:00
|
|
|
int32_t GetOriginalOffset() const {
|
2007-01-16 23:51:52 +03:00
|
|
|
return mOriginalStringOffset - mOriginalStringToSkipCharsOffset;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2014-01-10 13:42:05 +04:00
|
|
|
uint32_t GetSkippedOffset() const { return mSkippedStringOffset; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
int32_t GetOriginalEnd() const {
|
2008-12-17 05:14:33 +03:00
|
|
|
return mSkipChars->GetOriginalCharCount() -
|
|
|
|
mOriginalStringToSkipCharsOffset;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
private:
|
|
|
|
const gfxSkipChars* mSkipChars;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
// Current position
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mOriginalStringOffset;
|
|
|
|
uint32_t mSkippedStringOffset;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-10 13:42:05 +04:00
|
|
|
// Index of the last skippedRange that precedes or contains the current
|
|
|
|
// position in the original string.
|
|
|
|
// If index == -1 then we are before the first skipped char.
|
|
|
|
int32_t mCurrentRangeIndex;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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;
|
2007-01-16 23:51:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /*GFX_SKIP_CHARS_H*/
|