forked from hoffstadt/DearPyGui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvFontItems.cpp
230 lines (184 loc) · 5.08 KB
/
mvFontItems.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "mvFontItems.h"
#include "mvTextureItems.h"
#include "mvPythonExceptions.h"
#include "mvLog.h"
#include "mvToolManager.h"
#include "mvFontManager.h"
mvFontRegistry::mvFontRegistry(mvUUID uuid)
:
mvAppItem(uuid)
{
config.show = true;
}
void mvFontRegistry::resetFont()
{
for (auto& item : childslots[1])
{
static_cast<mvFont*>(item.get())->_default = false;
}
mvToolManager::GetFontManager()._resetDefault = true;
}
void mvFontRegistry::draw(ImDrawList* drawlist, float x, float y)
{
//ImGuiIO& io = ImGui::GetIO();
//io.Fonts->Clear();
//io.FontDefault = io.Fonts->AddFontDefault();
for (auto& item : childslots[1])
{
item->draw(drawlist, ImGui::GetCursorPosX(), ImGui::GetCursorPosY());
}
config.show = false;
}
void mvCharRemap::handleSpecificRequiredArgs(PyObject* dict)
{
if (!VerifyRequiredArguments(GetParsers()[GetEntityCommand(type)], dict))
return;
_source = ToInt(PyTuple_GetItem(dict, 0));
_target = ToInt(PyTuple_GetItem(dict, 1));
}
void mvFont::customAction(void* data)
{
if (!state.ok)
return;
ImGuiIO& io = ImGui::GetIO();
_fontPtr = io.Fonts->AddFontFromFileTTF(_file.c_str(), _size,
nullptr, _ranges.Data);
if (_fontPtr == nullptr)
{
mvThrowPythonError(mvErrorCode::mvNone, "Font file could not be found");
io.Fonts->Build();
return;
}
// handled by char remaps
//for (auto& item : font.charRemaps)
// _fontPtr->AddRemapChar(item.first, item.second);
io.Fonts->Build();
if (_default)
io.FontDefault = _fontPtr;
// check ranges
for (const auto& range : childslots[1])
{
if (range->type == mvAppItemType::mvCharRemap)
{
const auto rangePtr = static_cast<const mvCharRemap*>(range.get());
_fontPtr->AddRemapChar(rangePtr->getSourceChar(), rangePtr->getTargetChar());
}
}
}
void mvFont::draw(ImDrawList* drawlist, float x, float y)
{
if (!state.ok)
return;
ImFontGlyphRangesBuilder builder;
static ImFontAtlas atlas;
// check hints
if (childslots[0].empty())
builder.AddRanges(atlas.GetGlyphRangesDefault());
for (const auto& hint : childslots[0])
{
int hintSelection = static_cast<mvFontRangeHint*>(hint.get())->getHint();
switch (hintSelection)
{
case 1:
builder.AddRanges(atlas.GetGlyphRangesJapanese());
break;
case 2:
builder.AddRanges(atlas.GetGlyphRangesKorean());
break;
case 3:
builder.AddRanges(atlas.GetGlyphRangesChineseFull());
break;
case 4:
builder.AddRanges(atlas.GetGlyphRangesChineseSimplifiedCommon());
break;
case 5:
builder.AddRanges(atlas.GetGlyphRangesCyrillic());
break;
case 6:
builder.AddRanges(atlas.GetGlyphRangesThai());
break;
case 7:
builder.AddRanges(atlas.GetGlyphRangesVietnamese());
break;
default:
builder.AddRanges(atlas.GetGlyphRangesDefault());
}
}
// check ranges and chars
for (const auto& range : childslots[1])
{
if (range->type == mvAppItemType::mvFontRange)
{
const auto rangePtr = static_cast<const mvFontRange*>(range.get());
builder.AddRanges(rangePtr->getRange().data());
}
else if (range->type == mvAppItemType::mvFontChars)
{
const auto rangePtr = static_cast<const mvFontChars*>(range.get());
for (const auto& specificChar : rangePtr->getCharacters())
builder.AddChar(specificChar);
}
}
builder.BuildRanges(&_ranges); // Build the final result (ordered ranges with all the unique characters submitted)
//_dirty = true;
auto item = GetItem(*GContext->itemRegistry, MV_ATLAS_UUID);
if (item)
static_cast<mvStaticTexture*>(item)->markDirty();
mvToolManager::GetFontManager()._dirty = true;
}
void mvFont::handleSpecificRequiredArgs(PyObject* dict)
{
if (!VerifyRequiredArguments(GetParsers()[GetEntityCommand(type)], dict))
return;
_file = ToString(PyTuple_GetItem(dict, 0));
std::ifstream ifile;
ifile.open(_file);
if (ifile)
ifile.close();
else
{
state.ok = false;
mvThrowPythonError(mvErrorCode::mvNone, "Font file could not be found");
}
_size = ToFloat(PyTuple_GetItem(dict, 1));
}
void mvFontChars::applySpecificTemplate(mvAppItem* item)
{
auto titem = static_cast<mvFontChars*>(item);
_chars = titem->_chars;
}
void mvFontChars::handleSpecificRequiredArgs(PyObject* dict)
{
if (!VerifyRequiredArguments(GetParsers()[GetEntityCommand(type)], dict))
return;
auto charVect = ToIntVect(PyTuple_GetItem(dict, 0));
for (auto& item : charVect)
_chars.push_back((ImWchar)item);
}
void mvFontRange::applySpecificTemplate(mvAppItem* item)
{
auto titem = static_cast<mvFontRange*>(item);
_min = titem->_min;
_max = titem->_max;
_range = titem->_range;
}
void mvFontRange::handleSpecificRequiredArgs(PyObject* dict)
{
if (!VerifyRequiredArguments(GetParsers()[GetEntityCommand(type)], dict))
return;
_min = ToInt(PyTuple_GetItem(dict, 0));
_range[0] = (ImWchar)_min;
_max = ToInt(PyTuple_GetItem(dict, 1));
_range[1] = (ImWchar)_max;
}
void mvFontRangeHint::applySpecificTemplate(mvAppItem* item)
{
auto titem = static_cast<mvFontRangeHint*>(item);
_hint = titem->_hint;
}
void mvFontRangeHint::handleSpecificRequiredArgs(PyObject* dict)
{
if (!VerifyRequiredArguments(GetParsers()[GetEntityCommand(type)], dict))
return;
_hint = ToInt(PyTuple_GetItem(dict, 0));
}