Use libschrift instead of freetype

This commit is contained in:
Maschell 2023-01-05 16:45:52 +01:00
parent 3c5cdbba5e
commit 41af813a2d
6 changed files with 1687 additions and 63 deletions

View File

@ -51,7 +51,7 @@ endif
ASFLAGS := -g $(ARCH) ASFLAGS := -g $(ARCH)
LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map)
LIBS := -lfreetype -lpng -lbz2 -lwut -lz LIBS := -lwut -lz
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level # list of directories containing libraries, this must be the top level
@ -100,7 +100,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)

View File

@ -287,7 +287,10 @@ std::string EnvironmentSelectionScreen(const std::map<std::string, std::string>
OSScreenEnableEx(SCREEN_DRC, TRUE); OSScreenEnableEx(SCREEN_DRC, TRUE);
DrawUtils::initBuffers(screenBuffer, tvBufferSize, screenBuffer + tvBufferSize, drcBufferSize); DrawUtils::initBuffers(screenBuffer, tvBufferSize, screenBuffer + tvBufferSize, drcBufferSize);
DrawUtils::initFont();
if (!DrawUtils::initFont()) {
OSFatal("Failed to init font");
}
uint32_t selected = autobootIndex > 0 ? autobootIndex : 0; uint32_t selected = autobootIndex > 0 ? autobootIndex : 0;
int autoBoot = autobootIndex; int autoBoot = autobootIndex;

View File

@ -1,12 +1,13 @@
#include "DrawUtils.h" #include "DrawUtils.h"
#include <cmath> #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 <png.h> #include <png.h>
#include FT_FREETYPE_H
// buffer width // buffer width
#define TV_WIDTH 0x500 #define TV_WIDTH 0x500
@ -18,17 +19,15 @@ 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::initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize) { void DrawUtils::initBuffers(void *tvBuffer_, uint32_t tvSize_, void *drcBuffer_, uint32_t drcSize_) {
DrawUtils::tvBuffer = (uint8_t *) tvBuffer; DrawUtils::tvBuffer = (uint8_t *) tvBuffer_;
DrawUtils::tvSize = tvSize; DrawUtils::tvSize = tvSize_;
DrawUtils::drcBuffer = (uint8_t *) drcBuffer; DrawUtils::drcBuffer = (uint8_t *) drcBuffer_;
DrawUtils::drcSize = drcSize; DrawUtils::drcSize = drcSize_;
} }
void DrawUtils::beginDraw() { void DrawUtils::beginDraw() {
@ -80,10 +79,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;
@ -151,14 +157,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;
} }
@ -170,16 +176,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) {
@ -193,37 +199,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++) {
@ -231,14 +252,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) {
@ -254,32 +275,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;
}
textureWidth = (mtx.minWidth + 3) & ~3;
textureHeight = mtx.minHeight;
SFT_Image img = {
.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;
}
} }
FT_Load_Glyph(ft_face, FT_Get_Char_Index(ft_face, charcode), FT_LOAD_DEFAULT);
FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
draw_freetype_bitmap(&slot->bitmap, pen.x + slot->bitmap_left, pen.y - slot->bitmap_top);
pen.x += slot->advance.x >> 6;
} }
} }
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) {
@ -297,14 +350,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,9 +8,10 @@
#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;
} }
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
this->r = r; this->r = r;
this->g = g; this->g = g;
@ -17,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;
@ -29,25 +31,39 @@ union Color {
class DrawUtils { class DrawUtils {
public: public:
static void initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize); static void initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize);
static void beginDraw(); static void beginDraw();
static void endDraw(); static void endDraw();
static void clear(Color col); static void clear(Color col);
static void drawPixel(uint32_t x, uint32_t y, Color col) { drawPixel(x, y, col.r, col.g, col.b, col.a); } static void drawPixel(uint32_t x, uint32_t y, Color col) { drawPixel(x, y, col.r, col.g, col.b, col.a); }
static void drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a); static void drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
static void drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, Color col); static void drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, Color col);
static void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t borderSize, Color col); static void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t borderSize, Color col);
static void drawBitmap(uint32_t x, uint32_t y, uint32_t target_width, uint32_t target_height, const uint8_t *data); static void drawBitmap(uint32_t x, uint32_t y, uint32_t target_width, uint32_t target_height, const uint8_t *data);
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();
static void setFontSize(uint32_t size); static void setFontSize(uint32_t size);
static void setFontColor(Color col); static void setFontColor(Color col);
static void print(uint32_t x, uint32_t y, const char *string, bool alignRight = false); static void print(uint32_t x, uint32_t y, const char *string, bool alignRight = false);
static void print(uint32_t x, uint32_t y, const wchar_t *string, bool alignRight = false); static void print(uint32_t x, uint32_t y, const wchar_t *string, bool alignRight = false);
static uint32_t getTextWidth(const char *string); static uint32_t getTextWidth(const char *string);
static uint32_t getTextWidth(const wchar_t *string); static uint32_t getTextWidth(const wchar_t *string);
private: private:

1460
source/utils/schrift.c Normal file

File diff suppressed because it is too large Load Diff

88
source/utils/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