From db568881eace964f4fa233890cb3adece40f9d89 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Tue, 24 Apr 2018 22:10:58 -0400 Subject: [PATCH] shared_font example now displays a string. --- graphics/shared_font/source/main.c | 117 ++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 26 deletions(-) diff --git a/graphics/shared_font/source/main.c b/graphics/shared_font/source/main.c index 56ac2bb..94fe5c5 100644 --- a/graphics/shared_font/source/main.c +++ b/graphics/shared_font/source/main.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,24 +8,87 @@ //See also libnx pl.h. -//TODO: Finish this. - //This requires the switch-freetype package. //Freetype code here is based on the example code from freetype docs. +static u32 framebuf_width=0; + +//Note that this doesn't handle any blending. +void draw_glyph(FT_Bitmap* bitmap, u32* framebuf, u32 x, u32 y) +{ + u32 framex, framey; + u32 tmpx, tmpy; + u8* imageptr = bitmap->buffer; + + if (bitmap->pixel_mode!=FT_PIXEL_MODE_GRAY) return; + + for (tmpy=0; tmpyrows; tmpy++) + { + for (tmpx=0; tmpxwidth; tmpx++) + { + framex = x + tmpx; + framey = y + tmpy; + + framebuf[framey * framebuf_width + framex] = RGBA8_MAXALPHA(imageptr[tmpx], imageptr[tmpx], imageptr[tmpx]); + } + + imageptr+= bitmap->pitch; + } +} + +//Note that this doesn't handle newline, etc. +//str is UTF-8. +void draw_text(FT_Face face, u32* framebuf, u32 x, u32 y, const uint8_t* str) +{ + FT_Error ret=0; + FT_UInt glyph_index; + FT_GlyphSlot slot = face->glyph; + + u32 i; + u32 str_size = strlen((const char*)str); + uint32_t tmpchar; + ssize_t unitcount=0; + + for (i = 0; i < str_size; ) + { + unitcount = decode_utf8 (&tmpchar, &str[i]); + if (unitcount <= 0) break; + i+= unitcount; + + glyph_index = FT_Get_Char_Index(face, tmpchar); + //If using multiple fonts, you could check for glyph_index==0 here and attempt using the FT_Face for the other fonts with FT_Get_Char_Index. + + 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; + + draw_glyph(&slot->bitmap, framebuf, x + slot->bitmap_left, y - slot->bitmap_top); + + x += slot->advance.x >> 6; + y += slot->advance.y >> 6; + } +} + int main(int argc, char **argv) { Result rc=0; - //u32* framebuf; - u32 i; + u32* framebuf; u64 LanguageCode=0; PlFontData fonts[PlSharedFontType_Total]; size_t total_fonts=0; - FT_Error ret; + FT_Error ret=0, libret=0, faceret=0; FT_Library library; FT_Face face; - FT_UInt glyph_index; gfxInitDefault(); consoleInit(NULL); @@ -46,24 +110,21 @@ int main(int argc, char **argv) if (R_FAILED(rc)) printf("plGetSharedFont() failed: 0x%x\n", rc); - if (R_SUCCEEDED(rc)) - { - printf("total_fonts: %lu\n", total_fonts); - - for (i=0; i