2023-06-20 04:33:09 +00:00
|
|
|
#include "StarFont.hpp"
|
|
|
|
#include "StarFile.hpp"
|
|
|
|
#include "StarFormat.hpp"
|
|
|
|
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
|
|
|
|
namespace Star {
|
|
|
|
|
2023-07-03 04:21:51 +00:00
|
|
|
constexpr int FontLoadFlags = FT_LOAD_FORCE_AUTOHINT;
|
|
|
|
|
2023-06-20 04:33:09 +00:00
|
|
|
struct FTContext {
|
|
|
|
FT_Library library;
|
|
|
|
|
|
|
|
FTContext() {
|
|
|
|
library = nullptr;
|
|
|
|
if (FT_Init_FreeType(&library))
|
|
|
|
throw FontException("Could not initialize freetype library.");
|
|
|
|
}
|
|
|
|
|
|
|
|
~FTContext() {
|
|
|
|
if (library) {
|
|
|
|
FT_Done_FreeType(library);
|
|
|
|
library = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FTContext ftContext;
|
|
|
|
|
|
|
|
struct FontImpl {
|
|
|
|
FT_Face face;
|
|
|
|
};
|
|
|
|
|
|
|
|
FontPtr Font::loadTrueTypeFont(String const& fileName, unsigned pixelSize) {
|
|
|
|
return loadTrueTypeFont(make_shared<ByteArray>(File::readFile(fileName)), pixelSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
FontPtr Font::loadTrueTypeFont(ByteArrayConstPtr const& bytes, unsigned pixelSize) {
|
|
|
|
FontPtr font = make_shared<Font>();
|
|
|
|
font->m_fontBuffer = bytes;
|
|
|
|
|
|
|
|
shared_ptr<FontImpl> fontImpl = make_shared<FontImpl>();
|
|
|
|
if (FT_New_Memory_Face(
|
|
|
|
ftContext.library, (FT_Byte const*)font->m_fontBuffer->ptr(), font->m_fontBuffer->size(), 0, &fontImpl->face))
|
|
|
|
throw FontException("Could not load font from buffer");
|
|
|
|
|
|
|
|
font->m_fontImpl = fontImpl;
|
|
|
|
font->setPixelSize(pixelSize);
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2023-08-04 13:47:52 +00:00
|
|
|
Font::Font() : m_pixelSize(0), m_alphaThreshold(0) {}
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
FontPtr Font::clone() const {
|
|
|
|
return Font::loadTrueTypeFont(m_fontBuffer, m_pixelSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Font::setPixelSize(unsigned pixelSize) {
|
|
|
|
if (pixelSize == 0) {
|
|
|
|
pixelSize = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pixelSize == pixelSize)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (FT_Set_Pixel_Sizes(m_fontImpl->face, pixelSize, 0))
|
2023-06-27 10:23:44 +00:00
|
|
|
throw FontException(strf("Cannot set font pixel size to: {}", pixelSize));
|
2023-06-20 04:33:09 +00:00
|
|
|
m_pixelSize = pixelSize;
|
|
|
|
}
|
|
|
|
|
2023-08-04 13:47:52 +00:00
|
|
|
void Font::setAlphaThreshold(uint8_t alphaThreshold) {
|
|
|
|
m_alphaThreshold = alphaThreshold;
|
|
|
|
}
|
|
|
|
|
2023-06-20 04:33:09 +00:00
|
|
|
unsigned Font::height() const {
|
|
|
|
return m_pixelSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Font::width(String::Char c) {
|
|
|
|
if (auto width = m_widthCache.maybe({c, m_pixelSize})) {
|
|
|
|
return *width;
|
|
|
|
} else {
|
2023-07-03 04:21:51 +00:00
|
|
|
FT_Load_Char(m_fontImpl->face, c, FontLoadFlags);
|
2023-08-03 14:21:24 +00:00
|
|
|
unsigned newWidth = (m_fontImpl->face->glyph->linearHoriAdvance + 32768) / 65536;
|
2023-06-20 04:33:09 +00:00
|
|
|
m_widthCache.insert({c, m_pixelSize}, newWidth);
|
|
|
|
return newWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 04:21:51 +00:00
|
|
|
|
2023-07-03 09:46:56 +00:00
|
|
|
std::pair<Image, Vec2I> Font::render(String::Char c) {
|
2023-06-20 04:33:09 +00:00
|
|
|
if (!m_fontImpl)
|
2023-06-21 08:59:15 +00:00
|
|
|
throw FontException("Font::render called on uninitialized font.");
|
2023-06-20 04:33:09 +00:00
|
|
|
|
2023-07-03 04:21:51 +00:00
|
|
|
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)
|
2023-06-20 04:33:09 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
/* convert to an anti-aliased bitmap */
|
2023-07-03 04:21:51 +00:00
|
|
|
if (FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL) != 0)
|
2023-06-20 04:33:09 +00:00
|
|
|
return {};
|
|
|
|
|
2023-07-03 04:21:51 +00:00
|
|
|
FT_GlyphSlot slot = face->glyph;
|
2023-06-20 04:33:09 +00:00
|
|
|
|
2023-07-03 10:07:16 +00:00
|
|
|
unsigned width = slot->bitmap.width;
|
|
|
|
unsigned height = slot->bitmap.rows;
|
2023-06-20 04:33:09 +00:00
|
|
|
|
2023-07-03 10:07:16 +00:00
|
|
|
Image image(width + 2, height + 2, PixelFormat::RGBA32);
|
|
|
|
Vec4B white(255, 255, 255, 0);
|
|
|
|
image.fill(white);
|
|
|
|
|
|
|
|
for (unsigned y = 0; y != height; ++y) {
|
|
|
|
uint8_t* p = slot->bitmap.buffer + y * slot->bitmap.pitch;
|
|
|
|
for (unsigned x = 0; x != width; ++x) {
|
2024-02-28 17:11:55 +00:00
|
|
|
if (x < width && y < height) {
|
2023-08-04 13:47:52 +00:00
|
|
|
uint8_t value = *(p + x);
|
|
|
|
if (m_alphaThreshold) {
|
|
|
|
if (value >= m_alphaThreshold) {
|
|
|
|
white[3] = 255;
|
|
|
|
image.set(x + 1, height - y, white);
|
|
|
|
}
|
|
|
|
} else if (value) {
|
|
|
|
white[3] = value;
|
2023-07-03 10:07:16 +00:00
|
|
|
image.set(x + 1, height - y, white);
|
|
|
|
}
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 15:55:19 +00:00
|
|
|
return { std::move(image), {slot->bitmap_left - 1, (slot->bitmap_top - height) + (m_pixelSize / 4) - 1} };
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|