diff --git a/kandinsky/include/kandinsky/font.h b/kandinsky/include/kandinsky/font.h index caf9502b5..3eae2a3b1 100644 --- a/kandinsky/include/kandinsky/font.h +++ b/kandinsky/include/kandinsky/font.h @@ -37,13 +37,17 @@ private: } uint8_t charAsIndex(char c) const { // FIXME: This is most likely false for chars greater than 127 - return static_cast(c) - 0x20; - // FIXME: 0x20 is a magic value... + return static_cast(c) - k_magicCharOffsetValue; + } + int signedCharAsIndex(char c) const { + return static_cast(c) - k_magicCharOffsetValue; } KDSize m_glyphSize; const uint16_t * m_glyphDataOffset; const uint8_t * m_data; + static constexpr uint8_t k_magicCharOffsetValue = 0x20; // FIXME: Value from kandinsky/fonts/rasterizer.c (CHARACTER_RANGE_START). 0x20 because we do not want have a glyph for the first 20 ASCII characters + static constexpr uint8_t k_numberOfGlyphs = 120; // FIXME: Value from kandinsky/fonts/rasterizer.c (GLYPH_COUNT) }; #endif diff --git a/kandinsky/src/font.cpp b/kandinsky/src/font.cpp index f2242fc0f..38f947d7e 100644 --- a/kandinsky/src/font.cpp +++ b/kandinsky/src/font.cpp @@ -33,6 +33,15 @@ void KDFont::fetchGreyscaleGlyphForChar(char c, uint8_t * greyscaleBuffer) const } void KDFont::fetchGlyphForChar(char c, const KDFont::RenderPalette * renderPalette, KDColor * pixelBuffer) const { + int pixelCount = m_glyphSize.width() * m_glyphSize.height() - 1; + int charIndex = signedCharAsIndex(c); + if (charIndex < 0 || charIndex >= k_numberOfGlyphs) { + // There is no data for this glyph + for (int i = 0; i < pixelCount; i++) { + pixelBuffer[i] = KDColorBlack; + } + return; + } /* 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 @@ -44,7 +53,7 @@ void KDFont::fetchGlyphForChar(char c, const KDFont::RenderPalette * renderPalet fetchGreyscaleGlyphForChar(c, greyscaleBuffer); uint8_t mask = (0xFF >> (8-k_bitsPerPixel)); - int pixelIndex = m_glyphSize.width() * m_glyphSize.height() - 1; // Let's start at the final pixel + int pixelIndex = pixelCount; // Let's start at the final pixel int greyscaleByteIndex = pixelIndex * k_bitsPerPixel / 8; while (pixelIndex >= 0) { assert(greyscaleByteIndex == pixelIndex * k_bitsPerPixel / 8);