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

This commit is contained in:
Michael Lutz 2019-04-02 21:31:10 +02:00
parent 329bb52613
commit baf9229931
6 changed files with 40 additions and 39 deletions

View File

@ -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.
* 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 h = line->GetLeading();
int w = line.GetWidth();
int h = line.GetLeading();
/*
* 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
* 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('.');
dot_width = fc->GetGlyphWidth(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;
bool draw_shadow = false;
for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
for (int run_index = 0; run_index < line.CountRuns(); run_index++) {
const ParagraphLayouter::VisualRun &run = line.GetVisualRun(run_index);
const Font *f = (const Font*)run.GetFont();
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);
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 first_line = bottom;
for (const ParagraphLayouter::Line *line : layout) {
for (const auto &line : layout) {
int line_height = line->GetLeading();
if (y >= top && y < bottom) {
last_line = y + line_height;
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;
}

View File

@ -124,7 +124,7 @@ le_bool Font::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &poin
/**
* 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.
public:
/** Visual run contains data about the bit of text with the same font. */
@ -171,10 +171,10 @@ public:
~ICUParagraphLayout() override { delete p; }
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);
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);
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.
* @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:
* - 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;
FallbackLine *l = new FallbackLine();
std::unique_ptr<FallbackLine> l(new FallbackLine());
if (*this->buffer == '\0') {
/* Only a newline. */
this->buffer = NULL;
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;
@ -562,7 +562,7 @@ const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)
/* The character is wider than allowed width; don't know
* what to do with this case... bail out! */
this->buffer = NULL;
return l;
return std::move(l); // Not supposed to be needed, but clang-3.8 barfs otherwise.
}
if (last_space == NULL) {
@ -589,7 +589,7 @@ const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)
int w = l->GetWidth();
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. */
const ParagraphLayouter::Line *l;
while ((l = line.layout->NextLine(maxw)) != NULL) {
this->push_back(l);
/* Move all lines into a local cache so we can reuse them later on more easily. */
for (;;) {
auto l = line.layout->NextLine(maxw);
if (l == NULL) break;
this->push_back(std::move(l));
}
} while (c != '\0');
}
@ -746,7 +746,7 @@ Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsi
Dimension Layouter::GetBounds()
{
Dimension d = { 0, 0 };
for (const ParagraphLayouter::Line *l : *this) {
for (const auto &l : *this) {
d.width = max<uint>(d.width, l->GetWidth());
d.height += l->GetLeading();
}
@ -775,7 +775,7 @@ Point Layouter::GetCharPosition(const char *ch) const
if (str == ch) {
/* 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. */
if (*ch == '\0' || *ch == '\n') {
@ -808,7 +808,7 @@ Point Layouter::GetCharPosition(const char *ch) 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++) {
const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);

View File

@ -142,7 +142,7 @@ public:
};
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.
*/
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.
/** Key into the linecache */

View File

@ -111,7 +111,7 @@ public:
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;
}
/* 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;
@ -200,7 +200,7 @@ static CTRunDelegateCallbacks _sprite_font_callback = {
CTLineRef line = CTTypesetterCreateLine(this->typesetter, CFRangeMake(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)
@ -244,8 +244,8 @@ CoreTextParagraphLayout::CoreTextVisualRun::CoreTextVisualRun(CTRunRef run, Font
int CoreTextParagraphLayout::CoreTextLine::GetLeading() const
{
int leading = 0;
for (const CoreTextVisualRun * const &run : *this) {
leading = max(leading, run->GetLeading());
for (const auto &run : *this) {
leading = max(leading, run.GetLeading());
}
return leading;
@ -260,8 +260,8 @@ int CoreTextParagraphLayout::CoreTextLine::GetWidth() const
if (this->size() == 0) return 0;
int total_width = 0;
for (const CoreTextVisualRun * const &run : *this) {
total_width += run->GetAdvance();
for (const auto &run : *this) {
total_width += run.GetAdvance();
}
return total_width;

View File

@ -134,7 +134,7 @@ public:
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)
@ -318,7 +318,7 @@ static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutF
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 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;
/* Create line. */
UniscribeLine *line = new UniscribeLine();
std::unique_ptr<UniscribeLine> line(new UniscribeLine());
int cur_pos = 0;
for (std::vector<INT>::iterator l = vis_to_log.begin(); l != vis_to_log.end(); l++) {

View File

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