freetype fixes
[org.ibex.core.git] / src / org / ibex / graphics / Freetype.c
index f65f5c8..4387aec 100644 (file)
@@ -3,8 +3,6 @@
 #include <unistd.h>
 #include <freetype/freetype.h>
 
-char *user_info[1024];
-
 /* NOTE: _user_info is defined in crt0.c. It points to a 4096 byte
    block of memory that contains 1024 32-bit values that can be set
    with the setUserInfo() method of MIPSEmu.
@@ -36,6 +34,24 @@ char *user_info[1024];
 
 */
 
+#if 0
+extern int user_info[1024];
+#else
+/* HACK: This really doesn't belong here... */
+int user_info[1024];
+
+extern int mspack_main();
+int freetype_main();
+
+int main(int argc, char **argv) {
+    if(argc < 1) return 1;
+    if(strcmp(argv[0],"mspack")==0) return mspack_main();
+    if(strcmp(argv[0],"freetype")==0) return freetype_main();
+    return 1;
+}
+#endif
+
+
 #define FT_Check(expr) do { \
     if((expr) != 0) { \
         errprint(#expr " failed\n"); \
@@ -60,15 +76,19 @@ static int errprint(const char *s) {
 
 extern void _pause();
 
+static FT_Library  library;   /* handle to library     */
+static FT_Face     face;      /* handle to face object */
+
 int freetype_main() {
-    char *fontdata;
+    FT_Check(FT_Init_FreeType(&library));
+    _pause();
+    
+    /*char *fontdata;
     int glyph_index;
     short charcode;
-    FT_Library  library;   /* handle to library     */
-    FT_Face     face;      /* handle to face object */
+
 
     FT_Check(FT_Init_FreeType(&library));
-    FT_Check(FT_New_Memory_Face(library, user_info[0], (int)user_info[1], 0, &face));
 
     for(;;) {
       
@@ -89,9 +109,34 @@ int freetype_main() {
             user_info[11] = (char*)(face->glyph->advance.x >> 6);
 
         }
-    }
+    }*/
+}
+
+int load_font(char *buf, int length) __attribute__((section(".text")));
+int load_font(char *buf, int length) {
+    if(face != NULL) FT_Check(FT_Done_Face(face));
+    face = NULL;
+    return FT_New_Memory_Face(library, buf, length, 0, &face);
 }
 
+int render(int charcode, int size) __attribute__((section(".text")));
+int render(int charcode, int size) {
+    int glyph_index;
+    
+    FT_Check(FT_Set_Char_Size(face, 0, size * 64, 72, 72));
+    
+    glyph_index = FT_Get_Char_Index(face, charcode);
+    FT_Check(FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT | FT_LOAD_FORCE_AUTOHINT));
+    FT_Check(FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL));
+    
+    user_info[0]  = (int)face->glyph->bitmap.buffer;
+    user_info[1]  = face->glyph->bitmap.width;
+    user_info[2]  = face->glyph->bitmap.rows;
+    user_info[3]  = face->size->metrics.ascender >> 6;
+    user_info[4]  = (-1 * face->size->metrics.descender) >> 6;
+    user_info[5] = face->glyph->metrics.horiBearingY >> 6;
+    user_info[6] = face->glyph->advance.x >> 6;
+}
 
 // Kerning code; add back in later
 /*
@@ -105,12 +150,3 @@ if (old_glyph_index != -1) {
 }
 old_glyph_index = glyph_index;
 */
-
-
-extern int mspack_main();
-int main(int argc, char **argv) {
-  if(argc < 1) return 1;
-  if(strcmp(argv[0],"mspack")==0) return mspack_main();
-  if(strcmp(argv[0],"freetype")==0) return freetype_main();
-  return 1;
-}