|
|
@ -21,6 +21,9 @@
|
|
|
|
#endif /* WITH_ICU */
|
|
|
|
#endif /* WITH_ICU */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Cache of ParagraphLayout lines. */
|
|
|
|
|
|
|
|
Layouter::LineCache Layouter::linecache;
|
|
|
|
|
|
|
|
|
|
|
|
/** Cache of Font instances. */
|
|
|
|
/** Cache of Font instances. */
|
|
|
|
Layouter::FontColourMap Layouter::fonts[FS_END];
|
|
|
|
Layouter::FontColourMap Layouter::fonts[FS_END];
|
|
|
|
|
|
|
|
|
|
|
@ -134,6 +137,8 @@ ParagraphLayout *Layouter::GetParagraphLayout(UChar *buff, UChar *buff_end, Font
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LEErrorCode status = LE_NO_ERROR;
|
|
|
|
LEErrorCode status = LE_NO_ERROR;
|
|
|
|
|
|
|
|
/* ParagraphLayout does not copy "buff", so it must stay valid.
|
|
|
|
|
|
|
|
* "runs" is copied according to the ICU source, but the documentation does not specify anything, so this might break somewhen. */
|
|
|
|
return new ParagraphLayout(buff, length, &runs, NULL, NULL, NULL, _current_text_dir == TD_RTL ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, false, status);
|
|
|
|
return new ParagraphLayout(buff, length, &runs, NULL, NULL, NULL, _current_text_dir == TD_RTL ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, false, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -277,6 +282,14 @@ ParagraphLayout::ParagraphLayout(WChar *buffer, int length, FontMap &runs) : buf
|
|
|
|
assert(runs.End()[-1].first == length);
|
|
|
|
assert(runs.End()[-1].first == length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Reset the position to the start of the paragraph.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ParagraphLayout::reflow()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
this->buffer = this->buffer_begin;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Construct a new line with a maximum width.
|
|
|
|
* Construct a new line with a maximum width.
|
|
|
|
* @param max_width The maximum width of the string.
|
|
|
|
* @param max_width The maximum width of the string.
|
|
|
@ -300,7 +313,7 @@ ParagraphLayout::Line *ParagraphLayout::nextLine(int max_width)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const WChar *begin = this->buffer;
|
|
|
|
const WChar *begin = this->buffer;
|
|
|
|
WChar *last_space = NULL;
|
|
|
|
const WChar *last_space = NULL;
|
|
|
|
const WChar *last_char = begin;
|
|
|
|
const WChar *last_char = begin;
|
|
|
|
int width = 0;
|
|
|
|
int width = 0;
|
|
|
|
|
|
|
|
|
|
|
@ -412,62 +425,77 @@ ParagraphLayout *Layouter::GetParagraphLayout(WChar *buff, WChar *buff_end, Font
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize)
|
|
|
|
Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const CharType *buffer_last = lastof(this->buffer);
|
|
|
|
|
|
|
|
CharType *buff = this->buffer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FontState state(colour, fontsize);
|
|
|
|
FontState state(colour, fontsize);
|
|
|
|
WChar c = 0;
|
|
|
|
WChar c = 0;
|
|
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
Font *f = GetFont(state.fontsize, state.cur_colour);
|
|
|
|
/* Scan string for end of line */
|
|
|
|
CharType *buff_begin = buff;
|
|
|
|
const char *lineend = str;
|
|
|
|
FontMap fontMapping;
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
size_t len = Utf8Decode(&c, lineend);
|
|
|
|
/*
|
|
|
|
if (c == '\0' || c == '\n') break;
|
|
|
|
* Go through the whole string while adding Font instances to the font map
|
|
|
|
lineend += len;
|
|
|
|
* whenever the font changes, and convert the wide characters into a format
|
|
|
|
}
|
|
|
|
* usable by ParagraphLayout.
|
|
|
|
|
|
|
|
*/
|
|
|
|
LineCacheItem& line = GetCachedParagraphLayout(str, lineend - str, state);
|
|
|
|
for (; buff < buffer_last;) {
|
|
|
|
if (line.layout != NULL) {
|
|
|
|
c = Utf8Consume(const_cast<const char **>(&str));
|
|
|
|
/* Line is in cache */
|
|
|
|
if (c == '\0' || c == '\n') {
|
|
|
|
str = lineend + 1;
|
|
|
|
break;
|
|
|
|
state = line.state_after;
|
|
|
|
} else if (c >= SCC_BLUE && c <= SCC_BLACK) {
|
|
|
|
line.layout->reflow();
|
|
|
|
state.SetColour((TextColour)(c - SCC_BLUE));
|
|
|
|
} else {
|
|
|
|
} else if (c == SCC_PREVIOUS_COLOUR) { // Revert to the previous colour.
|
|
|
|
/* Line is new, layout it */
|
|
|
|
state.SetPreviousColour();
|
|
|
|
const CharType *buffer_last = lastof(line.buffer);
|
|
|
|
} else if (c == SCC_TINYFONT) {
|
|
|
|
CharType *buff_begin = line.buffer;
|
|
|
|
state.SetFontSize(FS_SMALL);
|
|
|
|
CharType *buff = buff_begin;
|
|
|
|
} else if (c == SCC_BIGFONT) {
|
|
|
|
FontMap &fontMapping = line.runs;
|
|
|
|
state.SetFontSize(FS_LARGE);
|
|
|
|
Font *f = GetFont(state.fontsize, state.cur_colour);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
buff += AppendToBuffer(buff, buffer_last, c);
|
|
|
|
/*
|
|
|
|
continue;
|
|
|
|
* Go through the whole string while adding Font instances to the font map
|
|
|
|
|
|
|
|
* whenever the font changes, and convert the wide characters into a format
|
|
|
|
|
|
|
|
* usable by ParagraphLayout.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (; buff < buffer_last;) {
|
|
|
|
|
|
|
|
c = Utf8Consume(const_cast<const char **>(&str));
|
|
|
|
|
|
|
|
if (c == '\0' || c == '\n') {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else if (c >= SCC_BLUE && c <= SCC_BLACK) {
|
|
|
|
|
|
|
|
state.SetColour((TextColour)(c - SCC_BLUE));
|
|
|
|
|
|
|
|
} else if (c == SCC_PREVIOUS_COLOUR) { // Revert to the previous colour.
|
|
|
|
|
|
|
|
state.SetPreviousColour();
|
|
|
|
|
|
|
|
} else if (c == SCC_TINYFONT) {
|
|
|
|
|
|
|
|
state.SetFontSize(FS_SMALL);
|
|
|
|
|
|
|
|
} else if (c == SCC_BIGFONT) {
|
|
|
|
|
|
|
|
state.SetFontSize(FS_LARGE);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
buff += AppendToBuffer(buff, buffer_last, c);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!fontMapping.Contains(buff - buff_begin)) {
|
|
|
|
|
|
|
|
fontMapping.Insert(buff - buff_begin, f);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
f = GetFont(state.fontsize, state.cur_colour);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Better safe than sorry. */
|
|
|
|
|
|
|
|
*buff = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
if (!fontMapping.Contains(buff - buff_begin)) {
|
|
|
|
if (!fontMapping.Contains(buff - buff_begin)) {
|
|
|
|
fontMapping.Insert(buff - buff_begin, f);
|
|
|
|
fontMapping.Insert(buff - buff_begin, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f = GetFont(state.fontsize, state.cur_colour);
|
|
|
|
line.layout = GetParagraphLayout(buff_begin, buff, fontMapping);
|
|
|
|
}
|
|
|
|
line.state_after = state;
|
|
|
|
|
|
|
|
|
|
|
|
/* Better safe than sorry. */
|
|
|
|
|
|
|
|
*buff = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!fontMapping.Contains(buff - buff_begin)) {
|
|
|
|
|
|
|
|
fontMapping.Insert(buff - buff_begin, f);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ParagraphLayout *p = GetParagraphLayout(buff_begin, buff, fontMapping);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Copy all lines into a local cache so we can reuse them later on more easily. */
|
|
|
|
/* Copy all lines into a local cache so we can reuse them later on more easily. */
|
|
|
|
ParagraphLayout::Line *l;
|
|
|
|
ParagraphLayout::Line *l;
|
|
|
|
while ((l = p->nextLine(maxw)) != NULL) {
|
|
|
|
while ((l = line.layout->nextLine(maxw)) != NULL) {
|
|
|
|
*this->Append() = l;
|
|
|
|
*this->Append() = l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
delete p;
|
|
|
|
} while (c != '\0');
|
|
|
|
|
|
|
|
|
|
|
|
} while (c != '\0' && buff < buffer_last);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -507,4 +535,40 @@ void Layouter::ResetFontCache(FontSize size)
|
|
|
|
delete it->second;
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fonts[size].Clear();
|
|
|
|
fonts[size].Clear();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* We must reset the linecache since it references the just freed fonts */
|
|
|
|
|
|
|
|
ResetLineCache();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Get reference to cache item.
|
|
|
|
|
|
|
|
* If the item does not exist yet, it is default constructed.
|
|
|
|
|
|
|
|
* @param str Source string of the line (including colour and font size codes).
|
|
|
|
|
|
|
|
* @param len Length of \a str in bytes (no termination).
|
|
|
|
|
|
|
|
* @param state State of the font at the beginning of the line.
|
|
|
|
|
|
|
|
* @return Reference to cache item.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
LineCacheKey key;
|
|
|
|
|
|
|
|
key.state_before = state;
|
|
|
|
|
|
|
|
key.str.assign(str, len);
|
|
|
|
|
|
|
|
return linecache[key];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Clear line cache.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Layouter::ResetLineCache()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
linecache.clear();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Reduce the size of linecache if necessary to prevent infinite growth.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Layouter::ReduceLineCache()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/* TODO LRU cache would be fancy, but not exactly necessary */
|
|
|
|
|
|
|
|
if (linecache.size() > 4096) ResetLineCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|