defination of Font and Block interface.

This commit is contained in:
my 2019-11-14 13:12:48 +01:00
parent 5c896dcd4e
commit 4a08c433ad
20 changed files with 631 additions and 80 deletions

View File

@ -21,10 +21,13 @@ file(GLOB_RECURSE TESTS test/*.cpp)
include_directories(src) include_directories(src)
include_directories(test) include_directories(test)
#toml
include_directories(lib/toml)
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library")
include_directories(${LIB_PATH}/include) include_directories(${LIB_PATH}/include)
add_executable(TextRenderer ${SOURCES} ${HEADERS} main/main.cpp) add_executable(TextRenderer ${SOURCES} ${HEADERS} main/main.cpp src/Font.cpp src/Font.h src/DritteFont.cpp src/DritteFont.h src/Config.cpp src/Config.h src/Block.cpp src/Block.h)
target_link_libraries(TextRenderer ${LIB_PATH}/lib/liblibrary.a) target_link_libraries(TextRenderer ${LIB_PATH}/lib/liblibrary.a)

View File

@ -1,8 +1,8 @@
#include <iostream> //#include "add.h"
#include "add.h"
#include "Bitmap.h" #include "Bitmap.h"
#include "Font.h" #include "MainFont.h"
#include <iostream>
/** /**
* @author: Julian Hinxlage * @author: Julian Hinxlage
@ -12,7 +12,7 @@
* Currently only to test. * Currently only to test.
*/ */
int main() { int main() {
Font font("../res/font.bmp"); MainFont font("../res/font2.bmp", "../res/font2.toml");
std::string str; std::string str;
std::cout << "string to draw: "; std::cout << "string to draw: ";
@ -33,4 +33,4 @@ int main() {
} }
return 0; return 0;
} }

View File

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

11
res/font1.toml Normal file
View File

@ -0,0 +1,11 @@
xOffset = 2
yOffset = 2
xSize = 5
ySize = 7
xCount = 18
yCount = 4
xStart = 1
yStart = 1
fillValue = 0xffffff
firstChar = 32
pixelSize = 1

BIN
res/font2.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 384 KiB

13
res/font2.toml Normal file
View File

@ -0,0 +1,13 @@
xOffset = 0
yOffset = 0
xSize = 8
ySize = 8
xCount = 16
yCount = 6
xStart = 0
yStart = 16
fillValue = 0xffffff
firstChar = 33
pixelSize = 4
#gap is a character that will get skipped
gap = 63

22
res/font3.ppm Normal file

File diff suppressed because one or more lines are too long

7
res/font3.toml Normal file
View File

@ -0,0 +1,7 @@
border_color=192 192 192
backgroud_color=255 255 255
font_color=0 0 0
border_width=2
row=8
column=32
columnOfFinalRow=6

View File

@ -52,9 +52,12 @@ int Bitmap::getBitsPerPixel() {
unsigned int Bitmap::getPixel(int x, int y) { unsigned int Bitmap::getPixel(int x, int y) {
unsigned int pixel = 0; unsigned int pixel = 0;
char *ptr = getData() + ((getHeight() - y) * getWidth() + x) * getBitsPerPixel() / 8; char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8);
for(int i = 0; i < getBitsPerPixel() / 8;i++){ for(int i = 0; i < getBitsPerPixel() / 8;i++){
*((char*)&pixel+i) = ptr[i]; *((char*)&pixel+i) = ptr[i];
} }
if(pixel != 0){
return pixel;
}
return pixel; return pixel;
} }

View File

@ -19,7 +19,7 @@ public:
* @since: v0.0.0 * @since: v0.0.0
* @brief: Used to load a Bitmap from a file. * @brief: Used to load a Bitmap from a file.
*/ */
void load(const std::string &file); void load(const std::string &file);
/** /**
* @author: Julian Hinxlage * @author: Julian Hinxlage

65
src/Block.cpp Normal file
View File

@ -0,0 +1,65 @@
//
// Created by my on 2019/11/14.
//
#include "Block.h"
void Block::clear() {
}
void Block::addString(std::string str) {
}
void Block::addChar(unsigned char character) {
}
void Block::newLine() {
}
void Block::setBackgroundWithSingleColor(Color color) {
}
void Block::setBackground(Color *) {
}
void Block::setBackgroundWithSlowChangedColor(Color color1, Color color2, int modul) {
}
void Block::setNowX() {
}
void Block::setNowY() {
}
void Block::addNowX() {
}
void Block::addNowY() {
}
int Block::getNowx() {
return 0;
}
int Block::getNowY() {
return 0;
}
void Block::addString(std::string str, Font font) {
}
unsigned char *Block::getDisplayColor() {
return nullptr;
}

35
src/Block.h Normal file
View File

@ -0,0 +1,35 @@
//
// Created by my on 2019/11/13.
//
#ifndef TRE_BLOCK_H
#define TRE_BLOCK_H
#include <string>
#include <Color.h>
#include "Font.h"
class Block {
public:
virtual void clear();
virtual void addString(std::string str);
virtual void addString(std::string str, Font font);
virtual void addChar(unsigned char character);
virtual void newLine();
virtual unsigned char * getDisplayColor();
virtual void setBackgroundWithSingleColor(Color color);
virtual void setBackground(Color[]);
virtual void setBackgroundWithSlowChangedColor(Color color1, Color color2, int modul);
virtual void setNowX();
virtual void setNowY();
virtual void addNowX();
virtual void addNowY();
virtual int getNowx();
virtual int getNowY();
};
#endif //TRE_BLOCK_H

66
src/Config.cpp Normal file
View File

@ -0,0 +1,66 @@
//
// Created by my on 2019/11/12.
//
#include "Config.h"
Config::Config() {
init();
}
void Config::init() {
std::ifstream infile(CONFIG_FILE_PATH);
std::string line;
std::getline(infile, line);
line = line.substr(BORDER_COLOR_STARTPOSITION);
border_color = readColor(line);
std::getline(infile, line);
line = line.substr(BACKGROUND_COLOR_STARTPOSITION);
background_color = readColor(line);
std::getline(infile, line);
line = line.substr(FONT_COLOR_STARTPOSITION);
font_color = readColor(line);
std::getline(infile, line);
line = line.substr(BORDER_WIDTH_STARTPOSITION);
border_width = readInteger(line);
std::getline(infile, line);
line = line.substr(ROW_STARTPOSITION);
row = readInteger(line);
std::getline(infile, line);
line = line.substr(COLUMN_STARTPOSITION);
column = readInteger(line);
std::getline(infile, line);
line = line.substr(COLUMN_OF_FINAL_ROW_STARTPOSITION);
column_of_finalRow = readInteger(line);
}
int Config::readInteger(std::string line) {
int value;
std::istringstream iss(line);
if(!(iss >> value)) {
std::cout << "failed when read interger from config" << std::endl;
}
return value;
}
Color Config::readColor(std::string line) {
int red, green, blue;
std::istringstream iss(line);
if(!(iss >> red >> green >> blue)) {
std::cout << "failed when read color from config" << std::endl;
}
Color color(red, green, blue);
return color;
}

42
src/Config.h Normal file
View File

@ -0,0 +1,42 @@
//
// Created by my on 2019/11/13.
//
#ifndef TRE_CONFIG_H
#define TRE_CONFIG_H
#include <fstream>
#include <sstream>
#include <iostream>
#include "Color.h"
#define CONFIG_FILE_PATH "../res/font4.config"
#define BORDER_COLOR_STARTPOSITION 13
#define BACKGROUND_COLOR_STARTPOSITION 16
#define FONT_COLOR_STARTPOSITION 11
#define BORDER_WIDTH_STARTPOSITION 13
#define ROW_STARTPOSITION 4
#define COLUMN_STARTPOSITION 7
#define COLUMN_OF_FINAL_ROW_STARTPOSITION 17
class Config {
public:
Color border_color = Color(0, 0, 0);
Color background_color = Color(0, 0, 0);
Color font_color = Color(0, 0, 0);
int border_width;
int row;
int column;
int column_of_finalRow;
Config();
private:
void init();
Color readColor(std::string line);
int readInteger(std::string line);
};
#endif //TRE_CONFIG_H

162
src/DritteFont.cpp Normal file
View File

@ -0,0 +1,162 @@
//
// 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.red;
}
}
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.red;
}
}
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.red) // only use red value because the red, green and blue values are equal.
counter++;
if(((unsigned int)data[offset] & 0xff) != config.border_color.red) {
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.red) {
characterWidth++;
}
tempOffset = offset;
while (((unsigned int)data[tempOffset] & 0xff) != config.border_color.red) {
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.red) // only use red value because the red, green and blue values are equal.
counter++;
if(((unsigned int)data[offset] & 0xff) != config.border_color.red) {
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;
}

42
src/DritteFont.h Normal file
View File

@ -0,0 +1,42 @@
//
// Created by my on 2019/11/13.
//
#ifndef TRE_DRITTEFONT_H
#define TRE_DRITTEFONT_H
#include <iostream>
#include <vector>
#include <fstream>
#include "Config.h"
#include "Font.h"
#define FIRST_CHARACTER_INDEX 25
#define NUMBER_OF_CHARACTERS 262
#define DATA_OFFSET 0xf;
#define WIDTH_OFFSET 3;
#define WIDTH_END_SIGNAL 0x20
#define HEIGHT_END_SIGNAL 0x0A
#define PATH_OF_FONT_BITMAP "../res/font4.ppm"
#define DEVIATION_NUMBER 10
#define PIXEL_SIZE 3
class DritteFont : public Font{
public:
DritteFont();
std::vector<char> test();
std::vector<std::vector<bool>> getCharacter(unsigned char character) override;
std::vector<std::vector<bool>> characters[NUMBER_OF_CHARACTERS];
private:
void readFont();
std::vector<char> loadBitmap();
int getBitmapSize(std::vector<char> data);
int getCharacterSize(std::vector<char> data, Config config, int width);
int findNextStartOffset(std::vector<char> data, int offset, Config config);
};
#endif //TRE_DRITTEFONT_H

View File

@ -1,47 +1,9 @@
// //
// Copyright (c) 2019 Julian Hinxlage. All rights reserved. // Created by my on 2019/11/14.
// //
#include "Font.h" #include "Font.h"
Font::Font() { std::vector<std::vector<bool>> Font::getCharacter(unsigned char character) {
xOffset = 2; return std::vector<std::vector<bool>>();
yOffset = 2;
xSize = 5;
ySize = 7;
xCount = 18;
yCount = 4;
xStart = 1;
yStart = 2;
fillValue = 0xffffff;
} }
Font::Font(const std::string &file) : Font() {
bitmap.load(file);
}
void Font::load(const std::string &file) {
bitmap.load(file);
}
int Font::width() {
return xSize;
}
int Font::height() {
return ySize;
}
bool Font::getPixel(char character, int x, int y) {
//index of character(x and y)
int index = (character - ' ');
int xIndex = index % xCount;
int yIndex = index / xCount;
//character index to pixel index conversion
int xPos = xIndex * (xSize + xOffset) + xStart;
int yPos = yIndex * (ySize + yOffset) + yStart;
return bitmap.getPixel(xPos + x, yPos + y) == fillValue;
}

View File

@ -1,42 +1,16 @@
// //
// Copyright (c) 2019 Julian Hinxlage. All rights reserved. // Created by my on 2019/11/14.
// //
#ifndef TEXTRENDERER_FONT_H #ifndef TEXTRENDERER_FONT_H
#define TEXTRENDERER_FONT_H #define TEXTRENDERER_FONT_H
#include "Bitmap.h"
#include <vector>
class Font { class Font {
public: public:
Bitmap bitmap; virtual std::vector<std::vector<bool>> getCharacter(unsigned char character);
//space between characters
int xOffset;
int yOffset;
//size of a character
int xSize;
int ySize;
//count of characters per row
int xCount;
//count of rows
int yCount;
//pixel offset of first character
int xStart;
int yStart;
unsigned int fillValue;
Font();
explicit Font(const std::string &file);
void load(const std::string &file);
int width();
int height();
bool getPixel(char character, int x, int y);
}; };

91
src/MainFont.cpp Normal file
View File

@ -0,0 +1,91 @@
//
// Copyright (c) 2019 Julian Hinxlage. All rights reserved.
//
#include "MainFont.h"
#include <cpptoml.h>
MainFont::MainFont() {
xOffset = 0;
yOffset = 0;
xSize = 0;
ySize = 0;
xCount = 0;
yCount = 0;
xStart = 0;
yStart = 0;
fillValue = 0;
firstChar = ' ';
}
MainFont::MainFont(const std::string &file, const std::string &configFile) : MainFont() {
load(file, configFile);
}
void MainFont::load(const std::string &file, const std::string &configFile) {
bitmap.load(file);
auto config = cpptoml::parse_file(configFile);
xOffset = config->get_as<int>("xOffset").value_or(0);
yOffset = config->get_as<int>("yOffset").value_or(0);
xSize = config->get_as<int>("xSize").value_or(0);
ySize = config->get_as<int>("ySize").value_or(0);
xCount = config->get_as<int>("xCount").value_or(0);
yCount = config->get_as<int>("yOffset").value_or(0);
xStart = config->get_as<int>("xStart").value_or(0);
yStart = config->get_as<int>("yStart").value_or(0);
fillValue = config->get_as<unsigned int>("fillValue").value_or(0);
firstChar = (char)config->get_as<int>("firstChar").value_or(0);
pixelSize = config->get_as<int>("pixelSize").value_or(0);
gap = config->get_as<int>("gap").value_or(-1);
}
int MainFont::width() {
return xSize;
}
int MainFont::height() {
return ySize;
}
bool MainFont::getPixel(char character, int x, int y) {
//index of character(x and y)
int index = (character - firstChar);
if(gap != -1){
if (index >= gap){
index++;
}
}
int xIndex = index % xCount;
int yIndex = index / xCount;
if(index < 0){
yIndex--;
xIndex += xCount;
}
//character index to pixel index conversion
int xPos = xIndex * (xSize + xOffset) + xStart;
int yPos = yIndex * (ySize + yOffset) + yStart;
return bitmap.getPixel((xPos + x) * pixelSize, (yPos + y) * pixelSize) == fillValue;
}
std::vector<std::vector<bool>> MainFont::getCharacter(unsigned char character) {
std::vector<std::vector<bool>> bitmap_character;
int fontHeight = height();
int fontWidth = width();
bitmap_character.resize(fontHeight);
for (int i = 0; i < fontHeight; i++) {
bitmap_character.resize(fontWidth);
for (int j = 0; j < fontWidth; j++) {
bitmap_character[i][j] = getPixel(character, j, i);
}
}
return bitmap_character;
}

53
src/MainFont.h Normal file
View File

@ -0,0 +1,53 @@
//
// Copyright (c) 2019 Julian Hinxlage. All rights reserved.
//
#ifndef TEXTRENDERER_MAINFONT_H
#define TEXTRENDERER_MAINFONT_H
#include "Bitmap.h"
#include "Font.h"
/**
* @author: Julian Hinxlage
* @since: v0.0.0
* @brief: this class provides pixel access based on characters
*/
class MainFont : public Font{
public:
Bitmap bitmap;
//space between characters
int xOffset;
int yOffset;
//size of a character
int xSize;
int ySize;
//count of characters per row
int xCount;
//count of rows
int yCount;
//pixel offset of first character
int xStart;
int yStart;
unsigned int fillValue;
char firstChar;
int pixelSize;
int gap;
MainFont();
explicit MainFont(const std::string &file, const std::string &configFile);
void load(const std::string &file, const std::string &configFile);
std::vector<std::vector<bool>> getCharacter(unsigned char character) override;
int width();
int height();
bool getPixel(char character, int x, int y);
};
#endif //TEXTRENDERER_MAINFONT_H