Codechange: Move GetCharPosInString/GetCharAtPosition to gfx_layout.

These functions are related more to layouting than graphics.
This commit is contained in:
Peter Nelson 2024-06-01 22:37:46 +01:00 committed by Peter Nelson
parent bbbf2b5282
commit 80ddcb9d7d
7 changed files with 39 additions and 35 deletions

View File

@ -17,6 +17,7 @@
#include "string_func.h"
#include "strings_func.h"
#include "gfx_func.h"
#include "gfx_layout.h"
#include "settings_type.h"
#include "console_func.h"
#include "rev.h"

View File

@ -895,39 +895,6 @@ Dimension GetStringListBoundingBox(std::span<const StringID> list, FontSize font
return d;
}
/**
* Get the leading corner of a character in a single-line string relative
* to the start of the string.
* @param str String containing the character.
* @param ch Pointer to the character in the string.
* @param start_fontsize Font size to start the text with.
* @return Upper left corner of the glyph associated with the character.
*/
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize)
{
/* Ensure "ch" is inside "str" or at the exact end. */
assert(ch >= str.data() && (ch - str.data()) <= static_cast<ptrdiff_t>(str.size()));
auto it_ch = str.begin() + (ch - str.data());
Layouter layout(str, INT32_MAX, start_fontsize);
return layout.GetCharPosition(it_ch);
}
/**
* Get the character from a string that is drawn at a specific position.
* @param str String to test.
* @param x Position relative to the start of the string.
* @param start_fontsize Font size to start the text with.
* @return Index of the character position or -1 if there is no character at the position.
*/
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
{
if (x < 0) return -1;
Layouter layout(str, INT32_MAX, start_fontsize);
return layout.GetCharAtPosition(x, 0);
}
/**
* Draw single character horizontally centered around (x,y)
* @param c Character (glyph) to draw

View File

@ -142,8 +142,6 @@ int GetStringLineCount(StringID str, int maxw);
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion);
Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion);
void LoadStringWidthTable(bool monospace = false);
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize = FS_NORMAL);
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL);
void DrawDirtyBlocks();
void AddDirtyBlock(int left, int top, int right, int bottom);

View File

@ -409,3 +409,36 @@ void Layouter::ReduceLineCache()
if (linecache->size() > 4096) ResetLineCache();
}
}
/**
* Get the leading corner of a character in a single-line string relative
* to the start of the string.
* @param str String containing the character.
* @param ch Pointer to the character in the string.
* @param start_fontsize Font size to start the text with.
* @return Upper left corner of the glyph associated with the character.
*/
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize)
{
/* Ensure "ch" is inside "str" or at the exact end. */
assert(ch >= str.data() && (ch - str.data()) <= static_cast<ptrdiff_t>(str.size()));
auto it_ch = str.begin() + (ch - str.data());
Layouter layout(str, INT32_MAX, start_fontsize);
return layout.GetCharPosition(it_ch);
}
/**
* Get the character from a string that is drawn at a specific position.
* @param str String to test.
* @param x Position relative to the start of the string.
* @param start_fontsize Font size to start the text with.
* @return Index of the character position or -1 if there is no character at the position.
*/
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
{
if (x < 0) return -1;
Layouter layout(str, INT32_MAX, start_fontsize);
return layout.GetCharAtPosition(x, 0);
}

View File

@ -185,4 +185,7 @@ public:
static void ReduceLineCache();
};
Point GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize = FS_NORMAL);
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL);
#endif /* GFX_LAYOUT_H */

View File

@ -12,6 +12,7 @@
#include "landscape.h"
#include "error.h"
#include "gui.h"
#include "gfx_layout.h"
#include "command_func.h"
#include "company_func.h"
#include "town.h"

View File

@ -14,6 +14,7 @@
#include "strings_func.h"
#include "gfx_type.h"
#include "gfx_func.h"
#include "gfx_layout.h"
#include "window_func.h"
#include "core/alloc_func.hpp"