Skip to content

Commit

Permalink
Use shared-font. Setting s_textLang is still disabled, until language…
Browse files Browse the repository at this point in the history
….c is updated. Check for setsysInitialize failure. Moved .nxfnt files into data/unused/. Currently text will not display with the pc-build since no font is loaded for it. Disabled y+=baseline in text-drawing, and adjusted all callers y-pos to manually add the original baseline (so that y-pos matches with the different font).
  • Loading branch information
yellows8 committed Apr 28, 2018
1 parent 4563794 commit 1e23237
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Makefile.nx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DIST_PATH := $(TARGET)_v$(APP_VERSION)
ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE

CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES)
$(ARCH) $(DEFINES) `freetype-config --cflags`

CFLAGS += $(INCLUDE) -D__SWITCH__ -DVERSION=\"v$(APP_VERSION)\"

Expand All @@ -53,7 +53,7 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

LIBS := -lnx -lm -lz
LIBS := `freetype-config --libs`

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
Expand Down
7 changes: 3 additions & 4 deletions Makefile.pc
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ test : pc_main/main.cpp pc_main/pc_launch.c \
common/menu-entry.c common/menu-list.c common/message-box.c common/text.c \
common/nanojpeg.c common/ui.c common/math.c common/theme.c \
common/netloader.c \
build_pc/tahoma24.o build_pc/tahoma12.o build_pc/interuimedium20.o build_pc/interuimedium30.o \
build_pc/interuiregular14.o build_pc/interuiregular18.o \
build_pc/invalid_icon.bin.o build_pc/folder_icon.bin.o \
build_pc/button_a_light.bin.o build_pc/button_a_dark.bin.o build_pc/button_b_light.bin.o build_pc/button_b_dark.bin.o build_pc/hbmenu_logo_light.bin.o build_pc/hbmenu_logo_dark.bin.o
gcc -Wall -O2 -g -DVERSION=\"v$(APP_VERSION)\" $(EXTRA_CFLAGS) $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ -lm -lz $(EXTRA_LDFLAGS) -I. -iquote $(DEVKITPRO)/libnx/include -Ibuild_pc -g -o $@
build_pc/button_a_light.bin.o build_pc/button_a_dark.bin.o build_pc/button_b_light.bin.o build_pc/button_b_dark.bin.o build_pc/hbmenu_logo_light.bin.o build_pc/hbmenu_logo_dark.bin.o \
#build_pc/tahoma24.o build_pc/tahoma12.o build_pc/interuimedium20.o build_pc/interuimedium30.o build_pc/interuiregular14.o build_pc/interuiregular18.o
gcc -Wall -O2 -g -DVERSION=\"v$(APP_VERSION)\" $(EXTRA_CFLAGS) `freetype-config --cflags` $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ `freetype-config --libs` -lm -lz $(EXTRA_LDFLAGS) -I. -iquote $(DEVKITPRO)/libnx/include -Ibuild_pc -g -o $@

build_pc/tahoma12.o : data/tahoma12.nxfnt
mkdir -p $(dir $@)
Expand Down
22 changes: 13 additions & 9 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
typedef uint8_t u8;
typedef uint32_t u32;
typedef uint64_t u64;
typedef u32 Result;


#ifdef _WIN32
Expand All @@ -42,6 +43,12 @@ typedef union {
};
} color_t;

// when building for pc we need to include these separately
#ifndef __SWITCH__
#include "switch/nro.h"
#include "switch/nacp.h"
#endif

#include "font.h"
#include "menu.h"
#include "text.h"
Expand All @@ -51,12 +58,6 @@ typedef union {
#include "math.h"
#include "theme.h"

// when building for pc we need to include these separately
#ifndef __SWITCH__
#include "switch/nro.h"
#include "switch/nacp.h"
#endif

void menuStartup();
void menuLoop();

Expand Down Expand Up @@ -147,6 +148,9 @@ static inline color_t FetchPixelColor(uint32_t x, uint32_t y)
#endif

void DrawPixel(uint32_t x, uint32_t y, color_t clr);
void DrawText(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t clr, const char* text);
void DrawTextTruncate(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t clr, const char* text, uint32_t max_width, const char* end_text);
void GetTextDimensions(const ffnt_header_t* font, const char* text, uint32_t* width_out, uint32_t* height_out);
void DrawText(u32 font, uint32_t x, uint32_t y, color_t clr, const char* text);
void DrawTextTruncate(u32 font, uint32_t x, uint32_t y, color_t clr, const char* text, uint32_t max_width, const char* end_text);
void GetTextDimensions(u32 font, const char* text, uint32_t* width_out, uint32_t* height_out);

bool fontInitialize(void);
void fontExit();
190 changes: 174 additions & 16 deletions common/font.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
#include "common.h"

static inline const ffnt_page_t* FontGetPage(const ffnt_header_t* font, uint32_t page_id)
#include <ft2build.h>
#include FT_FREETYPE_H

#ifdef __SWITCH__
#define FONT_FACES_MAX PlSharedFontType_Total
#else
#define FONT_FACES_MAX 1
#endif

static FT_Error s_font_libret=1, s_font_facesret[FONT_FACES_MAX];

static FT_Library s_font_library;
static FT_Face s_font_faces[FONT_FACES_MAX];
static FT_Face s_font_lastusedface;
static size_t s_font_faces_total = 0;

static bool FontSetType(u32 font)
{
u32 i=0;
u32 scale=0;
FT_Error ret=0;

switch(font)
{
case interuiregular14:
scale = 4;
break;

case interuiregular18:
scale = 5;
break;

case interuimedium20:
scale = 6;
break;

case interuimedium30:
scale = 8;
break;

default:
return false;
break;
}

for (i=0; i<s_font_faces_total; i++) {
ret = FT_Set_Char_Size(
s_font_faces[i], /* handle to face object */
0, /* char_width in 1/64th of points */
scale*64, /* char_height in 1/64th of points */
300, /* horizontal device resolution */
300); /* vertical device resolution */

if (ret) return false;
}

return true;
}

/*static inline const ffnt_page_t* FontGetPage(const ffnt_header_t* font, uint32_t page_id)
{
//__builtin_printf("GetPage %u\n", (unsigned int)page_id);
if (page_id >= font->npages)
Expand All @@ -9,27 +68,69 @@ static inline const ffnt_page_t* FontGetPage(const ffnt_header_t* font, uint32_t
if (ent->size == 0)
return NULL;
return (const ffnt_page_t*)((const uint8_t*)font + ent->offset);
}
}*/

static inline bool FontLoadGlyph(glyph_t* glyph, const ffnt_header_t* font, uint32_t codepoint)
static inline bool FontLoadGlyph(glyph_t* glyph, u32 font, uint32_t codepoint)
{
FT_Face face;
FT_Error ret=0;
FT_GlyphSlot slot;
FT_UInt glyph_index;
FT_Bitmap* bitmap;
u32 i=0;

//__builtin_printf("LoadGlyph %u\n", (unsigned int)codepoint);
const ffnt_page_t* page = FontGetPage(font, codepoint >> 8);
/*const ffnt_page_t* page = FontGetPage(font, codepoint >> 8);
if (!page)
return false;
codepoint &= 0xFF;
uint32_t off = page->hdr.pos[codepoint];
if (off == ~(uint32_t)0)
return false;
return false;*/

if (s_font_faces_total==0) return false;

for (i=0; i<s_font_faces_total; i++) {
face = s_font_faces[i];
s_font_lastusedface = face;
glyph_index = FT_Get_Char_Index(face, codepoint);
if (glyph_index==0) continue;

ret = FT_Load_Glyph(
face, /* handle to face object */
glyph_index, /* glyph index */
FT_LOAD_DEFAULT);

if (ret==0)
{
ret = FT_Render_Glyph( face->glyph, /* glyph slot */
FT_RENDER_MODE_NORMAL); /* render mode */
}

if (ret) return false;

break;
}

slot = face->glyph;
bitmap = &slot->bitmap;

//__builtin_printf("%c %u\n", (char)codepoint, (unsigned int)off);
glyph->width = page->hdr.widths[codepoint];
/*glyph->width = page->hdr.widths[codepoint];
glyph->height = page->hdr.heights[codepoint];
glyph->advance = page->hdr.advances[codepoint];
glyph->posX = page->hdr.posX[codepoint];
glyph->posY = page->hdr.posY[codepoint];
glyph->data = &page->data[off];
glyph->data = &page->data[off];*/

glyph->width = bitmap->width;
glyph->height = bitmap->rows;
glyph->pitch = bitmap->pitch;
glyph->data = bitmap->buffer;
glyph->advance = slot->advance.x >> 6;
glyph->posX = slot->bitmap_left;
glyph->posY = slot->bitmap_top;
return true;
}

Expand All @@ -38,16 +139,17 @@ static void DrawGlyph(uint32_t x, uint32_t y, color_t clr, const glyph_t* glyph)
uint32_t i, j;
const uint8_t* data = glyph->data;
x += glyph->posX;
y += glyph->posY;
y -= glyph->posY; //y += glyph->posY;
//__builtin_printf("DrawGlyph %u %u %08X\n", (unsigned int)x, (unsigned int)y, (unsigned int)clr.abgr);
for (j = 0; j < glyph->height; j ++)
{
for (i = 0; i < glyph->width; i ++)
{
clr.a = *data++;
clr.a = data[i];
if (!clr.a) continue;
DrawPixel(x+i, y+j, clr);
}
data+= glyph->pitch;
}
}

Expand Down Expand Up @@ -118,11 +220,15 @@ static inline uint32_t DecodeUTF8(const char** ptr)
return 0xFFFD;
}

static void DrawText_(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t clr, const char* text, uint32_t max_width, const char* end_text)
static void DrawText_(u32 font, uint32_t x, uint32_t y, color_t clr, const char* text, uint32_t max_width, const char* end_text)
{
//__builtin_printf("DrawText %u %u %08X %s\n", (unsigned int)x, (unsigned int)y, (unsigned int)clr.abgr, text);
y += font->baseline;
//y += font->baseline;
uint32_t origX = x;
if (s_font_faces_total==0) return;
if (!FontSetType(font)) return;
s_font_lastusedface = s_font_faces[0];

while (*text)
{
if (max_width && x-origX >= max_width) {
Expand All @@ -142,7 +248,7 @@ static void DrawText_(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t
}

x = origX;
y += font->height;
y += s_font_lastusedface->size->metrics.height / 64;
continue;
}

Expand All @@ -157,20 +263,24 @@ static void DrawText_(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t
}
}

void DrawText(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t clr, const char* text)
void DrawText(u32 font, uint32_t x, uint32_t y, color_t clr, const char* text)
{
DrawText_(font, x, y, clr, text, 0, NULL);
}

void DrawTextTruncate(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t clr, const char* text, uint32_t max_width, const char* end_text)
void DrawTextTruncate(u32 font, uint32_t x, uint32_t y, color_t clr, const char* text, uint32_t max_width, const char* end_text)
{
DrawText_(font, x, y, clr, text, max_width, end_text);
}

void GetTextDimensions(const ffnt_header_t* font, const char* text, uint32_t* width_out, uint32_t* height_out)
void GetTextDimensions(u32 font, const char* text, uint32_t* width_out, uint32_t* height_out)
{
uint32_t x = 0;
uint32_t width = 0, height = 0;
if (s_font_faces_total==0) return;
if (!FontSetType(font)) return;
s_font_lastusedface = s_font_faces[0];

while (*text)
{
glyph_t glyph;
Expand All @@ -179,7 +289,7 @@ void GetTextDimensions(const ffnt_header_t* font, const char* text, uint32_t* wi
if (codepoint == '\n')
{
x = 0;
height += font->height;
height += s_font_lastusedface->size->metrics.height / 64;
continue;
}

Expand All @@ -198,3 +308,51 @@ void GetTextDimensions(const ffnt_header_t* font, const char* text, uint32_t* wi
*width_out = width;
*height_out = height;
}

bool fontInitialize(void)
{
FT_Error ret=0;
u32 i;

for (i=0; i<FONT_FACES_MAX; i++) s_font_facesret[i] = 1;

ret = FT_Init_FreeType(&s_font_library);
s_font_libret = ret;
if (s_font_libret) return false;

#ifdef __SWITCH__
PlFontData fonts[PlSharedFontType_Total];

Result rc=0;
rc = plGetSharedFont(textGetLanguageCode(), fonts, FONT_FACES_MAX, &s_font_faces_total);
if (R_FAILED(rc)) return false;

for (i=0; i<s_font_faces_total; i++) {
ret = FT_New_Memory_Face( s_font_library,
fonts[i].address, /* first byte in memory */
fonts[i].size, /* size in bytes */
0, /* face_index */
&s_font_faces[i]);

s_font_facesret[i] = ret;
if (ret) return false;
}
#endif

#ifndef __SWITCH__
//TODO: How to handle this? Until this is impl'd no font will be displayed for pc-build.
#endif

return true;
}

void fontExit()
{
u32 i=0;

for (i=0; i<s_font_faces_total; i++)
if (s_font_facesret[i]==0) FT_Done_Face(s_font_faces[i]);

if (s_font_libret==0) FT_Done_FreeType(s_font_library);
}

22 changes: 11 additions & 11 deletions common/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ typedef struct {

typedef struct {
uint8_t width, height;
int8_t posX, posY, advance;
int8_t posX, posY, advance, pitch;
const uint8_t* data;
} glyph_t;

extern const ffnt_header_t tahoma24_nxfnt;
extern const ffnt_header_t tahoma12_nxfnt;
extern const ffnt_header_t interuimedium20_nxfnt;
//extern const ffnt_header_t tahoma24_nxfnt;//These tahoma fonts aren't used anymore.
//extern const ffnt_header_t tahoma12_nxfnt;
/*extern const ffnt_header_t interuimedium20_nxfnt;
extern const ffnt_header_t interuimedium30_nxfnt;
extern const ffnt_header_t interuiregular14_nxfnt;
extern const ffnt_header_t interuiregular18_nxfnt;
#define tahoma24 &tahoma24_nxfnt
#define tahoma12 &tahoma12_nxfnt
#define interuimedium20 &interuimedium20_nxfnt
#define interuimedium30 &interuimedium30_nxfnt
#define interuiregular14 &interuiregular14_nxfnt
#define interuiregular18 &interuiregular18_nxfnt
extern const ffnt_header_t interuiregular18_nxfnt;*/
//#define tahoma24 &tahoma24_nxfnt
//#define tahoma12 &tahoma12_nxfnt
#define interuimedium20 2//&interuimedium20_nxfnt
#define interuimedium30 3//&interuimedium30_nxfnt
#define interuiregular14 0//&interuiregular14_nxfnt
#define interuiregular18 1//&interuiregular18_nxfnt
Loading

0 comments on commit 1e23237

Please sign in to comment.