2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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 NSLINEBREAKER_H_
|
|
|
|
#define NSLINEBREAKER_H_
|
|
|
|
|
2017-12-19 12:19:35 +03:00
|
|
|
#include "mozilla/intl/LineBreaker.h"
|
2007-01-16 23:51:52 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
class nsAtom;
|
2011-05-04 15:29:56 +04:00
|
|
|
class nsHyphenator;
|
2007-01-16 23:51:52 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A receiver of line break data.
|
|
|
|
*/
|
|
|
|
class nsILineBreakSink {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Sets the break data for a substring of the associated text chunk.
|
|
|
|
* One or more of these calls will be performed; the union of all substrings
|
|
|
|
* will cover the entire text chunk. Substrings may overlap (i.e., we may
|
|
|
|
* set the break-before state of a character more than once).
|
|
|
|
* @param aBreakBefore the break-before states for the characters in the
|
2011-05-04 15:29:56 +04:00
|
|
|
* substring. These are enum values from gfxTextRun::CompressedGlyph:
|
|
|
|
* FLAG_BREAK_TYPE_NONE - no linebreak is allowed here
|
|
|
|
* FLAG_BREAK_TYPE_NORMAL - a normal (whitespace) linebreak
|
|
|
|
* FLAG_BREAK_TYPE_HYPHEN - a hyphenation point
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual void SetBreaks(uint32_t aStart, uint32_t aLength,
|
|
|
|
uint8_t* aBreakBefore) = 0;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2007-11-12 04:51:31 +03:00
|
|
|
/**
|
|
|
|
* Indicates which characters should be capitalized. Only called if
|
|
|
|
* BREAK_NEED_CAPITALIZATION was requested.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual void SetCapitalization(uint32_t aStart, uint32_t aLength,
|
|
|
|
bool* aCapitalize) = 0;
|
2007-01-16 23:51:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A line-breaking state machine. You feed text into it via AppendText calls
|
|
|
|
* and it computes the possible line breaks. Because break decisions can
|
|
|
|
* require a lot of context, the breaks for a piece of text are sometimes not
|
|
|
|
* known until later text has been seen (or all text ends). So breaks are
|
|
|
|
* returned via a call to SetBreaks on the nsILineBreakSink object passed
|
|
|
|
* with each text chunk, which might happen during the corresponding AppendText
|
|
|
|
* call, or might happen during a later AppendText call or even a Reset()
|
|
|
|
* call.
|
2017-07-06 15:00:35 +03:00
|
|
|
*
|
2007-01-16 23:51:52 +03:00
|
|
|
* The linebreak results MUST NOT depend on how the text is broken up
|
|
|
|
* into AppendText calls.
|
2017-07-06 15:00:35 +03:00
|
|
|
*
|
2007-01-16 23:51:52 +03:00
|
|
|
* The current strategy is that we break the overall text into
|
2017-12-14 07:17:32 +03:00
|
|
|
* whitespace-delimited "words". Then those words are passed to the LineBreaker
|
2007-10-20 11:30:26 +04:00
|
|
|
* service for deeper analysis if they contain a "complex" character as
|
|
|
|
* described below.
|
2017-07-06 15:00:35 +03:00
|
|
|
*
|
2007-11-12 04:51:31 +03:00
|
|
|
* This class also handles detection of which characters should be capitalized
|
|
|
|
* for text-transform:capitalize. This is a good place to handle that because
|
|
|
|
* we have all the context we need.
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
|
|
|
class nsLineBreaker {
|
|
|
|
public:
|
|
|
|
nsLineBreaker();
|
|
|
|
~nsLineBreaker();
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2017-12-14 07:17:32 +03:00
|
|
|
static inline bool IsSpace(char16_t u) {
|
|
|
|
return mozilla::intl::NS_IsSpace(u);
|
|
|
|
}
|
2007-06-07 08:17:55 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
static inline bool IsComplexASCIIChar(char16_t u) {
|
2007-07-12 19:52:51 +04:00
|
|
|
return !((0x0030 <= u && u <= 0x0039) || (0x0041 <= u && u <= 0x005A) ||
|
2010-07-27 04:05:11 +04:00
|
|
|
(0x0061 <= u && u <= 0x007A) || (0x000a == u));
|
2007-07-12 19:52:51 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
static inline bool IsComplexChar(char16_t u) {
|
2007-07-12 19:52:51 +04:00
|
|
|
return IsComplexASCIIChar(u) ||
|
2017-12-14 07:17:32 +03:00
|
|
|
mozilla::intl::NS_NeedsPlatformNativeHandling(u) ||
|
2007-07-19 07:26:51 +04:00
|
|
|
(0x1100 <= u && u <= 0x11ff) || // Hangul Jamo
|
|
|
|
(0x2000 <= u && u <= 0x21ff) || // Punctuations and Symbols
|
|
|
|
(0x2e80 <= u && u <= 0xd7ff) || // several CJK blocks
|
|
|
|
(0xf900 <= u && u <= 0xfaff) || // CJK Compatibility Idographs
|
|
|
|
(0xff00 <= u && u <= 0xffef); // Halfwidth and Fullwidth Forms
|
2007-06-07 08:17:55 +04:00
|
|
|
}
|
2007-01-16 23:51:52 +03:00
|
|
|
|
2007-10-20 11:30:26 +04:00
|
|
|
// Break opportunities exist at the end of each run of breakable whitespace
|
|
|
|
// (see IsSpace above). Break opportunities can also exist between pairs of
|
2017-12-14 07:17:32 +03:00
|
|
|
// non-whitespace characters, as determined by mozilla::intl::LineBreaker.
|
|
|
|
// We pass a whitespace-
|
|
|
|
// delimited word to LineBreaker if it contains at least one character
|
2007-06-07 08:17:55 +04:00
|
|
|
// matching IsComplexChar.
|
2007-05-31 02:27:27 +04:00
|
|
|
// We provide flags to control on a per-chunk basis where breaks are allowed.
|
|
|
|
// At any character boundary, exactly one text chunk governs whether a
|
|
|
|
// break is allowed at that boundary.
|
2007-05-23 03:45:47 +04:00
|
|
|
//
|
2007-05-31 02:27:27 +04:00
|
|
|
// We operate on text after whitespace processing has been applied, so
|
2007-05-23 03:45:47 +04:00
|
|
|
// other characters (e.g. tabs and newlines) may have been converted to
|
|
|
|
// spaces.
|
2007-10-20 11:30:26 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flags passed with each chunk of text.
|
|
|
|
*/
|
2007-01-16 23:51:52 +03:00
|
|
|
enum {
|
2007-10-20 11:30:26 +04:00
|
|
|
/*
|
|
|
|
* Do not introduce a break opportunity at the start of this chunk of text.
|
|
|
|
*/
|
|
|
|
BREAK_SUPPRESS_INITIAL = 0x01,
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
2007-10-20 11:30:26 +04:00
|
|
|
* Do not introduce a break opportunity in the interior of this chunk of
|
|
|
|
* text. Also, whitespace in this chunk is treated as non-breakable.
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
2007-10-20 11:30:26 +04:00
|
|
|
BREAK_SUPPRESS_INSIDE = 0x02,
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
2007-10-20 11:30:26 +04:00
|
|
|
* The sink currently is already set up to have no breaks in it;
|
|
|
|
* if no breaks are possible, nsLineBreaker does not need to call
|
|
|
|
* SetBreaks on it. This is useful when handling large quantities of
|
|
|
|
* preformatted text; the textruns will never have any breaks set on them,
|
|
|
|
* and there is no need to ever actually scan the text for breaks, except
|
|
|
|
* at the end of textruns in case context is needed for following breakable
|
|
|
|
* text.
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
2007-11-12 04:51:31 +03:00
|
|
|
BREAK_SKIP_SETTING_NO_BREAKS = 0x04,
|
|
|
|
/**
|
|
|
|
* We need to be notified of characters that should be capitalized
|
|
|
|
* (as in text-transform:capitalize) in this chunk of text.
|
|
|
|
*/
|
2011-05-04 15:29:56 +04:00
|
|
|
BREAK_NEED_CAPITALIZATION = 0x08,
|
|
|
|
/**
|
|
|
|
* Auto-hyphenation is enabled, so we need to get a hyphenator
|
|
|
|
* (if available) and use it to find breakpoints.
|
|
|
|
*/
|
|
|
|
BREAK_USE_AUTO_HYPHENATION = 0x10
|
2007-01-16 23:51:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2007-05-31 02:27:27 +04:00
|
|
|
* Append "invisible whitespace". This acts like whitespace, but there is
|
2007-10-20 11:30:26 +04:00
|
|
|
* no actual text associated with it. Only the BREAK_SUPPRESS_INSIDE flag
|
|
|
|
* is relevant here.
|
2007-05-31 02:27:27 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult AppendInvisibleWhitespace(uint32_t aFlags);
|
2007-05-31 02:27:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Feed Unicode text into the linebreaker for analysis. aLength must be
|
|
|
|
* nonzero.
|
2007-06-07 08:17:55 +04:00
|
|
|
* @param aSink can be null if the breaks are not actually needed (we may
|
|
|
|
* still be setting up state for later breaks)
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult AppendText(nsAtom* aHyphenationLanguage, const char16_t* aText,
|
|
|
|
uint32_t aLength, uint32_t aFlags,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsILineBreakSink* aSink);
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
2007-05-31 02:27:27 +04:00
|
|
|
* Feed 8-bit text into the linebreaker for analysis. aLength must be nonzero.
|
2007-06-07 08:17:55 +04:00
|
|
|
* @param aSink can be null if the breaks are not actually needed (we may
|
|
|
|
* still be setting up state for later breaks)
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult AppendText(nsAtom* aHyphenationLanguage, const uint8_t* aText,
|
|
|
|
uint32_t aLength, uint32_t aFlags,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsILineBreakSink* aSink);
|
2007-01-16 23:51:52 +03:00
|
|
|
/**
|
|
|
|
* Reset all state. This means the current run has ended; any outstanding
|
|
|
|
* calls through nsILineBreakSink are made, and all outstanding references to
|
|
|
|
* nsILineBreakSink objects are dropped.
|
|
|
|
* After this call, this linebreaker can be reused.
|
|
|
|
* This must be called at least once between any call to AppendText() and
|
|
|
|
* destroying the object.
|
2007-12-03 11:22:07 +03:00
|
|
|
* @param aTrailingBreak this is set to true when there is a break opportunity
|
|
|
|
* at the end of the text. This will normally only be declared true when there
|
|
|
|
* is breakable whitespace at the end.
|
2007-01-16 23:51:52 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult Reset(bool* aTrailingBreak);
|
2007-01-16 23:51:52 +03:00
|
|
|
|
2012-05-07 23:18:23 +04:00
|
|
|
/*
|
|
|
|
* Set word-break mode for linebreaker. This is set by word-break property.
|
2019-05-20 23:46:35 +03:00
|
|
|
* @param aMode is LineBreaker::WordBreak::* value.
|
2012-05-07 23:18:23 +04:00
|
|
|
*/
|
2019-05-20 23:46:35 +03:00
|
|
|
void SetWordBreak(mozilla::intl::LineBreaker::WordBreak aMode) {
|
2019-05-08 18:09:44 +03:00
|
|
|
// If current word is non-empty and mode is changing, flush the breaker.
|
|
|
|
if (aMode != mWordBreak && !mCurrentWord.IsEmpty()) {
|
|
|
|
nsresult rv = FlushCurrentWord();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("FlushCurrentWord failed, line-breaks may be wrong");
|
|
|
|
}
|
|
|
|
// If previous mode was break-all, we should allow a break here.
|
|
|
|
// XXX (jfkthame) css-text spec seems unclear on this, raised question in
|
|
|
|
// https://github.com/w3c/csswg-drafts/issues/3897
|
2019-05-20 23:46:35 +03:00
|
|
|
if (mWordBreak == mozilla::intl::LineBreaker::WordBreak::BreakAll) {
|
2019-05-08 18:09:44 +03:00
|
|
|
mBreakHere = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mWordBreak = aMode;
|
|
|
|
}
|
2012-05-07 23:18:23 +04:00
|
|
|
|
2019-05-20 23:46:35 +03:00
|
|
|
/*
|
|
|
|
* Set line-break rule strictness mode for linebreaker. This is set by the
|
|
|
|
* line-break property.
|
|
|
|
* @param aMode is LineBreaker::Strictness::* value.
|
|
|
|
*/
|
|
|
|
void SetStrictness(mozilla::intl::LineBreaker::Strictness aMode) {
|
|
|
|
if (aMode != mStrictness && !mCurrentWord.IsEmpty()) {
|
|
|
|
nsresult rv = FlushCurrentWord();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("FlushCurrentWord failed, line-breaks may be wrong");
|
|
|
|
}
|
|
|
|
// If previous mode was anywhere, we should allow a break here.
|
|
|
|
if (mStrictness == mozilla::intl::LineBreaker::Strictness::Anywhere) {
|
|
|
|
mBreakHere = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mStrictness = aMode;
|
|
|
|
}
|
|
|
|
|
2019-05-22 14:33:21 +03:00
|
|
|
/**
|
|
|
|
* Return whether the line-breaker has a buffered "current word" that may
|
|
|
|
* be extended with additional word-forming characters.
|
|
|
|
*/
|
|
|
|
bool InWord() const { return !mCurrentWord.IsEmpty(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the word-continuation state, which will suppress capitalization of
|
|
|
|
* the next letter that might otherwise apply.
|
|
|
|
*/
|
|
|
|
void SetWordContinuation(bool aContinuation) {
|
|
|
|
mWordContinuation = aContinuation;
|
|
|
|
}
|
|
|
|
|
2007-01-16 23:51:52 +03:00
|
|
|
private:
|
2007-05-31 02:27:27 +04:00
|
|
|
// This is a list of text sources that make up the "current word" (i.e.,
|
|
|
|
// run of text which does not contain any whitespace). All the mLengths
|
|
|
|
// are are nonzero, these cannot overlap.
|
2007-01-16 23:51:52 +03:00
|
|
|
struct TextItem {
|
2012-08-22 19:56:38 +04:00
|
|
|
TextItem(nsILineBreakSink* aSink, uint32_t aSinkOffset, uint32_t aLength,
|
|
|
|
uint32_t aFlags)
|
2007-01-16 23:51:52 +03:00
|
|
|
: mSink(aSink),
|
|
|
|
mSinkOffset(aSinkOffset),
|
|
|
|
mLength(aLength),
|
|
|
|
mFlags(aFlags) {}
|
|
|
|
|
|
|
|
nsILineBreakSink* mSink;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mSinkOffset;
|
|
|
|
uint32_t mLength;
|
|
|
|
uint32_t mFlags;
|
2007-01-16 23:51:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// State for the nonwhitespace "word" that started in previous text and hasn't
|
|
|
|
// finished yet.
|
|
|
|
|
|
|
|
// When the current word ends, this computes the linebreak opportunities
|
|
|
|
// *inside* the word (excluding either end) and sets them through the
|
|
|
|
// appropriate sink(s). Then we clear the current word state.
|
|
|
|
nsresult FlushCurrentWord();
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
void UpdateCurrentWordLanguage(nsAtom* aHyphenationLanguage);
|
2011-05-04 15:29:56 +04:00
|
|
|
|
|
|
|
void FindHyphenationPoints(nsHyphenator* aHyphenator,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aTextStart,
|
|
|
|
const char16_t* aTextLimit, uint8_t* aBreakState);
|
2011-05-04 15:29:56 +04:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<char16_t, 100> mCurrentWord;
|
2007-01-16 23:51:52 +03:00
|
|
|
// All the items that contribute to mCurrentWord
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<TextItem, 2> mTextItems;
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* mCurrentWordLanguage;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mCurrentWordContainsMixedLang;
|
|
|
|
bool mCurrentWordContainsComplexChar;
|
2019-05-20 23:47:09 +03:00
|
|
|
bool mScriptIsChineseOrJapanese;
|
2007-01-16 23:51:52 +03:00
|
|
|
|
2007-10-20 11:30:26 +04:00
|
|
|
// True if the previous character was breakable whitespace
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mAfterBreakableSpace;
|
2007-10-20 11:30:26 +04:00
|
|
|
// True if a break must be allowed at the current position because
|
|
|
|
// a run of breakable whitespace ends here
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mBreakHere;
|
2012-05-07 23:18:23 +04:00
|
|
|
// line break mode by "word-break" style
|
2019-05-20 23:46:35 +03:00
|
|
|
mozilla::intl::LineBreaker::WordBreak mWordBreak;
|
|
|
|
// strictness of break rules, from line-break property
|
|
|
|
mozilla::intl::LineBreaker::Strictness mStrictness;
|
2019-05-22 14:33:21 +03:00
|
|
|
// Should the text be treated as continuing a word-in-progress (for purposes
|
|
|
|
// of initial capitalization)? Normally this is set to false whenever we
|
|
|
|
// start using a linebreaker, but it may be set to true if the line-breaker
|
|
|
|
// has been explicitly flushed mid-word.
|
|
|
|
bool mWordContinuation;
|
2007-01-16 23:51:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /*NSLINEBREAKER_H_*/
|