2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/GuiSys/CRasterFont.hpp"
|
|
|
|
|
2020-04-05 10:01:24 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/CSimplePool.hpp"
|
2022-05-14 06:47:29 +00:00
|
|
|
#include "Runtime/Graphics/CGX.hpp"
|
2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/Graphics/CTexture.hpp"
|
|
|
|
#include "Runtime/GuiSys/CDrawStringOptions.hpp"
|
|
|
|
#include "Runtime/GuiSys/CTextRenderBuffer.hpp"
|
2016-03-11 22:52:55 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
|
|
|
CRasterFont::CRasterFont(metaforce::CInputStream& in, metaforce::IObjectStore& store) {
|
2019-04-07 05:14:48 +00:00
|
|
|
u32 magic = 0;
|
2022-02-18 07:37:54 +00:00
|
|
|
in.Get(reinterpret_cast<u8*>(&magic), 4);
|
2018-12-08 05:30:43 +00:00
|
|
|
if (magic != SBIG('FONT'))
|
|
|
|
return;
|
|
|
|
|
2022-02-18 07:37:54 +00:00
|
|
|
u32 version = in.ReadLong();
|
|
|
|
x4_monoWidth = in.ReadLong();
|
|
|
|
x8_monoHeight = in.ReadLong();
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
if (version >= 1)
|
2022-02-18 07:37:54 +00:00
|
|
|
x8c_baseline = in.ReadLong();
|
2018-12-08 05:30:43 +00:00
|
|
|
else
|
|
|
|
x8c_baseline = x8_monoHeight;
|
|
|
|
|
|
|
|
if (version >= 2)
|
2022-02-18 07:37:54 +00:00
|
|
|
x90_lineMargin = in.ReadLong();
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2022-02-18 07:37:54 +00:00
|
|
|
bool tmp1 = in.ReadBool();
|
|
|
|
bool tmp2 = in.ReadBool();
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2022-02-18 07:37:54 +00:00
|
|
|
u32 tmp3 = in.ReadLong();
|
|
|
|
u32 tmp4 = in.ReadLong();
|
|
|
|
std::string name = in.Get<std::string>();
|
|
|
|
u32 txtrId = (version == 5 ? in.ReadLongLong() : in.ReadLong());
|
2018-12-08 05:30:43 +00:00
|
|
|
x30_fontInfo = CFontInfo(tmp1, tmp2, tmp3, tmp4, name.c_str());
|
|
|
|
x80_texture = store.GetObj({FOURCC('TXTR'), txtrId});
|
2022-02-18 07:37:54 +00:00
|
|
|
x2c_mode = CTexture::EFontType(in.ReadLong());
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2022-02-18 07:37:54 +00:00
|
|
|
u32 glyphCount = in.ReadLong();
|
2018-12-08 05:30:43 +00:00
|
|
|
xc_glyphs.reserve(glyphCount);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < glyphCount; ++i) {
|
2022-02-18 07:37:54 +00:00
|
|
|
char16_t chr = in.ReadShort();
|
|
|
|
float startU = in.ReadFloat();
|
|
|
|
float startV = in.ReadFloat();
|
|
|
|
float endU = in.ReadFloat();
|
|
|
|
float endV = in.ReadFloat();
|
2018-12-08 05:30:43 +00:00
|
|
|
s32 layer = 0;
|
|
|
|
s32 a, b, c, cellWidth, cellHeight, baseline, kernStart;
|
|
|
|
if (version < 4) {
|
2022-02-18 07:37:54 +00:00
|
|
|
a = in.ReadInt32();
|
|
|
|
b = in.ReadInt32();
|
|
|
|
c = in.ReadInt32();
|
|
|
|
cellWidth = in.ReadInt32();
|
|
|
|
cellHeight = in.ReadInt32();
|
|
|
|
baseline = in.ReadInt32();
|
|
|
|
kernStart = in.ReadInt32();
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2022-02-18 07:37:54 +00:00
|
|
|
layer = in.ReadInt8();
|
|
|
|
a = in.ReadInt8();
|
|
|
|
b = in.ReadInt8();
|
|
|
|
c = in.ReadInt8();
|
|
|
|
cellWidth = in.ReadInt8();
|
|
|
|
cellHeight = in.ReadInt8();
|
|
|
|
baseline = in.ReadInt8();
|
|
|
|
kernStart = in.ReadInt16();
|
2016-03-15 23:44:59 +00:00
|
|
|
}
|
2020-03-21 04:12:13 +00:00
|
|
|
xc_glyphs.emplace_back(
|
|
|
|
chr, CGlyph(a, b, c, startU, startV, endU, endV, cellWidth, cellHeight, baseline, kernStart, layer));
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-03-15 23:44:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
std::sort(xc_glyphs.begin(), xc_glyphs.end(), [=](auto& a, auto& b) -> bool { return a.first < b.first; });
|
2016-03-15 23:44:59 +00:00
|
|
|
|
2022-02-18 07:37:54 +00:00
|
|
|
u32 kernCount = in.ReadLong();
|
2018-12-08 05:30:43 +00:00
|
|
|
x1c_kerning.reserve(kernCount);
|
2016-03-15 23:44:59 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
for (u32 i = 0; i < kernCount; ++i) {
|
2022-02-18 07:37:54 +00:00
|
|
|
char16_t first = in.ReadShort();
|
|
|
|
char16_t second = in.ReadShort();
|
|
|
|
s32 howMuch = in.ReadInt32();
|
2018-12-08 05:30:43 +00:00
|
|
|
x1c_kerning.emplace_back(first, second, howMuch);
|
|
|
|
}
|
2016-03-23 20:38:01 +00:00
|
|
|
|
2019-12-11 18:55:19 +00:00
|
|
|
if (magic == SBIG('FONT') && version <= 4)
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_initialized = true;
|
2016-03-11 22:52:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 10:01:24 +00:00
|
|
|
const CGlyph* CRasterFont::InternalGetGlyph(char16_t chr) const {
|
|
|
|
const auto iter =
|
|
|
|
std::find_if(xc_glyphs.cbegin(), xc_glyphs.cend(), [chr](const auto& entry) { return entry.first == chr; });
|
|
|
|
|
|
|
|
if (iter == xc_glyphs.cend()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &iter->second;
|
|
|
|
}
|
|
|
|
|
2016-03-16 19:53:06 +00:00
|
|
|
void CRasterFont::SinglePassDrawString(const CDrawStringOptions& opts, int x, int y, int& xout, int& yout,
|
2018-12-08 05:30:43 +00:00
|
|
|
CTextRenderBuffer* renderBuf, const char16_t* str, s32 length) const {
|
|
|
|
if (!x0_initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char16_t* chr = str;
|
|
|
|
const CGlyph* prevGlyph = nullptr;
|
|
|
|
while (*chr != u'\0') {
|
|
|
|
const CGlyph* glyph = GetGlyph(*chr);
|
|
|
|
if (glyph) {
|
|
|
|
if (opts.x0_direction == ETextDirection::Horizontal) {
|
|
|
|
x += glyph->GetLeftPadding();
|
|
|
|
|
2020-03-28 00:11:51 +00:00
|
|
|
if (prevGlyph != nullptr) {
|
2018-12-08 05:30:43 +00:00
|
|
|
x += KernLookup(x1c_kerning, prevGlyph->GetKernStart(), *chr);
|
2020-03-28 00:11:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
int left = 0;
|
|
|
|
int top = 0;
|
|
|
|
|
|
|
|
if (renderBuf) {
|
|
|
|
left += x;
|
|
|
|
top += y - glyph->GetBaseline();
|
|
|
|
renderBuf->AddCharacter(zeus::CVector2i(left, top), *chr, opts.x4_colors[2]);
|
2016-03-16 19:53:06 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
x += glyph->GetRightPadding() + glyph->GetAdvance();
|
|
|
|
}
|
2016-03-16 19:53:06 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
prevGlyph = glyph;
|
|
|
|
chr++;
|
|
|
|
if (length == -1)
|
|
|
|
continue;
|
2016-03-16 19:53:06 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if ((chr - str) >= length)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xout = x;
|
|
|
|
yout = y;
|
2016-03-16 19:53:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CRasterFont::DrawSpace(const CDrawStringOptions& opts, int x, int y, int& xout, int& yout, int len) const {
|
|
|
|
if (opts.x0_direction != ETextDirection::Horizontal)
|
|
|
|
return;
|
2016-03-16 19:53:06 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
xout = x + len;
|
|
|
|
yout = y;
|
2016-03-16 19:53:06 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 07:41:33 +00:00
|
|
|
void CRasterFont::DrawString(const CDrawStringOptions& opts, int x, int y, int& xout, int& yout,
|
2018-12-08 05:30:43 +00:00
|
|
|
CTextRenderBuffer* renderBuf, const char16_t* str, int len) const {
|
|
|
|
if (!x0_initialized)
|
|
|
|
return;
|
|
|
|
|
2022-05-14 06:47:29 +00:00
|
|
|
if (renderBuf != nullptr) {
|
|
|
|
CGraphicsPalette pal(EPaletteFormat::RGB5A3, 4);
|
|
|
|
pal.Lock();
|
|
|
|
*reinterpret_cast<u16*>(pal.GetPaletteData() + 0) = SBIG(zeus::CColor(0.f, 0.f, 0.f, 0.f).toRGB5A3());
|
|
|
|
*reinterpret_cast<u16*>(pal.GetPaletteData() + 2) = SBIG(opts.x4_colors[0].toRGB5A3());
|
|
|
|
*reinterpret_cast<u16*>(pal.GetPaletteData() + 4) = SBIG(opts.x4_colors[1].toRGB5A3());
|
|
|
|
*reinterpret_cast<u16*>(pal.GetPaletteData() + 6) = SBIG(zeus::CColor(0.f, 0.f, 0.f, 0.f).toRGB5A3());
|
|
|
|
pal.UnLock();
|
|
|
|
renderBuf->AddPaletteChange(pal);
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SinglePassDrawString(opts, x, y, xout, yout, renderBuf, str, len);
|
2016-03-16 19:53:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CRasterFont::GetSize(const CDrawStringOptions& opts, int& width, int& height, const char16_t* str, int len) const {
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
|
|
|
|
const char16_t* chr = str;
|
|
|
|
const CGlyph* prevGlyph = nullptr;
|
|
|
|
int prevWidth = 0;
|
|
|
|
while (*chr != u'\0') {
|
|
|
|
const CGlyph* glyph = GetGlyph(*chr);
|
|
|
|
|
|
|
|
if (glyph) {
|
|
|
|
if (opts.x0_direction == ETextDirection::Horizontal) {
|
|
|
|
int advance = 0;
|
|
|
|
if (prevGlyph)
|
|
|
|
advance = KernLookup(x1c_kerning, prevGlyph->GetKernStart(), *chr);
|
|
|
|
|
|
|
|
int curWidth = prevWidth + (glyph->GetLeftPadding() + glyph->GetAdvance() + glyph->GetRightPadding() + advance);
|
|
|
|
int curHeight = glyph->GetBaseline() - (x8_monoHeight + glyph->GetCellHeight());
|
2016-03-16 19:53:06 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
width = curWidth;
|
|
|
|
prevWidth = curWidth;
|
2016-03-16 19:53:06 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (curHeight > height)
|
|
|
|
height = curHeight;
|
|
|
|
}
|
2016-03-16 19:53:06 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
prevGlyph = glyph;
|
|
|
|
chr++;
|
|
|
|
if (len == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((chr - str) >= len)
|
|
|
|
break;
|
|
|
|
}
|
2016-03-16 19:53:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
bool CRasterFont::IsFinishedLoading() const {
|
|
|
|
if (!x80_texture || !x80_texture.IsLoaded())
|
|
|
|
return false;
|
|
|
|
return true;
|
2017-01-30 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 06:47:29 +00:00
|
|
|
void CRasterFont::SetupRenderState() {
|
|
|
|
static const GX::VtxDescList skDescList[3] = {
|
|
|
|
{GX::VA_POS, GX::DIRECT},
|
|
|
|
{GX::VA_TEX0, GX::DIRECT},
|
|
|
|
{GX::VA_NULL, GX::NONE}
|
|
|
|
};
|
|
|
|
|
|
|
|
x80_texture->Load(GX::TEXMAP0, EClampMode::Clamp);
|
|
|
|
CGX::SetTevKAlphaSel(GX::TEVSTAGE0, GX::TEV_KASEL_K0_A);
|
|
|
|
CGX::SetTevKColorSel(GX::TEVSTAGE0, GX::TEV_KCSEL_K0);
|
|
|
|
CGX::SetTevColorIn(GX::TEVSTAGE0, GX::CC_ZERO, GX::CC_TEXC, GX::CC_KONST, GX::CC_ZERO);
|
|
|
|
CGX::SetTevAlphaIn(GX::TEVSTAGE0, GX::CA_ZERO, GX::CA_TEXA, GX::CA_KONST, GX::CA_ZERO);
|
|
|
|
CGX::SetStandardTevColorAlphaOp(GX::TEVSTAGE0);
|
|
|
|
CGX::SetTevDirect(GX::TEVSTAGE0);
|
|
|
|
CGX::SetVtxDescv(skDescList);
|
|
|
|
CGX::SetNumChans(0);
|
|
|
|
CGX::SetNumTexGens(1);
|
|
|
|
CGX::SetNumTevStages(1);
|
|
|
|
CGX::SetNumIndStages(0);
|
|
|
|
CGX::SetTevOrder(GX::TEVSTAGE0, GX::TEXCOORD0, GX::TEXMAP0, GX::COLOR_NULL);
|
|
|
|
CGX::SetTexCoordGen(GX::TEXCOORD0, GX::TG_MTX2x4, GX::TG_TEX0, GX::IDENTITY, false, GX::PTIDENTITY);
|
|
|
|
}
|
2020-04-24 01:23:17 +00:00
|
|
|
std::unique_ptr<IObj> FRasterFontFactory([[maybe_unused]] const SObjectTag& tag, CInputStream& in,
|
|
|
|
const CVParamTransfer& vparms, [[maybe_unused]] CObjectReference* selfRef) {
|
2018-12-08 05:30:43 +00:00
|
|
|
CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>();
|
|
|
|
return TToken<CRasterFont>::GetIObjObjectFor(std::make_unique<CRasterFont>(in, *sp));
|
2016-03-12 05:10:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|