Merge branch 'main' into main

This commit is contained in:
Astazium 2026-10-15 16:19:06 +03:00 • committed by GitHub
commit bf59e520a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
109 changed files with 1908 additions and 474 deletions

View file

@ -29,6 +29,10 @@ Font::Font(
if (this->fontFile.has_value()) {
if (auto fontPtr = this->fontFile->lock()) {
monospace = fontPtr->isMonospace();
if (monospace && this->glyphs.size() > '0' &&
this->glyphs['0'].xAdvance > 0) {
glyphInterval = this->glyphs['0'].xAdvance;
}
}
}
}
@ -62,21 +66,13 @@ bool Font::isPrintableChar(uint codepoint) const {
int FontMetrics::calcWidth(std::wstring_view text, size_t offset, size_t length) const {
auto font = this->font.has_value() ? this->font->lock() : nullptr;
if (font == nullptr || font->isMonospace()) {
return std::min(text.length() - offset, length) * _glyphInterval;
if (font != nullptr) {
return font->calcWidth(text, offset, length);
}
int totalWidth = 0;
for (int i = offset; i < offset + length && i < text.length(); i++) {
auto codepoint = text[i];
if (!font->isPrintableChar(codepoint)) {
totalWidth += _glyphInterval;
} else if (auto glyph = font->getGlyph(codepoint)) {
totalWidth += glyph->xAdvance;
} else {
totalWidth += _glyphInterval;
}
if (offset >= text.length()) {
return 0;
}
return totalWidth;
return std::min(text.length() - offset, length) * _glyphInterval;
}
int Font::calcWidth(std::wstring_view text, size_t length) const {
@ -84,7 +80,24 @@ int Font::calcWidth(std::wstring_view text, size_t length) const {
}
int Font::calcWidth(std::wstring_view text, size_t offset, size_t length) const {
return std::min(text.length()-offset, length) * glyphInterval;
if (offset >= text.length()) {
return 0;
}
size_t end = offset + std::min(text.length() - offset, length);
int width = 0;
for (size_t i = offset; i < end; i++) {
width += getAdvance(text[i]);
}
return width;
}
int Font::getAdvance(uint codepoint) const {
if (isPrintableChar(codepoint)) {
if (auto glyph = getGlyph(codepoint)) {
return glyph->xAdvance;
}
}
return glyphInterval;
}
static inline void draw_glyph(
@ -176,7 +189,7 @@ static inline void draw_text(
int y = 0;
bool hasLines = false;
float baseAdvance = glm::length(right);
const float cellAdvance = interval * font.getLineHeight();
do {
for (size_t i = 0; i < text.length(); i++) {
@ -193,12 +206,11 @@ static inline void draw_text(
continue;
}
int yOffset = 0;
float advance = baseAdvance;
int xOffset = 0;
float advance = font.getAdvance(c) / cellAdvance;
if (auto glyph = font.getGlyph(c)) {
yOffset = glyph->yOffset;
advance = glyph->xAdvance /
static_cast<float>(font.getLineHeight()) * 2.0f *
baseAdvance;
xOffset = glyph->xOffset;
}
uint charpage = c >> 8;
if (charpage == page){
@ -207,7 +219,7 @@ static inline void draw_text(
batch,
pos,
glm::vec2(
x,
x + xOffset / cellAdvance,
y - yOffset * (is3d ? -1 : 1) /
static_cast<float>(font.getLineHeight())
),
@ -221,7 +233,7 @@ static inline void draw_text(
else if (charpage > page && charpage < next){
next = charpage;
}
x += advance / baseAdvance;
x += advance;
}
page = next;
next = MAX_CODEPAGES;
@ -325,27 +337,26 @@ std::unique_ptr<Font> Font::createBitmapFont(
return std::make_unique<Font>(std::move(textures), std::move(glyphs), res, 4);
}
const Glyph* Font::getGlyph(int codepoint) {
const Glyph* Font::getGlyph(int codepoint) const {
if (codepoint < 0) {
return nullptr;
}
if (codepoint < glyphs.size()) {
const int codepage = codepoint >> 8;
if (codepoint < glyphs.size() &&
(!fontFile.has_value() ||
(codepage < pages.size() && pages[codepage]))) {
return &glyphs.at(codepoint);
}
if (!this->fontFile.has_value() || this->fontFile->expired()) {
return nullptr;
}
int codepage = codepoint >> 8;
if (codepage >= 1024) {
return nullptr;
}
if (glyphs.size() < (codepage << 8)) {
glyphs.resize(codepage << 8);
}
auto fontFile = this->fontFile->lock();
if (pages.size() <= codepage) {
pages.resize(codepage);
pages.resize(codepage + 1);
}
pages.push_back(fontFile->renderPage(codepage, glyphs, lineHeight));
pages[codepage] = fontFile->renderPage(codepage, glyphs, lineHeight);
return &glyphs.at(codepoint);
}

View file

@ -26,6 +26,7 @@ namespace vector_fonts {
struct Glyph {
int yOffset;
int xAdvance;
int xOffset = 0;
};
class Font {
@ -56,6 +57,10 @@ public:
/// @return pixel width of the substring
int calcWidth(std::wstring_view text, size_t offset, size_t length) const;
/// @brief Get the horizontal advance of a character in pixels
/// @param codepoint character unicode codepoint
int getAdvance(uint codepoint) const;
/// @brief Check if character is visible (non-whitespace)
/// @param codepoint character unicode codepoint
bool isPrintableChar(uint codepoint) const;
@ -86,7 +91,7 @@ public:
return {std::nullopt, lineHeight, yoffset, glyphInterval};
}
const Glyph* getGlyph(int codepoint);
const Glyph* getGlyph(int codepoint) const;
static std::unique_ptr<Font> createBitmapFont(
std::vector<std::unique_ptr<ImageData>> pages
@ -96,7 +101,7 @@ private:
int yoffset;
int glyphInterval;
bool monospace = true;
std::vector<std::unique_ptr<Texture>> pages;
std::vector<Glyph> glyphs;
mutable std::vector<std::unique_ptr<Texture>> pages;
mutable std::vector<Glyph> glyphs;
std::optional<std::weak_ptr<vector_fonts::FontFile>> fontFile;
};

View file

@ -113,8 +113,6 @@ static inline auto shader_deleter = [](GLuint* shader) {
delete shader;
};
inline const uint GL_LOG_LEN = 512;
// shader should be deleted after shader program linking
using glshader = std::unique_ptr<GLuint, decltype(shader_deleter)>;
@ -125,12 +123,14 @@ glshader compile_shader(GLenum type, const GLchar* source, const std::string& fi
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[GL_LOG_LEN];
glGetShaderInfoLog(shader, GL_LOG_LEN, nullptr, infoLog);
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
auto infoLog = std::make_unique<GLchar[]>(infoLogLength);
glGetShaderInfoLog(shader, infoLogLength, nullptr, infoLog.get());
glDeleteShader(shader);
throw std::runtime_error(
"vertex shader compilation failed (" + file + "):\n" +
std::string(infoLog)
std::string(infoLog.get(), infoLogLength)
);
}
return glshader(new GLuint(shader), shader_deleter); //-V508
@ -172,10 +172,14 @@ static GLuint compile_program(
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
GLchar infoLog[GL_LOG_LEN];
glGetProgramInfoLog(program, GL_LOG_LEN, nullptr, infoLog);
GLint infoLogLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
auto infoLog = std::make_unique<GLchar[]>(infoLogLength);
glGetProgramInfoLog(program, infoLogLength, nullptr, infoLog.get());
glDeleteProgram(program);
throw std::runtime_error(
"shader program linking failed:\n" + std::string(infoLog)
"shader program linking failed:\n" +
std::string(infoLog.get(), infoLogLength)
);
}
return program;