98 lines
2.8 KiB
C++
98 lines
2.8 KiB
C++
//
|
|
// 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()) {
|
|
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++;
|
|
}
|
|
} |