Add initial version of font::print_wrap

graphics.PrintWrap is now also deprecated. An advantage of the new
version (with flags) is that it'll be possible to do things like put
a border around wrapped text, wrap text at larger scales, etc, but
these things don't work perfectly yet.

This commit also has some other fixes, like the default advance of
6 pixels for characters 0x00-0x1F in 8x8 fonts.
This commit is contained in:
Dav999-v
2023-01-06 16:21:42 +01:00
committed by Misa Elizabeth Kai
parent 0475539075
commit 1d8494db8d
4 changed files with 101 additions and 48 deletions

View File

@@ -7,6 +7,7 @@
#include "Constants.h"
#include "FileSystemUtils.h"
#include "Graphics.h"
#include "Localization.h"
#include "UtilityClass.h"
#include "XMLUtils.h"
@@ -174,6 +175,7 @@ static void load_font(Font* f, const char* name)
* font.txt takes priority over <chars> in the XML.
* If neither exist, it's just ASCII. */
bool charset_loaded = false;
bool special_loaded = false;
unsigned char* charmap = NULL;
size_t length;
if (FILESYSTEM_areAssetsInSameRealDir(name_png, name_txt))
@@ -259,6 +261,7 @@ static void load_font(Font* f, const char* name)
}
}
}
special_loaded = true;
}
}
@@ -266,11 +269,25 @@ static void load_font(Font* f, const char* name)
{
/* If we don't have font.txt and no <chars> tag either,
* this font is 2.2-and-below-style plain ASCII. */
for (uint8_t codepoint = 0x00; codepoint < 0x80; codepoint++)
for (uint32_t codepoint = 0x00; codepoint < 0x80; codepoint++)
{
add_glyphinfo(f, codepoint, codepoint);
}
}
if (!special_loaded && f->glyph_w == 8 && f->glyph_h == 8)
{
/* If we don't have <special>, and the font is 8x8,
* 0x00-0x1F will be less wide because that's how it has always been. */
for (uint32_t codepoint = 0x00; codepoint < 0x20; codepoint++)
{
GlyphInfo* glyph = get_glyphinfo(f, codepoint);
if (glyph != NULL)
{
glyph->advance = 6;
}
}
}
}
void load_main(void)
@@ -442,4 +459,63 @@ void print(
}
}
int print_wrap(
const uint32_t flags,
const int x,
int y,
const std::string& text,
const uint8_t r,
const uint8_t g,
const uint8_t b,
int linespacing /*= -1*/,
int maxwidth /*= -1*/
)
{
if (linespacing == -1)
{
linespacing = 10;
}
linespacing = SDL_max(linespacing, loc::get_langmeta()->font_h);
if (maxwidth == -1)
{
maxwidth = 304;
}
// TODO look through all the flags
const char* str = text.c_str();
// This could fit 64 non-BMP characters onscreen, should be plenty
char buffer[256];
size_t start = 0;
if (graphics.flipmode)
{
// Correct for the height of the resulting print.
size_t len = 0;
while (graphics.next_wrap(&start, &len, &str[start], maxwidth))
{
y += linespacing;
}
y -= linespacing;
start = 0;
}
while (graphics.next_wrap_s(buffer, sizeof(buffer), &start, str, maxwidth))
{
print(flags, x, y, buffer, r, g, b);
if (graphics.flipmode)
{
y -= linespacing;
}
else
{
y += linespacing;
}
}
return y + linespacing;
}
} // namespace font