From 29d2e8167658e16e29df115388f9b0594bd6f767 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 28 Feb 2014 23:04:34 -0800 Subject: [PATCH] Cleanup errors in sceFontGet*ImageRect(). --- Core/Font/PGF.cpp | 6 +-- Core/Font/PGF.h | 2 +- Core/HLE/sceFont.cpp | 87 ++++++++++++++++++++++++++++++-------------- 3 files changed, 63 insertions(+), 32 deletions(-) diff --git a/Core/Font/PGF.cpp b/Core/Font/PGF.cpp index f9e03ce0e7..4f98b59538 100644 --- a/Core/Font/PGF.cpp +++ b/Core/Font/PGF.cpp @@ -323,16 +323,16 @@ int PGF::GetCharIndex(int charCode, const std::vector &charmapCompressed) { return -1; } -bool PGF::GetCharInfo(int charCode, PGFCharInfo *charInfo, int altCharCode) const { +bool PGF::GetCharInfo(int charCode, PGFCharInfo *charInfo, int altCharCode, int glyphType) const { Glyph glyph; memset(charInfo, 0, sizeof(*charInfo)); - if (!GetCharGlyph(charCode, FONT_PGF_CHARGLYPH, glyph)) { + if (!GetCharGlyph(charCode, glyphType, glyph)) { if (charCode < firstGlyph) { // Character not in font, return zeroed charInfo as on real PSP. return false; } - if (!GetCharGlyph(altCharCode, FONT_PGF_CHARGLYPH, glyph)) { + if (!GetCharGlyph(altCharCode, glyphType, glyph)) { return false; } } diff --git a/Core/Font/PGF.h b/Core/Font/PGF.h index 4fd224c810..eafc7224e0 100644 --- a/Core/Font/PGF.h +++ b/Core/Font/PGF.h @@ -264,7 +264,7 @@ public: bool ReadPtr(const u8 *ptr, size_t dataSize); - bool GetCharInfo(int charCode, PGFCharInfo *ci, int altCharCode) const; + bool GetCharInfo(int charCode, PGFCharInfo *ci, int altCharCode, int glyphType = FONT_PGF_CHARGLYPH) const; void GetFontInfo(PGFFontInfo *fi) const; void DrawCharacter(const GlyphImage *image, int clipX, int clipY, int clipWidth, int clipHeight, int charCode, int altCharCode, int glyphType) const; diff --git a/Core/HLE/sceFont.cpp b/Core/HLE/sceFont.cpp index b736fbf1f6..ee9c3ae982 100644 --- a/Core/HLE/sceFont.cpp +++ b/Core/HLE/sceFont.cpp @@ -258,11 +258,15 @@ public: LoadedFont(Font *font, u32 fontLibID, u32 handle) : fontLibID_(fontLibID), font_(font), handle_(handle), open_(true) {} - Font *GetFont() { return font_; } - PGF *GetPGF() { return font_->GetPGF(); } + const Font *GetFont() const { return font_; } + const PGF *GetPGF() const { return font_->GetPGF(); } + const FontLib *GetFontLib() const { return fontLibList[fontLibID_]; } FontLib *GetFontLib() { return fontLibList[fontLibID_]; } u32 Handle() const { return handle_; } + bool GetCharInfo(int charCode, PGFCharInfo *charInfo, int glyphType = FONT_PGF_CHARGLYPH) const; + void DrawCharacter(const GlyphImage *image, int clipX, int clipY, int clipWidth, int clipHeight, int charCode, int glyphType) const; + bool IsOpen() const { return open_; } void Close() { open_ = false; @@ -361,6 +365,11 @@ struct NativeFontLib { u16_le unk5; }; +struct FontImageRect { + s16_le width; + s16_le height; +}; + // A "fontLib" is a container of loaded fonts. // One can open either "internal" fonts or custom fonts into a fontlib. class FontLib { @@ -554,6 +563,18 @@ void PostOpenCallback::run(MipsCall &call) { fontLib->SetFileFontHandle(v0); } +inline bool LoadedFont::GetCharInfo(int charCode, PGFCharInfo *charInfo, int glyphType) const { + auto fontLib = GetFontLib(); + int altCharCode = fontLib == NULL ? -1 : fontLib->GetAltCharCode(); + return GetPGF()->GetCharInfo(charCode, charInfo, altCharCode, glyphType); +} + +inline void LoadedFont::DrawCharacter(const GlyphImage *image, int clipX, int clipY, int clipWidth, int clipHeight, int charCode, int glyphType) const { + auto fontLib = GetFontLib(); + int altCharCode = fontLib == NULL ? -1 : fontLib->GetAltCharCode(); + GetPGF()->DrawCharacter(image, clipX, clipY, clipWidth, clipHeight, charCode, altCharCode, glyphType); +} + FontLib *GetFontLib(u32 handle) { if (fontLibMap.find(handle) != fontLibMap.end()) { return fontLibList[fontLibMap[handle]]; @@ -1021,10 +1042,8 @@ int sceFontGetCharInfo(u32 fontHandle, u32 charCode, u32 charInfoPtr) { } DEBUG_LOG(SCEFONT, "sceFontGetCharInfo(%08x, %i, %08x)", fontHandle, charCode, charInfoPtr); - auto fontLib = font->GetFontLib(); - int altCharCode = fontLib == NULL ? -1 : fontLib->GetAltCharCode(); auto charInfo = PSPPointer::Create(charInfoPtr); - font->GetPGF()->GetCharInfo(charCode, charInfo, altCharCode); + font->GetCharInfo(charCode, charInfo); return 0; } @@ -1037,26 +1056,42 @@ int sceFontGetShadowInfo(u32 fontHandle, u32 charCode, u32 shadowCharInfoPtr) { } int sceFontGetCharImageRect(u32 fontHandle, u32 charCode, u32 charRectPtr) { - DEBUG_LOG(SCEFONT, "sceFontGetCharImageRect(%08x, %i, %08x)", fontHandle, charCode, charRectPtr); - if (!Memory::IsValidAddress(charRectPtr)) - return -1; - - PGFCharInfo charInfo; - LoadedFont *font = GetLoadedFont(fontHandle, false); - if (font) { - auto fontLib = font->GetFontLib(); - int altCharCode = fontLib == NULL ? -1 : fontLib->GetAltCharCode(); - font->GetPGF()->GetCharInfo(charCode, &charInfo, altCharCode); - Memory::Write_U16(charInfo.bitmapWidth, charRectPtr); // character bitmap width in pixels - Memory::Write_U16(charInfo.bitmapHeight, charRectPtr + 2); // character bitmap height in pixels - } else { - ERROR_LOG(SCEFONT, "sceFontGetCharImageRect - invalid font"); + auto charRect = PSPPointer::Create(charRectPtr); + LoadedFont *font = GetLoadedFont(fontHandle, true); + if (!font) { + ERROR_LOG_REPORT(SCEFONT, "sceFontGetCharImageRect(%08x, %i, %08x): bad font", fontHandle, charCode, charRectPtr); + return ERROR_FONT_INVALID_PARAMETER; } + if (!charRect.IsValid()) { + ERROR_LOG_REPORT(SCEFONT, "sceFontGetCharImageRect(%08x, %i, %08x): invalid rect pointer", fontHandle, charCode, charRectPtr); + return ERROR_FONT_INVALID_PARAMETER; + } + + DEBUG_LOG(SCEFONT, "sceFontGetCharImageRect(%08x, %i, %08x)", fontHandle, charCode, charRectPtr); + PGFCharInfo charInfo; + font->GetCharInfo(charCode, &charInfo); + charRect->width = charInfo.bitmapWidth; + charRect->height = charInfo.bitmapHeight; return 0; } int sceFontGetShadowImageRect(u32 fontHandle, u32 charCode, u32 charRectPtr) { - ERROR_LOG_REPORT(SCEFONT, "UNIMPL sceFontGetShadowImageRect()"); + auto charRect = PSPPointer::Create(charRectPtr); + LoadedFont *font = GetLoadedFont(fontHandle, true); + if (!font) { + ERROR_LOG_REPORT(SCEFONT, "sceFontGetShadowImageRect(%08x, %i, %08x): bad font", fontHandle, charCode, charRectPtr); + return ERROR_FONT_INVALID_PARAMETER; + } + if (!charRect.IsValid()) { + ERROR_LOG_REPORT(SCEFONT, "sceFontGetShadowImageRect(%08x, %i, %08x): invalid rect pointer", fontHandle, charCode, charRectPtr); + return ERROR_FONT_INVALID_PARAMETER; + } + + DEBUG_LOG(SCEFONT, "sceFontGetShadowImageRect(%08x, %i, %08x)", fontHandle, charCode, charRectPtr); + PGFCharInfo charInfo; + font->GetCharInfo(charCode, &charInfo, FONT_PGF_SHADOWGLYPH); + charRect->width = charInfo.bitmapWidth; + charRect->height = charInfo.bitmapHeight; return 0; } @@ -1073,8 +1108,7 @@ int sceFontGetCharGlyphImage(u32 fontHandle, u32 charCode, u32 glyphImagePtr) { DEBUG_LOG(SCEFONT, "sceFontGetCharGlyphImage(%x, %x, %x)", fontHandle, charCode, glyphImagePtr); auto glyph = PSPPointer::Create(glyphImagePtr); - int altCharCode = font->GetFontLib()->GetAltCharCode(); - font->GetPGF()->DrawCharacter(glyph, -1, -1, -1, -1, charCode, altCharCode, FONT_PGF_CHARGLYPH); + font->DrawCharacter(glyph, -1, -1, -1, -1, charCode, FONT_PGF_CHARGLYPH); return 0; } @@ -1091,8 +1125,7 @@ int sceFontGetCharGlyphImage_Clip(u32 fontHandle, u32 charCode, u32 glyphImagePt DEBUG_LOG(SCEFONT, "sceFontGetCharGlyphImage_Clip(%08x, %i, %08x, %i, %i, %i, %i)", fontHandle, charCode, glyphImagePtr, clipXPos, clipYPos, clipWidth, clipHeight); auto glyph = PSPPointer::Create(glyphImagePtr); - int altCharCode = font->GetFontLib()->GetAltCharCode(); - font->GetPGF()->DrawCharacter(glyph, clipXPos, clipYPos, clipWidth, clipHeight, charCode, altCharCode, FONT_PGF_CHARGLYPH); + font->DrawCharacter(glyph, clipXPos, clipYPos, clipWidth, clipHeight, charCode, FONT_PGF_CHARGLYPH); return 0; } @@ -1236,8 +1269,7 @@ int sceFontGetShadowGlyphImage(u32 fontHandle, u32 charCode, u32 glyphImagePtr) DEBUG_LOG(SCEFONT, "sceFontGetShadowGlyphImage(%x, %x, %x)", fontHandle, charCode, glyphImagePtr); auto glyph = PSPPointer::Create(glyphImagePtr); - int altCharCode = font->GetFontLib()->GetAltCharCode(); - font->GetPGF()->DrawCharacter(glyph, -1, -1, -1, -1, charCode, altCharCode, FONT_PGF_SHADOWGLYPH); + font->DrawCharacter(glyph, -1, -1, -1, -1, charCode, FONT_PGF_SHADOWGLYPH); return 0; } @@ -1254,8 +1286,7 @@ int sceFontGetShadowGlyphImage_Clip(u32 fontHandle, u32 charCode, u32 glyphImage DEBUG_LOG(SCEFONT, "sceFontGetShadowGlyphImage_Clip(%08x, %i, %08x, %i, %i, %i, %i)", fontHandle, charCode, glyphImagePtr, clipXPos, clipYPos, clipWidth, clipHeight); auto glyph = PSPPointer::Create(glyphImagePtr); - int altCharCode = font->GetFontLib()->GetAltCharCode(); - font->GetPGF()->DrawCharacter(glyph, clipXPos, clipYPos, clipWidth, clipHeight, charCode, altCharCode, FONT_PGF_SHADOWGLYPH); + font->DrawCharacter(glyph, clipXPos, clipYPos, clipWidth, clipHeight, charCode, FONT_PGF_SHADOWGLYPH); return 0; }