Skip to content

Commit

Permalink
FTC can at least draw text.
Browse files Browse the repository at this point in the history
It's still not prime-time; button text draws wrong
and books appear to not cache well.
  • Loading branch information
rhaleblian committed Nov 19, 2018
1 parent d279392 commit c3cb193
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ $(TARGET).ds.gba : $(TARGET).nds
#-------------------------------------------------------------------------------
$(TARGET).nds : $(TARGET).arm7 $(TARGET).arm9
ndstool -b data/icon.bmp \
"dslibris;an ebook reader;for the Nintendo DS" \
"dslibris;an ebook reader;for Nintendo DS" \
-g LBRS YO 'dslibris' 2 \
-c $(TARGET).nds -7 arm7/$(TARGET).arm7 -9 arm9/$(TARGET).arm9

#-------------------------------------------------------------------------------
Expand Down
72 changes: 56 additions & 16 deletions arm9/source/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <sys/param.h>
#include <nds.h>
#include <fat.h>
#include <string.h>
#include "Text.h"
#include "App.h"
#include "main.h"
Expand Down Expand Up @@ -97,7 +98,9 @@ TextFaceRequester( FTC_FaceID face_id,

int Text::InitWithCacheManager(void) {
//! Use FreeType's cache manager. borken!

char msg[256];
app->Log("initializing FTC manager.\n");

int error = FT_Init_FreeType(&library);
if(error) return error;

Expand All @@ -107,15 +110,24 @@ int Text::InitWithCacheManager(void) {
FTC_SBitCache_New(cache.manager,&cache.sbit);
FTC_CMapCache_New(cache.manager,&cache.cmap);

face_id.file_path = filenames[TEXT_STYLE_NORMAL].c_str();
face_id.file_path = filenames[TEXT_STYLE_NORMAL].c_str();
face_id.face_index = 0;
sprintf(msg, "looking up %s\n", face_id.file_path);
app->Log(msg);
error = FTC_Manager_LookupFace(cache.manager, (FTC_FaceID)&face_id, &faces[TEXT_STYLE_NORMAL]);
if(error) return error;
if(error) {
app->Log("failed!\n");
return error;
}
ReportFace(faces[TEXT_STYLE_NORMAL]);
FT_Select_Charmap(GetFace(TEXT_STYLE_NORMAL), FT_ENCODING_UNICODE);
charmap_index = FT_Get_Charmap_Index(GetFace(TEXT_STYLE_NORMAL)->charmap);
imagetype.face_id = (FTC_FaceID)&face_id;
imagetype.height = pixelsize;
imagetype.width = pixelsize;

screen = screenleft;
InitPen();
ftc = true;
initialized = true;
return 0;
Expand Down Expand Up @@ -150,6 +162,7 @@ int Text::InitDefault(void) {

textCache.insert(make_pair(face, new Cache()));
faces[iter->first] = face;
ReportFace(face);
}

screen = screenleft;
Expand All @@ -166,6 +179,24 @@ int Text::Init()
else return InitDefault();
}

void Text::ReportFace(FT_Face face)
{
char msg[256];
sprintf(msg, "%ld faces\n", face->num_faces);
app->Log(msg);
sprintf(msg, "%ld glyphs\n", face->num_glyphs);
app->Log(msg);
sprintf(msg, "%d sizes\n", face->num_fixed_sizes);
app->Log(msg);
for (int i=0;i<face->num_fixed_sizes;i++)
{
sprintf(msg, "%d %d\n",
face->available_sizes[i].width,
face->available_sizes[i].height);
app->Log(msg);
}
}

void Text::Begin()
{
bold = false;
Expand Down Expand Up @@ -230,16 +261,14 @@ FT_UInt Text::GetGlyphIndex(u32 ucs)
charmap_index,ucs);
}

int Text::GetGlyphBitmap(u32 ucs, FTC_SBit *sbit)
int Text::GetGlyphBitmap(u32 ucs, FTC_SBit *sbit, FTC_Node *anode)
{
//! Given a UCS code, fills sbit with a bitmap.
//! Given a UCS code, fills sbit and anode.

//! Returns nonzero on error.Glyth
imagetype.flags = FT_LOAD_RENDER|FT_LOAD_TARGET_NORMAL;
error = FTC_SBitCache_Lookup(cache.sbit,&imagetype,
GetGlyphIndex(ucs),sbit,NULL);
if(error) return error;
return 0;
//! Returns nonzero on error.
imagetype.flags = FT_LOAD_DEFAULT|FT_LOAD_RENDER;
return FTC_SBitCache_Lookup(cache.sbit,&imagetype,
GetGlyphIndex(ucs),sbit,anode);
}

FT_GlyphSlot Text::GetGlyph(u32 ucs, int flags)
Expand Down Expand Up @@ -510,13 +539,15 @@ void Text::PrintChar(u32 ucs, FT_Face face) {
u16 bx, by, width, height = 0;
FT_Byte *buffer = NULL;
FT_UInt advance = 0;
FTC_Node anode;
char msg[256];

// get metrics and glyph pointer.

if(ftc)
{
// use the FT cache.
error = GetGlyphBitmap(ucs,&sbit);
error = GetGlyphBitmap(ucs,&sbit,&anode);
buffer = sbit->buffer;
bx = sbit->left;
by = sbit->top;
Expand All @@ -538,9 +569,12 @@ void Text::PrintChar(u32 ucs, FT_Face face) {
advance = glyph->advance.x >> 6;
buffer = bitmap.buffer;
}
//sprintf(msg, "%ld %d %d %d %d %d\n", ucs, bx, by, width, height, advance);
//app->Log(msg);

// kern.
#ifdef EXPERIMENTAL_KERNING
// kern.

if(codeprev) {
FT_Vector k;
error = FT_Get_Kerning(face,codeprev,ucs,FT_KERNING_UNSCALED,&k);
Expand Down Expand Up @@ -570,6 +604,8 @@ void Text::PrintChar(u32 ucs, FT_Face face) {
for (gy=0; gy<height; gy++) {
for (gx=0; gx<width; gx++) {
u8 a = buffer[gy*width+gx];
//sprintf(msg, "%d", a);
//app->Log(msg);
if (a) {
u16 sx = (pen.x+gx+bx);
u16 sy = (pen.y+gy-by);
Expand All @@ -594,9 +630,12 @@ void Text::PrintChar(u32 ucs, FT_Face face) {
}
}
}
//app->Log("\n");
}
pen.x += advance;
codeprev = ucs;
if (ftc)
FTC_Node_Unref(anode,cache.manager);
}

bool Text::PrintNewLine(void) {
Expand Down Expand Up @@ -633,7 +672,8 @@ void Text::PrintString(const char *s, u8 style) {

void Text::PrintString(const char *s, FT_Face face) {
//! Render a character string starting at the pen position.
u32 clast = 0;
//app->Log(s);
//u32 clast = 0;
u8 i=0;
while(i<strlen((char*)s)) {
u32 c = s[i];
Expand All @@ -643,7 +683,7 @@ void Text::PrintString(const char *s, FT_Face face) {
} else {
i+=GetCharCode(&(s[i]),&c);
PrintChar(c, face);
clast = c;
//clast = c;
}
}
}
Expand All @@ -669,7 +709,7 @@ void Text::PrintStatusMessage(const char *msg)
screen = screenleft;
SetInvert(false);
SetPixelSize(10);
SetPen(10,16);
SetPen(16, PAGE_HEIGHT-32);
PrintString(msg);

SetInvert(invert);
Expand Down
4 changes: 3 additions & 1 deletion include/Text.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,16 @@ class Text {
FT_GlyphSlot GetGlyph(u32 ucs, int flags);
FT_GlyphSlot GetGlyph(u32 ucs, int flags, u8 style);
FT_GlyphSlot GetGlyph(u32 ucs, int flags, FT_Face face);
FT_Error GetGlyphBitmap(u32 ucs, FTC_SBit *asbit);
FT_Error GetGlyphBitmap(u32 ucs, FTC_SBit *asbit, FTC_Node *anode=NULL);
FT_UInt GetGlyphIndex(u32 ucs);

u8 GetAdvance(u32 ucs, FT_Face face);
u8 GetStringWidth(const char *txt, FT_Face face);
void ClearCache(FT_Face face);
void PrintChar(u32 ucs, FT_Face face);
void PrintString(const char *string, FT_Face face);

void ReportFace(FT_Face face);

public:
App *app;
Expand Down
3 changes: 2 additions & 1 deletion tool/configure-freetype
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
./configure --prefix=$(realpath ..) --host=arm-none-eabi
./configure --prefix=$(realpath ..) --host=arm-none-eabi \
CPPFLAGS='-I../include' LDFLAGS='-L../lib'

0 comments on commit c3cb193

Please sign in to comment.