String Umsetzung und DriiteFont finished.
This commit is contained in:
parent
4a08c433ad
commit
c9cba998dc
|
@ -27,7 +27,7 @@ include_directories(lib/toml)
|
|||
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library")
|
||||
|
||||
include_directories(${LIB_PATH}/include)
|
||||
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)
|
||||
add_executable(TextRenderer ${SOURCES} ${HEADERS} main/main.cpp src/TextRenderer.cpp src/TextRenderer.h)
|
||||
|
||||
target_link_libraries(TextRenderer ${LIB_PATH}/lib/liblibrary.a)
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
//#include "add.h"
|
||||
#include "Bitmap.h"
|
||||
#include "MainFont.h"
|
||||
#include "Font.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
bool isQuit(std::string command);
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
|
@ -11,26 +13,55 @@
|
|||
* A string is converted and displayed in the console.
|
||||
* Currently only to test.
|
||||
*/
|
||||
|
||||
#include "TextRenderer.h"
|
||||
|
||||
int main() {
|
||||
MainFont font("../res/font2.bmp", "../res/font2.toml");
|
||||
Font font("../res/font1.bmp", "../res/font1.toml");
|
||||
|
||||
std::string str;
|
||||
std::cout << "string to draw: ";
|
||||
std::getline(std::cin, str);
|
||||
std::string oldText;
|
||||
std::string currentText;
|
||||
TextRenderer textRenderer(vkvm::Color(255, 255, 255), vkvm::Color(0, 0, 0));
|
||||
|
||||
for (int i = 0; i < font.height(); i++) {
|
||||
for (char c : str) {
|
||||
for (int j = 0; j < font.width(); j++) {
|
||||
if (font.getPixel(c,j,i)) {
|
||||
std::cout << "█";
|
||||
} else {
|
||||
std::cout << " ";
|
||||
}
|
||||
}
|
||||
std::cout << " ";
|
||||
std::string command;
|
||||
|
||||
std::cout << "TextRender: " << std::endl;
|
||||
std::getline(std::cin, command);
|
||||
while(!isQuit(command)) {
|
||||
if(command.compare("update")) {
|
||||
currentText = vkvm::getText();
|
||||
textRenderer.update(currentText);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << "TextRender: " << std::endl;
|
||||
std::getline(std::cin, command);
|
||||
}
|
||||
|
||||
std::cout << "TextRender finished." << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// std::string str;
|
||||
// std::cout << "string to draw: ";
|
||||
// std::getline(std::cin, str);
|
||||
//
|
||||
// for (int i = 0; i < font.height(); i++) {
|
||||
// for (char c : str) {
|
||||
// for (int j = 0; j < font.width(); j++) {
|
||||
// if (font.getPixel(c,j,i)) {
|
||||
// std::cout << "█";
|
||||
// } else {
|
||||
// std::cout << " ";
|
||||
// }
|
||||
// }
|
||||
// std::cout << " ";
|
||||
// }
|
||||
// std::cout << std::endl;
|
||||
// }
|
||||
//
|
||||
// return 0;
|
||||
|
||||
bool isQuit(std::string command) {
|
||||
return command.compare("quit") == 0;
|
||||
}
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 448 KiB |
File diff suppressed because one or more lines are too long
|
@ -1,7 +1,11 @@
|
|||
border_color=192 192 192
|
||||
backgroud_color=255 255 255
|
||||
font_color=0 0 0
|
||||
border_width=2
|
||||
row=8
|
||||
column=32
|
||||
columnOfFinalRow=6
|
||||
xOffset = 0
|
||||
yOffset = 0
|
||||
xSize = 22
|
||||
ySize = 31
|
||||
xCount = 32
|
||||
yCount = 7
|
||||
xStart = 0
|
||||
yStart = 0
|
||||
fillValue = 0xffffff
|
||||
firstChar = 20
|
||||
pixelSize = 4
|
||||
|
|
|
@ -1,65 +0,0 @@
|
|||
//
|
||||
// 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
35
src/Block.h
|
@ -1,35 +0,0 @@
|
|||
//
|
||||
// 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
|
|
@ -1,66 +0,0 @@
|
|||
//
|
||||
// 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
42
src/Config.h
|
@ -1,42 +0,0 @@
|
|||
//
|
||||
// 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
|
|
@ -1,162 +0,0 @@
|
|||
//
|
||||
// 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;
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
//
|
||||
// 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
|
100
src/Font.cpp
100
src/Font.cpp
|
@ -1,9 +1,103 @@
|
|||
//
|
||||
// Created by my on 2019/11/14.
|
||||
// Copyright (c) 2019 Julian Hinxlage. All rights reserved.
|
||||
//
|
||||
|
||||
#include "Font.h"
|
||||
#include <cpptoml.h>
|
||||
|
||||
std::vector<std::vector<bool>> Font::getCharacter(unsigned char character) {
|
||||
return std::vector<std::vector<bool>>();
|
||||
Font::Font() {
|
||||
xOffset = 0;
|
||||
yOffset = 0;
|
||||
xSize = 0;
|
||||
ySize = 0;
|
||||
xCount = 0;
|
||||
yCount = 0;
|
||||
xStart = 0;
|
||||
yStart = 0;
|
||||
fillValue = 0;
|
||||
firstChar = ' ';
|
||||
}
|
||||
|
||||
Font::Font(const std::string &file, const std::string &configFile) : Font() {
|
||||
load(file, configFile);
|
||||
// std::vector<std::string> tempString;
|
||||
|
||||
// if(std::regex_match(file, std::regex("(.+)\\.ppm$"))) {
|
||||
// loadPPMFile(file, configFile);
|
||||
// } else {
|
||||
// std::cout << "heloo1" << std::endl;
|
||||
// load(file, configFile);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void Font::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 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 - 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;
|
||||
}
|
||||
|
||||
bool** Font::getCharacter(unsigned char character) {
|
||||
int fontHeight = height();
|
||||
int fontWidth = width();
|
||||
bool **bitmap_character;
|
||||
|
||||
bitmap_character = (bool**)malloc(fontHeight * sizeof(bool*));
|
||||
for (int i = 0; i < fontHeight; i++) {
|
||||
bitmap_character[i] = (bool*)malloc(fontWidth * sizeof(bool));
|
||||
for (int j = 0; j < fontWidth; j++) {
|
||||
bitmap_character[i][j] = getPixel(character, j, i);
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap_character;
|
||||
}
|
||||
|
||||
void Font::loadPPMFile(const std::string &file, const std::string &configFile) {
|
||||
|
||||
}
|
||||
|
||||
|
|
46
src/Font.h
46
src/Font.h
|
@ -1,16 +1,52 @@
|
|||
//
|
||||
// Created by my on 2019/11/14.
|
||||
// Copyright (c) 2019 Julian Hinxlage. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef TEXTRENDERER_FONT_H
|
||||
#define TEXTRENDERER_FONT_H
|
||||
|
||||
#include "Bitmap.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Font {
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
* @since: v0.0.0
|
||||
* @brief: this class provides pixel access based on characters
|
||||
*/
|
||||
class Font{
|
||||
public:
|
||||
virtual std::vector<std::vector<bool>> getCharacter(unsigned char character);
|
||||
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;
|
||||
|
||||
Font();
|
||||
explicit Font(const std::string &file, const std::string &configFile);
|
||||
void load(const std::string &file, const std::string &configFile);
|
||||
void loadPPMFile(const std::string &file, const std::string &configFile);
|
||||
bool** getCharacter(unsigned char character);
|
||||
|
||||
int width();
|
||||
int height();
|
||||
bool getPixel(char character, int x, int y);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,91 +0,0 @@
|
|||
//
|
||||
// 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;
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
//
|
||||
// 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
|
|
@ -0,0 +1,100 @@
|
|||
//
|
||||
// Created by my on 2019/11/16.
|
||||
//
|
||||
#include "TextRenderer.h"
|
||||
|
||||
TextRenderer::TextRenderer(vkvm::Color backgroundColor, vkvm::Color fontColor)
|
||||
: backgroundColor(backgroundColor), fontColor(fontColor) {
|
||||
windowWidth = vkvm::getWidth();
|
||||
fontWidth = font.width();
|
||||
fontHeight = font.height();
|
||||
}
|
||||
|
||||
void TextRenderer::update(std::string text) {
|
||||
int startX;
|
||||
int startY;
|
||||
int i;
|
||||
int fontNumbersInOneLine = windowWidth / fontWidth;
|
||||
bool** characterBitmap;
|
||||
|
||||
std::cout << "get window's width from shared memery: (" << windowWidth << ")" << std::endl;
|
||||
|
||||
std::string newText = vkvm::getText();
|
||||
|
||||
std::cout << "get text:" << newText << std::endl;
|
||||
|
||||
for(i = 0; i < newText.size(); i++) {
|
||||
if(i > oldText.size() || oldText[i] != newText[i]) {
|
||||
startX = i % fontNumbersInOneLine * fontWidth;
|
||||
startY = i / fontNumbersInOneLine * fontHeight;
|
||||
characterBitmap = getCharacter(newText[i], font);
|
||||
fontProcessing(characterBitmap);
|
||||
translateToSharedMemory(characterBitmap, startX, startY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextRenderer::setOldText(std::string text) {
|
||||
|
||||
}
|
||||
|
||||
bool** TextRenderer::getCharacter(unsigned char character, Font font) {
|
||||
int fontHeight = font.height();
|
||||
int fontWidth = font.width();
|
||||
bool **bitmap_character;
|
||||
|
||||
bitmap_character = (bool**)malloc(fontHeight * sizeof(bool*));
|
||||
for (int i = 0; i < fontHeight; i++) {
|
||||
bitmap_character[i] = (bool*)malloc(fontWidth * sizeof(bool));
|
||||
for (int j = 0; j < fontWidth; j++) {
|
||||
bitmap_character[i][j] = font.getPixel(character, j, i);
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap_character;
|
||||
}
|
||||
|
||||
void TextRenderer::fontProcessing(bool **characterBitmap) {
|
||||
if(isBold()) {
|
||||
|
||||
}
|
||||
|
||||
if(isItalics()) {
|
||||
|
||||
}
|
||||
|
||||
if(isUnderline()) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
bool TextRenderer::isBold() {
|
||||
return type & BOLD != 0;
|
||||
}
|
||||
|
||||
bool TextRenderer::isItalics() {
|
||||
return type & ITALICS != 0;
|
||||
}
|
||||
|
||||
bool TextRenderer::isUnderline() {
|
||||
return type & UNDERLINE != 0;
|
||||
}
|
||||
|
||||
void TextRenderer::translateToSharedMemory(bool **characterBitmap, int startX, int startY) {
|
||||
int x, y;
|
||||
int currentX = startX;
|
||||
int currentY = startY;
|
||||
|
||||
for(y = 0; y < fontHeight; y++) {
|
||||
for(x = 0; x < fontWidth; x++) {
|
||||
if(characterBitmap[y + startY][x + startX])
|
||||
setPixel(currentX, currentY, fontColor);
|
||||
else
|
||||
setPixel(currentX, currentY, backgroundColor);
|
||||
currentX++;
|
||||
}
|
||||
currentX = startX;
|
||||
currentY++;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// Created by my on 2019/11/16.
|
||||
//
|
||||
|
||||
#ifndef TEXTRENDERER_TEXTRENDERER_H
|
||||
#define TEXTRENDERER_TEXTRENDERER_H
|
||||
|
||||
#define BOLD 0b001
|
||||
#define ITALICS 0b010
|
||||
#define UNDERLINE 0b100
|
||||
|
||||
#include <string>
|
||||
#include <Color.hpp>
|
||||
#include <vkvm.hpp>
|
||||
#include <iostream>
|
||||
#include "Font.h"
|
||||
|
||||
/**
|
||||
* @author: Yajie Qi, Shaohua Tong
|
||||
* @since: v0.0.0
|
||||
* @brief: gets a string from the Shared Memory and converts the text into a bitmap-array.
|
||||
*/
|
||||
class TextRenderer {
|
||||
public:
|
||||
TextRenderer(vkvm::Color defaultBackgroundColor, vkvm::Color fontColor);
|
||||
|
||||
void update(std::string text);
|
||||
void setOldText(std::string text);
|
||||
bool** getCharacter(unsigned char character, Font font);
|
||||
|
||||
private:
|
||||
std::string oldText;
|
||||
vkvm::Color backgroundColor;
|
||||
vkvm::Color fontColor;
|
||||
vkvm::FontType fontType = vkvm::getFont();
|
||||
Font font = Font(fontType.getName(), "../res/font3.toml");
|
||||
int type;
|
||||
int windowWidth;
|
||||
int fontWidth;
|
||||
int fontHeight;
|
||||
bool isBold();
|
||||
bool isItalics();
|
||||
bool isUnderline();
|
||||
void fontProcessing(bool **characterBitmap);
|
||||
void translateToSharedMemory(bool **characterBitmap, int startX, int startY);
|
||||
};
|
||||
|
||||
|
||||
#endif //TEXTRENDERER_TEXTRENDERER_H
|
Loading…
Reference in New Issue