Reorganize font and string related code

This commit is contained in:
PikalaxALT
2021-09-05 08:52:18 -04:00
parent 9aaac32d70
commit 66f4bf3667
9 changed files with 98 additions and 102 deletions

View File

@@ -1386,7 +1386,7 @@
.extern FontData_ModeSwitch
.extern TryLoadGlyph
.extern GetStringWidth
.extern StringGetWidth
.extern GetStringWidthMultiline
.extern StringGetWidth_SingleLine_HandleClearToControlCode
.extern String_ctor
.extern String_dtor

View File

@@ -179,7 +179,7 @@ THUMB_FUNC s32 FUN_02002F08(u32 param0, struct String *str, u32 param2)
{
GF_ASSERT(UNK_02106FC8->unk94[param0] != NULL);
return StringGetWidth(UNK_02106FC8->unk94[param0], String_c_str(str), param2);
return GetStringWidthMultiline(UNK_02106FC8->unk94[param0], String_c_str(str), param2);
}
THUMB_FUNC u32 FUN_02002F40(u32 param0, struct String *str, u32 param2, u32 param3)

View File

@@ -13,6 +13,7 @@
#include "script_buffers.h"
#include "unk_02024E64.h"
#include "text.h"
#include "string16.h"
#include "msgdata/msg.naix"
#include "graphic/font.naix"
@@ -20,8 +21,6 @@
extern u32 GetCityNamesMsgdataIdByCountry(u32);
extern void GetECWordIntoStringByIndex(u32 a0, struct String * a1);
extern void StringCat_HandleTrainerName(struct String * dest, const struct String * src);
extern void StrAddChar(struct String * str, u16 val);
extern void * GfGfxLoader_GetCharData(NarcId, s32, s32, struct UnkStruct_0200B870_sub **, u32);
extern void * GfGfxLoader_LoadFromNarc(NarcId narcId, s32 memberNo, BOOL isCompressed, u32 heap_id, BOOL allocAtEnd);

View File

@@ -210,3 +210,53 @@ THUMB_FUNC int GetGlyphWidth_FixedWidth(struct FontData * ptr, int a1)
#pragma unused(a1)
return ptr->gfxHeader.fixedWidth;
}
THUMB_FUNC s32 GetStringWidthMultiline(struct FontData * r7, const u16 * arr, u32 r6)
{
s32 ret = 0;
u32 r4 = 0;
while (*arr != 0xFFFF)
{
if (*arr == 0xFFFE)
{
arr = MsgArray_SkipControlCode(arr);
}
else if (*arr == 0xE000) // newline
{
if (ret < r4 - r6)
ret = (int)(r4 - r6);
r4 = 0;
arr++;
}
else
{
r4 += (r6 + r7->glyphWidthFunc(r7, *arr - 1));
arr++;
}
}
if (ret < r4 - r6)
ret = (int)(r4 - r6);
return ret;
}
THUMB_FUNC s32 StringGetWidth_SingleLine_HandleClearToControlCode(struct FontData * r6, const u16 * arr)
{
s32 ret = 0;
while (*arr != 0xFFFF)
{
if (*arr == 0xFFFE)
{
if (MsgArray_GetControlCode(arr) == 515)
{
ret = MsgArray_ControlCodeGetField(arr, 0) - 12;
}
arr = MsgArray_SkipControlCode(arr);
}
else
{
ret += r6->glyphWidthFunc(r6, *arr - 1);
arr++;
}
}
return ret;
}

View File

@@ -2,65 +2,11 @@
#include "string16.h"
#include "heap.h"
#include "string_util.h"
#include "unk_0201B8B8.h"
#include "unk_02021590.h"
#pragma thumb on
#define ASSERT_STR16(_str) ({ GF_ASSERT(_str != NULL); GF_ASSERT(_str->magic == STR16_MAGIC); })
void StrAddChar(struct String * str, u16 val);
s32 StringGetWidth(struct FontData * r7, const u16 * arr, u32 r6)
{
s32 ret = 0;
u32 r4 = 0;
while (*arr != 0xFFFF)
{
if (*arr == 0xFFFE)
{
arr = MsgArray_SkipControlCode(arr);
}
else if (*arr == 0xE000) // newline
{
if (ret < r4 - r6)
ret = (int)(r4 - r6);
r4 = 0;
arr++;
}
else
{
r4 += (r6 + r7->glyphWidthFunc(r7, *arr - 1));
arr++;
}
}
if (ret < r4 - r6)
ret = (int)(r4 - r6);
return ret;
}
s32 StringGetWidth_SingleLine_HandleClearToControlCode(struct FontData * r6, const u16 * arr)
{
s32 ret = 0;
while (*arr != 0xFFFF)
{
if (*arr == 0xFFFE)
{
if (MsgArray_GetControlCode(arr) == 515)
{
ret = MsgArray_ControlCodeGetField(arr, 0) - 12;
}
arr = MsgArray_SkipControlCode(arr);
}
else
{
ret += r6->glyphWidthFunc(r6, *arr - 1);
arr++;
}
}
return ret;
}
struct String * String_ctor(u32 length, u32 heap_id)
{
struct String * ret = AllocFromHeap(heap_id, length * 2 + 10);
@@ -366,3 +312,43 @@ void StrUpperFirstChar(struct String * str)
str->data[0] -= 26;
}
}
BOOL String_IsTrainerName(struct String * string)
{
return string->size != 0 && string->data[0] == 0xF100;
}
void StringCat_HandleTrainerName(struct String * dest, struct String * src)
{
if (String_IsTrainerName(src))
{
u16 * dest_p = &dest->data[dest->size];
u16 * src_p = &src->data[1];
s32 bit = 0;
u32 outsize = 0;
u16 cur_char = 0;
while (1)
{
cur_char = (u16)((*src_p >> bit) & 0x1FF);
bit += 9;
if (bit >= 15)
{
src_p++;
bit -= 15;
if (bit != 0)
{
cur_char |= (*src_p << (9 - bit)) & 0x1FF;
}
}
if (cur_char == 0x1FF)
break;
*dest_p++ = cur_char;
outsize++;
}
*dest_p = EOS;
dest->size += outsize;
}
else
StringCat(dest, src);
}

View File

@@ -5,46 +5,6 @@
#pragma thumb on
BOOL String_IsTrainerName(struct String * string)
{
return string->size != 0 && string->data[0] == 0xF100;
}
void StringCat_HandleTrainerName(struct String * dest, struct String * src)
{
if (String_IsTrainerName(src))
{
u16 * dest_p = &dest->data[dest->size];
u16 * src_p = &src->data[1];
s32 bit = 0;
u32 outsize = 0;
u16 cur_char = 0;
while (1)
{
cur_char = (u16)((*src_p >> bit) & 0x1FF);
bit += 9;
if (bit >= 15)
{
src_p++;
bit -= 15;
if (bit != 0)
{
cur_char |= (*src_p << (9 - bit)) & 0x1FF;
}
}
if (cur_char == 0x1FF)
break;
*dest_p++ = cur_char;
outsize++;
}
*dest_p = EOS;
dest->size += outsize;
}
else
StringCat(dest, src);
}
struct UnkStruct_020220C4 * FUN_020220C4(u8 * a0, u32 a1, void (*a2)(s32, s32, u32), u32 a3, u32 a4)
{
struct UnkStruct_020220C4 * ret;

View File

@@ -20,8 +20,6 @@ BOOL StringCompare(struct String *, struct String *);
void CopyU16ArrayToStringN(struct String *, u16 *, u32); // copy
void StringSetEmpty(struct String *); // set empty
struct String * StringDup(struct String *, u32 heap_id);
s32 StringGetWidth(struct FontData * r7, const u16 * arr, u32 r6);
s32 StringGetWidth_SingleLine_HandleClearToControlCode(struct FontData * r6, const u16 * arr);
void String_dtor(struct String * str);
void StringCopy(struct String * dest, struct String * src);
void String16_FormatInteger(struct String * str, int num, u32 ndigits, int strConvMode, BOOL whichCharset);
@@ -32,7 +30,10 @@ void StringGetLineN(struct String * dest, volatile struct String * src, u32 n);
void CopyU16ArrayToString(struct String * str, u16 * buf);
void CopyStringToU16Array(struct String * str, u16 * buf, u32 length);
u16 * String_c_str(struct String * str);
void StrAddChar(struct String * str, u16 val);
void StringCat(struct String * dest, struct String * src);
void StrUpperFirstChar(struct String * str);
BOOL String_IsTrainerName(struct String * string);
void StringCat_HandleTrainerName(struct String * dest, struct String * src);
#endif //POKEDIAMOND_STRING16_H

View File

@@ -48,5 +48,7 @@ void DecompressGlyphTiles_LazyFromNarc(struct FontData * ptr, u16 param1, struct
u32 GetStringWidth(struct FontData * ptr, const u16 * str, u32 letterSpacing);
int GetGlyphWidth_VariableWidth(struct FontData * ptr, int a1);
int GetGlyphWidth_FixedWidth(struct FontData * ptr, int a1);
s32 GetStringWidthMultiline(struct FontData * r7, const u16 * arr, u32 r6);
s32 StringGetWidth_SingleLine_HandleClearToControlCode(struct FontData * r6, const u16 * arr);
#endif //POKEDIAMOND_UNK_02021590_H

View File

@@ -17,8 +17,6 @@ struct UnkStruct_020220C4
struct UnkStruct_020220C4_sub * field_10;
};
BOOL String_IsTrainerName(struct String * string);
void StringCat_HandleTrainerName(struct String * dest, struct String * src);
struct UnkStruct_020220C4 * FUN_020220C4(u8 * a0, u32 a1, void (*a2)(s32, s32, u32), u32 a3, u32 a4);
void FUN_02022120(struct UnkStruct_020220C4_sub * sub);
void FUN_0202212C(struct UnkStruct_020220C4 * unk);