1 /*
2     Copyright © 2020, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module creator.core.font;
8 import creator.core;
9 import bindbc.imgui;
10 import core.stdc.stdlib : malloc;
11 import core.stdc.string : memcpy;
12 import std.string;
13 import std.array;
14 
15 private {
16 
17 
18     // ImFont* loadFont(ImFontAtlas* atlas, string name, ubyte[] data, uint size = 14, ImWchar* range = null, bool merge = false) {
19 
20     //     ubyte* c_data = cast(ubyte*)igMemAlloc(data.length);
21     //     memcpy(c_data, data.ptr, data.length);
22 
23     //     if (range is null) {
24     //         range = ImFontAtlas_GetGlyphRangesJapanese(atlas);
25     //     }
26     //     ImFontConfig* cfg = ImFontConfig_ImFontConfig();
27     //     cfg.MergeMode = merge;
28 
29     //     // Add name
30     //     const char* c_name = cast(char*)igMemAlloc(name.length);
31     //     memcpy(cast(void*)c_name, name.ptr, name.length);
32     //     cfg.Name[0..name.length] = c_name[0..name.length];
33 
34     //     // Load Font
35     //     ImFont* font = ImFontAtlas_AddFontDefault(atlas, cfg); //ImFontAtlas_AddFontFromMemoryTTF(atlas, c_data, size, size, cfg, range);
36 
37     //     return font;
38     // }
39 }
40 
41 private {
42     ImFontAtlas* atlas;
43 
44     FontEntry[] families;
45     void _incInitFontList() {
46         string fontsPath = incGetAppFontsPath();
47         // TODO: load fonts
48     }
49 
50     void _incAddFontData(string name, ref ubyte[] data, float size = 14, const ImWchar* ranges = null, ImVec2 offset = ImVec2(0f, 0f)) {
51         auto cfg = ImFontConfig_ImFontConfig();
52         cfg.FontDataOwnedByAtlas = false;
53         cfg.MergeMode = atlas.Fonts.empty() ? false : true;
54         cfg.GlyphOffset = offset;
55 
56         char[40] nameDat;
57         nameDat[0..name.length] = name[0..name.length];
58         cfg.Name = nameDat;
59         ImFontAtlas_AddFontFromMemoryTTF(atlas, cast(void*)data.ptr, cast(int)data.length, size, cfg, ranges);
60     }
61 
62     ubyte[] KOSUGI_MARU = cast(ubyte[])import("KosugiMaru-Regular.ttf");
63     ubyte[] ICONS = cast(ubyte[])import("MaterialIcons.ttf");
64 }
65 
66 /**
67     A font entry in the fonts list
68 */
69 struct FontEntry {
70     /**
71         Family name of the font
72     */
73     string name;
74     
75     /**
76         Main language of the font
77     */
78     string lang;
79 
80     /**
81         The file of the font
82     */
83     string file;
84 }
85 
86 /**
87     Initializes fonts
88 */
89 void incInitFonts() {
90     _incInitFontList();
91     atlas = igGetIO().Fonts;
92         _incAddFontData("APP\0", KOSUGI_MARU, 28, ImFontAtlas_GetGlyphRangesJapanese(atlas));
93         _incAddFontData("Icons\0", ICONS, 32, [cast(ImWchar)0xE000, cast(ImWchar)0xF23B].ptr, ImVec2(0, 4));
94     ImFontAtlas_Build(atlas);
95     incSetUIScale(incGetUIScale());
96 }
97 
98 /**
99     Sets the UI scale for fonts
100 */
101 void incSetUIScale(float scale) {
102     incSettingsSet("UIScale", scale);
103     igGetIO().DisplayFramebufferScale = ImVec2(scale, scale);
104     igGetIO().FontGlobalScale = 0.5f;
105 }
106 
107 /**
108     Get the UI scale in terms of font size
109 */
110 float incGetUIScaleFont() {
111     return incGetUIScale()/2;
112 }
113 
114 /**
115     Returns the UI Scale
116 */
117 float incGetUIScale() {
118     return incSettingsGet!float("UIScale", 1.0);
119 }
120 
121 /**
122     Gets the UI scale in text form
123 */
124 string incGetUIScaleText() {
125     import std.format : format;
126     return "%s%%".format(cast(int)(incGetUIScale()*100));
127 }
128 
129 /**
130     Begins a section where text is double size
131 */
132 void incFontsBeginLarge() {
133     igGetIO().FontGlobalScale = incGetUIScaleFont()*2;
134 }
135 
136 /**
137     Ends a section where text is double size
138 */
139 void incFontsEndLarge() {
140     igGetIO().FontGlobalScale = incGetUIScaleFont();
141 }
142 
143 /**
144     Returns a list of fonts
145 */
146 FontEntry[] incFontsGet() {
147     return families;
148 }
149 
150 // void incFontSet(string file) {
151 
152 // }