Text-Renderer/src/TextRenderer.cpp

418 lines
12 KiB
C++

//
// Created by my on 2019/11/16.
//
#include <internal.hpp>
#include "TextRenderer.h"
TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color _backGroundColor, vkvm::Color fontColor, vkvm::Color speicialFontColor,
Font font, int fontSize)
: backgroundColor(_backGroundColor), fontColor(fontColor), font(font), speicialFontColor(speicialFontColor) {
this-> windowWidth = windowWidth;
this-> windowHeight = windowHeight;
currentX = 0;
currentY = 0;
fontHeight = font.height() * fontSize;
fontWidth = font.width() * fontSize;
this -> fontSize = fontSize;
// startBlinkThread();
// blink.setblinkHeight(font.height());
// blink.startThread();
// std::thread thread1(&TextRenderer::blink1, this);
// std::thread thread1(&Blink::blink, std::ref(blink));
// thread1.detach();
// thread1.join();
// blink.blink();
// blink.blink();
}
//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;
//
// blink.setblinkHeight(font.height());
//// blink.blink();
//}
void TextRenderer::update(std::string newText) {
currentX = 0;
currentY = 0;
int i;
int space = 0;
int hidenLine = 0;
int currentLine;
int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin);
std::vector<std::vector<bool>> characterBitmap;
// std::cout << fontNumbersInOneLine << "..." << std::endl;
if(newText.size() < oldTextsize) {
clear(currentX, currentY, fontWidth, currentY + fontHeight);
}
newText = adjustText(newText);
std::cout << newText << "\n" << std::endl;
for(i = 0; i < newText.size(); i++) {
if(i > oldText.size() || oldText[i] != newText[i]) {
if(newText[i] == returnCharacter) {
space += (fontNumbersInOneLine - ((i + space) % fontNumbersInOneLine) - 1);
} else {
if(newText[i] == specialCharacter) {
space -= 1;
currentX = ((i + space) % fontNumbersInOneLine) * (fontWidth + left_margin);
currentY = ((i + space) / fontNumbersInOneLine) * (fontHeight + bottom_margin);
// blink.setCurrentX(currentX + fontWidth);
// blink.setCurrentY(currentY);
int tempBlinkX = blinkX;
int tempBlinkY = blinkY;
blinkX = currentX + fontWidth;
blinkY = currentY;
clear(tempBlinkX, tempBlinkY, tempBlinkX + 1, tempBlinkY + fontHeight);
characterBitmap = getCharacter(newText[i - 1], font);
translateToSharedMemory(characterBitmap, currentX, currentY, speicialFontColor);
} else {
currentLine = ((i + space) / fontNumbersInOneLine);
// if(currentLine - hidenLine >= totalLine) {
// hidenLine++;
// }
currentX = ((i + space) % fontNumbersInOneLine) * (fontWidth + left_margin);
currentY = (currentLine) * (fontHeight + bottom_margin);
characterBitmap = getCharacter(newText[i], font);
translateToSharedMemory(characterBitmap, currentX, currentY, fontColor);
}
}
}
}
if(newText.size() < oldTextsize) {
clear(currentX, currentY, fontWidth, currentY + fontHeight);
}
oldTextsize = newText.size();
setOldText(newText);
}
std::string TextRenderer::adjustText(std::string newText) {
int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin);
int totalLine = windowHeight / (fontHeight + bottom_margin) - 1;
int stringLine = 1;
int characterNumberOfLastLine = 0;
for(int i = 0; i < newText.size(); i++) {
if(newText[i] == returnCharacter) {
stringLine++;
characterNumberOfLastLine = 0;
} else if(newText[i] != specialCharacter) {
characterNumberOfLastLine++;
if(characterNumberOfLastLine == fontNumbersInOneLine) {
stringLine++;
characterNumberOfLastLine = 0;
}
}
}
if(stringLine > totalLine) {
int startLine = stringLine - totalLine;
int currentLine = 0;
int cursorLine;
for(int i = 0; i < newText.size(); i++) {
if(newText[i] == returnCharacter) {
currentLine++;
characterNumberOfLastLine = 0;
} else if(newText[i] != specialCharacter) {
characterNumberOfLastLine++;
if(characterNumberOfLastLine == fontNumbersInOneLine) {
currentLine++;
characterNumberOfLastLine = 0;
}
} else {
cursorLine = currentLine;
if(currentLine < startLine) {
return adjustText(newText, cursorLine, cursorLine + totalLine);
}
}
if(currentLine == startLine) {
return newText.substr(i, newText.size());
}
}
}
return newText;
}
std::string TextRenderer::adjustText(std::string newText, int startLine, int endLine) {
int currentLine = 0;
int startIndex, endIndex;
int characterNumberOfCurrentLine = 0;
int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin);
for(int i = 0; i < newText.size(); i++) {
if(newText[i] == returnCharacter) {
characterNumberOfCurrentLine = 0;
currentLine++;
if(currentLine == startLine) {
startIndex = i;
}
if(currentLine == endLine) {
endIndex = i;
}
} else if (newText[i] != specialCharacter) {
characterNumberOfCurrentLine++;
if(characterNumberOfCurrentLine == fontNumbersInOneLine) {
characterNumberOfCurrentLine = 0;
currentLine++;
if(currentLine == startLine) {
startIndex = i;
}
if(currentLine == endLine) {
endIndex = i;
}
}
}
}
return std::string();
}
void TextRenderer::blink1() {
bool signal;
int startY = fontHeight / 4;
int endX = fontHeight * 3 / 4 + 1;
while(true) {
for(int y = startY; y < endX; y++) {
if(signal)
vkvm::setPixel(blinkX, blinkY + y, backgroundColor);
else
vkvm::setPixel(blinkX, blinkY + y, fontColor);
}
signal = !signal;
vkvm::callEvent(vkvm::Redraw);
std::this_thread::sleep_for(std::chrono::milliseconds(800));
}
}
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, vkvm::Color fontColor) {
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::startBlinkThread() {
blink_thread = std::thread(&TextRenderer::blink1, this);
}
//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);
// }
// }
//}