zoneminder/src/zm_font.cpp

75 lines
1.8 KiB
C++
Raw Normal View History

#include <cstdio>
#include <cstring>
2020-11-28 22:14:45 +08:00
#include <sys/stat.h>
#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) {
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
size_t header_size = 8 + (sizeof(ZMFONT_BH) * NUM_FONT_SIZES);
2020-11-29 12:55:06 +08:00
// MAGIC + pad + BitmapHeaders
size_t readsize = fread(&font[0], 1, header_size, f);
if ( readsize < header_size ) {
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 < 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
*/
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) ) {
delete font;
font = nullptr;
2020-11-29 12:55:06 +08:00
return -4;
}
} // end foreach font size
2020-11-28 22:14:45 +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)];
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;
}
fclose(f);
return 0;
}
ZmFont::~ZmFont() {
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];
}