162 lines
5.3 KiB
C++
162 lines
5.3 KiB
C++
//
|
|
// Created by my on 2019/11/11.
|
|
//
|
|
|
|
#include "DritteFont.h"
|
|
|
|
DritteFont::DritteFont(){
|
|
readFont();
|
|
}
|
|
|
|
std::vector<char> DritteFont::test() {
|
|
return loadBitmap();
|
|
}
|
|
|
|
std::vector<std::vector<bool>> DritteFont::getCharacter(unsigned char character) {
|
|
return characters[character - FIRST_CHARACTER_INDEX];
|
|
}
|
|
|
|
void DritteFont::readFont() {
|
|
std::vector<char> data = loadBitmap();
|
|
Config config;
|
|
|
|
int offset = DATA_OFFSET;
|
|
int row;
|
|
int column;
|
|
int i;
|
|
int j;
|
|
int size = getBitmapSize(data); // size = width * 10000 + height
|
|
int width = size / 10000;
|
|
int characterSize = getCharacterSize(data, config, width); // characterSize = width * 10000 + height
|
|
int characterWidth = characterSize / 10000;
|
|
int characterHeight = characterSize % 10000;
|
|
|
|
// characters->resize(NUMBER_OF_CHARACTERS);
|
|
|
|
for(i = 0; i < NUMBER_OF_CHARACTERS; i++) {
|
|
characters[i].resize(characterHeight);
|
|
for(j = 0; j < characterHeight; j++) {
|
|
characters[i][j].resize(characterWidth);
|
|
}
|
|
}
|
|
|
|
offset = findNextStartOffset(data, offset, config);
|
|
for(row = 0; row < config.row - 1; row++) {
|
|
for(column = 0; column < config.column; column++) {
|
|
for(i = 0; i < characterHeight; i++) {
|
|
for(j = 0; j < characterWidth; j++) {
|
|
characters[row * config.column + column][i][j] =
|
|
data[offset + (i * width + j) * PIXEL_SIZE] != (char)config.background_color.getRed();
|
|
}
|
|
}
|
|
offset = findNextStartOffset(data, offset, config);
|
|
}
|
|
offset += characterHeight * width * PIXEL_SIZE;
|
|
offset = findNextStartOffset(data, offset, config);
|
|
}
|
|
|
|
for(column = 0; column < config.column_of_finalRow; column++) {
|
|
for(i = 0; i < characterHeight; i++) {
|
|
for(j = 0; j < characterWidth; j++) {
|
|
characters[config.row * config.column + column][i][j] =
|
|
data[offset + (i * width + j) * PIXEL_SIZE] != (char)config.background_color.getRed();
|
|
}
|
|
}
|
|
offset = findNextStartOffset(data, offset, config);
|
|
}
|
|
}
|
|
|
|
std::vector<char> DritteFont::loadBitmap() {
|
|
std::ifstream ifstream;
|
|
ifstream.open(PATH_OF_FONT_BITMAP);
|
|
if(ifstream.is_open()) {
|
|
std::string str((std::istreambuf_iterator<char>(ifstream)), std::istreambuf_iterator<char>());
|
|
std::vector<char> data;
|
|
data.resize(str.size());
|
|
for(int i = 0; i < str.size(); i++){
|
|
data[i] = str[i];
|
|
}
|
|
return data;
|
|
}
|
|
|
|
std::cout << "read failed" << std::endl;
|
|
return std::vector<char>();
|
|
}
|
|
|
|
int DritteFont::getBitmapSize(std::vector<char> data) {
|
|
int width = 0;
|
|
int height = 0;
|
|
int offset = WIDTH_OFFSET;
|
|
|
|
while(data[offset] != WIDTH_END_SIGNAL) {
|
|
width = width * 10 + data[offset++] - '0';
|
|
}
|
|
offset++;
|
|
|
|
while(data[offset] != HEIGHT_END_SIGNAL) {
|
|
height = height * 10 + data[offset++] - '0';
|
|
}
|
|
|
|
return width * 10000 + height;
|
|
}
|
|
|
|
int DritteFont::getCharacterSize(std::vector<char> data, Config config, int width) {
|
|
int characterWidth = 0;
|
|
int characterHeight = 0;
|
|
int tempOffset;
|
|
|
|
int offset = DATA_OFFSET;
|
|
int counter = 0;
|
|
|
|
// because the red green blue values of border color is equal,
|
|
// so i simple the method, falls the values is different,
|
|
// the method must be modified
|
|
for(; offset < data.size(); offset++) {
|
|
|
|
if(((unsigned int)data[offset] & 0xff) == config.border_color.getRed()) // only use red value because the red, green and blue values are equal.
|
|
counter++;
|
|
|
|
if(((unsigned int)data[offset] & 0xff) != config.border_color.getRed()) {
|
|
if(counter > 0 && counter < config.border_width * 3 + DEVIATION_NUMBER) { // mal 3 because color has 3 values, red, green and blue values.
|
|
tempOffset = offset;
|
|
while (((unsigned int)data[tempOffset++] & 0xff) != config.border_color.getRed()) {
|
|
characterWidth++;
|
|
}
|
|
tempOffset = offset;
|
|
while (((unsigned int)data[tempOffset] & 0xff) != config.border_color.getRed()) {
|
|
characterHeight++;
|
|
tempOffset += width * PIXEL_SIZE;
|
|
}
|
|
|
|
return characterWidth / PIXEL_SIZE * 10000 + characterHeight;
|
|
}
|
|
|
|
counter = 0;
|
|
}
|
|
}
|
|
|
|
std::cout << "not get chacter size\n" << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
|
|
int DritteFont::findNextStartOffset(std::vector<char> data, int offset, Config config) {
|
|
|
|
int counter = 0;
|
|
|
|
for(; offset < data.size(); offset += PIXEL_SIZE) {
|
|
|
|
if(((unsigned int)data[offset] & 0xff) == config.border_color.getRed()) // only use red value because the red, green and blue values are equal.
|
|
counter++;
|
|
|
|
if(((unsigned int)data[offset] & 0xff) != config.border_color.getRed()) {
|
|
if(counter > 0 && counter < config.border_width + DEVIATION_NUMBER) { // mal 3 because color has 3 values, red, green and blue values.
|
|
return offset;
|
|
}
|
|
|
|
counter = 0;
|
|
}
|
|
}
|
|
std::cout << "not find offset\n" << std::endl;
|
|
return 0;
|
|
} |