mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-11-22 20:06:40 +00:00
fatal: use STB instead of freetype
This commit is contained in:
parent
5b1060b30e
commit
f3fa680d5d
5 changed files with 4975 additions and 100 deletions
|
@ -3,13 +3,6 @@
|
|||
#---------------------------------------------------------------------------------
|
||||
include $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/../../libraries/config/templates/stratosphere.mk
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# atmosphere-fatal needs freetype.
|
||||
#---------------------------------------------------------------------------------
|
||||
LIBS += `freetype-config --libs`
|
||||
CFLAGS += `freetype-config --cflags`
|
||||
CXXFLAGS += `freetype-config --cflags`
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# no real need to edit anything past this point unless you need to add additional
|
||||
# rules for different file extensions
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"title_id": "0x0100000000000034",
|
||||
"title_id_range_min": "0x0100000000000034",
|
||||
"title_id_range_max": "0x0100000000000034",
|
||||
"main_thread_stack_size": "0x00020000",
|
||||
"main_thread_stack_size": "0x00008000",
|
||||
"main_thread_priority": 15,
|
||||
"default_cpu_id": 3,
|
||||
"process_category": 0,
|
||||
|
|
|
@ -13,12 +13,15 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
#include "fatal_config.hpp"
|
||||
#include "fatal_font.hpp"
|
||||
|
||||
#define STBTT_STATIC
|
||||
#define STB_TRUETYPE_IMPLEMENTATION
|
||||
#include "stb_truetype.h"
|
||||
#undef STBTT_STATIC
|
||||
#undef STB_TRUETYPE_IMPLEMENTATION
|
||||
|
||||
/* Define color conversion macros. */
|
||||
#define RGB888_TO_RGB565(r, g, b) ((((r >> 3) << 11) & 0xF800) | (((g >> 2) << 5) & 0x7E0) | ((b >> 3) & 0x1F))
|
||||
#define RGB565_GET_R8(c) ((((c >> 11) & 0x1F) << 3) | ((c >> 13) & 7))
|
||||
|
@ -33,17 +36,15 @@ namespace ams::fatal::srv::font {
|
|||
u16 *g_frame_buffer = nullptr;
|
||||
u32 (*g_unswizzle_func)(u32, u32) = nullptr;
|
||||
u16 g_font_color = 0xFFFF; /* White. */
|
||||
float g_font_line_pixels = 16.0f;
|
||||
float g_font_size = 16.0f;
|
||||
u32 g_line_x = 0, g_cur_x = 0, g_cur_y = 0;
|
||||
|
||||
u32 g_mono_adv = 0;
|
||||
|
||||
PlFontData g_font;
|
||||
PlFontData g_fonts[PlSharedFontType_Total];
|
||||
|
||||
FT_Library g_library;
|
||||
FT_Face g_face;
|
||||
FT_Error g_ft_err = 0;
|
||||
stbtt_fontinfo g_stb_font;
|
||||
|
||||
/* Helpers. */
|
||||
u16 Blend(u16 color, u16 bg, u8 alpha) {
|
||||
|
@ -61,69 +62,67 @@ namespace ams::fatal::srv::font {
|
|||
return RGB888_TO_RGB565(r, g, b);
|
||||
}
|
||||
|
||||
void DrawGlyph(FT_Bitmap *bitmap, u32 x, u32 y) {
|
||||
u8* imageptr = bitmap->buffer;
|
||||
void DrawCodePoint(u32 codepoint, u32 x, u32 y) {
|
||||
int width = 0, height = 0;
|
||||
u8* imageptr = stbtt_GetCodepointBitmap(&g_stb_font, g_font_size, g_font_size, codepoint, &width, &height, 0, 0);
|
||||
ON_SCOPE_EXIT { std::free(imageptr); };
|
||||
|
||||
if (bitmap->pixel_mode!=FT_PIXEL_MODE_GRAY) return;
|
||||
|
||||
for (u32 tmpy = 0; tmpy < bitmap->rows; tmpy++) {
|
||||
for (u32 tmpx = 0; tmpx < bitmap->width; tmpx++) {
|
||||
for (int tmpy = 0; tmpy < height; tmpy++) {
|
||||
for (int tmpx = 0; tmpx < width; tmpx++) {
|
||||
/* Implement very simple blending, as the bitmap value is an alpha value. */
|
||||
u16 *ptr = &g_frame_buffer[g_unswizzle_func(x + tmpx, y + tmpy)];
|
||||
*ptr = Blend(g_font_color, *ptr, imageptr[tmpx]);
|
||||
*ptr = Blend(g_font_color, *ptr, imageptr[width * tmpy + tmpx]);
|
||||
}
|
||||
imageptr += bitmap->pitch;
|
||||
}
|
||||
}
|
||||
|
||||
void DrawString(const char *str, bool add_line, bool mono = false) {
|
||||
FT_UInt glyph_index;
|
||||
FT_GlyphSlot slot = g_face->glyph;
|
||||
|
||||
const size_t len = strlen(str);
|
||||
|
||||
u32 cur_x = g_cur_x, cur_y = g_cur_y;
|
||||
ON_SCOPE_EXIT {
|
||||
if (add_line) {
|
||||
/* Advance to next line. */
|
||||
g_cur_x = g_line_x;
|
||||
g_cur_y = cur_y + (g_face->size->metrics.height >> 6);
|
||||
} else {
|
||||
g_cur_x = cur_x;
|
||||
g_cur_y = cur_y;
|
||||
}
|
||||
};
|
||||
|
||||
u32 prev_char = 0;
|
||||
for (u32 i = 0; i < len; ) {
|
||||
u32 cur_char;
|
||||
ssize_t unit_count = decode_utf8(&cur_char, reinterpret_cast<const u8 *>(&str[i]));
|
||||
if (unit_count <= 0) break;
|
||||
|
||||
if (!g_mono_adv && i > 0) {
|
||||
cur_x += g_font_size * stbtt_GetCodepointKernAdvance(&g_stb_font, prev_char, cur_char);
|
||||
}
|
||||
|
||||
i += unit_count;
|
||||
|
||||
if (cur_char == '\n') {
|
||||
cur_x = g_line_x;
|
||||
cur_y += g_face->size->metrics.height >> 6;
|
||||
cur_y += g_font_line_pixels;
|
||||
continue;
|
||||
}
|
||||
|
||||
glyph_index = FT_Get_Char_Index(g_face, cur_char);
|
||||
int adv_width, left_side_bearing;
|
||||
stbtt_GetCodepointHMetrics(&g_stb_font, cur_char, &adv_width, &left_side_bearing);
|
||||
const u32 cur_width = static_cast<u32>(adv_width) * g_font_size;
|
||||
|
||||
g_ft_err = FT_Load_Glyph(g_face, glyph_index, FT_LOAD_DEFAULT);
|
||||
int x0, y0, x1, y1;
|
||||
stbtt_GetCodepointBitmapBoxSubpixel(&g_stb_font, cur_char, g_font_size, g_font_size, 0, 0, &x0, &y0, &x1, &y1);
|
||||
|
||||
if (g_ft_err == 0) {
|
||||
g_ft_err = FT_Render_Glyph(g_face->glyph, FT_RENDER_MODE_NORMAL);
|
||||
}
|
||||
DrawCodePoint(cur_char, cur_x + x0 + ((mono && g_mono_adv > cur_width) ? ((g_mono_adv - cur_width) / 2) : 0), cur_y + y0);
|
||||
|
||||
if (g_ft_err) {
|
||||
return;
|
||||
}
|
||||
cur_x += (mono ? g_mono_adv : cur_width);
|
||||
|
||||
DrawGlyph(&slot->bitmap, cur_x + slot->bitmap_left + ((mono && g_mono_adv > slot->advance.x) ? ((g_mono_adv - slot->advance.x) >> 7) : 0), cur_y - slot->bitmap_top);
|
||||
prev_char = cur_char;
|
||||
}
|
||||
|
||||
cur_x += (mono ? g_mono_adv : slot->advance.x) >> 6;
|
||||
cur_y += slot->advance.y >> 6;
|
||||
if (add_line) {
|
||||
/* Advance to next line. */
|
||||
g_cur_x = g_line_x;
|
||||
g_cur_y = cur_y + g_font_line_pixels;
|
||||
} else {
|
||||
g_cur_x = cur_x;
|
||||
g_cur_y = cur_y;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void PrintLine(const char *str) {
|
||||
|
@ -133,9 +132,9 @@ namespace ams::fatal::srv::font {
|
|||
void PrintFormatLine(const char *format, ...) {
|
||||
char char_buf[0x400];
|
||||
|
||||
va_list va_arg;
|
||||
std::va_list va_arg;
|
||||
va_start(va_arg, format);
|
||||
vsnprintf(char_buf, sizeof(char_buf), format, va_arg);
|
||||
std::vsnprintf(char_buf, sizeof(char_buf), format, va_arg);
|
||||
va_end(va_arg);
|
||||
|
||||
PrintLine(char_buf);
|
||||
|
@ -148,9 +147,9 @@ namespace ams::fatal::srv::font {
|
|||
void PrintFormat(const char *format, ...) {
|
||||
char char_buf[0x400];
|
||||
|
||||
va_list va_arg;
|
||||
std::va_list va_arg;
|
||||
va_start(va_arg, format);
|
||||
vsnprintf(char_buf, sizeof(char_buf), format, va_arg);
|
||||
std::vsnprintf(char_buf, sizeof(char_buf), format, va_arg);
|
||||
va_end(va_arg);
|
||||
|
||||
Print(char_buf);
|
||||
|
@ -158,23 +157,21 @@ namespace ams::fatal::srv::font {
|
|||
|
||||
void PrintMonospaceU64(u64 x) {
|
||||
char char_buf[0x400];
|
||||
snprintf(char_buf, sizeof(char_buf), "%016lX", x);
|
||||
std::snprintf(char_buf, sizeof(char_buf), "%016lX", x);
|
||||
|
||||
DrawString(char_buf, false, true);
|
||||
}
|
||||
|
||||
void PrintMonospaceU32(u32 x) {
|
||||
char char_buf[0x400];
|
||||
snprintf(char_buf, sizeof(char_buf), "%08X", x);
|
||||
std::snprintf(char_buf, sizeof(char_buf), "%08X", x);
|
||||
|
||||
DrawString(char_buf, false, true);
|
||||
}
|
||||
|
||||
void PrintMonospaceBlank(u32 width) {
|
||||
char char_buf[0x400] = {0};
|
||||
for (size_t i = 0; i < width && i < sizeof(char_buf); i++) {
|
||||
char_buf[i] = ' ';
|
||||
}
|
||||
std::memset(char_buf, ' ', std::min(size_t(width), sizeof(char_buf)));
|
||||
|
||||
DrawString(char_buf, false, true);
|
||||
}
|
||||
|
@ -198,22 +195,21 @@ namespace ams::fatal::srv::font {
|
|||
}
|
||||
|
||||
void SetFontSize(float fsz) {
|
||||
g_font_size = fsz;
|
||||
g_ft_err = FT_Set_Char_Size(g_face, 0, static_cast<u32>(g_font_size * 64.0f), 96, 96);
|
||||
g_font_size = stbtt_ScaleForPixelHeight(&g_stb_font, fsz * 1.375);
|
||||
|
||||
g_ft_err = FT_Load_Glyph(g_face, FT_Get_Char_Index(g_face, 'A'), FT_LOAD_DEFAULT);
|
||||
int ascent;
|
||||
stbtt_GetFontVMetrics(&g_stb_font, &ascent,0,0);
|
||||
g_font_line_pixels = ascent * g_font_size * 1.125;
|
||||
|
||||
if (g_ft_err == 0) {
|
||||
g_ft_err = FT_Render_Glyph(g_face->glyph, FT_RENDER_MODE_NORMAL);
|
||||
}
|
||||
if (g_ft_err == 0) {
|
||||
g_mono_adv = g_face->glyph->advance.x;
|
||||
}
|
||||
int adv_width, left_side_bearing;
|
||||
stbtt_GetCodepointHMetrics(&g_stb_font, 'A', &adv_width, &left_side_bearing);
|
||||
|
||||
g_mono_adv = adv_width * g_font_size;
|
||||
}
|
||||
|
||||
void AddSpacingLines(float num_lines) {
|
||||
g_cur_x = g_line_x;
|
||||
g_cur_y += static_cast<u32>((static_cast<float>(g_face->size->metrics.height) * num_lines) / 64.0f);
|
||||
g_cur_y += static_cast<u32>(g_font_line_pixels * num_lines);
|
||||
}
|
||||
|
||||
void ConfigureFontFramebuffer(u16 *fb, u32 (*unswizzle_func)(u32, u32)) {
|
||||
|
@ -222,19 +218,13 @@ namespace ams::fatal::srv::font {
|
|||
}
|
||||
|
||||
Result InitializeSharedFont() {
|
||||
s32 total_fonts = 0;
|
||||
|
||||
R_TRY(plGetSharedFont(GetFatalConfig().GetLanguageCode(), g_fonts, PlSharedFontType_Total, &total_fonts));
|
||||
R_TRY(plGetSharedFontByType(&g_font, PlSharedFontType_Standard));
|
||||
|
||||
g_ft_err = FT_Init_FreeType(&g_library);
|
||||
if (g_ft_err) return g_ft_err;
|
||||
u8 *font_buffer = reinterpret_cast<u8 *>(g_font.address);
|
||||
stbtt_InitFont(&g_stb_font, font_buffer, stbtt_GetFontOffsetForIndex(font_buffer, 0));
|
||||
|
||||
g_ft_err = FT_New_Memory_Face(g_library, reinterpret_cast<const FT_Byte *>(g_font.address), g_font.size, 0, &g_face);
|
||||
if (g_ft_err) return g_ft_err;
|
||||
|
||||
SetFontSize(g_font_size);
|
||||
return g_ft_err;
|
||||
SetFontSize(16.0f);
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -198,15 +198,16 @@ namespace ams::fatal::srv {
|
|||
tiled_buf[i] = 0x39C9;
|
||||
}
|
||||
|
||||
/* Draw the atmosphere logo in the bottom right corner. */
|
||||
/* Draw the atmosphere logo in the upper right corner. */
|
||||
const u32 start_x = 32, start_y = 64;
|
||||
for (size_t y = 0; y < AtmosphereLogoHeight; y++) {
|
||||
for (size_t x = 0; x < AtmosphereLogoWidth; x++) {
|
||||
tiled_buf[GetPixelOffset(FatalScreenWidth - AtmosphereLogoWidth - 32 + x, 32 + y)] = AtmosphereLogoData[y * AtmosphereLogoWidth + x];
|
||||
tiled_buf[GetPixelOffset(FatalScreenWidth - AtmosphereLogoWidth - start_x + x, start_x + y)] = AtmosphereLogoData[y * AtmosphereLogoWidth + x];
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: Actually draw meaningful shit here. */
|
||||
font::SetPosition(32, 64);
|
||||
/* Draw error message and firmware. */
|
||||
font::SetPosition(start_x, start_y);
|
||||
font::SetFontSize(16.0f);
|
||||
font::PrintFormat(config.GetErrorMessage(), this->context->result.GetModule(), this->context->result.GetDescription(), this->context->result.GetValue());
|
||||
font::AddSpacingLines(0.5f);
|
||||
|
@ -227,7 +228,7 @@ namespace ams::fatal::srv {
|
|||
}
|
||||
|
||||
/* Add a line. */
|
||||
for (size_t x = 32; x < FatalScreenWidth - 32; x++) {
|
||||
for (size_t x = start_x; x < FatalScreenWidth - start_x; x++) {
|
||||
tiled_buf[GetPixelOffset(x, font::GetY())] = 0xFFFF;
|
||||
}
|
||||
|
||||
|
@ -235,6 +236,7 @@ namespace ams::fatal::srv {
|
|||
|
||||
u32 backtrace_y = font::GetY();
|
||||
u32 backtrace_x = 0;
|
||||
u32 pc_x = 0;
|
||||
|
||||
/* Note architecutre. */
|
||||
const bool is_aarch32 = this->context->cpu_ctx.architecture == CpuContext::Architecture_Aarch32;
|
||||
|
@ -242,19 +244,8 @@ namespace ams::fatal::srv {
|
|||
/* Print GPRs. */
|
||||
font::SetFontSize(14.0f);
|
||||
font::Print("General Purpose Registers ");
|
||||
{
|
||||
font::SetPosition(font::GetX() + 2, font::GetY());
|
||||
u32 x = font::GetX();
|
||||
font::Print("PC: ");
|
||||
font::SetPosition(x + 47, font::GetY());
|
||||
}
|
||||
if (is_aarch32) {
|
||||
font::PrintMonospaceU32(this->context->cpu_ctx.aarch32_ctx.pc);
|
||||
} else {
|
||||
font::PrintMonospaceU64(this->context->cpu_ctx.aarch64_ctx.pc);
|
||||
}
|
||||
font::PrintLine("");
|
||||
font::SetPosition(32, font::GetY());
|
||||
font::SetPosition(start_x, font::GetY());
|
||||
font::AddSpacingLines(0.5f);
|
||||
if (is_aarch32) {
|
||||
for (size_t i = 0; i < (aarch32::RegisterName_GeneralPurposeCount / 2); i++) {
|
||||
|
@ -268,9 +259,9 @@ namespace ams::fatal::srv {
|
|||
font::PrintMonospaceBlank(16);
|
||||
}
|
||||
font::Print(" ");
|
||||
x = font::GetX();
|
||||
pc_x = font::GetX();
|
||||
font::PrintFormat("%s:", aarch32::CpuContext::RegisterNameStrings[i + (aarch32::RegisterName_GeneralPurposeCount / 2)]);
|
||||
font::SetPosition(x + 47, font::GetY());
|
||||
font::SetPosition(pc_x + 47, font::GetY());
|
||||
if (this->context->cpu_ctx.aarch32_ctx.HasRegisterValue(static_cast<aarch32::RegisterName>(i + (aarch32::RegisterName_GeneralPurposeCount / 2)))) {
|
||||
font::PrintMonospaceU32(this->context->cpu_ctx.aarch32_ctx.r[i + (aarch32::RegisterName_GeneralPurposeCount / 2)]);
|
||||
font::PrintMonospaceBlank(8);
|
||||
|
@ -284,7 +275,7 @@ namespace ams::fatal::srv {
|
|||
}
|
||||
|
||||
font::PrintLine("");
|
||||
font::SetPosition(32, font::GetY());
|
||||
font::SetPosition(start_x, font::GetY());
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < aarch64::RegisterName_GeneralPurposeCount / 2; i++) {
|
||||
|
@ -297,9 +288,9 @@ namespace ams::fatal::srv {
|
|||
font::PrintMonospaceBlank(16);
|
||||
}
|
||||
font::Print(" ");
|
||||
x = font::GetX();
|
||||
pc_x = font::GetX();
|
||||
font::PrintFormat("%s:", aarch64::CpuContext::RegisterNameStrings[i + (aarch64::RegisterName_GeneralPurposeCount / 2)]);
|
||||
font::SetPosition(x + 47, font::GetY());
|
||||
font::SetPosition(pc_x + 47, font::GetY());
|
||||
if (this->context->cpu_ctx.aarch64_ctx.HasRegisterValue(static_cast<aarch64::RegisterName>(i + (aarch64::RegisterName_GeneralPurposeCount / 2)))) {
|
||||
font::PrintMonospaceU64(this->context->cpu_ctx.aarch64_ctx.x[i + (aarch64::RegisterName_GeneralPurposeCount / 2)]);
|
||||
} else {
|
||||
|
@ -312,10 +303,23 @@ namespace ams::fatal::srv {
|
|||
}
|
||||
|
||||
font::PrintLine("");
|
||||
font::SetPosition(32, font::GetY());
|
||||
font::SetPosition(start_x, font::GetY());
|
||||
}
|
||||
}
|
||||
|
||||
/* Print PC. */
|
||||
{
|
||||
font::SetPosition(pc_x, backtrace_y);
|
||||
const u32 x = font::GetX();
|
||||
font::Print("PC: ");
|
||||
font::SetPosition(x + 47, font::GetY());
|
||||
}
|
||||
if (is_aarch32) {
|
||||
font::PrintMonospaceU32(this->context->cpu_ctx.aarch32_ctx.pc);
|
||||
} else {
|
||||
font::PrintMonospaceU64(this->context->cpu_ctx.aarch64_ctx.pc);
|
||||
}
|
||||
|
||||
/* Print Backtrace. */
|
||||
u32 bt_size;
|
||||
if (is_aarch32) {
|
||||
|
|
4888
stratosphere/fatal/source/stb_truetype.h
Normal file
4888
stratosphere/fatal/source/stb_truetype.h
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue