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-29 13:24:09 +01:00
|
|
|
pixelSize = config->get_as<int>("pixelSize").value_or(0);
|
|
|
|
gap = config->get_as<int>("gap").value_or(-1);
|
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-29 13:24:09 +01:00
|
|
|
|
|
|
|
if(gap != -1){
|
|
|
|
if (index >= gap){
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 15:34:51 +02:00
|
|
|
int xIndex = index % xCount;
|
|
|
|
int yIndex = index / xCount;
|
|
|
|
|
2019-10-29 13:24:09 +01:00
|
|
|
if(index < 0){
|
|
|
|
yIndex--;
|
|
|
|
xIndex += xCount;
|
|
|
|
}
|
|
|
|
|
2019-10-22 15:34:51 +02:00
|
|
|
//character index to pixel index conversion
|
|
|
|
int xPos = xIndex * (xSize + xOffset) + xStart;
|
|
|
|
int yPos = yIndex * (ySize + yOffset) + yStart;
|
|
|
|
|
2019-10-29 13:24:09 +01:00
|
|
|
return bitmap.getPixel((xPos + x) * pixelSize, (yPos + y) * pixelSize) == fillValue;
|
2019-10-22 15:34:51 +02:00
|
|
|
}
|
|
|
|
|