Codechange: Replace AutoDeleteSmallVector with direct std::vector use in text layout code.

pull/88/head
Michael Lutz 6 years ago
parent 329bb52613
commit baf9229931

@ -340,12 +340,12 @@ static void SetColourRemap(TextColour colour)
* @return In case of left or center alignment the right most pixel we have drawn to. * @return In case of left or center alignment the right most pixel we have drawn to.
* In case of right alignment the left most pixel we have drawn to. * In case of right alignment the left most pixel we have drawn to.
*/ */
static int DrawLayoutLine(const ParagraphLayouter::Line *line, int y, int left, int right, StringAlignment align, bool underline, bool truncation) static int DrawLayoutLine(const ParagraphLayouter::Line &line, int y, int left, int right, StringAlignment align, bool underline, bool truncation)
{ {
if (line->CountRuns() == 0) return 0; if (line.CountRuns() == 0) return 0;
int w = line->GetWidth(); int w = line.GetWidth();
int h = line->GetLeading(); int h = line.GetLeading();
/* /*
* The following is needed for truncation. * The following is needed for truncation.
@ -376,7 +376,7 @@ static int DrawLayoutLine(const ParagraphLayouter::Line *line, int y, int left,
* another size would be chosen it won't have truncated too little for * another size would be chosen it won't have truncated too little for
* the truncation dots. * the truncation dots.
*/ */
FontCache *fc = ((const Font*)line->GetVisualRun(0).GetFont())->fc; FontCache *fc = ((const Font*)line.GetVisualRun(0).GetFont())->fc;
GlyphID dot_glyph = fc->MapCharToGlyph('.'); GlyphID dot_glyph = fc->MapCharToGlyph('.');
dot_width = fc->GetGlyphWidth(dot_glyph); dot_width = fc->GetGlyphWidth(dot_glyph);
dot_sprite = fc->GetGlyph(dot_glyph); dot_sprite = fc->GetGlyph(dot_glyph);
@ -421,8 +421,8 @@ static int DrawLayoutLine(const ParagraphLayouter::Line *line, int y, int left,
TextColour colour = TC_BLACK; TextColour colour = TC_BLACK;
bool draw_shadow = false; bool draw_shadow = false;
for (int run_index = 0; run_index < line->CountRuns(); run_index++) { for (int run_index = 0; run_index < line.CountRuns(); run_index++) {
const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index); const ParagraphLayouter::VisualRun &run = line.GetVisualRun(run_index);
const Font *f = (const Font*)run.GetFont(); const Font *f = (const Font*)run.GetFont();
FontCache *fc = f->fc; FontCache *fc = f->fc;
@ -512,7 +512,7 @@ int DrawString(int left, int right, int top, const char *str, TextColour colour,
Layouter layout(str, INT32_MAX, colour, fontsize); Layouter layout(str, INT32_MAX, colour, fontsize);
if (layout.size() == 0) return 0; if (layout.size() == 0) return 0;
return DrawLayoutLine(layout.front(), top, left, right, align, underline, true); return DrawLayoutLine(*layout.front(), top, left, right, align, underline, true);
} }
/** /**
@ -648,14 +648,14 @@ int DrawStringMultiLine(int left, int right, int top, int bottom, const char *st
int last_line = top; int last_line = top;
int first_line = bottom; int first_line = bottom;
for (const ParagraphLayouter::Line *line : layout) { for (const auto &line : layout) {
int line_height = line->GetLeading(); int line_height = line->GetLeading();
if (y >= top && y < bottom) { if (y >= top && y < bottom) {
last_line = y + line_height; last_line = y + line_height;
if (first_line > y) first_line = y; if (first_line > y) first_line = y;
DrawLayoutLine(line, y, left, right, align, underline, false); DrawLayoutLine(*line, y, left, right, align, underline, false);
} }
y += line_height; y += line_height;
} }

@ -124,7 +124,7 @@ le_bool Font::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &poin
/** /**
* Wrapper for doing layouts with ICU. * Wrapper for doing layouts with ICU.
*/ */
class ICUParagraphLayout : public AutoDeleteSmallVector<ParagraphLayouter::Line *>, public ParagraphLayouter { class ICUParagraphLayout : public ParagraphLayouter {
icu::ParagraphLayout *p; ///< The actual ICU paragraph layout. icu::ParagraphLayout *p; ///< The actual ICU paragraph layout.
public: public:
/** Visual run contains data about the bit of text with the same font. */ /** Visual run contains data about the bit of text with the same font. */
@ -171,10 +171,10 @@ public:
~ICUParagraphLayout() override { delete p; } ~ICUParagraphLayout() override { delete p; }
void Reflow() override { p->reflow(); } void Reflow() override { p->reflow(); }
ParagraphLayouter::Line *NextLine(int max_width) override std::unique_ptr<const Line> NextLine(int max_width) override
{ {
icu::ParagraphLayout::Line *l = p->nextLine(max_width); icu::ParagraphLayout::Line *l = p->nextLine(max_width);
return l == NULL ? NULL : new ICULine(l); return std::unique_ptr<const Line>(l == NULL ? NULL : new ICULine(l));
} }
}; };
@ -286,7 +286,7 @@ public:
FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs); FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs);
void Reflow() override; void Reflow() override;
const ParagraphLayouter::Line *NextLine(int max_width) override; std::unique_ptr<const Line> NextLine(int max_width) override;
}; };
/** /**
@ -498,7 +498,7 @@ void FallbackParagraphLayout::Reflow()
* @param max_width The maximum width of the string. * @param max_width The maximum width of the string.
* @return A Line, or NULL when at the end of the paragraph. * @return A Line, or NULL when at the end of the paragraph.
*/ */
const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width) std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
{ {
/* Simple idea: /* Simple idea:
* - split a line at a newline character, or at a space where we can break a line. * - split a line at a newline character, or at a space where we can break a line.
@ -506,13 +506,13 @@ const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)
*/ */
if (this->buffer == NULL) return NULL; if (this->buffer == NULL) return NULL;
FallbackLine *l = new FallbackLine(); std::unique_ptr<FallbackLine> l(new FallbackLine());
if (*this->buffer == '\0') { if (*this->buffer == '\0') {
/* Only a newline. */ /* Only a newline. */
this->buffer = NULL; this->buffer = NULL;
l->emplace_back(this->runs.front().second, this->buffer, 0, 0); l->emplace_back(this->runs.front().second, this->buffer, 0, 0);
return l; return std::move(l); // Not supposed to be needed, but clang-3.8 barfs otherwise.
} }
int offset = this->buffer - this->buffer_begin; int offset = this->buffer - this->buffer_begin;
@ -562,7 +562,7 @@ const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)
/* The character is wider than allowed width; don't know /* The character is wider than allowed width; don't know
* what to do with this case... bail out! */ * what to do with this case... bail out! */
this->buffer = NULL; this->buffer = NULL;
return l; return std::move(l); // Not supposed to be needed, but clang-3.8 barfs otherwise.
} }
if (last_space == NULL) { if (last_space == NULL) {
@ -589,7 +589,7 @@ const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)
int w = l->GetWidth(); int w = l->GetWidth();
l->emplace_back(iter->second, begin, last_char - begin, w); l->emplace_back(iter->second, begin, last_char - begin, w);
} }
return l; return std::move(l); // Not supposed to be needed, but clang-3.8 barfs otherwise.
} }
/** /**
@ -730,12 +730,12 @@ Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsi
} }
} }
/* Copy all lines into a local cache so we can reuse them later on more easily. */ /* Move all lines into a local cache so we can reuse them later on more easily. */
const ParagraphLayouter::Line *l; for (;;) {
while ((l = line.layout->NextLine(maxw)) != NULL) { auto l = line.layout->NextLine(maxw);
this->push_back(l); if (l == NULL) break;
this->push_back(std::move(l));
} }
} while (c != '\0'); } while (c != '\0');
} }
@ -746,7 +746,7 @@ Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsi
Dimension Layouter::GetBounds() Dimension Layouter::GetBounds()
{ {
Dimension d = { 0, 0 }; Dimension d = { 0, 0 };
for (const ParagraphLayouter::Line *l : *this) { for (const auto &l : *this) {
d.width = max<uint>(d.width, l->GetWidth()); d.width = max<uint>(d.width, l->GetWidth());
d.height += l->GetLeading(); d.height += l->GetLeading();
} }
@ -775,7 +775,7 @@ Point Layouter::GetCharPosition(const char *ch) const
if (str == ch) { if (str == ch) {
/* Valid character. */ /* Valid character. */
const ParagraphLayouter::Line *line = this->front(); const auto &line = this->front();
/* Pointer to the end-of-string/line marker? Return total line width. */ /* Pointer to the end-of-string/line marker? Return total line width. */
if (*ch == '\0' || *ch == '\n') { if (*ch == '\0' || *ch == '\n') {
@ -808,7 +808,7 @@ Point Layouter::GetCharPosition(const char *ch) const
*/ */
const char *Layouter::GetCharAtPosition(int x) const const char *Layouter::GetCharAtPosition(int x) const
{ {
const ParagraphLayouter::Line *line = this->front(); const auto &line = this->front();
for (int run_index = 0; run_index < line->CountRuns(); run_index++) { for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index); const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);

@ -142,7 +142,7 @@ public:
}; };
virtual void Reflow() = 0; virtual void Reflow() = 0;
virtual const Line *NextLine(int max_width) = 0; virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
}; };
/** /**
@ -150,7 +150,7 @@ public:
* *
* It also accounts for the memory allocations and frees. * It also accounts for the memory allocations and frees.
*/ */
class Layouter : public AutoDeleteSmallVector<const ParagraphLayouter::Line *> { class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
const char *string; ///< Pointer to the original string. const char *string; ///< Pointer to the original string.
/** Key into the linecache */ /** Key into the linecache */

@ -111,7 +111,7 @@ public:
this->cur_offset = 0; this->cur_offset = 0;
} }
const Line *NextLine(int max_width) override; std::unique_ptr<const Line> NextLine(int max_width) override;
}; };
@ -188,7 +188,7 @@ static CTRunDelegateCallbacks _sprite_font_callback = {
return typesetter != NULL ? new CoreTextParagraphLayout(typesetter, buff, length, fontMapping) : NULL; return typesetter != NULL ? new CoreTextParagraphLayout(typesetter, buff, length, fontMapping) : NULL;
} }
/* virtual */ const CoreTextParagraphLayout::Line *CoreTextParagraphLayout::NextLine(int max_width) /* virtual */ std::unique_ptr<const ParagraphLayouter::Line> CoreTextParagraphLayout::NextLine(int max_width)
{ {
if (this->cur_offset >= this->length) return NULL; if (this->cur_offset >= this->length) return NULL;
@ -200,7 +200,7 @@ static CTRunDelegateCallbacks _sprite_font_callback = {
CTLineRef line = CTTypesetterCreateLine(this->typesetter, CFRangeMake(this->cur_offset, len)); CTLineRef line = CTTypesetterCreateLine(this->typesetter, CFRangeMake(this->cur_offset, len));
this->cur_offset += len; this->cur_offset += len;
return line != NULL ? new CoreTextLine(line, this->font_map, this->text_buffer) : NULL; return std::unique_ptr<const Line>(line != NULL ? new CoreTextLine(line, this->font_map, this->text_buffer) : NULL);
} }
CoreTextParagraphLayout::CoreTextVisualRun::CoreTextVisualRun(CTRunRef run, Font *font, const CoreTextParagraphLayoutFactory::CharType *buff) : font(font) CoreTextParagraphLayout::CoreTextVisualRun::CoreTextVisualRun(CTRunRef run, Font *font, const CoreTextParagraphLayoutFactory::CharType *buff) : font(font)
@ -244,8 +244,8 @@ CoreTextParagraphLayout::CoreTextVisualRun::CoreTextVisualRun(CTRunRef run, Font
int CoreTextParagraphLayout::CoreTextLine::GetLeading() const int CoreTextParagraphLayout::CoreTextLine::GetLeading() const
{ {
int leading = 0; int leading = 0;
for (const CoreTextVisualRun * const &run : *this) { for (const auto &run : *this) {
leading = max(leading, run->GetLeading()); leading = max(leading, run.GetLeading());
} }
return leading; return leading;
@ -260,8 +260,8 @@ int CoreTextParagraphLayout::CoreTextLine::GetWidth() const
if (this->size() == 0) return 0; if (this->size() == 0) return 0;
int total_width = 0; int total_width = 0;
for (const CoreTextVisualRun * const &run : *this) { for (const auto &run : *this) {
total_width += run->GetAdvance(); total_width += run.GetAdvance();
} }
return total_width; return total_width;

@ -134,7 +134,7 @@ public:
this->cur_range_offset = 0; this->cur_range_offset = 0;
} }
const Line *NextLine(int max_width) override; std::unique_ptr<const Line> NextLine(int max_width) override;
}; };
void UniscribeResetScriptCache(FontSize size) void UniscribeResetScriptCache(FontSize size)
@ -318,7 +318,7 @@ static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutF
return new UniscribeParagraphLayout(ranges, buff); return new UniscribeParagraphLayout(ranges, buff);
} }
/* virtual */ const ParagraphLayouter::Line *UniscribeParagraphLayout::NextLine(int max_width) /* virtual */ std::unique_ptr<const ParagraphLayouter::Line> UniscribeParagraphLayout::NextLine(int max_width)
{ {
std::vector<UniscribeRun>::iterator start_run = this->cur_range; std::vector<UniscribeRun>::iterator start_run = this->cur_range;
std::vector<UniscribeRun>::iterator last_run = this->cur_range; std::vector<UniscribeRun>::iterator last_run = this->cur_range;
@ -404,7 +404,7 @@ static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutF
if (FAILED(ScriptLayout((int)bidi_level.size(), &bidi_level[0], &vis_to_log[0], NULL))) return NULL; if (FAILED(ScriptLayout((int)bidi_level.size(), &bidi_level[0], &vis_to_log[0], NULL))) return NULL;
/* Create line. */ /* Create line. */
UniscribeLine *line = new UniscribeLine(); std::unique_ptr<UniscribeLine> line(new UniscribeLine());
int cur_pos = 0; int cur_pos = 0;
for (std::vector<INT>::iterator l = vis_to_log.begin(); l != vis_to_log.end(); l++) { for (std::vector<INT>::iterator l = vis_to_log.begin(); l != vis_to_log.end(); l++) {

@ -83,6 +83,7 @@
#include <cstdlib> #include <cstdlib>
#include <climits> #include <climits>
#include <cassert> #include <cassert>
#include <memory>
#ifndef SIZE_MAX #ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1) #define SIZE_MAX ((size_t)-1)

Loading…
Cancel
Save