1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-28 15:38:30 +02:00

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

View File

@ -106,7 +106,23 @@ void destroy(void);
int get_advance(const Font* f, uint32_t codepoint); // TODO de-api
void print(uint32_t flags, int x, int y, const std::string& text, uint8_t r, uint8_t g, uint8_t b);
void print(
uint32_t flags,
int x,
int y,
const std::string& text,
uint8_t r, uint8_t g, uint8_t b
);
int print_wrap(
uint32_t flags,
int x,
int y,
const std::string& text,
uint8_t r, uint8_t g, uint8_t b,
int linespacing = -1,
int maxwidth = -1
);
} // namespace font

View File

@ -443,7 +443,7 @@ bool Graphics::next_wrap_s(
int Graphics::PrintWrap(
const int x,
int y,
std::string s,
const std::string& text,
const int r,
const int g,
const int b,
@ -451,50 +451,11 @@ int Graphics::PrintWrap(
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;
}
const char* str = s.c_str();
/* Screen width is 320 pixels. The shortest a char can be is 6 pixels wide.
* 320 / 6 is 54, rounded up. 4 bytes per char. */
char buffer[54*4 + 1];
size_t start = 0;
if (flipmode)
{
/* Correct for the height of the resulting print. */
size_t len = 0;
while (next_wrap(&start, &len, &str[start], maxwidth))
{
y += linespacing;
}
y -= linespacing;
start = 0;
}
while (next_wrap_s(buffer, sizeof(buffer), &start, str, maxwidth))
{
Print(x, y, buffer, r, g, b, cen);
if (flipmode)
{
y -= linespacing;
}
else
{
y += linespacing;
}
}
return y + linespacing;
// DEPRECATED
if (cen)
return font::print_wrap(PR_CEN, -1, y, text, r, g, b, linespacing /*= -1 */, maxwidth /*= -1 */);
else
return font::print_wrap(0, x, y, text, r, g, b, linespacing /*= -1 */, maxwidth /*= -1 */);
}

View File

@ -210,7 +210,7 @@ public:
bool next_wrap_s(char buffer[], size_t buffer_size, size_t* start, const char* str, int maxwidth);
int PrintWrap(int x, int y, std::string s, int r, int g, int b, bool cen = false, int linespacing = -1, int maxwidth = -1);
int PrintWrap(int x, int y, const std::string& s, int r, int g, int b, bool cen = false, int linespacing = -1, int maxwidth = -1);
void bprint(int x, int y, const std::string& t, int r, int g, int b, bool cen = false);