Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions core/libs/prx/libSceFont/include/FontInternal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ struct FontState {
std::shared_ptr<const std::vector<unsigned char>> faceData;
FT_Face face = nullptr;
std::vector<FallbackFace> fallbacks;
float scaleW = 16.0f;
float scaleH = 16.0f;
std::map<int, int> scriptLanguages;
std::map<int, int> typographicFeatures;
int attribute = 0;
Expand Down Expand Up @@ -134,7 +132,7 @@ void CachedStyleSetScalar(CachedStyle& style, float value);
void ClearRenderOutputs(FontGlyphMetrics* metrics, FontRenderOutput* result);
int ComputeHorizontalLayout(FontHandle handle, const StyleStateBlock* style, std::uint8_t* outWords);
int ComputeVerticalLayout(FontHandle handle, const StyleStateBlock* style, std::uint8_t* outWords);
int GetCharGlyphMetrics(FontHandle handle, std::uint32_t code, FontGlyphMetrics* metrics, bool useCachedStyle);
int GetCharGlyphMetrics(FontHandle handle, std::uint32_t code, FontGlyphMetrics* metrics, bool useCachedStyle, float* usedScaleW = nullptr, float* usedScaleH = nullptr);
int RenderCharGlyphImageCore(FontHandle handle, std::uint32_t code, FontRenderSurface* surface, float x, float y, FontGlyphMetrics* metrics, FontRenderOutput* result);

}
Expand Down
4 changes: 3 additions & 1 deletion core/libs/prx/libSceFont/src/Layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ int Font::ComputeVerticalLayout(FontHandle handle, const StyleStateBlock* style,
return SCE_FONT_OK;
}

int Font::GetCharGlyphMetrics(FontHandle handle, std::uint32_t code, FontGlyphMetrics* metrics, bool useCachedStyle) {
int Font::GetCharGlyphMetrics(FontHandle handle, std::uint32_t code, FontGlyphMetrics* metrics, bool useCachedStyle, float* usedScaleW, float* usedScaleH) {
const auto clearMetrics = [&] {
if (metrics) *metrics = {};
};
Expand Down Expand Up @@ -434,6 +434,8 @@ int Font::GetCharGlyphMetrics(FontHandle handle, std::uint32_t code, FontGlyphMe
metrics->Vertical.bearingX = static_cast<float>(vertBearingX) / 64.0f * ratioX;
metrics->Vertical.bearingY = static_cast<float>(vertBearingY) / 64.0f * ratioY;
metrics->Vertical.advance = static_cast<float>(slot->metrics.vertAdvance) / 64.0f * ratioY;
if (usedScaleW) *usedScaleW = scaleW;
if (usedScaleH) *usedScaleH = scaleH;
return finish(SCE_FONT_OK);
}

Expand Down
2 changes: 0 additions & 2 deletions core/libs/prx/libSceFont/src/Open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,6 @@ int APS5_VABI sceFontOpenFontInstance(FontHandle fontHandle, FontHandle setupFon
ReleaseFontLock(source, sourceLock);
FontState& targetState = ResetState(target);
if (FontState* sourceState = TryGetState(fontHandle)) {
targetState.scaleW = sourceState->scaleW;
targetState.scaleH = sourceState->scaleH;
if (sourceState->faceData && !sourceState->faceData->empty()) LoadStateFace(targetState, sourceState->faceData, subFontIndex);
for (const FallbackFace& fallback : sourceState->fallbacks) AddStateFallbackFace(targetState, fallback.data, fallback.subFontIndex);
}
Expand Down
20 changes: 13 additions & 7 deletions core/libs/prx/libSceFont/src/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ int APS5_VABI sceFontGetKerning(FontHandle fontHandle, std::uint32_t preCode, st
if (!font || font->magic != HANDLE_MAGIC || !AcquireFontLock(font, fontLock)) return SCE_FONT_ERROR_INVALID_FONT_HANDLE;
*kerning = {};
const FontState* state = TryGetState(fontHandle);
if (state) LoadKerning(*state, state->scaleW, state->scaleH, preCode, code, kerning);
if (state) {
float scaleW = 0.0f;
float scaleH = 0.0f;
StyleStateGetScalePixel(&font->style, &scaleW, &scaleH);
LoadKerning(*state, scaleW, scaleH, preCode, code, kerning);
}
ReleaseFontLock(font, fontLock);
return SCE_FONT_OK;
}
Expand Down Expand Up @@ -373,8 +378,7 @@ int APS5_VABI sceFontGenerateCharGlyph(FontHandle fontHandle, std::uint32_t code
*pGlyph = nullptr;
if (!fontHandle) return SCE_FONT_ERROR_INVALID_FONT_HANDLE;
if (code == 0) return SCE_FONT_ERROR_NO_SUPPORT_CODE;
const FontState* state = TryGetState(fontHandle);
if (!state) return SCE_FONT_ERROR_INVALID_FONT_HANDLE;
if (!TryGetState(fontHandle)) return SCE_FONT_ERROR_INVALID_FONT_HANDLE;
const std::uint8_t glyphForm = detail ? detail->glyph_form : 0;
const std::uint8_t metricsForm = detail ? detail->metrics_form : 0;
const std::uint16_t formOptions = detail ? detail->form_options : 0;
Expand All @@ -385,7 +389,9 @@ int APS5_VABI sceFontGenerateCharGlyph(FontHandle fontHandle, std::uint32_t code
if ((glyphForm != 0 && metricsForm == 0) || glyphForm > 1 || metricsForm > 4) return SCE_FONT_ERROR_INVALID_PARAMETER;
if (glyphMemory && glyphMemory->mem_kind != MEMORY_MAGIC) return SCE_FONT_ERROR_INVALID_PARAMETER;
FontGlyphMetrics metrics{};
const int rc = GetCharGlyphMetrics(fontHandle, code, &metrics, false);
float scaleW = 0.0f;
float scaleH = 0.0f;
const int rc = GetCharGlyphMetrics(fontHandle, code, &metrics, false, &scaleW, &scaleH);
if (rc != SCE_FONT_OK) return rc;
auto* generated = new (std::nothrow) GeneratedGlyph();
if (!generated) return SCE_FONT_ERROR_ALLOCATION_FAILED;
Expand All @@ -395,13 +401,13 @@ int APS5_VABI sceFontGenerateCharGlyph(FontHandle fontHandle, std::uint32_t code
generated->glyph.flags = formOptions;
generated->glyph.glyph_form = glyphForm;
generated->glyph.metrics_form = metricsForm;
generated->glyph.em_size = ClampToU16(state->scaleH);
generated->glyph.em_size = ClampToU16(scaleH);
generated->glyph.baseline = ClampToU16(metrics.Horizontal.bearingY);
generated->glyph.height_px = ClampToU16(metrics.height);
generated->glyph.origin_x = 0;
generated->glyph.origin_y = generated->glyph.baseline;
generated->glyph.scale_x = state->scaleW;
generated->glyph.base_scale = state->scaleH;
generated->glyph.scale_x = scaleW;
generated->glyph.base_scale = scaleH;
generated->glyph.memory = glyphMemory;
generated->owner = fontHandle;
generated->outline.outline_flags = generated->glyph.flags;
Expand Down
9 changes: 1 addition & 8 deletions core/libs/prx/libSceFont/src/Style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,7 @@ int APS5_VABI sceFontGetTypographicDesign(FontHandle fontHandle, int typographic
}

int APS5_VABI sceFontSetScalePixel(FontHandle fontHandle, float w, float h) {
return UpdateFontStyle(fontHandle, [&](FontHandleNative* font) {
const int changed = StyleStateSetScalePixel(&font->style, w, h);
if (FontState* state = TryGetState(fontHandle)) {
state->scaleW = w;
state->scaleH = h;
}
return changed;
});
return UpdateFontStyle(fontHandle, [&](FontHandleNative* font) { return StyleStateSetScalePixel(&font->style, w, h); });
}

int APS5_VABI sceFontSetScalePoint(FontHandle fontHandle, float w, float h) {
Expand Down
69 changes: 69 additions & 0 deletions core/libs/tests/GuestFont.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "prx/libSceFont/include/FontDriver.hpp"
#include "tests/FontFixture.hpp"
#include <cstddef>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -30,6 +31,7 @@ int APS5_VABI sceFontOpenFontSet(FontLibrary, std::uint32_t, std::uint32_t, cons
int APS5_VABI sceFontOpenFontMemory(FontLibrary, const void*, std::uint32_t, const FontOpenDetail*, FontHandle*);
int APS5_VABI sceFontOpenFontFile(FontLibrary, const char*, std::uint32_t, const FontOpenDetail*, FontHandle*);
int APS5_VABI sceFontCloseFont(FontHandle);
int APS5_VABI sceFontOpenFontInstance(FontHandle, FontHandle, FontHandle*);
int APS5_VABI sceFontSetScriptLanguage(FontHandle, int, int);
int APS5_VABI sceFontGetScriptLanguage(FontHandle, int, int*);
int APS5_VABI sceFontSetTypographicDesign(FontHandle, int, int);
Expand All @@ -39,6 +41,9 @@ int APS5_VABI sceFontGetAttribute(FontHandle, int, int*);
int APS5_VABI sceFontSetResolutionDpi(FontHandle, std::uint32_t, std::uint32_t);
int APS5_VABI sceFontGetResolutionDpi(FontHandle, std::uint32_t*, std::uint32_t*);
int APS5_VABI sceFontSetScalePixel(FontHandle, float, float);
int APS5_VABI sceFontSetScalePoint(FontHandle, float, float);
int APS5_VABI sceFontGetScalePixel(FontHandle, float*, float*);
int APS5_VABI sceFontGlyphGetScalePixel(FontGlyph, float*, float*);
int APS5_VABI sceFontBindRenderer(FontHandle, FontRenderer);
int APS5_VABI sceFontUnbindRenderer(FontHandle);
int APS5_VABI sceFontSetupRenderScalePixel(FontHandle, float, float);
Expand Down Expand Up @@ -198,6 +203,68 @@ static bool KerningIs(const FontKerning& kerning, float offsetX) {
return kerning.offsetX == offsetX && kerning.offsetY == 0.0f && kerning.positionX == 0.0f && kerning.positionY == 0.0f;
}

static void CheckGlyphScale(FontHandle font, const FontMemory* memory, float expectedW, float expectedH) {
float width = 0.0f;
float height = 0.0f;
Require(sceFontGetScalePixel(font, &width, &height) == SCE_FONT_OK);
Require(width == expectedW && height == expectedH);
FontGlyph glyph = nullptr;
Require(sceFontGenerateCharGlyph(font, 'A', nullptr, &glyph) == SCE_FONT_OK && glyph != nullptr);
Require(sceFontGlyphGetScalePixel(glyph, &width, &height) == SCE_FONT_OK);
if (width != expectedW || height != expectedH) {
std::fprintf(stderr, "Generated glyph scale %.3f x %.3f differs from font scale %.3f x %.3f\n", width, height, expectedW, expectedH);
}
Require(width == expectedW && height == expectedH);
Require(glyph->em_size == static_cast<std::uint16_t>(std::lround(expectedH)));
Require(sceFontDeleteGlyph(memory, &glyph) == SCE_FONT_OK && glyph == nullptr);
}

static void CheckPointAndDpiScale(FontHandle font, FontLibrary library, const FontMemory* memory, FontRenderer renderer) {
FontKerning kerning{};
Require(sceFontSetScalePoint(font, 20.0f, 30.0f) == SCE_FONT_OK);
CheckGlyphScale(font, memory, 40.0f, 60.0f);
Require(sceFontGetKerning(font, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -8.0f));
Require(sceFontSetResolutionDpi(font, 216, 288) == SCE_FONT_OK);
CheckGlyphScale(font, memory, 60.0f, 120.0f);
Require(sceFontGetKerning(font, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -12.0f));
Require(sceFontSetScalePoint(font, 50.0f, 40.0f) == SCE_FONT_OK);
CheckGlyphScale(font, memory, 150.0f, 160.0f);
Require(sceFontGetKerning(font, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -30.0f));
Require(sceFontSetResolutionDpi(font, 0, 0) == SCE_FONT_OK);
CheckGlyphScale(font, memory, 50.0f, 40.0f);
Require(sceFontGetKerning(font, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -10.0f));
Require(sceFontSetScalePixel(font, 100.0f, 75.0f) == SCE_FONT_OK);
Require(sceFontSetResolutionDpi(font, 144, 216) == SCE_FONT_OK);
CheckGlyphScale(font, memory, 100.0f, 75.0f);
Require(sceFontGetKerning(font, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -20.0f));
Require(sceFontSetScalePoint(font, 20.0f, 30.0f) == SCE_FONT_OK);
CheckGlyphScale(font, memory, 40.0f, 90.0f);
Require(sceFontGetKerning(font, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -8.0f));
FontHandle instance = nullptr;
Require(sceFontOpenFontInstance(font, nullptr, &instance) == SCE_FONT_OK && instance != nullptr);
CheckGlyphScale(instance, memory, 40.0f, 90.0f);
Require(sceFontGetKerning(instance, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -8.0f));
Require(sceFontSetScalePoint(instance, 30.0f, 40.0f) == SCE_FONT_OK);
CheckGlyphScale(instance, memory, 60.0f, 120.0f);
CheckGlyphScale(font, memory, 40.0f, 90.0f);
Require(sceFontCloseFont(instance) == SCE_FONT_OK);
Require(sceFontBindRenderer(font, renderer) == SCE_FONT_OK);
Require(sceFontSetupRenderScalePixel(font, 100.0f, 100.0f) == SCE_FONT_OK);
Require(sceFontGetRenderScaledKerning(font, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -20.0f));
Require(sceFontGetKerning(font, 'A', 'V', &kerning) == SCE_FONT_OK && KerningIs(kerning, -8.0f));
Require(sceFontUnbindRenderer(font) == SCE_FONT_OK);
const std::vector<unsigned char> data = KerningFont();
FontHandle fresh = nullptr;
Require(sceFontOpenFontMemory(library, data.data(), static_cast<std::uint32_t>(data.size()), nullptr, &fresh) == SCE_FONT_OK);
float width = 0.0f;
float height = 0.0f;
Require(sceFontGetScalePixel(fresh, &width, &height) == SCE_FONT_OK);
CheckGlyphScale(fresh, memory, width, height);
Require(sceFontCloseFont(fresh) == SCE_FONT_OK);
Require(sceFontSetResolutionDpi(font, 144, 144) == SCE_FONT_OK);
Require(sceFontSetScalePixel(font, 100.0f, 100.0f) == SCE_FONT_OK);
}

int main() {
constexpr std::uint32_t SystemFontSet = 0x18070043u;
const std::filesystem::path fontRoot = std::filesystem::temp_directory_path() / ("anyps5_guest_font-" + std::to_string(std::random_device{}()));
Expand Down Expand Up @@ -421,6 +488,8 @@ int main() {
Require(sceFontUnbindRenderer(font) == SCE_FONT_OK);
Require(sceFontGetRenderScaledKerning(font, 'A', 'V', &kerning) == SCE_FONT_ERROR_NOT_BOUND_RENDERER);

CheckPointAndDpiScale(font, library, &memory, renderer);

ParsedText parsed{"AV", 0, font};
FontTextSource source{};
Require(sceFontTextSourceInit(&source, parsed.text, 2, ParseText, &parsed) == SCE_FONT_OK);
Expand Down
Loading