mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
Text can be drawn in a rect that cannot contain the whole character. Luckily, we have fillRectWithPixels handy to handle such cases.
36 lines
1.2 KiB
C++
36 lines
1.2 KiB
C++
#include <assert.h>
|
|
#include <kandinsky/context.h>
|
|
#include <kandinsky/font.h>
|
|
|
|
constexpr int maxGlyphPixelCount = 180;
|
|
constexpr static int k_tabCharacterWidth = 4;
|
|
|
|
KDPoint KDContext::drawString(const char * text, KDPoint p, const KDFont * font, KDColor textColor, KDColor backgroundColor, int maxLength) {
|
|
|
|
KDPoint position = p;
|
|
KDSize glyphSize = font->glyphSize();
|
|
KDFont::RenderPalette palette = font->renderPalette(textColor, backgroundColor);
|
|
|
|
KDColor glyph[maxGlyphPixelCount];
|
|
|
|
const char * end = text + maxLength;
|
|
while(*text != 0 && text != end) {
|
|
if (*text == '\n') {
|
|
position = KDPoint(0, position.y() + glyphSize.height());
|
|
} else if (*text == '\t') {
|
|
position = position.translatedBy(KDPoint(k_tabCharacterWidth * glyphSize.width(), 0));
|
|
} else {
|
|
// Fetch and draw glyph for current char
|
|
font->fetchGlyphForChar(*text, &palette, glyph);
|
|
fillRectWithPixels(
|
|
KDRect(position, glyphSize.width(), glyphSize.height()),
|
|
glyph,
|
|
glyph // It's OK to trash the content of glyph since we'll re-fetch it for the next char anyway
|
|
);
|
|
position = position.translatedBy(KDPoint(glyphSize.width(), 0));
|
|
}
|
|
text++;
|
|
}
|
|
return position;
|
|
}
|