Text-Renderer/src/TextRenderer.cpp

250 lines
6.8 KiB
C++

//
// Created by my on 2019/11/16.
//
#include "TextRenderer.h"
TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color backGroundColor, vkvm::Color fontColor,
Font font, int fontSize): backgroundColor(backgroundColor), fontColor(fontColor), font(font) {
this-> windowWidth = windowWidth;
this-> windowHeight = windowHeight;
currentX = 0;
currentY = 0;
fontHeight = font.height() * fontSize;
fontWidth = font.width() * fontSize;
this -> fontSize = fontSize;
}
TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color fontColor, Font font)
: backgroundColor(backgroundColor), fontColor(fontColor), font(font) {
this-> windowWidth = windowWidth;
this-> windowHeight = windowHeight;
currentX = 0;
currentY = 0;
fontHeight = font.height() * fontSize;
fontWidth = font.width() * fontSize;
}
void TextRenderer::update(std::string newText) {
currentX = 0;
currentY = 0;
int i;
int space = 0;
int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin);
std::vector<std::vector<bool>> characterBitmap;
if(newText.size() < oldTextsize) {
clear();
}
for(i = 0; i < newText.size(); i++) {
if(i > oldText.size() || oldText[i] != newText[i]) {
if(newText[i] == '\n') {
space += (fontNumbersInOneLine - ((i + space) % fontNumbersInOneLine) - 1);
} else {
currentX = ((i + space) % fontNumbersInOneLine) * (fontWidth + left_margin);
currentY = ((i + space) / fontNumbersInOneLine) * (fontHeight + bottom_margin);
characterBitmap = getCharacter(newText[i], font);
// fontProcessing(characterBitmap);
translateToSharedMemory(characterBitmap, currentX, currentY);
}
}
}
oldTextsize = newText.size();
setOldText(newText);
}
void TextRenderer::clear() {
int x, y;
for(y = 0; y < windowHeight; y++) {
for(x = 0; x < windowWidth; x++) {
vkvm::setPixel(x, y, backgroundColor);
}
}
}
void TextRenderer::clear(int startX, int startY, int endX, int endY) {
int x, y;
for(y = startY; y < endY; y++) {
for(x = startX; x < endX; x++) {
vkvm::setPixel(x, y, backgroundColor);
}
}
}
void TextRenderer::setOldText(std::string text) {
oldText = text;
}
std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char character, Font font) {
std::vector<std::vector<bool>> bitmap_character;
// bitmap_character.resize(fontHeight);
// bitmap_character.resize(fontWidth);
// return bitmap_character;
// bitmap_character = (bool**)malloc(fontHeight * sizeof(bool*));
bitmap_character.resize(fontHeight);
for (int y = 0; y < fontHeight; y += fontSize) {
// bitmap_character[i] = (bool*)malloc(fontWidth * sizeof(bool));
for(int i = 0; i < fontSize; i++) {
bitmap_character[y + i].resize(fontWidth);
}
for (int x = 0; x < fontWidth; x += fontSize) {
bitmap_character[y][x] = font.getPixel(character, x / fontSize, y / fontSize);
if(bitmap_character[y][x] && fontSize != 1) {
if(y != 0) {
for (int i = y - fontSize + 1; i < y; i++) {
bitmap_character[i][x] = bitmap_character[y - fontSize][x];
}
}
if(x != 0) {
for (int i = x - fontSize + 1; i < x; i++) {
bitmap_character[y][i] = bitmap_character[y][x - fontSize];
}
}
if(x != 0 && y != 0) {
for (int i = 1; i < fontSize; i++) {
bitmap_character[y - i][x - i] = bitmap_character[y - fontSize][x - fontSize];
}
}
}
}
}
return bitmap_character;
}
void TextRenderer::fontProcessing(std::vector<std::vector<bool>> characterBitmap) {
// if(isBold()) {
for (int i = fontHeight - 1; i >= 0; i--) {
for (int j = fontWidth - 1; j >= 0; j--) {
if (i != fontHeight - 1 && j != fontWidth - 1 && characterBitmap[i][j]) {
characterBitmap[i + 1][j] = true;
characterBitmap[i][j + 1] = true;
}
}
}
// }
// if(isUnderline()) {
// for (int j = 0; j < fontWidth; j++)
// characterBitmap[fontHeight - 1][j] = true;
// }
}
bool TextRenderer::isBold() {
return type & BOLD != 0;
}
bool TextRenderer::isUnderline() {
return type & UNDERLINE != 0;
}
void TextRenderer::translateToSharedMemory(std::vector<std::vector<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][x]) {
vkvm::setPixel(_currentX, _currentY, fontColor);
}
else {
vkvm::setPixel(_currentX, _currentY, backgroundColor);
}
_currentX++;
}
_currentX = startX;
_currentY++;
}
for(x = 0; x < left_margin; x++) {
for(y = 0; y < fontHeight; y++) {
vkvm::setPixel(startX + fontWidth + x, startY + y, backgroundColor);
}
}
for(y = 0; y < bottom_margin; y++) {
for(x = 0; x < fontWidth + left_margin; x++) {
vkvm::setPixel(startX + x, startY + fontHeight + y, backgroundColor);
}
}
}
void TextRenderer::setLeftMargin(int margin) {
left_margin = margin;
}
void TextRenderer::setBottomMargin(int margin) {
bottom_margin = margin;
}
void TextRenderer::check() {
checkWindowSize();
checkFontColor();
checkBackgroundColor();
}
void TextRenderer::setWindowWidth(int windowWidth) {
this -> windowWidth = windowWidth;
}
void TextRenderer::setWindowHeight(int windowHeight) {
this -> windowHeight = windowHeight;
}
void TextRenderer::checkWindowSize() {
}
void TextRenderer::checkFontColor() {
}
void TextRenderer::checkBackgroundColor() {
}
int TextRenderer::getWindowWidth() {
return windowWidth;
}
int TextRenderer::getWindowHeight() {
return windowHeight;
}
//void TextRenderer::setPixelRange(int startX, int startY, int endX, int endY, int type) {
// if(type == 0) {
// for(int i = startX + 1; i < endX; i++) {
// vkvm::setPixel(i, startY, fontColor);
// }
// }
//
// if(type == 1) {
// for(int i = 1; i < endX; i++) {
// vkvm::setPixel(startX + i, startY - i, fontColor);
// }
// }
//
// if(type == 2) {
// for(int i = startY + 1; i < endY; i++) {
// vkvm::setPixel(i, startY, fontColor);
// }
// }
//}