Renamed FontAtlas to Fonts. Further cleanup/comments.

This commit is contained in:
ocornut 2015-01-18 10:46:49 +00:00
parent 1f8d209202
commit 1916a0c78c
6 changed files with 103 additions and 111 deletions

View File

@ -321,7 +321,7 @@ void CleanupDevice()
// InitImGui // InitImGui
if (g_pFontSampler) g_pFontSampler->Release(); if (g_pFontSampler) g_pFontSampler->Release();
if (ID3D11ShaderResourceView* font_texture_view = (ID3D11ShaderResourceView*)ImGui::GetIO().FontAtlas->TexID) if (ID3D11ShaderResourceView* font_texture_view = (ID3D11ShaderResourceView*)ImGui::GetIO().Fonts->TexID)
font_texture_view->Release(); font_texture_view->Release();
if (g_pVB) g_pVB->Release(); if (g_pVB) g_pVB->Release();
@ -377,17 +377,17 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hWnd, msg, wParam, lParam); return DefWindowProc(hWnd, msg, wParam, lParam);
} }
void LoadFontTexture() void LoadFontsTexture()
{ {
// Load one or more font // Load one or more font
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
//ImFont* my_font = io.FontAtlas->AddFontDefault(); //ImFont* my_font = io.Fonts->AddFontDefault();
//ImFont* my_font2 = io.FontAtlas->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, ImFontAtlas::GetGlyphRangesJapanese()); //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, io.Fonts->GetGlyphRangesJapanese());
// Build // Build
unsigned char* pixels; unsigned char* pixels;
int width, height; int width, height;
io.FontAtlas->GetTexDataAsRGBA32(&pixels, &width, &height); io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
// Create texture // Create texture
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
@ -421,7 +421,7 @@ void LoadFontTexture()
pTexture->Release(); pTexture->Release();
// Store our identifier // Store our identifier
io.FontAtlas->TexID = (void *)font_texture_view; io.Fonts->TexID = (void *)font_texture_view;
} }
void InitImGui() void InitImGui()
@ -470,7 +470,7 @@ void InitImGui()
} }
// Load fonts // Load fonts
LoadFontTexture(); LoadFontsTexture();
// Create texture sampler // Create texture sampler
{ {

View File

@ -129,7 +129,7 @@ void CleanupDevice()
if (g_pVB) g_pVB->Release(); if (g_pVB) g_pVB->Release();
// InitDeviceD3D // InitDeviceD3D
if (LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)ImGui::GetIO().FontAtlas->TexID) if (LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)ImGui::GetIO().Fonts->TexID)
tex->Release(); tex->Release();
if (g_pd3dDevice) g_pd3dDevice->Release(); if (g_pd3dDevice) g_pd3dDevice->Release();
if (g_pD3D) g_pD3D->Release(); if (g_pD3D) g_pD3D->Release();
@ -173,17 +173,17 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hWnd, msg, wParam, lParam); return DefWindowProc(hWnd, msg, wParam, lParam);
} }
void LoadFontTexture() void LoadFontsTexture()
{ {
// Load one or more font // Load one or more font
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
//ImFont* my_font = io.FontAtlas->AddFontDefault(); //ImFont* my_font = io.Fonts->AddFontDefault();
//ImFont* my_font2 = io.FontAtlas->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, ImFontAtlas::GetGlyphRangesJapanese()); //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, io.Fonts->GetGlyphRangesJapanese());
// Build // Build
unsigned char* pixels; unsigned char* pixels;
int width, height, bytes_per_pixel; int width, height, bytes_per_pixel;
io.FontAtlas->GetTexDataAsAlpha8(&pixels, &width, &height, &bytes_per_pixel); io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height, &bytes_per_pixel);
// Create texture // Create texture
LPDIRECT3DTEXTURE9 pTexture = NULL; LPDIRECT3DTEXTURE9 pTexture = NULL;
@ -205,7 +205,7 @@ void LoadFontTexture()
pTexture->UnlockRect(0); pTexture->UnlockRect(0);
// Store our identifier // Store our identifier
io.FontAtlas->TexID = (void *)pTexture; io.Fonts->TexID = (void *)pTexture;
} }
void InitImGui() void InitImGui()
@ -245,7 +245,7 @@ void InitImGui()
return; return;
} }
LoadFontTexture(); LoadFontsTexture();
} }
INT64 ticks_per_second = 0; INT64 ticks_per_second = 0;

View File

@ -235,15 +235,15 @@ void InitGL()
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
void LoadFontTexture() void LoadFontsTexture()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
//ImFont* my_font = io.FontAtlas->AddFontDefault(); //ImFont* my_font = io.Fonts->AddFontDefault();
//ImFont* my_font2 = io.FontAtlas->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, ImFontAtlas::GetGlyphRangesJapanese()); //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, io.Fonts->GetGlyphRangesJapanese());
unsigned char* pixels; unsigned char* pixels;
int width, height; int width, height;
io.FontAtlas->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits for OpenGL3 demo because it is more likely to be compatible with user's existing shader. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits for OpenGL3 demo because it is more likely to be compatible with user's existing shader.
GLuint tex_id; GLuint tex_id;
glGenTextures(1, &tex_id); glGenTextures(1, &tex_id);
@ -253,7 +253,7 @@ void LoadFontTexture()
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// Store our identifier // Store our identifier
io.FontAtlas->TexID = (void *)(intptr_t)tex_id; io.Fonts->TexID = (void *)(intptr_t)tex_id;
} }
void InitImGui() void InitImGui()
@ -282,7 +282,7 @@ void InitImGui()
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
LoadFontTexture(); LoadFontsTexture();
} }
void UpdateImGui() void UpdateImGui()

View File

@ -145,15 +145,15 @@ void InitGL()
glewInit(); glewInit();
} }
void LoadFontTexture() void LoadFontsTexture()
{ {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
//ImFont* my_font = io.FontAtlas->AddFontDefault(); //ImFont* my_font = io.Fonts->AddFontDefault();
//ImFont* my_font2 = io.FontAtlas->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, ImFontAtlas::GetGlyphRangesJapanese()); //ImFont* my_font2 = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 20.0f, io.Fonts->GetGlyphRangesJapanese());
unsigned char* pixels; unsigned char* pixels;
int width, height; int width, height;
io.FontAtlas->GetTexDataAsAlpha8(&pixels, &width, &height); io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
GLuint tex_id; GLuint tex_id;
glGenTextures(1, &tex_id); glGenTextures(1, &tex_id);
@ -163,7 +163,7 @@ void LoadFontTexture()
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
// Store our identifier // Store our identifier
io.FontAtlas->TexID = (void *)(intptr_t)tex_id; io.Fonts->TexID = (void *)(intptr_t)tex_id;
} }
void InitImGui() void InitImGui()
@ -191,6 +191,8 @@ void InitImGui()
io.RenderDrawListsFn = ImImpl_RenderDrawLists; io.RenderDrawListsFn = ImImpl_RenderDrawLists;
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
LoadFontsTexture();
} }
void UpdateImGui() void UpdateImGui()

View File

@ -90,9 +90,9 @@
// Load texture // Load texture
unsigned char* pixels; unsigned char* pixels;
int width, height; int width, height;
io.FontAtlas->GetTexDataAsRGBA32(pixels, &width, &height); io.Fonts->GetTexDataAsRGBA32(pixels, &width, &height);
// TODO: copy texture to graphics memory. // TODO: copy texture to graphics memory.
// TODO: store your texture pointer/identifier in 'io.FontAtlas->TexID' // TODO: store your texture pointer/identifier in 'io.Fonts->TexID'
// Application main loop // Application main loop
while (true) while (true)
@ -129,7 +129,7 @@
Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code. Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code.
- 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader. - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader.
(1.30) - removed GetDefaultFontData(). uses io.FontAtlas->GetTextureData*() API to retrieve uncompressed pixels. (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels.
this sequence: this sequence:
const void* png_data; const void* png_data;
unsigned int png_size; unsigned int png_size;
@ -138,11 +138,11 @@
became: became:
unsigned char* pixels; unsigned char* pixels;
int width, height; int width, height;
io.FontAtlas->GetTexDataAsRGBA32(&pixels, &width, &height); io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
// <Copy to GPU> // <Copy to GPU>
io.FontAtlas->TexID = (your_texture_identifier); io.Fonts->TexID = (your_texture_identifier);
but we now have much more flexibility to load multiple TTF fonts and manage the texture buffer with the FontAtlas. but we now have much more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs.
(1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.FontAtlas->TexID. (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID.
(1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix) (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix)
- 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver) - 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver)
- 2014/11/28 (1.17) - moved IO.Font*** options to inside the IO.Font-> structure. - 2014/11/28 (1.17) - moved IO.Font*** options to inside the IO.Font-> structure.
@ -183,23 +183,23 @@
If you want to load a different font than the default (ProggyClean.ttf, size 13) If you want to load a different font than the default (ProggyClean.ttf, size 13)
io.FontAtlas.AddFontFromFileTTF("myfontfile.ttf", size_in_pixels); io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels);
io.FontAtlas.GetTexDataAs****() io.Fonts->GetTexDataAs****()
If you want to load multiple fonts, use the FontAtlas to pack them into a single texture! If you want to load multiple fonts, use the font atlas to pack them into a single texture!
ImFont* font0 = io.FontAtlas.AddFontDefault(); ImFont* font0 = io.Fonts->AddFontDefault();
ImFont* font1 = io.FontAtlas.AddFontFromFileTTF("myfontfile.ttf", size_in_pixels); ImFont* font1 = io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels);
io.FontAtlas.GetTexDataAs****() io.Fonts->GetTexDataAs****()
If you want to display Chinese, Japanese, Korean characters, pass custom Unicode ranges when loading a font: If you want to display Chinese, Japanese, Korean characters, pass custom Unicode ranges when loading a font:
io.FontAtlas.AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, ImFontAtlas::GetGlyphRangesJapanese()); // Load Japanese characters io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, io.Fonts->GetGlyphRangesJapanese()); // Load Japanese characters
io.FontAtlas.GetTexDataAs****() io.Fonts->GetTexDataAs****()
If you want to input Japanese/Chinese/Korean in the text input widget: If you want to input Japanese/Chinese/Korean in the text input widget:
- when loading the font, pass a range that contains the characters you need, e.g.: ImFont::GetGlyphRangesJapanese() - when loading the font, pass a range that contains the characters you need, e.g.: io.Fonts->GetGlyphRangesJapanese()
- to have the Microsoft IME cursor appears at the right location in the screen, setup a handler for the io.ImeSetInputScreenPosFn function: - to have the Microsoft IME cursor appears at the right location in the screen, setup a handler for the io.ImeSetInputScreenPosFn function:
#include <Windows.h> #include <Windows.h>
@ -484,7 +484,7 @@ ImGuiIO::ImGuiIO()
IniSavingRate = 5.0f; IniSavingRate = 5.0f;
IniFilename = "imgui.ini"; IniFilename = "imgui.ini";
LogFilename = "imgui_log.txt"; LogFilename = "imgui_log.txt";
FontAtlas = &GDefaultFontAtlas; Fonts = &GDefaultFontAtlas;
FontGlobalScale = 1.0f; FontGlobalScale = 1.0f;
FontAllowUserScaling = false; FontAllowUserScaling = false;
MousePos = ImVec2(-1,-1); MousePos = ImVec2(-1,-1);
@ -1564,8 +1564,8 @@ void ImGui::NewFrame()
IM_ASSERT(g.IO.DeltaTime > 0.0f); IM_ASSERT(g.IO.DeltaTime > 0.0f);
IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f); IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f);
IM_ASSERT(g.IO.RenderDrawListsFn != NULL); // Must be implemented IM_ASSERT(g.IO.RenderDrawListsFn != NULL); // Must be implemented
IM_ASSERT(g.IO.FontAtlas->IsBuilt()); // Font not created. Did you call io.FontAtlas->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ? IM_ASSERT(g.IO.Fonts->Fonts.size() > 0); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
IM_ASSERT(g.IO.FontAtlas->Fonts[0]->IsLoaded()); // Font not created. Did you call io.FontAtlas->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ? IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
if (!g.Initialized) if (!g.Initialized)
{ {
@ -1578,7 +1578,7 @@ void ImGui::NewFrame()
g.Initialized = true; g.Initialized = true;
} }
SetFont(g.IO.FontAtlas->Fonts[0]); SetFont(g.IO.Fonts->Fonts[0]);
g.Time += g.IO.DeltaTime; g.Time += g.IO.DeltaTime;
g.FrameCount += 1; g.FrameCount += 1;
@ -1732,7 +1732,7 @@ void ImGui::Shutdown()
fclose(g.LogFile); fclose(g.LogFile);
g.LogFile = NULL; g.LogFile = NULL;
} }
g.IO.FontAtlas->Clear(); g.IO.Fonts->Clear();
if (g.PrivateClipboard) if (g.PrivateClipboard)
{ {
@ -2841,7 +2841,7 @@ void ImGui::PushFont(ImFont* font)
ImGuiState& g = GImGui; ImGuiState& g = GImGui;
if (!font) if (!font)
font = g.IO.FontAtlas->Fonts[0]; font = g.IO.Fonts->Fonts[0];
SetFont(font); SetFont(font);
g.FontStack.push_back(font); g.FontStack.push_back(font);
@ -2854,7 +2854,7 @@ void ImGui::PopFont()
g.CurrentWindow->DrawList->PopTextureID(); g.CurrentWindow->DrawList->PopTextureID();
g.FontStack.pop_back(); g.FontStack.pop_back();
SetFont(g.FontStack.empty() ? g.IO.FontAtlas->Fonts[0] : g.FontStack.back()); SetFont(g.FontStack.empty() ? g.IO.Fonts->Fonts[0] : g.FontStack.back());
} }
void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus) void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus)
@ -6321,8 +6321,7 @@ ImFontAtlas::ImFontAtlas()
ImFontAtlas::~ImFontAtlas() ImFontAtlas::~ImFontAtlas()
{ {
ClearInputData(); Clear();
ClearTexData();
} }
void ImFontAtlas::ClearInputData() void ImFontAtlas::ClearInputData()
@ -6346,8 +6345,10 @@ void ImFontAtlas::ClearTexData()
TexPixelsRGBA32 = NULL; TexPixelsRGBA32 = NULL;
} }
void ImFontAtlas::ClearFonts() void ImFontAtlas::Clear()
{ {
ClearInputData();
ClearTexData();
for (size_t i = 0; i < Fonts.size(); i++) for (size_t i = 0; i < Fonts.size(); i++)
{ {
Fonts[i]->~ImFont(); Fonts[i]->~ImFont();
@ -6411,7 +6412,7 @@ ImFont* ImFontAtlas::AddFontDefault()
stb_decompress(buf_decompressed, (unsigned char*)ttf_compressed, ttf_compressed_size); stb_decompress(buf_decompressed, (unsigned char*)ttf_compressed, ttf_compressed_size);
// Add // Add
ImFont* font = AddFontFromMemoryTTF(buf_decompressed, buf_decompressed_size, 13.0f, ImFontAtlas::GetGlyphRangesDefault(), 0); ImFont* font = AddFontFromMemoryTTF(buf_decompressed, buf_decompressed_size, 13.0f, GetGlyphRangesDefault(), 0);
font->DisplayOffset.y += 1; font->DisplayOffset.y += 1;
return font; return font;
} }
@ -6467,6 +6468,7 @@ bool ImFontAtlas::Build()
for (size_t input_i = 0; input_i < InputData.size(); input_i++) for (size_t input_i = 0; input_i < InputData.size(); input_i++)
{ {
ImFontAtlasData& data = *InputData[input_i]; ImFontAtlasData& data = *InputData[input_i];
IM_ASSERT(data.OutFont && !data.OutFont->IsLoaded());
const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)data.TTFData, data.FontNo); const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)data.TTFData, data.FontNo);
IM_ASSERT(font_offset >= 0); IM_ASSERT(font_offset >= 0);
if (!stbtt_InitFont(&data.FontInfo, (unsigned char*)data.TTFData, font_offset)) if (!stbtt_InitFont(&data.FontInfo, (unsigned char*)data.TTFData, font_offset))
@ -6497,7 +6499,7 @@ bool ImFontAtlas::Build()
{ {
ImFontAtlasData& data = *InputData[input_i]; ImFontAtlasData& data = *InputData[input_i];
if (!data.GlyphRanges) if (!data.GlyphRanges)
data.GlyphRanges = ImFontAtlas::GetGlyphRangesDefault(); data.GlyphRanges = GetGlyphRangesDefault();
// Setup ranges // Setup ranges
int glyph_count = 0; int glyph_count = 0;
@ -6621,11 +6623,6 @@ ImFont::ImFont()
Clear(); Clear();
} }
ImFont::~ImFont()
{
Clear();
}
void ImFont::Clear() void ImFont::Clear()
{ {
FontSize = 0.0f; FontSize = 0.0f;
@ -7494,10 +7491,10 @@ void ImGui::ShowTestWindow(bool* opened)
if (ImGui::TreeNode("Fonts")) if (ImGui::TreeNode("Fonts"))
{ {
ImGui::TextWrapped("Tip: Load fonts with GetIO().FontAtlas->AddFontFromFileTTF()."); ImGui::TextWrapped("Tip: Load fonts with GetIO().Fonts->AddFontFromFileTTF().");
for (size_t i = 0; i < ImGui::GetIO().FontAtlas->Fonts.size(); i++) for (size_t i = 0; i < ImGui::GetIO().Fonts->Fonts.size(); i++)
{ {
ImFont* font = ImGui::GetIO().FontAtlas->Fonts[i]; ImFont* font = ImGui::GetIO().Fonts->Fonts[i];
ImGui::BulletText("Font %d: %.2f pixels, %d glyphs", i, font->FontSize, font->Glyphs.size()); ImGui::BulletText("Font %d: %.2f pixels, %d glyphs", i, font->FontSize, font->Glyphs.size());
ImGui::TreePush((void*)i); ImGui::TreePush((void*)i);
ImGui::PushFont(font); ImGui::PushFont(font);
@ -7505,8 +7502,8 @@ void ImGui::ShowTestWindow(bool* opened)
ImGui::PopFont(); ImGui::PopFont();
if (i > 0 && ImGui::Button("Set as default")) if (i > 0 && ImGui::Button("Set as default"))
{ {
ImGui::GetIO().FontAtlas->Fonts[i] = ImGui::GetIO().FontAtlas->Fonts[0]; ImGui::GetIO().Fonts->Fonts[i] = ImGui::GetIO().Fonts->Fonts[0];
ImGui::GetIO().FontAtlas->Fonts[0] = font; ImGui::GetIO().Fonts->Fonts[0] = font;
} }
ImGui::SliderFloat("font scale", &font->Scale, 0.3f, 2.0f, "%.1f"); // scale only this font ImGui::SliderFloat("font scale", &font->Scale, 0.3f, 2.0f, "%.1f"); // scale only this font
ImGui::TreePop(); ImGui::TreePop();
@ -7612,9 +7609,9 @@ void ImGui::ShowTestWindow(bool* opened)
{ {
ImGui::TextWrapped("Below we are displaying the font texture (which is the only texture we have access to in this demo). Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. Hover the texture for a zoomed view!"); ImGui::TextWrapped("Below we are displaying the font texture (which is the only texture we have access to in this demo). Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. Hover the texture for a zoomed view!");
ImVec2 tex_screen_pos = ImGui::GetCursorScreenPos(); ImVec2 tex_screen_pos = ImGui::GetCursorScreenPos();
float tex_w = (float)ImGui::GetIO().FontAtlas->TexWidth; float tex_w = (float)ImGui::GetIO().Fonts->TexWidth;
float tex_h = (float)ImGui::GetIO().FontAtlas->TexHeight; float tex_h = (float)ImGui::GetIO().Fonts->TexHeight;
ImTextureID tex_id = ImGui::GetIO().FontAtlas->TexID; ImTextureID tex_id = ImGui::GetIO().Fonts->TexID;
ImGui::Image(tex_id, ImVec2(tex_w, tex_h), ImVec2(0,0), ImVec2(1,1), 0xFFFFFFFF, 0x999999FF); ImGui::Image(tex_id, ImVec2(tex_w, tex_h), ImVec2(0,0), ImVec2(1,1), 0xFFFFFFFF, 0x999999FF);
if (ImGui::IsItemHovered()) if (ImGui::IsItemHovered())
{ {

83
imgui.h
View File

@ -494,7 +494,7 @@ struct ImGuiIO
int KeyMap[ImGuiKey_COUNT]; // <unset> // Map of indices into the KeysDown[512] entries array int KeyMap[ImGuiKey_COUNT]; // <unset> // Map of indices into the KeysDown[512] entries array
void* UserData; // = NULL // Store your own data for retrieval by callbacks. void* UserData; // = NULL // Store your own data for retrieval by callbacks.
ImFontAtlas* FontAtlas; // <auto> // Load and assemble one or more fonts into a single tightly packed texture. Output to Fonts array. ImFontAtlas* Fonts; // <auto> // Load and assemble one or more fonts into a single tightly packed texture. Output to Fonts array.
float FontGlobalScale; // = 1.0f // Global scale all fonts float FontGlobalScale; // = 1.0f // Global scale all fonts
bool FontAllowUserScaling; // = false // Allow user scaling text of individual window with CTRL+Wheel. bool FontAllowUserScaling; // = false // Allow user scaling text of individual window with CTRL+Wheel.
@ -748,61 +748,58 @@ struct ImDrawList
}; };
// Load and rasterize multiple TTF fonts into a same texture. // Load and rasterize multiple TTF fonts into a same texture.
// We also add custom graphic data into the texture that serves for ImGui.
// Sharing a texture for multiple fonts allows us to reduce the number of draw calls during rendering. // Sharing a texture for multiple fonts allows us to reduce the number of draw calls during rendering.
// The simple use case, if you don't intent to load custom or multiple fonts, is: // We also add custom graphic data into the texture that serves for ImGui.
// 1. GetTexDataAsRGBA32() or GetTexDataAsAlpha8() // to obtain pixels // 1. (Optional) Call AddFont*** functions. If you don't call any, the default font will be loaded for you.
// 2. <upload the texture to graphics memory> // 2. Call GetTexDataAsAlpha8() or GetTexDataAsRGBA32() to build and retrieve pixels data.
// 3. SetTexID(my_engine_id); // use the pointer/id to your texture in your engine format // 3. Upload the pixels data into a texture within your graphics system.
// 4. ClearPixelsData() // to save memory // 4. Call SetTexID(my_tex_id); and pass the pointer/identifier to your texture. This value will be passed back to you during rendering to identify the texture.
// 5. Call ClearPixelsData() to free textures memory on the heap.
struct ImFontAtlas struct ImFontAtlas
{ {
// Methods
IMGUI_API ImFontAtlas(); IMGUI_API ImFontAtlas();
IMGUI_API ~ImFontAtlas(); IMGUI_API ~ImFontAtlas();
IMGUI_API ImFont* AddFontDefault(); IMGUI_API ImFont* AddFontDefault();
IMGUI_API ImFont* AddFontFromFileTTF(const char* filename, float size_pixels, const ImWchar* glyph_ranges = NULL, int font_no = 0); IMGUI_API ImFont* AddFontFromFileTTF(const char* filename, float size_pixels, const ImWchar* glyph_ranges = NULL, int font_no = 0);
IMGUI_API ImFont* AddFontFromMemoryTTF(void* in_ttf_data, size_t in_ttf_data_size, float size_pixels, const ImWchar* glyph_ranges = NULL, int font_no = 0); // Pass ownership of 'in_ttf_data' memory. IMGUI_API ImFont* AddFontFromMemoryTTF(void* in_ttf_data, size_t in_ttf_data_size, float size_pixels, const ImWchar* glyph_ranges = NULL, int font_no = 0); // Pass ownership of 'in_ttf_data' memory.
IMGUI_API bool Build(); IMGUI_API void ClearTexData(); // Saves RAM once the texture has been copied to graphics memory.
IMGUI_API void ClearInputData(); IMGUI_API void Clear();
IMGUI_API void ClearFonts();
IMGUI_API void ClearTexData(); // Saves RAM once the texture has been copied to graphics memory.
IMGUI_API void Clear() { ClearInputData(); ClearTexData(); ClearFonts(); }
IMGUI_API bool IsBuilt() const { return !Fonts.empty(); }
// Methods: Retrieve texture data // Retrieve texture data
// User is in charge of copying the pixels into graphics memory, then call SetTextureUserID() // User is in charge of copying the pixels into graphics memory, then call SetTextureUserID()
// After loading the texture into your graphic system, store your texture handle in 'TexID' (ignore if you aren't using multiple fonts nor images) // After loading the texture into your graphic system, store your texture handle in 'TexID' (ignore if you aren't using multiple fonts nor images)
// RGBA32 format is provided for convenience and high compatibility, but note that all RGB pixels are white, so 75% of the memory is wasted. // RGBA32 format is provided for convenience and high compatibility, but note that all RGB pixels are white, so 75% of the memory is wasted.
IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel
IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel
IMGUI_API void SetTexID(void* id) { TexID = id; } IMGUI_API void SetTexID(void* id) { TexID = id; }
// Static helper: Retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list) // Helpers to retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list)
static IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin // (Those functions could be static, aren't so simple use case doesn't have to refer to the ImFontAtlas:: type ever if in their code)
static IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin
static IMGUI_API const ImWchar* GetGlyphRangesChinese(); // Japanese + full set of about 21000 CJK Unified Ideographs IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs
IMGUI_API const ImWchar* GetGlyphRangesChinese(); // Japanese + full set of about 21000 CJK Unified Ideographs
// Members // Members
// Access texture data via GetTextureData*() calls which will setup a default font for you. // (Access texture data via GetTexData*() calls which will setup a default font for you.)
void* TexID; // User data to refer to the texture once it has been uploaded to user's graphic systems. It ia passed back to you during rendering. void* TexID; // User data to refer to the texture once it has been uploaded to user's graphic systems. It ia passed back to you during rendering.
unsigned char* TexPixelsAlpha8; // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight unsigned char* TexPixelsAlpha8; // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight
unsigned int* TexPixelsRGBA32; // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4 unsigned int* TexPixelsRGBA32; // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4
int TexWidth; int TexWidth;
int TexHeight; int TexHeight;
ImVec2 TexExtraDataPos; // Position of our rectangle where we draw non-font graphics ImVec2 TexExtraDataPos; // Position of our rectangle where we draw non-font graphics
ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel (part of the TexExtraData block) ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel (part of the TexExtraData block)
ImVector<ImFont*> Fonts; ImVector<ImFont*> Fonts;
// Private
struct ImFontAtlasData; struct ImFontAtlasData;
ImVector<ImFontAtlasData*> InputData; // Internal data ImVector<ImFontAtlasData*> InputData; // Internal data
IMGUI_API bool Build(); // Build pixels data. This is automatically for you by the GetTexData*** functions.
IMGUI_API void ClearInputData(); // Clear the input TTF data.
}; };
// TTF font loading and rendering // TTF font loading and rendering
// - ImGui automatically loads a default embedded font for you // ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32().
// - Call GetTextureData() to retrieve pixels data so you can upload the texture to your graphics system. // Kerning isn't supported. At the moment some ImGui code does per-character CalcTextSize calls, need something more state-ful.
// - Store your texture handle in 'TexID'. It will be passed back to you when rendering ('texture_id' field in ImDrawCmd)
// (NB: kerning isn't supported. At the moment some ImGui code does per-character CalcTextSize calls, need something more state-ful)
struct ImFont struct ImFont
{ {
// Members: Settings // Members: Settings
@ -822,20 +819,16 @@ struct ImFont
}; };
ImFontAtlas* ContainerAtlas; // What we has been loaded into ImFontAtlas* ContainerAtlas; // What we has been loaded into
ImVector<Glyph> Glyphs; ImVector<Glyph> Glyphs;
ImVector<int> IndexLookup; ImVector<int> IndexLookup; // Index glyphs by Unicode code-point
const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar) const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar)
// Methods // Methods
IMGUI_API ImFont(); IMGUI_API ImFont();
IMGUI_API ~ImFont(); IMGUI_API ~ImFont() { Clear(); }
IMGUI_API void Clear(); IMGUI_API void Clear();
IMGUI_API void BuildLookupTable(); IMGUI_API void BuildLookupTable();
IMGUI_API const Glyph* FindGlyph(unsigned short c) const; IMGUI_API const Glyph* FindGlyph(unsigned short c) const;
IMGUI_API bool IsLoaded() const { return ContainerAtlas != NULL; } IMGUI_API bool IsLoaded() const { return ContainerAtlas != NULL; }
IMGUI_API ImTextureID GetTexID() const { IM_ASSERT(ContainerAtlas != NULL); return ContainerAtlas->TexID; }
IMGUI_API int GetTexWidth() const { IM_ASSERT(ContainerAtlas != NULL); return ContainerAtlas->TexWidth; }
IMGUI_API int GetTexHeight() const { IM_ASSERT(ContainerAtlas != NULL); return ContainerAtlas->TexHeight; }
// 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable. // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
// 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable. // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.