// // Created by my on 2019/11/16. // #include "TextRenderer.h" 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; fontWidth = font.width(); fontHeight = font.height(); } void TextRenderer::update(std::string newText) { int startX = 0; int startY = 0; int i; int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin); std::vector> characterBitmap; for(i = 0; i < newText.size(); i++) { if(i > oldText.size() || oldText[i] != newText[i]) { startX = (i % fontNumbersInOneLine) * (fontWidth + left_margin); startY = (i / fontNumbersInOneLine) * (fontHeight + bottom_margin); characterBitmap = getCharacter(newText[i], font); // fontProcessing(characterBitmap); translateToSharedMemory(characterBitmap, startX, startY); } } 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::setOldText(std::string text) { oldText = text; } std::vector> TextRenderer::getCharacter(unsigned char character, Font font) { int fontHeight = font.height(); int fontWidth = font.width(); std::vector> bitmap_character; // bitmap_character = (bool**)malloc(fontHeight * sizeof(bool*)); bitmap_character.resize(fontHeight); for (int i = 0; i < fontHeight; i++) { // bitmap_character[i] = (bool*)malloc(fontWidth * sizeof(bool)); bitmap_character[i].resize(fontWidth); for (int j = 0; j < fontWidth; j++) { bitmap_character[i][j] = font.getPixel(character, j, i); } } return bitmap_character; } void TextRenderer::fontProcessing(std::vector> 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> 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; }