2023-03-14 20:18:33 +00:00
|
|
|
#include <cstdint>
|
2020-09-06 08:30:57 +00:00
|
|
|
#include "overlay.h"
|
|
|
|
#include "file_utils.h"
|
|
|
|
#include "font_default.h"
|
2021-04-13 00:13:07 +00:00
|
|
|
#include "IconsForkAwesome.h"
|
2021-04-13 22:49:00 +00:00
|
|
|
#include "forkawesome.h"
|
2022-07-28 20:05:58 +00:00
|
|
|
|
|
|
|
void create_fonts(ImFontAtlas* font_atlas, const overlay_params& params, ImFont*& small_font, ImFont*& text_font)
|
2020-09-06 08:30:57 +00:00
|
|
|
{
|
|
|
|
auto& io = ImGui::GetIO();
|
2022-07-28 20:05:58 +00:00
|
|
|
if (!font_atlas)
|
|
|
|
font_atlas = io.Fonts;
|
|
|
|
font_atlas->Clear();
|
|
|
|
|
2020-09-06 08:30:57 +00:00
|
|
|
ImGui::GetIO().FontGlobalScale = params.font_scale; // set here too so ImGui::CalcTextSize is correct
|
|
|
|
float font_size = params.font_size;
|
|
|
|
if (font_size < FLT_EPSILON)
|
|
|
|
font_size = 24;
|
|
|
|
|
|
|
|
float font_size_text = params.font_size_text;
|
|
|
|
if (font_size_text < FLT_EPSILON)
|
|
|
|
font_size_text = font_size;
|
|
|
|
static const ImWchar default_range[] =
|
|
|
|
{
|
|
|
|
0x0020, 0x00FF, // Basic Latin + Latin Supplement
|
2021-06-22 16:55:48 +00:00
|
|
|
0x2018, 0x201F, // Bunch of quotation marks
|
2020-09-06 08:30:57 +00:00
|
|
|
//0x0100, 0x017F, // Latin Extended-A
|
|
|
|
//0x2103, 0x2103, // Degree Celsius
|
|
|
|
//0x2109, 0x2109, // Degree Fahrenheit
|
|
|
|
0,
|
|
|
|
};
|
2021-04-13 02:36:41 +00:00
|
|
|
// Load Icon file and merge to exisitng font
|
|
|
|
ImFontConfig config;
|
|
|
|
config.MergeMode = true;
|
|
|
|
static const ImWchar icon_ranges[] = { ICON_MIN_FK, ICON_MAX_FK, 0 };
|
2020-09-06 08:30:57 +00:00
|
|
|
|
|
|
|
ImVector<ImWchar> glyph_ranges;
|
|
|
|
ImFontGlyphRangesBuilder builder;
|
2022-07-28 20:05:58 +00:00
|
|
|
builder.AddRanges(font_atlas->GetGlyphRangesDefault());
|
2020-09-06 08:30:57 +00:00
|
|
|
if (params.font_glyph_ranges & FG_KOREAN)
|
2022-07-28 20:05:58 +00:00
|
|
|
builder.AddRanges(font_atlas->GetGlyphRangesKorean());
|
2020-09-06 08:30:57 +00:00
|
|
|
if (params.font_glyph_ranges & FG_CHINESE_FULL)
|
2022-07-28 20:05:58 +00:00
|
|
|
builder.AddRanges(font_atlas->GetGlyphRangesChineseFull());
|
2020-09-06 08:30:57 +00:00
|
|
|
if (params.font_glyph_ranges & FG_CHINESE_SIMPLIFIED)
|
2022-07-28 20:05:58 +00:00
|
|
|
builder.AddRanges(font_atlas->GetGlyphRangesChineseSimplifiedCommon());
|
2020-09-06 08:30:57 +00:00
|
|
|
if (params.font_glyph_ranges & FG_JAPANESE)
|
2022-07-28 20:05:58 +00:00
|
|
|
builder.AddRanges(font_atlas->GetGlyphRangesJapanese()); // Not exactly Shift JIS compatible?
|
2020-09-06 08:30:57 +00:00
|
|
|
if (params.font_glyph_ranges & FG_CYRILLIC)
|
2022-07-28 20:05:58 +00:00
|
|
|
builder.AddRanges(font_atlas->GetGlyphRangesCyrillic());
|
2020-09-06 08:30:57 +00:00
|
|
|
if (params.font_glyph_ranges & FG_THAI)
|
2022-07-28 20:05:58 +00:00
|
|
|
builder.AddRanges(font_atlas->GetGlyphRangesThai());
|
2020-09-06 08:30:57 +00:00
|
|
|
if (params.font_glyph_ranges & FG_VIETNAMESE)
|
2022-07-28 20:05:58 +00:00
|
|
|
builder.AddRanges(font_atlas->GetGlyphRangesVietnamese());
|
2020-09-06 08:30:57 +00:00
|
|
|
if (params.font_glyph_ranges & FG_LATIN_EXT_A) {
|
2020-11-16 13:51:37 +00:00
|
|
|
constexpr ImWchar latin_ext_a[] { 0x0100, 0x017F, 0 };
|
2020-09-06 08:30:57 +00:00
|
|
|
builder.AddRanges(latin_ext_a);
|
|
|
|
}
|
|
|
|
if (params.font_glyph_ranges & FG_LATIN_EXT_B) {
|
2020-11-16 13:51:37 +00:00
|
|
|
constexpr ImWchar latin_ext_b[] { 0x0180, 0x024F, 0 };
|
2020-09-06 08:30:57 +00:00
|
|
|
builder.AddRanges(latin_ext_b);
|
|
|
|
}
|
|
|
|
builder.BuildRanges(&glyph_ranges);
|
|
|
|
|
|
|
|
bool same_font = (params.font_file == params.font_file_text || params.font_file_text.empty());
|
|
|
|
bool same_size = (font_size == font_size_text);
|
|
|
|
|
|
|
|
// ImGui takes ownership of the data, no need to free it
|
|
|
|
if (!params.font_file.empty() && file_exists(params.font_file)) {
|
2022-07-28 20:05:58 +00:00
|
|
|
font_atlas->AddFontFromFileTTF(params.font_file.c_str(), font_size, nullptr, same_font && same_size ? glyph_ranges.Data : default_range);
|
|
|
|
font_atlas->AddFontFromMemoryCompressedBase85TTF(forkawesome_compressed_data_base85, font_size, &config, icon_ranges);
|
2020-09-23 11:02:27 +00:00
|
|
|
if (params.no_small_font)
|
2022-07-28 20:05:58 +00:00
|
|
|
small_font = font_atlas->Fonts[0];
|
2021-04-14 01:03:26 +00:00
|
|
|
else {
|
2022-07-28 20:05:58 +00:00
|
|
|
small_font = font_atlas->AddFontFromFileTTF(params.font_file.c_str(), font_size * 0.55f, nullptr, default_range);
|
|
|
|
font_atlas->AddFontFromMemoryCompressedBase85TTF(forkawesome_compressed_data_base85, font_size * 0.55f, &config, icon_ranges);
|
2021-04-14 01:03:26 +00:00
|
|
|
}
|
2020-09-06 08:30:57 +00:00
|
|
|
} else {
|
|
|
|
const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85();
|
2022-07-28 20:05:58 +00:00
|
|
|
font_atlas->AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, font_size, nullptr, default_range);
|
|
|
|
font_atlas->AddFontFromMemoryCompressedBase85TTF(forkawesome_compressed_data_base85, font_size, &config, icon_ranges);
|
2020-09-23 11:02:27 +00:00
|
|
|
if (params.no_small_font)
|
2022-07-28 20:05:58 +00:00
|
|
|
small_font = font_atlas->Fonts[0];
|
2021-04-14 01:03:26 +00:00
|
|
|
else {
|
2022-07-28 20:05:58 +00:00
|
|
|
small_font = font_atlas->AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, font_size * 0.55f, nullptr, default_range);
|
|
|
|
font_atlas->AddFontFromMemoryCompressedBase85TTF(forkawesome_compressed_data_base85, font_size * 0.55f, &config, icon_ranges);
|
2021-04-14 01:03:26 +00:00
|
|
|
}
|
2020-09-06 08:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto font_file_text = params.font_file_text;
|
|
|
|
if (font_file_text.empty())
|
|
|
|
font_file_text = params.font_file;
|
|
|
|
|
|
|
|
if ((!same_font || !same_size) && file_exists(font_file_text))
|
2022-07-28 20:05:58 +00:00
|
|
|
text_font = font_atlas->AddFontFromFileTTF(font_file_text.c_str(), font_size_text, nullptr, glyph_ranges.Data);
|
2020-09-06 08:30:57 +00:00
|
|
|
else
|
2022-07-28 20:05:58 +00:00
|
|
|
text_font = font_atlas->Fonts[0];
|
2020-09-06 08:30:57 +00:00
|
|
|
|
2022-07-28 20:05:58 +00:00
|
|
|
font_atlas->Build();
|
2020-11-16 13:51:37 +00:00
|
|
|
}
|