diff --git a/src/console_gui.cpp b/src/console_gui.cpp index 3fbf93faf5..a95a5e0c80 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -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" diff --git a/src/gfx.cpp b/src/gfx.cpp index 5cc3ee9b90..51a053a354 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -895,39 +895,6 @@ Dimension GetStringListBoundingBox(std::span 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(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 diff --git a/src/gfx_func.h b/src/gfx_func.h index d40431ba7b..63927c475d 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -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); diff --git a/src/gfx_layout.cpp b/src/gfx_layout.cpp index fbdfe10ec2..fc355d79af 100644 --- a/src/gfx_layout.cpp +++ b/src/gfx_layout.cpp @@ -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(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); +} diff --git a/src/gfx_layout.h b/src/gfx_layout.h index 90aa7bd84b..88aa28deb7 100644 --- a/src/gfx_layout.h +++ b/src/gfx_layout.h @@ -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 */ diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 7ed51cac1d..ea5e01cb86 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -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" diff --git a/src/textbuf.cpp b/src/textbuf.cpp index 4b5f1e5b7f..ee09223a75 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -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"