2020-11-28 21:16:20 +08:00
|
|
|
#include <stdio.h>
|
2020-11-28 22:14:45 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-11-28 21:16:20 +08:00
|
|
|
#include "zm.h"
|
|
|
|
#include "zm_font.h"
|
|
|
|
#include "zm_utils.h"
|
|
|
|
|
2020-11-29 12:55:06 +08:00
|
|
|
int ZmFont::ReadFontFile(const std::string &loc) {
|
2020-11-28 21:16:20 +08:00
|
|
|
FILE *f = fopen(loc.c_str(), "rb");
|
2020-11-29 12:55:06 +08:00
|
|
|
if ( !f ) return -1; // FILE NOT FOUND
|
2020-11-28 22:14:45 +08:00
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
stat(loc.c_str(), &st);
|
2020-11-29 12:55:06 +08:00
|
|
|
|
|
|
|
font = new ZMFONT;
|
2020-11-28 22:14:45 +08:00
|
|
|
|
2020-11-29 12:55:06 +08:00
|
|
|
// MAGIC + pad + BitmapHeaders
|
2020-11-30 20:27:59 +08:00
|
|
|
size_t readsize = fread(&font[0], 1, 8 + (sizeof(ZMFONT_BH) * 4), f);
|
|
|
|
if ( readsize < 8 + (sizeof(ZMFONT_BH) * 4) ) {
|
|
|
|
delete font;
|
|
|
|
font = nullptr;
|
|
|
|
return -2; // EOF reached, invalid file
|
|
|
|
}
|
2020-11-29 12:55:06 +08:00
|
|
|
|
|
|
|
if ( memcmp(font->MAGIC, "ZMFNT", 5) != 0 ) // Check whether magic is correct
|
2020-11-28 22:14:45 +08:00
|
|
|
return -3;
|
2020-11-29 12:55:06 +08:00
|
|
|
|
|
|
|
for ( int i = 0; i < 4; i++ ) {
|
|
|
|
/* Character Width cannot be greater than 64 as a row is represented as a uint64_t,
|
|
|
|
height cannot be greater than 200(arbitary number which i have chosen, shouldn't need more than this) and
|
2020-11-28 22:14:45 +08:00
|
|
|
idx should not be more than filesize
|
|
|
|
*/
|
2020-11-29 12:55:06 +08:00
|
|
|
if ( (font->header[i].charWidth > 64 && font->header[i].charWidth == 0) || \
|
|
|
|
(font->header[i].charHeight > 200 && font->header[i].charHeight == 0) || \
|
|
|
|
(font->header[i].idx > st.st_size) ) {
|
2020-11-30 20:27:59 +08:00
|
|
|
delete font;
|
|
|
|
font = nullptr;
|
2020-11-29 12:55:06 +08:00
|
|
|
return -4;
|
|
|
|
}
|
2020-11-28 22:14:45 +08:00
|
|
|
}
|
|
|
|
|
2020-11-29 12:55:06 +08:00
|
|
|
datasize = st.st_size - (8 + sizeof(ZMFONT_BH) * 4);
|
|
|
|
|
|
|
|
font->data = new uint64_t[datasize/sizeof(uint64_t)];
|
2020-11-30 20:27:59 +08:00
|
|
|
readsize = fread(&font->data[0], 1, datasize, f);
|
|
|
|
if( readsize < datasize) { // Shouldn't happen
|
|
|
|
delete[] font->data;
|
|
|
|
font->data = nullptr;
|
|
|
|
delete font;
|
|
|
|
font = nullptr;
|
|
|
|
return -2;
|
|
|
|
}
|
2020-11-28 21:16:20 +08:00
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-30 20:27:59 +08:00
|
|
|
ZmFont::~ZmFont() {
|
2020-11-29 12:55:06 +08:00
|
|
|
if ( font->data ) {
|
|
|
|
delete[] font->data;
|
|
|
|
font->data = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( font ) {
|
|
|
|
delete font;
|
|
|
|
font = nullptr;
|
|
|
|
}
|
2020-11-28 22:14:45 +08:00
|
|
|
}
|
|
|
|
|
2020-11-29 12:55:06 +08:00
|
|
|
uint64_t *ZmFont::GetBitmapData() {
|
|
|
|
return &font->data[font->header[size].idx];
|
|
|
|
}
|