charcoal/OpenGLEngine/Font.cpp

49 lines
1.5 KiB
C++

#include "Font.h"
#include <map>
namespace charcoal
{
image_loader::ImageRGBA Font::get_text_image(const std::string& text)
{
image_loader::ImageRGBA output;
std::map<char, const image_loader::ImageRGBA&> image_map;
output.width = 0;
output.height = 0;
for (std::string::size_type i = 0; i < text.size(); ++i)
{
// Load the image for the character
auto character_image = image_map.find(text[i]);
if (character_image == image_map.end())
{
auto insertion_result = image_map.emplace(text[i], get_character(text[i]));
character_image = insertion_result.first;
}
output.width += character_image->second.width;
output.height = character_image->second.height > output.height ? character_image->second.height : output.height;
}
// Allocate the vector
output.data.resize(output.width * output.height * 4); // * 4 for R, G, B, A channels
int character_offset = 0;
for (std::string::size_type i = 0; i < text.size(); ++i)
{
const image_loader::ImageRGBA& character_image = image_map.find(text[i])->second;
for (unsigned int y = 0; y < character_image.height; ++y)
{
for (unsigned int x = 0; x < character_image.width; ++x)
{
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
character_image.get_pixel(x, y, red, green, blue, alpha);
output.set_pixel(x + character_offset, y, red, green, blue, alpha);
}
}
character_offset += character_image.width;
}
return output;
}
}