1
0
Fork 0

Codechange: manage the ParagraphLayouter's buffer with std::unique_ptr

pull/13331/head
Rubidium 2025-02-16 09:23:16 +01:00 committed by rubidium42
parent 3790f29156
commit bb2b890c88
2 changed files with 6 additions and 8 deletions

View File

@ -8,7 +8,6 @@
/** @file gfx_layout.cpp Handling of laying out text. */
#include "stdafx.h"
#include "core/alloc_func.hpp"
#include "core/math_func.hpp"
#include "gfx_layout.h"
#include "string_func.h"
@ -64,15 +63,15 @@ Font::Font(FontSize size, TextColour colour) :
template <typename T>
static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
{
free(line.buffer);
typename T::CharType *buff_begin = new typename T::CharType[str.size() + 1];
/* Move ownership of buff_begin into the Buffer/unique_ptr. */
line.buffer = Layouter::LineCacheItem::Buffer(buff_begin, [](void *p) { delete[] reinterpret_cast<T::CharType *>(p); });
typename T::CharType *buff_begin = MallocT<typename T::CharType>(str.size() + 1);
const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
typename T::CharType *buff = buff_begin;
FontMap &font_mapping = line.runs;
Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
line.buffer = buff_begin;
font_mapping.clear();
auto cur = str.begin();

View File

@ -166,15 +166,14 @@ class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Lin
public:
/** Item in the linecache */
struct LineCacheItem {
/* Due to the type of data in the buffer differing depending on the Layouter, we need to pass our own deleter routine. */
using Buffer = std::unique_ptr<void, void(*)(void *)>;
/* Stuff that cannot be freed until the ParagraphLayout is freed */
void *buffer; ///< Accessed by our ParagraphLayout::nextLine.
Buffer buffer{nullptr, [](void *){}}; ///< Accessed by our ParagraphLayout::nextLine.
FontMap runs; ///< Accessed by our ParagraphLayout::nextLine.
FontState state_after; ///< Font state after the line.
std::unique_ptr<ParagraphLayouter> layout = nullptr; ///< Layout of the line.
LineCacheItem() : buffer(nullptr) {}
~LineCacheItem() { free(buffer); }
};
private:
typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;