From baf9229931e4d5a3479892007e9bcc875bc9930b Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Tue, 2 Apr 2019 21:31:10 +0200 Subject: [PATCH] Codechange: Replace AutoDeleteSmallVector with direct std::vector use in text layout code. --- src/gfx.cpp | 20 ++++++++--------- src/gfx_layout.cpp | 34 ++++++++++++++--------------- src/gfx_layout.h | 4 ++-- src/os/macosx/string_osx.cpp | 14 ++++++------ src/os/windows/string_uniscribe.cpp | 6 ++--- src/stdafx.h | 1 + 6 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/gfx.cpp b/src/gfx.cpp index 7c90285ba8..cb6aa45361 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -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; } diff --git a/src/gfx_layout.cpp b/src/gfx_layout.cpp index f0dee2f538..ff176e81f0 100644 --- a/src/gfx_layout.cpp +++ b/src/gfx_layout.cpp @@ -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, 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 NextLine(int max_width) override { icu::ParagraphLayout::Line *l = p->nextLine(max_width); - return l == NULL ? NULL : new ICULine(l); + return std::unique_ptr(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 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 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 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(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); diff --git a/src/gfx_layout.h b/src/gfx_layout.h index 8a642c795b..1047366111 100644 --- a/src/gfx_layout.h +++ b/src/gfx_layout.h @@ -142,7 +142,7 @@ public: }; virtual void Reflow() = 0; - virtual const Line *NextLine(int max_width) = 0; + virtual std::unique_ptr NextLine(int max_width) = 0; }; /** @@ -150,7 +150,7 @@ public: * * It also accounts for the memory allocations and frees. */ -class Layouter : public AutoDeleteSmallVector { +class Layouter : public std::vector> { const char *string; ///< Pointer to the original string. /** Key into the linecache */ diff --git a/src/os/macosx/string_osx.cpp b/src/os/macosx/string_osx.cpp index 55eac42f99..580be35203 100644 --- a/src/os/macosx/string_osx.cpp +++ b/src/os/macosx/string_osx.cpp @@ -111,7 +111,7 @@ public: this->cur_offset = 0; } - const Line *NextLine(int max_width) override; + std::unique_ptr 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 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(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; diff --git a/src/os/windows/string_uniscribe.cpp b/src/os/windows/string_uniscribe.cpp index f47d9236a7..5abe848bd2 100644 --- a/src/os/windows/string_uniscribe.cpp +++ b/src/os/windows/string_uniscribe.cpp @@ -134,7 +134,7 @@ public: this->cur_range_offset = 0; } - const Line *NextLine(int max_width) override; + std::unique_ptr NextLine(int max_width) override; }; void UniscribeResetScriptCache(FontSize size) @@ -318,7 +318,7 @@ static std::vector UniscribeItemizeString(UniscribeParagraphLayoutF return new UniscribeParagraphLayout(ranges, buff); } -/* virtual */ const ParagraphLayouter::Line *UniscribeParagraphLayout::NextLine(int max_width) +/* virtual */ std::unique_ptr UniscribeParagraphLayout::NextLine(int max_width) { std::vector::iterator start_run = this->cur_range; std::vector::iterator last_run = this->cur_range; @@ -404,7 +404,7 @@ static std::vector 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 line(new UniscribeLine()); int cur_pos = 0; for (std::vector::iterator l = vis_to_log.begin(); l != vis_to_log.end(); l++) { diff --git a/src/stdafx.h b/src/stdafx.h index a9ed460c26..f85d68aa04 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -83,6 +83,7 @@ #include #include #include +#include #ifndef SIZE_MAX #define SIZE_MAX ((size_t)-1)