2013-06-25 20:29:31 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file gfx_layout.h Functions related to laying out the texts. */
|
|
|
|
|
|
|
|
#ifndef GFX_LAYOUT_H
|
|
|
|
#define GFX_LAYOUT_H
|
|
|
|
|
|
|
|
#include "fontcache.h"
|
|
|
|
#include "gfx_func.h"
|
2023-05-16 19:50:41 +00:00
|
|
|
#include "core/math_func.hpp"
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2018-04-19 18:33:21 +00:00
|
|
|
#include <stack>
|
2021-08-16 09:18:47 +00:00
|
|
|
#include <string_view>
|
2013-07-06 19:00:33 +00:00
|
|
|
|
2013-07-06 18:54:26 +00:00
|
|
|
/**
|
|
|
|
* Text drawing parameters, which can change while drawing a line, but are kept between multiple parts
|
|
|
|
* of the same text, e.g. on line breaks.
|
|
|
|
*/
|
|
|
|
struct FontState {
|
|
|
|
FontSize fontsize; ///< Current font size.
|
|
|
|
TextColour cur_colour; ///< Current text colour.
|
|
|
|
|
2019-03-02 08:06:02 +00:00
|
|
|
std::stack<TextColour, std::vector<TextColour>> colour_stack; ///< Stack of colours to assist with colour switching.
|
2018-04-19 18:33:21 +00:00
|
|
|
|
|
|
|
FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
|
|
|
|
FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
|
2013-07-06 18:54:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to new colour \a c.
|
|
|
|
* @param c New colour to use.
|
|
|
|
*/
|
|
|
|
inline void SetColour(TextColour c)
|
|
|
|
{
|
2023-04-28 20:07:56 +00:00
|
|
|
assert((c & TC_COLOUR_MASK) >= TC_BLUE && (c & TC_COLOUR_MASK) <= TC_BLACK);
|
|
|
|
assert((c & (TC_COLOUR_MASK | TC_FLAGS_MASK)) == c);
|
2019-11-26 00:28:03 +00:00
|
|
|
if ((this->cur_colour & TC_FORCED) == 0) this->cur_colour = c;
|
2013-07-06 18:54:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 18:33:21 +00:00
|
|
|
/**
|
|
|
|
* Switch to and pop the last saved colour on the stack.
|
|
|
|
*/
|
|
|
|
inline void PopColour()
|
|
|
|
{
|
|
|
|
if (colour_stack.empty()) return;
|
|
|
|
SetColour(colour_stack.top());
|
|
|
|
colour_stack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push the current colour on to the stack.
|
|
|
|
*/
|
|
|
|
inline void PushColour()
|
2013-07-06 18:54:26 +00:00
|
|
|
{
|
2018-04-19 18:33:21 +00:00
|
|
|
colour_stack.push(this->cur_colour);
|
2013-07-06 18:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to using a new font \a f.
|
|
|
|
* @param f New font to use.
|
|
|
|
*/
|
|
|
|
inline void SetFontSize(FontSize f)
|
|
|
|
{
|
|
|
|
this->fontsize = f;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
/**
|
|
|
|
* Container with information about a font.
|
|
|
|
*/
|
2023-04-30 18:37:40 +00:00
|
|
|
class Font {
|
2013-06-25 20:29:31 +00:00
|
|
|
public:
|
|
|
|
FontCache *fc; ///< The font we are using.
|
|
|
|
TextColour colour; ///< The colour this font has to be.
|
|
|
|
|
|
|
|
Font(FontSize size, TextColour colour);
|
|
|
|
};
|
|
|
|
|
2023-05-18 07:36:54 +00:00
|
|
|
/** Mapping from index to font. The pointer is owned by FontColourMap. */
|
2023-05-16 19:50:41 +00:00
|
|
|
using FontMap = std::map<int, Font *>;
|
2013-06-25 20:29:31 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-16 20:57:54 +00:00
|
|
|
* Interface to glue fallback and normal layouter into one.
|
2013-06-25 20:29:31 +00:00
|
|
|
*/
|
2013-11-16 20:57:54 +00:00
|
|
|
class ParagraphLayouter {
|
2013-06-25 20:29:31 +00:00
|
|
|
public:
|
2023-05-14 21:31:03 +00:00
|
|
|
virtual ~ParagraphLayouter() = default;
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2013-11-16 20:57:54 +00:00
|
|
|
/** Visual run contains data about the bit of text with the same font. */
|
|
|
|
class VisualRun {
|
2013-06-25 20:29:31 +00:00
|
|
|
public:
|
2023-05-14 21:31:03 +00:00
|
|
|
virtual ~VisualRun() = default;
|
2013-11-16 21:05:26 +00:00
|
|
|
virtual const Font *GetFont() const = 0;
|
|
|
|
virtual int GetGlyphCount() const = 0;
|
2024-01-12 01:39:44 +00:00
|
|
|
virtual const std::vector<GlyphID> &GetGlyphs() const = 0;
|
2024-01-18 21:06:43 +00:00
|
|
|
virtual const std::vector<Point> &GetPositions() const = 0;
|
2013-11-16 21:05:26 +00:00
|
|
|
virtual int GetLeading() const = 0;
|
2024-01-12 01:39:44 +00:00
|
|
|
virtual const std::vector<int> &GetGlyphToCharMap() const = 0;
|
2013-06-25 20:29:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** A single line worth of VisualRuns. */
|
2013-11-16 20:57:54 +00:00
|
|
|
class Line {
|
2013-06-25 20:29:31 +00:00
|
|
|
public:
|
2023-05-14 21:31:03 +00:00
|
|
|
virtual ~Line() = default;
|
2013-11-16 21:05:26 +00:00
|
|
|
virtual int GetLeading() const = 0;
|
|
|
|
virtual int GetWidth() const = 0;
|
|
|
|
virtual int CountRuns() const = 0;
|
2019-04-02 19:30:53 +00:00
|
|
|
virtual const VisualRun &GetVisualRun(int run) const = 0;
|
2023-05-08 17:01:06 +00:00
|
|
|
virtual int GetInternalCharLength(char32_t c) const = 0;
|
2013-06-25 20:29:31 +00:00
|
|
|
};
|
|
|
|
|
2013-11-16 21:05:26 +00:00
|
|
|
virtual void Reflow() = 0;
|
2019-04-02 19:31:10 +00:00
|
|
|
virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
|
2013-06-25 20:29:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The layouter performs all the layout work.
|
|
|
|
*
|
|
|
|
* It also accounts for the memory allocations and frees.
|
|
|
|
*/
|
2019-04-02 19:31:10 +00:00
|
|
|
class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
|
2023-05-06 17:53:02 +00:00
|
|
|
std::string_view string; ///< Pointer to the original string.
|
2013-08-05 20:35:23 +00:00
|
|
|
|
2013-07-06 19:00:33 +00:00
|
|
|
/** Key into the linecache */
|
|
|
|
struct LineCacheKey {
|
|
|
|
FontState state_before; ///< Font state at the beginning of the line.
|
|
|
|
std::string str; ///< Source string of the line (including colour and font size codes).
|
2021-08-16 09:18:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LineCacheQuery {
|
|
|
|
FontState state_before; ///< Font state at the beginning of the line.
|
|
|
|
std::string_view str; ///< Source string of the line (including colour and font size codes).
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Comparator for std::map */
|
|
|
|
struct LineCacheCompare {
|
|
|
|
using is_transparent = void; ///< Enable map queries with various key types
|
2013-07-06 19:00:33 +00:00
|
|
|
|
2021-08-16 09:18:47 +00:00
|
|
|
/** Comparison operator for LineCacheKey and LineCacheQuery */
|
|
|
|
template<typename Key1, typename Key2>
|
|
|
|
bool operator()(const Key1 &lhs, const Key2 &rhs) const
|
2013-07-06 19:00:33 +00:00
|
|
|
{
|
2021-08-16 09:18:47 +00:00
|
|
|
if (lhs.state_before.fontsize != rhs.state_before.fontsize) return lhs.state_before.fontsize < rhs.state_before.fontsize;
|
|
|
|
if (lhs.state_before.cur_colour != rhs.state_before.cur_colour) return lhs.state_before.cur_colour < rhs.state_before.cur_colour;
|
|
|
|
if (lhs.state_before.colour_stack != rhs.state_before.colour_stack) return lhs.state_before.colour_stack < rhs.state_before.colour_stack;
|
|
|
|
return lhs.str < rhs.str;
|
2013-07-06 19:00:33 +00:00
|
|
|
}
|
|
|
|
};
|
2013-11-16 20:57:54 +00:00
|
|
|
public:
|
2013-07-06 19:00:33 +00:00
|
|
|
/** Item in the linecache */
|
|
|
|
struct LineCacheItem {
|
|
|
|
/* Stuff that cannot be freed until the ParagraphLayout is freed */
|
2023-04-30 18:37:40 +00:00
|
|
|
void *buffer; ///< Accessed by our ParagraphLayout::nextLine.
|
2013-11-16 20:57:54 +00:00
|
|
|
FontMap runs; ///< Accessed by our ParagraphLayout::nextLine.
|
2013-07-06 19:00:33 +00:00
|
|
|
|
2013-11-16 20:32:55 +00:00
|
|
|
FontState state_after; ///< Font state after the line.
|
|
|
|
ParagraphLayouter *layout; ///< Layout of the line.
|
2013-07-06 19:00:33 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
LineCacheItem() : buffer(nullptr), layout(nullptr) {}
|
2013-11-16 20:57:54 +00:00
|
|
|
~LineCacheItem() { delete layout; free(buffer); }
|
2013-07-06 19:00:33 +00:00
|
|
|
};
|
2013-11-16 20:57:54 +00:00
|
|
|
private:
|
2021-08-16 09:18:47 +00:00
|
|
|
typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
|
2013-07-07 12:07:06 +00:00
|
|
|
static LineCache *linecache;
|
2013-07-06 19:00:33 +00:00
|
|
|
|
2023-05-06 17:53:02 +00:00
|
|
|
static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state);
|
2013-07-06 18:56:23 +00:00
|
|
|
|
2023-05-18 07:36:54 +00:00
|
|
|
using FontColourMap = std::map<TextColour, std::unique_ptr<Font>>;
|
2013-07-06 18:56:23 +00:00
|
|
|
static FontColourMap fonts[FS_END];
|
2013-11-16 20:57:54 +00:00
|
|
|
public:
|
2013-07-06 18:56:23 +00:00
|
|
|
static Font *GetFont(FontSize size, TextColour colour);
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2023-05-06 17:53:02 +00:00
|
|
|
Layouter(std::string_view str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
|
2013-06-25 20:29:31 +00:00
|
|
|
Dimension GetBounds();
|
2023-05-06 17:53:02 +00:00
|
|
|
Point GetCharPosition(std::string_view::const_iterator ch) const;
|
2021-04-02 22:49:57 +00:00
|
|
|
ptrdiff_t GetCharAtPosition(int x, size_t line_index) const;
|
2013-07-06 18:56:23 +00:00
|
|
|
|
2024-01-27 20:13:42 +00:00
|
|
|
static void Initialize();
|
2013-07-06 18:56:23 +00:00
|
|
|
static void ResetFontCache(FontSize size);
|
2013-07-06 19:00:33 +00:00
|
|
|
static void ResetLineCache();
|
|
|
|
static void ReduceLineCache();
|
2013-06-25 20:29:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* GFX_LAYOUT_H */
|