#include "font.h"

#include "image.h"

font::font(image const& image, bool proportional) {
	if(!proportional)
		throw std::runtime_error("not yet implemented");

	auto x = 0;
	std::vector<int> slices;
	for(int i = 0; i < image.size().x; ++i) {
		if(image.get(vec2i(i, 0)) & 0xff000000)
			slices.push_back(i);
	}
	int count = slices.size();
	slices.push_back(image.size().x);
	auto height = image.size().y;

	auto charset = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~";
	for(int i = 0; i < count; ++i) {
		auto r = recti(vec2i(slices[i], 1), vec2i(slices[i + 1] - slices[i], height - 1));
		//auto img = ::image(r.size);
		auto ch = charset[i];
		_glyphs.emplace(ch, r.size);
		_glyphs.at(ch).clear(0);
		_glyphs.at(ch).draw(image, vec2i::zero, r);
	}
}

font::font(std::string const& path, bool proportional) :
	font(image(path), proportional) { }

image const& font::operator[](char ch) const {
	if(_glyphs.contains(ch))
		return _glyphs.at(ch);
	else
		return _glyphs.at(' ');
}
