5#include "fifechan/imagefont.hpp"
11#include "fifechan/color.hpp"
12#include "fifechan/exception.hpp"
13#include "fifechan/graphics.hpp"
14#include "fifechan/image.hpp"
15#include "fifechan/rectangle.hpp"
27 for (; startColumn <
mImage->getWidth(); ++startColumn) {
28 if (separator !=
mImage->getPixel(startColumn, 0)) {
34 if (startColumn >=
mImage->getWidth()) {
35 throwException(
"Corrupt image.");
40 for (
int j = 0; j <
mImage->getHeight(); ++j) {
41 if (separator ==
mImage->getPixel(startColumn, j)) {
53 for (
char const glyph : glyphs) {
54 auto const k =
static_cast<unsigned char>(glyph);
61 mImage->convertToDisplayFormat();
70 if (image ==
nullptr) {
71 throwException(
"Font image is nullptr.");
78 for (i = 0; i <
mImage->getWidth() && separator ==
mImage->getPixel(i, 0); ++i) { }
80 if (i >=
mImage->getWidth()) {
81 throwException(
"Corrupt image.");
85 for (j = 0; j <
mImage->getHeight(); ++j) {
86 if (separator ==
mImage->getPixel(i, j)) {
94 unsigned char glyph = 0;
96 for (i = 0; std::cmp_less(i, glyphs.size()); ++i) {
105 mImage->convertToDisplayFormat();
118 for (i = 0; separator ==
mImage->getPixel(i, 0) && i < mImage->
getWidth(); ++i) { }
120 if (i >=
mImage->getWidth()) {
121 throwException(
"Corrupt image.");
125 for (j = 0; j <
mImage->getHeight(); ++j) {
126 if (separator ==
mImage->getPixel(i, j)) {
135 for (i = glyphsFrom; i < glyphsTo + 1; i++) {
142 mImage->convertToDisplayFormat();
148 ImageFont::~ImageFont()
155 if (
mGlyph[glyph].width == 0) {
173 if (
mGlyph[glyph].width == 0) {
177 mGlyph[
static_cast<int>((
' '))].width - 1,
178 mGlyph[
static_cast<int>((
' '))].height - 2);
191 for (
char const c : text) {
220 bool foundGlyphStart =
false;
223 for (; !foundGlyphStart; ++x) {
224 if (x >=
mImage->getWidth()) {
227 if (y >=
mImage->getHeight()) {
228 std::ostringstream os;
229 os <<
"Image " <<
mFilename <<
" with font is corrupt near character '" << glyph <<
"'";
230 throwException(os.str());
233 color =
mImage->getPixel(x, y);
234 if (color != separator) {
235 foundGlyphStart =
true;
241 bool foundGlyphEnd =
false;
244 for (; !foundGlyphEnd; ++width) {
245 if (x + width >=
mImage->getWidth()) {
246 std::ostringstream os;
247 os <<
"Image " <<
mFilename <<
" with font is corrupt near character '" << glyph <<
"'";
248 throwException(os.str());
250 color =
mImage->getPixel(x + width, y);
251 if (color == separator) {
252 foundGlyphEnd =
true;
257 return {x, y, width - 1,
mHeight};
265 for (i = 0; i < text.size(); ++i) {
277 for (i = 0; i < text.size(); ++i) {
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
virtual void drawImage(Image const *image, int srcX, int srcY, int dstX, int dstY, int width, int height)=0
Draws a part of an image.
virtual void drawRectangle(Rectangle const &rectangle)=0
Draws a simple, non-filled rectangle with a one pixel width.
virtual int getRowSpacing()
Gets the space between rows in pixels.
int mHeight
Holds the height of the image font.
virtual int drawGlyph(Graphics *graphics, unsigned char glyph, int x, int y)
Draws a glyph.
int getHeight() const override
Gets the height of the glyphs in the font.
int getStringIndexAt(std::string const &text, int x) const override
Gets a string index in a string providing an x coordinate.
Rectangle scanForGlyph(unsigned char glyph, int x, int y, Color const &separator)
Scans for a certain glyph.
Image * mImage
Holds the image with the font data.
std::array< Rectangle, 256 > mGlyph
Holds the glyphs areas in the image.
std::string mFilename
Holds the filename of the image with the font data.
virtual void setRowSpacing(int spacing)
Sets the space between rows in pixels.
ImageFont(std::string const &filename, std::string const &glyphs)
Constructor.
virtual int getWidth(unsigned char glyph) const
Gets a width of a glyph in pixels.
virtual int getGlyphSpacing()
Gets the spacing between letters in pixels.
virtual void setGlyphSpacing(int spacing)
Sets the spacing between glyphs in pixels.
int mGlyphSpacing
Holds the glyph spacing of the image font.
int mRowSpacing
Holds the row spacing of the image font.
void drawString(Graphics *graphics, std::string const &text, int x, int y) override
Draws a string.
Abstract holder for image data.
Represents a rectangular area (X, Y, Width, Height).