[kandinsky] Move to KDFont

This commit is contained in:
Romain Goyet
2018-10-09 16:41:24 +02:00
committed by LeaNumworks
parent 29882768bd
commit f8beae3b86
149 changed files with 564 additions and 610 deletions

View File

@@ -1,19 +1,40 @@
#include "font.h"
#include <assert.h>
#include <kandinsky/font.h>
#include "external/lz4/lz4.h"
constexpr static int k_tabCharacterWidth = 4;
KDSize KDFont::stringSize(const char * text) const {
if (text == nullptr) {
return KDSizeZero;
}
KDSize stringSize = KDSize(0, m_glyphSize.height());
while (*text != 0) {
KDSize cSize = KDSize(m_glyphSize.width(), 0);
if (*text == '\t') {
cSize = KDSize(k_tabCharacterWidth*m_glyphSize.width(), 0);
}
if (*text == '\n') {
cSize = KDSize(0, m_glyphSize.height());
}
stringSize = KDSize(stringSize.width()+cSize.width(), stringSize.height()+cSize.height());
text++;
}
return stringSize;
}
void KDFont::fetchGreyscaleGlyphForChar(char c, uint8_t * greyscaleBuffer) const {
//TODO: If debug, use LZ4_decompress_safe, otherwise LZ4_decompress_fast
int resultSize = LZ4_decompress_safe(
reinterpret_cast<const char *>(compressedGlyphData(c)),
reinterpret_cast<char *>(greyscaleBuffer),
compressedGlyphDataSize(c),
m_glyphWidth * m_glyphHeight * k_bitsPerPixel/8
m_glyphSize.width() * m_glyphSize.height() * k_bitsPerPixel/8
);
assert(resultSize == m_glyphWidth * m_glyphHeight * k_bitsPerPixel/8);
assert(resultSize == m_glyphSize.width() * m_glyphSize.height() * k_bitsPerPixel/8);
}
void KDFont::fetchGlyphForChar(char c, const KDFont::RenderPalette & renderPalette, KDColor * pixelBuffer) const {
void KDFont::fetchGlyphForChar(char c, const KDFont::RenderPalette * renderPalette, KDColor * pixelBuffer) const {
/* Since a greyscale value is smaller than a color value (see assertion), we
* can store the temporary greyscale values in the output pixel buffer.
* What's great is that now, if we fill the pixel buffer right-to-left with
@@ -25,7 +46,7 @@ void KDFont::fetchGlyphForChar(char c, const KDFont::RenderPalette & renderPalet
fetchGreyscaleGlyphForChar(c, greyscaleBuffer);
uint8_t mask = (0xFF >> (8-k_bitsPerPixel));
int pixelIndex = m_glyphWidth * m_glyphHeight - 1; // Let's start at the final pixel
int pixelIndex = m_glyphSize.width() * m_glyphSize.height() - 1; // Let's start at the final pixel
int greyscaleByteIndex = pixelIndex * k_bitsPerPixel / 8;
while (pixelIndex >= 0) {
assert(greyscaleByteIndex == pixelIndex * k_bitsPerPixel / 8);
@@ -34,25 +55,7 @@ void KDFont::fetchGlyphForChar(char c, const KDFont::RenderPalette & renderPalet
uint8_t greyscale = greyscaleByte & mask;
greyscaleByte = greyscaleByte >> k_bitsPerPixel;
assert(pixelIndex >= 0);
pixelBuffer[pixelIndex--] = renderPalette.colorAtIndex(greyscale);
pixelBuffer[pixelIndex--] = renderPalette->colorAtIndex(greyscale);
}
}
}
/*
constexpr int glyphPixelCount = 12;
void drawString(const char * text, KDColor textColor, KDColor backgroundColor) {
const KDFont * font = KDFont::LargeFont;
KDFont::RenderPalette palette = font->renderPalette(textColor, backgroundColor);
assert(glyphPixelCount >= font->glyphWidth() * font->glyphHeight());
char c;
do {
char c = text[0];
KDColor glyph[glyphPixelCount];
font->fetchGlyphForChar(c, palette, glyph);
} while (c != 0);
}
*/