Use libschrift instead of freetype

This commit is contained in:
Maschell 2023-01-05 17:43:20 +01:00
parent 96c02e85b3
commit a7b301ce2e
7 changed files with 1694 additions and 63 deletions

View File

@ -36,7 +36,7 @@ CXXFLAGS := $(CFLAGS) -std=c++20 -fno-rtti
ASFLAGS := -g $(ARCH) ASFLAGS := -g $(ARCH)
LDFLAGS = -g $(ARCH) $(RPXSPECS) --entry=_start -Wl,-Map,$(notdir $*.map) LDFLAGS = -g $(ARCH) $(RPXSPECS) --entry=_start -Wl,-Map,$(notdir $*.map)
LIBS := -lfreetype -lpng -lbz2 -lmocha -lwut -lz LIBS := -lpng -lmocha -lwut -lz
ifeq ($(DEBUG),1) ifeq ($(DEBUG),1)
CXXFLAGS += -DDEBUG -g CXXFLAGS += -DDEBUG -g
@ -95,7 +95,7 @@ export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD) -I$(DEVKITPRO)/portlibs/ppc/include/freetype2 -I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
@ -105,7 +105,7 @@ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
all: $(BUILD) all: $(BUILD)
$(BUILD): $(BUILD):
@[ -d $@ ] || mkdir -p $@ @$(shell [ ! -d $(BUILD) ] && mkdir -p $(BUILD))
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------

View File

@ -1,14 +1,14 @@
#include "DrawUtils.h" #include "DrawUtils.h"
#include <malloc.h>
#include <cmath> #include "MenuUtils.h"
#include "logger.h"
#include "utils.h"
#include <coreinit/cache.h> #include <coreinit/cache.h>
#include <coreinit/memory.h> #include <coreinit/memory.h>
#include <coreinit/screen.h> #include <coreinit/screen.h>
#include <ft2build.h> #include <cstdlib>
#include <malloc.h>
#include <png.h> #include <png.h>
#include FT_FREETYPE_H
#include "MenuUtils.h"
// buffer width // buffer width
#define TV_WIDTH 0x500 #define TV_WIDTH 0x500
@ -20,11 +20,9 @@ uint8_t *DrawUtils::tvBuffer = nullptr;
uint32_t DrawUtils::tvSize = 0; uint32_t DrawUtils::tvSize = 0;
uint8_t *DrawUtils::drcBuffer = nullptr; uint8_t *DrawUtils::drcBuffer = nullptr;
uint32_t DrawUtils::drcSize = 0; uint32_t DrawUtils::drcSize = 0;
static SFT pFont = {};
// Don't put those into the class or we have to include ft everywhere static Color font_col(0xFFFFFFFF);
static FT_Library ft_lib = nullptr;
static FT_Face ft_face = nullptr;
static Color font_col = {0xFFFFFFFF};
void *DrawUtils::InitOSScreen() { void *DrawUtils::InitOSScreen() {
OSScreenInit(); OSScreenInit();
@ -101,10 +99,17 @@ void DrawUtils::drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t
} }
} }
uint32_t USED_TV_WIDTH = TV_WIDTH;
float scale = 1.5f;
if (DrawUtils::tvSize == 0x00FD2000) {
USED_TV_WIDTH = 1920;
scale = 2.25f;
}
// scale and put pixel in the tv buffer // scale and put pixel in the tv buffer
for (uint32_t yy = (y * 1.5); yy < ((y * 1.5) + 1); yy++) { for (uint32_t yy = (y * scale); yy < ((y * scale) + (uint32_t) scale); yy++) {
for (uint32_t xx = (x * 1.5); xx < ((x * 1.5) + 1); xx++) { for (uint32_t xx = (x * scale); xx < ((x * scale) + (uint32_t) scale); xx++) {
uint32_t i = (xx + yy * TV_WIDTH) * 4; uint32_t i = (xx + yy * USED_TV_WIDTH) * 4;
if (i + 3 < tvSize / 2) { if (i + 3 < tvSize / 2) {
if (isBackBuffer) { if (isBackBuffer) {
i += tvSize / 2; i += tvSize / 2;
@ -172,14 +177,14 @@ static void png_read_data(png_structp png_ptr, png_bytep outBytes, png_size_t by
} }
void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t *data) { void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t *data) {
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (png_ptr == NULL) { if (png_ptr == nullptr) {
return; return;
} }
png_infop info_ptr = png_create_info_struct(png_ptr); png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) { if (info_ptr == nullptr) {
png_destroy_read_struct(&png_ptr, NULL, NULL); png_destroy_read_struct(&png_ptr, nullptr, nullptr);
return; return;
} }
@ -191,16 +196,16 @@ void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t *data) {
uint32_t height = 0; uint32_t height = 0;
int bitDepth = 0; int bitDepth = 0;
int colorType = -1; int colorType = -1;
uint32_t retval = png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType, NULL, NULL, NULL); uint32_t retval = png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType, nullptr, nullptr, nullptr);
if (retval != 1) { if (retval != 1) {
return; return;
} }
uint32_t bytesPerRow = png_get_rowbytes(png_ptr, info_ptr); uint32_t bytesPerRow = png_get_rowbytes(png_ptr, info_ptr);
uint8_t *rowData = new uint8_t[bytesPerRow]; auto *rowData = new uint8_t[bytesPerRow];
for (uint32_t yy = y; yy < y + height; yy++) { for (uint32_t yy = y; yy < y + height; yy++) {
png_read_row(png_ptr, (png_bytep) rowData, NULL); png_read_row(png_ptr, (png_bytep) rowData, nullptr);
for (uint32_t xx = x; xx < x + width; xx++) { for (uint32_t xx = x; xx < x + width; xx++) {
if (colorType == PNG_COLOR_TYPE_RGB_ALPHA) { if (colorType == PNG_COLOR_TYPE_RGB_ALPHA) {
@ -214,37 +219,52 @@ void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t *data) {
} }
delete[] rowData; delete[] rowData;
png_destroy_read_struct(&png_ptr, &info_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
} }
void DrawUtils::initFont() { bool DrawUtils::initFont() {
void *font = NULL; void *font = nullptr;
uint32_t size = 0; uint32_t size = 0;
OSGetSharedData(OS_SHAREDDATATYPE_FONT_STANDARD, 0, &font, &size); OSGetSharedData(OS_SHAREDDATATYPE_FONT_STANDARD, 0, &font, &size);
if (font && size) { if (font && size) {
FT_Init_FreeType(&ft_lib); pFont.xScale = 20;
FT_New_Memory_Face(ft_lib, (FT_Byte *) font, size, 0, &ft_face); pFont.yScale = 20,
pFont.flags = SFT_DOWNWARD_Y;
pFont.font = sft_loadmem(font, size);
if (!pFont.font) {
return false;
} }
OSMemoryBarrier();
return true;
}
return false;
} }
void DrawUtils::deinitFont() { void DrawUtils::deinitFont() {
FT_Done_Face(ft_face); sft_freefont(pFont.font);
FT_Done_FreeType(ft_lib); pFont.font = nullptr;
pFont = {};
} }
void DrawUtils::setFontSize(uint32_t size) { void DrawUtils::setFontSize(uint32_t size) {
FT_Set_Pixel_Sizes(ft_face, 0, size); pFont.xScale = size;
pFont.yScale = size;
SFT_LMetrics metrics;
sft_lmetrics(&pFont, &metrics);
} }
void DrawUtils::setFontColor(Color col) { void DrawUtils::setFontColor(Color col) {
font_col = col; font_col = col;
} }
static void draw_freetype_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y) { static void draw_freetype_bitmap(SFT_Image *bmp, int32_t x, int32_t y) {
FT_Int i, j, p, q; int32_t i, j, p, q;
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows; int32_t x_max = x + bmp->width;
int32_t y_max = y + bmp->height;
auto *src = (uint8_t *) bmp->pixels;
for (i = x, p = 0; i < x_max; i++, p++) { for (i = x, p = 0; i < x_max; i++, p++) {
for (j = y, q = 0; j < y_max; j++, q++) { for (j = y, q = 0; j < y_max; j++, q++) {
@ -252,14 +272,14 @@ static void draw_freetype_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y) {
continue; continue;
} }
float opacity = bitmap->buffer[q * bitmap->pitch + p] / 255.0f; float opacity = src[q * bmp->width + p] / 255.0f;
DrawUtils::drawPixel(i, j, font_col.r, font_col.g, font_col.b, font_col.a * opacity); DrawUtils::drawPixel(i, j, font_col.r, font_col.g, font_col.b, font_col.a * opacity);
} }
} }
} }
void DrawUtils::print(uint32_t x, uint32_t y, const char *string, bool alignRight) { void DrawUtils::print(uint32_t x, uint32_t y, const char *string, bool alignRight) {
wchar_t *buffer = new wchar_t[strlen(string) + 1]; auto *buffer = new wchar_t[strlen(string) + 1];
size_t num = mbstowcs(buffer, string, strlen(string)); size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) { if (num > 0) {
@ -275,32 +295,64 @@ void DrawUtils::print(uint32_t x, uint32_t y, const char *string, bool alignRigh
} }
void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignRight) { void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignRight) {
FT_GlyphSlot slot = ft_face->glyph; auto penX = (int32_t) x;
FT_Vector pen = {(int) x, (int) y}; auto penY = (int32_t) y;
if (alignRight) { if (alignRight) {
pen.x -= getTextWidth(string); penX -= getTextWidth(string);
} }
uint16_t textureWidth = 0, textureHeight = 0;
for (; *string; string++) { for (; *string; string++) {
uint32_t charcode = *string; SFT_Glyph gid; // unsigned long gid;
if (sft_lookup(&pFont, *string, &gid) >= 0) {
SFT_GMetrics mtx;
if (sft_gmetrics(&pFont, gid, &mtx) < 0) {
DEBUG_FUNCTION_LINE_ERR("Failed to get glyph metrics");
return;
}
if (charcode == '\n') { if (*string == '\n') {
pen.y += ft_face->size->metrics.height >> 6; penY += mtx.minHeight;
pen.x = x; penX = x;
continue; continue;
} }
FT_Load_Glyph(ft_face, FT_Get_Char_Index(ft_face, charcode), FT_LOAD_DEFAULT); textureWidth = (mtx.minWidth + 3) & ~3;
FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL); textureHeight = mtx.minHeight;
draw_freetype_bitmap(&slot->bitmap, pen.x + slot->bitmap_left, pen.y - slot->bitmap_top); SFT_Image img = {
pen.x += slot->advance.x >> 6; .pixels = nullptr,
.width = textureWidth,
.height = textureHeight,
};
if (textureWidth == 0) {
textureWidth = 4;
}
if (textureHeight == 0) {
textureHeight = 4;
}
auto buffer = make_unique_nothrow<uint8_t[]>((uint32_t) (img.width * img.height));
if (!buffer) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate memory for glyph");
return;
}
img.pixels = buffer.get();
if (sft_render(&pFont, gid, img) < 0) {
DEBUG_FUNCTION_LINE_ERR("Failed to render glyph");
return;
} else {
draw_freetype_bitmap(&img, (int32_t) (penX + mtx.leftSideBearing), (int32_t) (penY + mtx.yOffset));
penX += (int32_t) mtx.advanceWidth;
}
}
} }
} }
uint32_t DrawUtils::getTextWidth(const char *string) { uint32_t DrawUtils::getTextWidth(const char *string) {
wchar_t *buffer = new wchar_t[strlen(string) + 1]; auto *buffer = new wchar_t[strlen(string) + 1];
size_t num = mbstowcs(buffer, string, strlen(string)); size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) { if (num > 0) {
@ -318,14 +370,18 @@ uint32_t DrawUtils::getTextWidth(const char *string) {
} }
uint32_t DrawUtils::getTextWidth(const wchar_t *string) { uint32_t DrawUtils::getTextWidth(const wchar_t *string) {
FT_GlyphSlot slot = ft_face->glyph;
uint32_t width = 0; uint32_t width = 0;
for (; *string; string++) { for (; *string; string++) {
FT_Load_Glyph(ft_face, FT_Get_Char_Index(ft_face, *string), FT_LOAD_BITMAP_METRICS_ONLY); SFT_Glyph gid; // unsigned long gid;
if (sft_lookup(&pFont, *string, &gid) >= 0) {
width += slot->advance.x >> 6; SFT_GMetrics mtx;
if (sft_gmetrics(&pFont, gid, &mtx) < 0) {
DEBUG_FUNCTION_LINE_ERR("bad glyph metrics");
}
width += (int32_t) mtx.advanceWidth;
}
} }
return width; return (uint32_t) width;
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "schrift.h"
#include <cstdint> #include <cstdint>
// visible screen sizes // visible screen sizes
@ -7,7 +8,7 @@
#define SCREEN_HEIGHT 480 #define SCREEN_HEIGHT 480
union Color { union Color {
Color(uint32_t color) { explicit Color(uint32_t color) {
this->color = color; this->color = color;
} }
@ -18,7 +19,7 @@ union Color {
this->a = a; this->a = a;
} }
uint32_t color; uint32_t color{};
struct { struct {
uint8_t r; uint8_t r;
uint8_t g; uint8_t g;
@ -51,7 +52,7 @@ public:
static void drawPNG(uint32_t x, uint32_t y, const uint8_t *data); static void drawPNG(uint32_t x, uint32_t y, const uint8_t *data);
static void initFont(); static bool initFont();
static void deinitFont(); static void deinitFont();

View File

@ -70,7 +70,9 @@ int32_t handleMenuScreen(std::string &configPath, int32_t autobootOptionInput, c
uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC); uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC);
DrawUtils::initBuffers(screenBuffer, tvBufferSize, (void *) ((uint32_t) screenBuffer + tvBufferSize), drcBufferSize); DrawUtils::initBuffers(screenBuffer, tvBufferSize, (void *) ((uint32_t) screenBuffer + tvBufferSize), drcBufferSize);
DrawUtils::initFont(); if (!DrawUtils::initFont()) {
OSFatal("Failed to init font");
}
int32_t selectedIndex = autobootOptionInput > 0 ? autobootOptionInput : 0; int32_t selectedIndex = autobootOptionInput > 0 ? autobootOptionInput : 0;
int autobootIndex = autobootOptionInput; int autobootIndex = autobootOptionInput;
@ -209,7 +211,9 @@ nn::act::SlotNo handleAccountSelectScreen(const std::vector<std::shared_ptr<Acco
uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC); uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC);
DrawUtils::initBuffers(screenBuffer, tvBufferSize, (void *) ((uint32_t) screenBuffer + tvBufferSize), drcBufferSize); DrawUtils::initBuffers(screenBuffer, tvBufferSize, (void *) ((uint32_t) screenBuffer + tvBufferSize), drcBufferSize);
DrawUtils::initFont(); if (!DrawUtils::initFont()) {
OSFatal("Failed to init font");
}
int32_t selected = 0; int32_t selected = 0;
bool redraw = true; bool redraw = true;
@ -347,7 +351,9 @@ void handleUpdateWarningScreen() {
uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC); uint32_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC);
DrawUtils::initBuffers(screenBuffer, tvBufferSize, (void *) ((uint32_t) screenBuffer + tvBufferSize), drcBufferSize); DrawUtils::initBuffers(screenBuffer, tvBufferSize, (void *) ((uint32_t) screenBuffer + tvBufferSize), drcBufferSize);
DrawUtils::initFont(); if (!DrawUtils::initFont()) {
OSFatal("Failed to init font");
}
DrawUtils::beginDraw(); DrawUtils::beginDraw();
DrawUtils::clear(COLOR_BACKGROUND_WARN); DrawUtils::clear(COLOR_BACKGROUND_WARN);

1460
source/schrift.c Normal file

File diff suppressed because it is too large Load Diff

88
source/schrift.h Normal file
View File

@ -0,0 +1,88 @@
/* This file is part of libschrift.
*
* © 2019-2022 Thomas Oltmann and contributors
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#ifndef SCHRIFT_H
#define SCHRIFT_H 1
#include <stddef.h> /* size_t */
#include <stdint.h> /* uint_fast32_t, uint_least32_t */
#ifdef __cplusplus
extern "C" {
#endif
#define SFT_DOWNWARD_Y 0x01
typedef struct SFT SFT;
typedef struct SFT_Font SFT_Font;
typedef uint_least32_t SFT_UChar; /* Guaranteed to be compatible with char32_t. */
typedef uint_fast32_t SFT_Glyph;
typedef struct SFT_LMetrics SFT_LMetrics;
typedef struct SFT_GMetrics SFT_GMetrics;
typedef struct SFT_Kerning SFT_Kerning;
typedef struct SFT_Image SFT_Image;
struct SFT {
SFT_Font *font;
double xScale;
double yScale;
double xOffset;
double yOffset;
int flags;
};
struct SFT_LMetrics {
double ascender;
double descender;
double lineGap;
};
struct SFT_GMetrics {
double advanceWidth;
double leftSideBearing;
int yOffset;
int minWidth;
int minHeight;
};
struct SFT_Kerning {
double xShift;
double yShift;
};
struct SFT_Image {
void *pixels;
int width;
int height;
};
const char *sft_version(void);
SFT_Font *sft_loadmem(const void *mem, size_t size);
void sft_freefont(SFT_Font *font);
int sft_lmetrics(const SFT *sft, SFT_LMetrics *metrics);
int sft_lookup(const SFT *sft, SFT_UChar codepoint, SFT_Glyph *glyph);
int sft_gmetrics(const SFT *sft, SFT_Glyph glyph, SFT_GMetrics *metrics);
int sft_kerning(const SFT *sft, SFT_Glyph leftGlyph, SFT_Glyph rightGlyph,
SFT_Kerning *kerning);
int sft_render(const SFT *sft, SFT_Glyph glyph, SFT_Image image);
#ifdef __cplusplus
}
#endif
#endif

20
source/utils.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <malloc.h>
#include <memory>
template<class T, class... Args>
std::unique_ptr<T> make_unique_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
return std::unique_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
}
template<typename T>
inline typename std::unique_ptr<T> make_unique_nothrow(size_t num) noexcept {
return std::unique_ptr<T>(new (std::nothrow) std::remove_extent_t<T>[num]());
}
template<class T, class... Args>
std::shared_ptr<T> make_shared_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
return std::shared_ptr<T>(new (std::nothrow) T(std::forward<Args>(args)...));
}