Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarFont.cpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-07-03 14:21:51 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-07-03 14:21:51 +1000
commitd018957b098f00536d0d7641e13aaf1ebddf2238 (patch)
tree0a739eb78c402dd261e06381f77c822c607922ad /source/core/StarFont.cpp
parent069d61e487947a8be38f6a3db52695db94fd306e (diff)
Fix font glyph generation to work correctly with other fonts
Diffstat (limited to 'source/core/StarFont.cpp')
-rw-r--r--source/core/StarFont.cpp34
1 files changed, 17 insertions, 17 deletions
diff --git a/source/core/StarFont.cpp b/source/core/StarFont.cpp
index 6308784..9c8636a 100644
--- a/source/core/StarFont.cpp
+++ b/source/core/StarFont.cpp
@@ -7,6 +7,8 @@
namespace Star {
+constexpr int FontLoadFlags = FT_LOAD_FORCE_AUTOHINT;
+
struct FTContext {
FT_Library library;
@@ -49,9 +51,7 @@ FontPtr Font::loadTrueTypeFont(ByteArrayConstPtr const& bytes, unsigned pixelSiz
return font;
}
-Font::Font() {
- m_pixelSize = 0;
-}
+Font::Font() : m_pixelSize(0) {}
FontPtr Font::clone() const {
return Font::loadTrueTypeFont(m_fontBuffer, m_pixelSize);
@@ -78,38 +78,38 @@ unsigned Font::width(String::Char c) {
if (auto width = m_widthCache.maybe({c, m_pixelSize})) {
return *width;
} else {
- FT_Load_Char(m_fontImpl->face, c, FT_LOAD_FORCE_AUTOHINT);
+ FT_Load_Char(m_fontImpl->face, c, FontLoadFlags);
unsigned newWidth = (m_fontImpl->face->glyph->advance.x + 32) / 64;
m_widthCache.insert({c, m_pixelSize}, newWidth);
return newWidth;
}
}
-Image Font::render(String::Char c) {
+
+std::pair<Image, int> Font::render(String::Char c) {
if (!m_fontImpl)
throw FontException("Font::render called on uninitialized font.");
- FT_UInt glyph_index = FT_Get_Char_Index(m_fontImpl->face, c);
- if (FT_Load_Glyph(m_fontImpl->face, glyph_index, FT_LOAD_FORCE_AUTOHINT) != 0)
+ FT_Face face = m_fontImpl->face;
+ FT_UInt glyph_index = FT_Get_Char_Index(face, c);
+ if (FT_Load_Glyph(face, glyph_index, FontLoadFlags) != 0)
return {};
/* convert to an anti-aliased bitmap */
- if (FT_Render_Glyph(m_fontImpl->face->glyph, FT_RENDER_MODE_NORMAL) != 0)
+ if (FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL) != 0)
return {};
- FT_GlyphSlot slot = m_fontImpl->face->glyph;
+ FT_GlyphSlot slot = face->glyph;
- int width = (slot->advance.x + 32) / 64;
- int height = m_pixelSize;
+ unsigned int width = slot->bitmap.width;
+ unsigned int height = slot->bitmap.rows;
Image image(width, height, PixelFormat::RGBA32);
for (int y = 0; y < height; ++y) {
+ unsigned char* p = slot->bitmap.buffer + y * slot->bitmap.pitch;
for (int x = 0; x < width; ++x) {
- int bx = x;
- int by = y + slot->bitmap_top - m_fontImpl->face->size->metrics.ascender / 64;
- if (bx >= 0 && by >= 0 && bx < (int)slot->bitmap.width && by < (int)slot->bitmap.rows) {
- unsigned char* p = slot->bitmap.buffer + by * slot->bitmap.pitch;
- unsigned char val = *(p + bx);
+ if (x >= 0 && y >= 0 && x < width && y < height) {
+ unsigned char val = *(p + x);
image.set(x, height - y - 1, Vec4B(255, 255, 255, val));
} else {
image.set(x, height - y - 1, Vec4B(255, 255, 255, 0));
@@ -117,7 +117,7 @@ Image Font::render(String::Char c) {
}
}
- return image;
+ return { move(image), (slot->bitmap_top - (int)height) + m_pixelSize / 4 };
}
}