summaryrefslogtreecommitdiff
path: root/src/fonts.cpp
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrikboy@gmail.com>2024-03-16 17:30:52 +0100
committerAldrik Ramaekers <aldrikboy@gmail.com>2024-03-16 17:30:52 +0100
commit253c967318e6a75c6c4947b01f12ee3797db1b40 (patch)
tree662c8638ab0be7efec6793dc5e980a163425209d /src/fonts.cpp
parent4b917c469bffdb8f390d126934a60c2bbebca7b4 (diff)
load font & ranges based on pc locale
Diffstat (limited to 'src/fonts.cpp')
-rw-r--r--src/fonts.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/fonts.cpp b/src/fonts.cpp
new file mode 100644
index 0000000..3c1c520
--- /dev/null
+++ b/src/fonts.cpp
@@ -0,0 +1,94 @@
+#include "fonts.h"
+#include "../fonts/SourceSansProRegular.h"
+#include "../fonts/GmarketSans.h"
+#include "../fonts/NotoSansJP.h"
+#include "../fonts/NotoSerifTC.h"
+#include "../fonts/NotoSansSC.h"
+#include "../fonts/NotoSansThai.h"
+#include "imgui.h"
+
+#include <stdio.h>
+
+void ts_load_fonts(float size, ts_font_range locale) {
+ ImGuiIO& io = ImGui::GetIO();
+ ImFontConfig config;
+ config.MergeMode = true;
+
+ static const ImWchar arrow_r[] =
+ {
+ 0x2192, 0x2193, // → character.
+ 0,
+ };
+
+ static const ImWchar triangles[] =
+ {
+ 0x25B6, 0x25BC, // ▶ ▼ characters.
+ 0,
+ };
+
+ // We always use Source Sans Regular for the english, cyrillic and greek alphabet.
+ // other languages will be displayed in their respective font.
+
+ ImFontGlyphRangesBuilder builder;
+ ImVector<ImWchar> ranges;
+ builder.AddRanges(arrow_r);
+ builder.AddRanges(triangles);
+
+ builder.AddRanges(io.Fonts->GetGlyphRangesDefault());
+ if (locale == FONT_RANGE_GREEK)
+ builder.AddRanges(io.Fonts->GetGlyphRangesGreek());
+
+ if (locale == FONT_RANGE_CYRILLIC)
+ builder.AddRanges(io.Fonts->GetGlyphRangesCyrillic());
+
+ builder.BuildRanges(&ranges);
+
+ ImFont* font = io.Fonts->AddFontFromMemoryCompressedTTF(
+ SourceSansProRegular_compressed_data,
+ SourceSansProRegular_compressed_size,
+ size, nullptr, ranges.Data);
+
+ IM_ASSERT(font != nullptr);
+ io.FontDefault = font;
+
+ // Fonts can be found in fonts/ folder.
+ if (locale == FONT_RANGE_KOREAN) {
+ size /= 1.5f;
+ io.Fonts->AddFontFromMemoryCompressedTTF(
+ GmarketSans_compressed_data,
+ GmarketSans_compressed_size,
+ size, &config, io.Fonts->GetGlyphRangesKorean());
+ }
+ else if (locale == FONT_RANGE_JAPANESE) {
+ io.Fonts->AddFontFromMemoryCompressedTTF(
+ NotoSansJP_compressed_data,
+ NotoSansJP_compressed_size,
+ size, &config, io.Fonts->GetGlyphRangesJapanese());
+ }
+ else if (locale == FONT_RANGE_CHINESE_FULL) {
+ io.Fonts->AddFontFromMemoryCompressedTTF(
+ NotoSerifTC_compressed_data,
+ NotoSerifTC_compressed_size,
+ size, &config, io.Fonts->GetGlyphRangesChineseFull());
+ }
+ else if (locale == FONT_RANGE_CHINESE_SIMPLE) {
+ io.Fonts->AddFontFromMemoryCompressedTTF(
+ NotoSansSC_compressed_data,
+ NotoSansSC_compressed_size,
+ size, &config, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
+ }
+ else if (locale == FONT_RANGE_THAI) {
+ io.Fonts->AddFontFromMemoryCompressedTTF(
+ NotoSansThai_compressed_data,
+ NotoSansThai_compressed_size,
+ size, &config, io.Fonts->GetGlyphRangesThai());
+ }
+ else if (locale == FONT_RANGE_VIETNAMESE) {
+ io.Fonts->AddFontFromMemoryCompressedTTF(
+ NotoSansJP_compressed_data,
+ NotoSansJP_compressed_size,
+ size, &config, io.Fonts->GetGlyphRangesVietnamese());
+ }
+
+ io.Fonts->Build();
+} \ No newline at end of file