Text-Renderer/src/TextRenderer.cpp

96 lines
2.8 KiB
C++

//
// Created by my on 2019/11/16.
//
#include "TextRenderer.h"
TextRenderer::TextRenderer(vkvm::Color backgroundColor, vkvm::Color fontColor, Font font)
: backgroundColor(backgroundColor), fontColor(fontColor), font(font) {
windowWidth = vkvm::getWidth();
fontWidth = font.width();
fontHeight = font.height();
}
void TextRenderer::update(std::string newText) {
int startX;
int startY;
int i;
int fontNumbersInOneLine = windowWidth / fontWidth;
bool** characterBitmap;
std::cout << "get window's width from shared memery: (" << windowWidth << ")" << 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);
}
}
oldText = newText;
}
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()) {
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(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++;
}
}