Text-Renderer/src/Font.cpp

61 lines
1.6 KiB
C++
Raw Normal View History

2019-10-22 15:34:51 +02:00
//
// Copyright (c) 2019 Julian Hinxlage. All rights reserved.
//
#include "Font.h"
2019-10-23 13:03:16 +02:00
#include <cpptoml.h>
2019-10-22 15:34:51 +02:00
Font::Font() {
2019-10-23 13:03:16 +02:00
xOffset = 0;
yOffset = 0;
xSize = 0;
ySize = 0;
xCount = 0;
yCount = 0;
xStart = 0;
yStart = 0;
fillValue = 0;
2019-10-23 13:39:51 +02:00
firstChar = ' ';
2019-10-22 15:34:51 +02:00
}
2019-10-23 13:03:16 +02:00
Font::Font(const std::string &file, const std::string &configFile) : Font() {
load(file, configFile);
2019-10-22 15:34:51 +02:00
}
2019-10-23 13:03:16 +02:00
void Font::load(const std::string &file, const std::string &configFile) {
2019-10-22 15:34:51 +02:00
bitmap.load(file);
2019-10-23 13:03:16 +02:00
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);
2019-10-23 13:39:51 +02:00
firstChar = (char)config->get_as<int>("firstChar").value_or(0);
2019-10-22 15:34:51 +02:00
}
int Font::width() {
return xSize;
}
int Font::height() {
return ySize;
}
2019-10-23 11:42:32 +02:00
bool Font::getPixel(char character, int x, int y) {
2019-10-22 15:34:51 +02:00
//index of character(x and y)
2019-10-23 13:39:51 +02:00
int index = (character - firstChar);
2019-10-22 15:34:51 +02:00
int xIndex = index % xCount;
int yIndex = index / xCount;
//character index to pixel index conversion
int xPos = xIndex * (xSize + xOffset) + xStart;
int yPos = yIndex * (ySize + yOffset) + yStart;
2019-10-23 11:42:32 +02:00
return bitmap.getPixel(xPos + x, yPos + y) == fillValue;
2019-10-22 15:34:51 +02:00
}