Richard Parker / EALCD

graphics/EAFont.cpp

Committer:
richardparker
Date:
2010-05-06
Revision:
6:4fe6f365cbeb
Parent:
3:24fbf4dbd7e5

File content as of revision 6:4fe6f365cbeb:

// Copyright 2010 Richard Parker

#include "mbed.h"
#include "EAFont.h"

#include "../screen/EALCD.h"

EAFont::EAFont()
:   _path(NULL),
    _chars(NULL),
    _noChars(0)
{
    _img.setCached(true);
    _img.setMask(true);
}

EAFont::~EAFont()
{
    // Remember to free up allocated memory.
    if (_chars != NULL)
    {
        delete[] _chars;
        _chars = NULL;
    }

    if (_path != NULL)
    {
        delete[] _path;
        _path = NULL;
    }
}

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);
    
    // Now load the cached image.
    _img.load(_path);
   
    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;
}