Added more default fonts, and fixed issue with font selector at different GUI scales

This commit is contained in:
Yousef Khadadeh 2025-11-15 14:05:05 +03:00
parent e451e8b181
commit 5e8016b504
19 changed files with 438 additions and 21 deletions

View file

@ -15,6 +15,10 @@
#include <include/ports/SkFontScanner_FreeType.h>
#endif
#include <src/base/SkUTF.h>
bool fallbackOnEmoji = false;
FontData::FontData()
{
#ifdef __EMSCRIPTEN__
@ -34,8 +38,14 @@ FontData::FontData()
map["Roboto"] = defaultFontMgr->makeFromFile("data/fonts/Roboto-variable.ttf");
collection = sk_make_sp<skia::textlayout::FontCollection>();
collection->setDefaultFontManager(defaultFontMgr, {SkString{"Roboto"}});
collection->setDefaultFontManager(defaultFontMgr, std::vector<SkString>{SkString{"Roboto"}, SkString{"Noto Emoji"}, SkString{"Noto Kufi Arabic"}});
collection->setDynamicFontManager(localFontMgr);
std::string s = "🙂";
const char* sPtr = s.data();
SkFontStyle defaultStyle;
SkString defaultLocale("en");
auto defaultEmojiFallback = collection->defaultEmojiFallback(SkUTF::NextUTF8(&sPtr, s.c_str() + s.length()), defaultStyle, defaultLocale);
fallbackOnEmoji = !defaultEmojiFallback;
}
FontData::~FontData() {
@ -47,3 +57,21 @@ Vector2f get_str_font_bounds(const SkFont& font, const std::string& str) {
float nextText = font.measureText(str.data(), str.length(), SkTextEncoding::kUTF8, nullptr);
return Vector2f{nextText, - metrics.fAscent + metrics.fDescent};
}
const std::vector<SkString>& get_default_font_families() {
static std::vector<SkString> defaultFontFamilies;
if(defaultFontFamilies.empty()) {
defaultFontFamilies.emplace_back("Roboto");
if(fallbackOnEmoji)
defaultFontFamilies.emplace_back("Noto Emoji");
defaultFontFamilies.emplace_back("Noto Kufi Arabic");
}
return defaultFontFamilies;
}
void push_default_font_families(std::vector<SkString>& fontFamilies) {
fontFamilies.emplace_back("Roboto");
if(fallbackOnEmoji)
fontFamilies.emplace_back("Noto Emoji");
fontFamilies.emplace_back("Noto Kufi Arabic");
}

View file

@ -18,3 +18,5 @@ struct FontData {
};
Vector2f get_str_font_bounds(const SkFont& font, const std::string& str);
const std::vector<SkString>& get_default_font_families();
void push_default_font_families(std::vector<SkString>& fontFamilies);

View file

@ -140,13 +140,13 @@ bool GUIStuff::FontPicker::update(UpdateInputData& io, std::string* fontName, GU
entryColor = io.theme->backColor1;
CLAY({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)},
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(25)},
.padding = {.left = 2, .right = 2},
.childAlignment = {.x = CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER}
},
.backgroundColor = convert_vec4<Clay_Color>(entryColor)
}) {
gui->text_paragraph("font name", std::move(p), std::numeric_limits<float>::max());
gui->text_paragraph("font name", std::move(p), 10000.0f); // Setting width to numeric_limits::max will lead to multiplying it by any number greater than 1 to = infinity, which will cause issues
if(io.mouse.leftClick && Clay_Hovered()) {
*fontName = sortedFontList[i];

View file

@ -3,6 +3,7 @@
#include "../../RichText/TextBox.hpp"
#include <limits>
#include <modules/skparagraph/include/TextStyle.h>
#include "../../FontData.hpp"
namespace GUIStuff {
@ -17,14 +18,13 @@ template <typename T> class TextBox : public Element {
data = newData;
fromStr = newFromStr;
toStr = newToStr;
singleLine = newSingleLine;
force_update_textbox(io, false);
textbox->set_width(singleLine ? std::numeric_limits<float>::max() : bb.width());
textbox->set_width(std::numeric_limits<float>::max());
CLAY({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(static_cast<float>(io.fontSize * 2)), .height = singleLine ? CLAY_SIZING_FIXED(static_cast<float>(io.fontSize * 1.25f)) : CLAY_SIZING_GROW(static_cast<float>(io.fontSize * 1.25f))}
.sizing = {.width = CLAY_SIZING_GROW(static_cast<float>(io.fontSize * 2)), .height = CLAY_SIZING_FIXED(static_cast<float>(io.fontSize * 1.25f))}
},
.custom = { .customData = this }
}) {
@ -71,7 +71,9 @@ template <typename T> class TextBox : public Element {
canvas->clipRect(SkRect::MakeXYWH(bb.min.x(), bb.min.y(), bb.width(), bb.height()));
canvas->translate(bb.min.x() + 2.0f, bb.min.y());
float yOffset = bb.height() * 0.5f;
yOffset -= textbox->get_height() * 0.5f;
canvas->translate(bb.min.x() + 2.0f, bb.min.y() + yOffset);
RichText::TextBox::PaintOpts paintOpts;
if(selection.selected)
@ -79,9 +81,7 @@ template <typename T> class TextBox : public Element {
paintOpts.cursorColor = {io.theme->fillColor1.fR, io.theme->fillColor1.fG, io.theme->fillColor1.fB};
skia::textlayout::TextStyle tStyle;
std::vector<SkString> families;
families.emplace_back("Roboto");
tStyle.setFontFamilies(families);
tStyle.setFontFamilies(get_default_font_families());
tStyle.setFontSize(io.fontSize);
tStyle.setForegroundPaint(SkPaint{io.theme->frontColor1});
textbox->set_initial_text_style(tStyle);
@ -99,7 +99,7 @@ template <typename T> class TextBox : public Element {
textbox = std::make_shared<RichText::TextBox>();
textbox->set_font_collection(io.fontCollection);
textbox->set_allow_newlines(!singleLine);
textbox->set_allow_newlines(false);
cur = std::make_shared<RichText::TextBox::Cursor>();
@ -120,8 +120,6 @@ template <typename T> class TextBox : public Element {
T* data = nullptr;
T oldData;
bool singleLine = true;
std::function<std::optional<T>(const std::string&)> fromStr;
std::function<std::string(const T&)> toStr;

View file

@ -24,6 +24,7 @@
#include <modules/skparagraph/include/TextStyle.h>
#include <modules/skunicode/include/SkUnicode_icu.h>
#include <Helpers/Random.hpp>
#include "../FontData.hpp"
namespace GUIStuff {
@ -43,7 +44,7 @@ Clay_Dimensions GUIManager::clay_skia_measure_text(Clay_StringSlice str, Clay_Te
pStyle.setTextAlign(skia::textlayout::TextAlign::kLeft);
skia::textlayout::TextStyle tStyle;
tStyle.setFontSize(config->fontSize);
tStyle.setFontFamilies({SkString{"Roboto"}});
tStyle.setFontFamilies(get_default_font_families());
pStyle.setTextStyle(tStyle);
skia::textlayout::ParagraphBuilderImpl a(pStyle, window->io->fontCollection, SkUnicodes::ICU::Make());
@ -116,7 +117,7 @@ void GUIManager::draw(SkCanvas* canvas) {
pStyle.setTextAlign(skia::textlayout::TextAlign::kLeft);
skia::textlayout::TextStyle tStyle;
tStyle.setFontSize(config->fontSize);
tStyle.setFontFamilies({SkString{"Roboto"}});
tStyle.setFontFamilies(get_default_font_families());
tStyle.setColor(convert_vec4<SkColor4f>(config->textColor).toSkColor());
pStyle.setTextStyle(tStyle);

View file

@ -19,7 +19,6 @@
using namespace Eigen;
namespace GUIStuff {
class GUIManager {

View file

@ -52,8 +52,8 @@ InputManager::InputManager() {
// Without this, SDL eats the CTRL-V event that initiates the paste event
// https://github.com/pthom/hello_imgui/issues/3#issuecomment-1564536870
EM_ASM({
window.addEventListener('keydown', function(event){
if (event.ctrlKey && event.key == 'v') {
window.addEventListener('keydown', function(event) {
if((event.ctrlKey || event.metaKey) && (event.key == 'v' || event.code == 'KeyV')) {
if(Module["ccall"]('is_text_input_happening', 'number', [], []) === 1)
event.stopImmediatePropagation();
}

View file

@ -771,6 +771,11 @@ void TextBox::set_width(float newWidth) {
}
}
float TextBox::get_height() {
rebuild();
return paragraphs.back().heightOffset + paragraphs.back().p->getHeight();
}
void TextBox::set_allow_newlines(bool allow) {
newlinesAllowed = allow;
}
@ -795,6 +800,16 @@ void TextBox::rebuild() {
ParagraphData& pData = paragraphs[pIndex];
skia::textlayout::ParagraphStyle pStyle = pData.pStyleData.get_paragraph_style();
pStyle.setTextStyle(tStyle);
if(!newlinesAllowed) {
pStyle.setTextHeightBehavior(skia::textlayout::kDisableAll);
skia::textlayout::StrutStyle strutStyle;
strutStyle.setStrutEnabled(true);
strutStyle.setForceStrutHeight(true);
pStyle.setStrutStyle(strutStyle);
}
size_t tIndex = 0;
skia::textlayout::ParagraphBuilderImpl a(pStyle, fontCollection, SkUnicodes::ICU::Make());

View file

@ -99,6 +99,7 @@ class TextBox {
void set_font_collection(const sk_sp<skia::textlayout::FontCollection>& fC);
void paint(SkCanvas* canvas, const PaintOpts& paintOpts);
void set_allow_newlines(bool allow);
float get_height();
enum class InputKey {
LEFT,

View file

@ -3,6 +3,7 @@
#include <Helpers/Serializers.hpp>
#include <include/core/SkFontStyle.h>
#include <modules/skparagraph/include/TextStyle.h>
#include "../FontData.hpp"
#define WEIGHT_VALUE_MODIFIER 100
@ -190,7 +191,7 @@ void FontFamiliesTextStyleModifier::load(cereal::PortableBinaryInputArchive& a)
void FontFamiliesTextStyleModifier::modify_text_style(skia::textlayout::TextStyle& style) const {
auto familiesToPush = families;
familiesToPush.emplace_back("Roboto");
push_default_font_families(familiesToPush);
style.setFontFamilies(familiesToPush);
}

View file

@ -1,6 +1,7 @@
#include "Toolbar.hpp"
#include "DrawingProgram/DrawingProgramToolBase.hpp"
#include "DrawingProgram/ScreenshotTool.hpp"
#include "FontData.hpp"
#include "Helpers/ConvertVec.hpp"
#include "Helpers/FileDownloader.hpp"
#include "Helpers/Networking/NetLibrary.hpp"
@ -823,7 +824,7 @@ std::unique_ptr<skia::textlayout::Paragraph> Toolbar::build_paragraph_from_chat_
pStyle.setTextAlign(skia::textlayout::TextAlign::kLeft);
skia::textlayout::TextStyle tStyle;
tStyle.setFontSize(io->fontSize);
tStyle.setFontFamilies({SkString{"Roboto"}});
tStyle.setFontFamilies(get_default_font_families());
tStyle.setFontStyle(SkFontStyle::Bold());
tStyle.setForegroundColor(SkPaint{color_mul_alpha(message.type == ChatMessage::JOIN ? io->theme->warningColor : io->theme->frontColor1, alpha)});
pStyle.setTextStyle(tStyle);
@ -1400,7 +1401,6 @@ void Toolbar::options_menu() {
gui.push_id("general settings");
gui.input_text_field("display name input", "Display Name", &main.displayName);
main.update_display_names();
gui.text_label_light("Note: Changes in display name dont affect canvases that are connected online");
SkColor4f newBackColor{main.defaultCanvasBackgroundColor.x(), main.defaultCanvasBackgroundColor.y(), main.defaultCanvasBackgroundColor.z(), 1.0f};
gui.color_picker_button_field("defaultCanvasBackgroundColor", "Default canvas background color", &newBackColor, false);
main.defaultCanvasBackgroundColor = convert_vec3<Vector3f>(newBackColor);