Improve code readability. Use a define for header size
parent
fc9c87cf9f
commit
330327ded0
|
@ -15,9 +15,11 @@ int ZmFont::ReadFontFile(const std::string &loc) {
|
||||||
|
|
||||||
font = new ZMFONT;
|
font = new ZMFONT;
|
||||||
|
|
||||||
|
size_t header_size = 8 + (sizeof(ZMFONT_BH) * NUM_FONT_SIZES);
|
||||||
|
|
||||||
// MAGIC + pad + BitmapHeaders
|
// MAGIC + pad + BitmapHeaders
|
||||||
size_t readsize = fread(&font[0], 1, 8 + (sizeof(ZMFONT_BH) * 4), f);
|
size_t readsize = fread(&font[0], 1, header_size, f);
|
||||||
if ( readsize < 8 + (sizeof(ZMFONT_BH) * 4) ) {
|
if ( readsize < header_size ) {
|
||||||
delete font;
|
delete font;
|
||||||
font = nullptr;
|
font = nullptr;
|
||||||
return -2; // EOF reached, invalid file
|
return -2; // EOF reached, invalid file
|
||||||
|
@ -26,25 +28,25 @@ int ZmFont::ReadFontFile(const std::string &loc) {
|
||||||
if ( memcmp(font->MAGIC, "ZMFNT", 5) != 0 ) // Check whether magic is correct
|
if ( memcmp(font->MAGIC, "ZMFNT", 5) != 0 ) // Check whether magic is correct
|
||||||
return -3;
|
return -3;
|
||||||
|
|
||||||
for ( int i = 0; i < 4; i++ ) {
|
for ( int i = 0; i < NUM_FONT_SIZES; i++ ) {
|
||||||
/* Character Width cannot be greater than 64 as a row is represented as a uint64_t,
|
/* 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
|
height cannot be greater than 200(arbitary number which i have chosen, shouldn't need more than this) and
|
||||||
idx should not be more than filesize
|
idx should not be more than filesize
|
||||||
*/
|
*/
|
||||||
if ( (font->header[i].charWidth > 64 && font->header[i].charWidth == 0) || \
|
if ( (font->header[i].charWidth > 64 && font->header[i].charWidth == 0) ||
|
||||||
(font->header[i].charHeight > 200 && font->header[i].charHeight == 0) || \
|
(font->header[i].charHeight > 200 && font->header[i].charHeight == 0) ||
|
||||||
(font->header[i].idx > st.st_size) ) {
|
(font->header[i].idx > st.st_size) ) {
|
||||||
delete font;
|
delete font;
|
||||||
font = nullptr;
|
font = nullptr;
|
||||||
return -4;
|
return -4;
|
||||||
}
|
}
|
||||||
}
|
} // end foreach font size
|
||||||
|
|
||||||
datasize = st.st_size - (8 + sizeof(ZMFONT_BH) * 4);
|
datasize = st.st_size - header_size;
|
||||||
|
|
||||||
font->data = new uint64_t[datasize/sizeof(uint64_t)];
|
font->data = new uint64_t[datasize/sizeof(uint64_t)];
|
||||||
readsize = fread(&font->data[0], 1, datasize, f);
|
readsize = fread(&font->data[0], 1, datasize, f);
|
||||||
if( readsize < datasize) { // Shouldn't happen
|
if ( readsize < datasize ) { // Shouldn't happen
|
||||||
delete[] font->data;
|
delete[] font->data;
|
||||||
font->data = nullptr;
|
font->data = nullptr;
|
||||||
delete font;
|
delete font;
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#define NUM_FONT_SIZES 4
|
||||||
|
|
||||||
struct ZMFONT_BH{
|
struct ZMFONT_BH{
|
||||||
uint16_t charHeight; // Height of every character
|
uint16_t charHeight; // Height of every character
|
||||||
uint16_t charWidth; // Width of every character
|
uint16_t charWidth; // Width of every character
|
||||||
|
@ -15,7 +17,7 @@ struct ZMFONT_BH{
|
||||||
struct ZMFONT {
|
struct ZMFONT {
|
||||||
char MAGIC[6]; // ZMFNT\0
|
char MAGIC[6]; // ZMFNT\0
|
||||||
char pad[2];
|
char pad[2];
|
||||||
ZMFONT_BH header[4];
|
ZMFONT_BH header[NUM_FONT_SIZES];
|
||||||
uint64_t *data;
|
uint64_t *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue