Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
graphics/EAFont.cpp
- Committer:
- richardparker
- Date:
- 2010-02-11
- Revision:
- 0:839ecbf5cb2a
- Child:
- 3:24fbf4dbd7e5
File content as of revision 0:839ecbf5cb2a:
// 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;
}