Richard Parker / EALCD
Revision:
0:839ecbf5cb2a
Child:
3:24fbf4dbd7e5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graphics/EAFont.cpp	Thu Feb 11 12:21:18 2010 +0000
@@ -0,0 +1,137 @@
+// Copyright 2010 Richard Parker
+
+#include "mbed.h"
+#include "EAFont.h"
+
+#include "../widgets/EAImage.h"
+#include "../screen/EALCD.h"
+
+EAFont::EAFont()
+:   _path(NULL),
+    _chars(NULL),
+    _noChars(0)
+{
+}
+
+EAFont::~EAFont()
+{
+    // Remember to free up allocated memory.
+    if (_chars != NULL)
+    {
+        delete[] _chars;
+    }
+}
+
+bool EAFont::load(const char* path)
+{
+    if (path == NULL)
+    {
+        // Invalid path passed in.
+        return false;
+    }
+    
+    // Load font descriptor into look up table.  
+
+    // Try and open the file, check type and load width and height.
+    FILE* fp = fopen(path, "r");
+    if (fp == NULL)
+    {
+        return false;
+    }
+    
+    int noChars = 0;
+       
+    // The first line contains the number of characters,
+    if (fscanf(fp, "%d", &noChars) != 1)
+    {
+        fclose(fp);
+        return false;
+    }
+    
+    // Store the path for later
+    int pathLen = strlen(path);
+    
+    // If already loaded an image then clear to load the new one.
+    if (_path != NULL)
+    {
+        delete[] _path;
+        _path = NULL;
+    }
+    
+    // Now allocate enough space to hold path. Note +1 for null character.
+    _path = new char[pathLen+1];
+    
+    // Now copy over passed in path to path variable.
+    strcpy(_path, path);
+    
+    // Now change extension to fnb.
+    _path[pathLen-1] = 'b';
+    
+    _noChars = noChars;
+   
+    // Now that have the correct number of characters want to reserve enough space to hold. First
+    // check that there is not already memory allocated.
+    if (_chars != NULL)
+    {
+        delete[] _chars;
+    }       
+    
+    // Now create large enough array.
+    _chars = new EACharacter[noChars];
+    
+    // Now loop through loading in the values.
+    for (int i = 0; i < noChars; i++)
+    {   
+        int v = 0;
+        
+        // id.
+        fscanf(fp, "%d", &v);
+        _chars[i].id = v;
+        
+        // x.
+        fscanf(fp, "%d", &v);
+        _chars[i].x = v;
+
+        // y.
+        fscanf(fp, "%d", &v);
+        _chars[i].y = v;
+
+        // width.
+        fscanf(fp, "%d", &v);
+        _chars[i].width = v;
+
+        // height.
+        fscanf(fp, "%d", &v);
+        _chars[i].height = v;
+
+        // xoffset.
+        fscanf(fp, "%d", &v);
+        _chars[i].xOffset = v;
+
+        // yoffset.
+        fscanf(fp, "%d", &v);
+        _chars[i].yOffset = v;
+
+        // xAdvance.
+        fscanf(fp, "%d", &v);
+        _chars[i].xAdvance = v;
+    }
+    
+    fclose(fp);       
+   
+    return true;    
+}
+
+bool EAFont::getCharacter(char c, EACharacter& detail)
+{
+    for (int i = 0; i < _noChars; i++)
+    {
+        if (_chars[i].id == c)
+        {
+            detail = _chars[i];
+            return true;
+        }
+    }
+    
+    return false;
+}