Text-Renderer/src/Font.cpp

104 lines
2.7 KiB
C++

//
// Copyright (c) 2019 Julian Hinxlage. All rights reserved.
//
#include "Font.h"
#include <cpptoml.h>
Font::Font() {
xOffset = 0;
yOffset = 0;
xSize = 0;
ySize = 0;
xCount = 0;
yCount = 0;
xStart = 0;
yStart = 0;
fillValue = 0;
firstChar = ' ';
}
Font::Font(const std::string &file, const std::string &configFile) : Font() {
load(file, configFile);
// std::vector<std::string> tempString;
// if(std::regex_match(file, std::regex("(.+)\\.ppm$"))) {
// loadPPMFile(file, configFile);
// } else {
// std::cout << "heloo1" << std::endl;
// load(file, configFile);
// }
}
void Font::load(const std::string &file, const std::string &configFile) {
bitmap.load(file);
auto config = cpptoml::parse_file(configFile);
xOffset = config->get_as<int>("xOffset").value_or(0);
yOffset = config->get_as<int>("yOffset").value_or(0);
xSize = config->get_as<int>("xSize").value_or(0);
ySize = config->get_as<int>("ySize").value_or(0);
xCount = config->get_as<int>("xCount").value_or(0);
yCount = config->get_as<int>("yOffset").value_or(0);
xStart = config->get_as<int>("xStart").value_or(0);
yStart = config->get_as<int>("yStart").value_or(0);
fillValue = config->get_as<unsigned int>("fillValue").value_or(0);
firstChar = (char)config->get_as<int>("firstChar").value_or(0);
pixelSize = config->get_as<int>("pixelSize").value_or(0);
gap = config->get_as<int>("gap").value_or(-1);
}
int Font::width() {
return xSize;
}
int Font::height() {
return ySize;
}
bool Font::getPixel(char character, int x, int y) {
//index of character(x and y)
int index = (character - firstChar);
if(gap != -1){
if (index >= gap){
index++;
}
}
int xIndex = index % xCount;
int yIndex = index / xCount;
if(index < 0){
yIndex--;
xIndex += xCount;
}
//character index to pixel index conversion
int xPos = xIndex * (xSize + xOffset) + xStart;
int yPos = yIndex * (ySize + yOffset) + yStart;
return bitmap.getPixel((xPos + x) * pixelSize, (yPos + y) * pixelSize) == fillValue;
}
bool** Font::getCharacter(unsigned char character) {
int fontHeight = height();
int fontWidth = 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] = getPixel(character, j, i);
}
}
return bitmap_character;
}
void Font::loadPPMFile(const std::string &file, const std::string &configFile) {
}