2021-02-04 06:56:42 +08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2020-11-28 22:14:45 +08:00
|
|
|
#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-12-04 05:21:18 +08:00
|
|
|
size_t header_size = 8 + (sizeof(ZMFONT_BH) * NUM_FONT_SIZES);
|
|
|
|
|
2020-11-29 12:55:06 +08:00
|
|
|
// MAGIC + pad + BitmapHeaders
|
2020-12-04 05:21:18 +08:00
|
|
|
size_t readsize = fread(&font[0], 1, header_size, f);
|
|
|
|
if ( readsize < header_size ) {
|
2020-11-30 20:27:59 +08:00
|
|
|
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
|
|
|
|
2020-12-04 05:21:18 +08:00
|
|
|
for ( int i = 0; i < NUM_FONT_SIZES; i++ ) {
|
2020-11-29 12:55:06 +08:00
|
|
|
/* 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-12-04 05:21:18 +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-12-04 05:21:18 +08:00
|
|
|
} // end foreach font size
|
2020-11-28 22:14:45 +08:00
|
|
|
|
2020-12-04 05:21:18 +08:00
|
|
|
datasize = st.st_size - header_size;
|
2020-11-29 12:55:06 +08:00
|
|
|
|
|
|
|
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);
|
2020-12-04 05:21:18 +08:00
|
|
|
if ( readsize < datasize ) { // Shouldn't happen
|
2020-11-30 20:27:59 +08:00
|
|
|
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-12-02 12:14:49 +08:00
|
|
|
if ( font && font->data ) {
|
2020-11-29 12:55:06 +08:00
|
|
|
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];
|
|
|
|
}
|