//~ImFontAtlas(); ImFont AddFont(ImFontConfig font_cfg) { System.Diagnostics.Debug.Assert(font_cfg.FontData != null && font_cfg.FontDataSize > 0); System.Diagnostics.Debug.Assert(font_cfg.SizePixels > 0.0f); // Create new font if (!font_cfg.MergeMode) { ImFont font = new ImFont(); //IM_PLACEMENT_NEW(font) ImFont(); Fonts.push_back(font); } ConfigData.push_back(font_cfg); ImFontConfig new_font_cfg = ConfigData[ConfigData.Size - 1]; new_font_cfg.DstFont = Fonts[Fonts.Size - 1]; if (!new_font_cfg.FontDataOwnedByAtlas) { //new_font_cfg.FontData = ImGui::MemAlloc(new_font_cfg.FontDataSize); new_font_cfg.FontDataOwnedByAtlas = true; var fontData = new byte[font_cfg.FontData.Length]; Array.Copy(font_cfg.FontData, fontData, fontData.Length); new_font_cfg.FontData = fontData; //memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize); } var library = new Library(); new_font_cfg.Face = new Face(library, new_font_cfg.FontData, 0); //new_font_cfg.Face.SetCharSize(0, new_font_cfg.SizePixels, 0, 96); new_font_cfg.Face.SetPixelSizes(0, (uint)new_font_cfg.SizePixels); // Invalidate texture ClearTexData(); return(Fonts[Fonts.Size - 1]); }
void RenderCustomTexData(MaxRectsBinPack spc, int pass, ImVector <ImFontPackingRect> rects) { // A work of art lies ahead! (. = white layer, X = black layer, others are blank) // The white texels on the top left are the ones we'll use everywhere in ImGui to render filled shapes. const int TEX_DATA_W = 90; const int TEX_DATA_H = 27; const string texture_data = "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX" + "..- -X.....X- X.X - X.X -X.....X - X.....X" + "--- -XXX.XXX- X...X - X...X -X....X - X....X" + "X - X.X - X.....X - X.....X -X...X - X...X" + "XX - X.X -X.......X- X.......X -X..X.X - X.X..X" + "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X" + "X..X - X.X - X.X - X.X -XX X.X - X.X XX" + "X...X - X.X - X.X - XX X.X XX - X.X - X.X " + "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X " + "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X " + "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X " + "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X " + "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X " + "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X " + "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X " + "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X " + "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX " + "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------" + "X.X X..X - -X.......X- X.......X - XX XX - " + "XX X..X - - X.....X - X.....X - X.X X.X - " + " X..X - X...X - X...X - X..X X..X - " + " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - " + "------------ - X - X -X.....................X- " + " ----------------------------------- X...XXXXXXXXXXXXX...X - " + " - X..X X..X - " + " - X.X X.X - " + " - XX XX - "; //ImVector < stbrp_rect > &rects = *(ImVector<stbrp_rect>*)p_rects; if (pass == 0) { // Request rectangles var custom = new ImFontPackingRect(); custom.w = TEX_DATA_W * 2 + 1; custom.h = TEX_DATA_H + 1; rects.push_back(custom); custom.pack(spc); } else if (pass == 1) { // Render/copy pixels //the first rect in rects will always be custom font data var r = rects[0]; for (int y = 0, n = 0; y < TEX_DATA_H; y++) { for (int x = 0; x < TEX_DATA_W; x++, n++) { int offset0 = (r.x + x) + (r.y + y) * TexWidth; int offset1 = offset0 + 1 + TEX_DATA_W; TexPixelsAlpha8[offset0] = (byte)(texture_data[n] == '.' ? 0xFF : 0x00); TexPixelsAlpha8[offset1] = (byte)(texture_data[n] == 'X' ? 0xFF : 0x00); } } ImVec2 tex_uv_scale = new ImVec2(1.0f / TexWidth, 1.0f / TexHeight); TexUvWhitePixel = new ImVec2((r.x + 0.5f) * tex_uv_scale.x, (r.y + 0.5f) * tex_uv_scale.y); //TODO: Finish render custom text //// Setup mouse cursors var cursor_datas = new ImVec2[, ] { // Pos ........ Size ......... Offset ...... { new ImVec2(0, 3), new ImVec2(12, 19), new ImVec2(0, 0) }, // ImGuiMouseCursor_Arrow { new ImVec2(13, 0), new ImVec2(7, 16), new ImVec2(4, 8) }, // ImGuiMouseCursor_TextInput { new ImVec2(31, 0), new ImVec2(23, 23), new ImVec2(11, 11) }, // ImGuiMouseCursor_Move { new ImVec2(21, 0), new ImVec2(9, 23), new ImVec2(5, 11) }, // ImGuiMouseCursor_ResizeNS { new ImVec2(55, 18), new ImVec2(23, 9), new ImVec2(11, 5) }, // ImGuiMouseCursor_ResizeEW { new ImVec2(73, 0), new ImVec2(17, 17), new ImVec2(9, 9) }, // ImGuiMouseCursor_ResizeNESW { new ImVec2(55, 0), new ImVec2(17, 17), new ImVec2(9, 9) }, // ImGuiMouseCursor_ResizeNWSE }; for (int type = 0; type < 7; type++) { ImGuiMouseCursorData cursor_data = ImGui.Instance.State.MouseCursorData[type]; ImVec2 pos = cursor_datas[type, 0] + new ImVec2((float)r.x, (float)r.y); ImVec2 size = cursor_datas[type, 1]; cursor_data.Type = (ImGuiMouseCursor)type; cursor_data.Size = size; cursor_data.HotOffset = cursor_datas[type, 2]; cursor_data.TexUvMin[0] = (pos) * tex_uv_scale; cursor_data.TexUvMax[0] = (pos + size) * tex_uv_scale; pos.x += TEX_DATA_W + 1; cursor_data.TexUvMin[1] = (pos) * tex_uv_scale; cursor_data.TexUvMax[1] = (pos + size) * tex_uv_scale; } } }
//public ImRect[] PackFont(MaxRectsBinPack packer, Texture2D texture, Texture2D[] textures, int width, int height, int maxSize) //{ // if (width > maxSize && height > maxSize) return null; // if (width > maxSize || height > maxSize) { int temp = width; width = height; height = temp; } // MaxRectsBinPack bp = new MaxRectsBinPack(width, height); // ImRect[] rects = new ImRect[textures.Length]; // for (int i = 0; i < textures.Length; i++) // { // Texture2D tex = textures[i]; // ImRect rect = bp.Insert(tex.width, tex.height, MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit); // if (rect.width == 0 || rect.height == 0) // { // return PackTextures(texture, textures, width * (width <= height ? 2 : 1), height * (height < width ? 2 : 1), maxSize); // } // rects[i] = rect; // } // texture.Resize(width, height); // texture.SetPixels(new ImColor[width * height]); // for (int i = 0; i < textures.Length; i++) // { // Texture2D tex = textures[i]; // ImRect rect = rects[i]; // ImColor[] colors = tex.GetPixels(); // if (rect.width != tex.width) // { // ImColor[] newColors = tex.GetPixels(); // for (int x = 0; x < rect.width; x++) // { // for (int y = 0; y < rect.height; y++) // { // int prevIndex = ((int)rect.height - (y + 1)) + x * (int)tex.width; // newColors[x + y * (int)rect.width] = colors[prevIndex]; // } // } // colors = newColors; // } // texture.SetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height, colors); // rect.x /= width; // rect.y /= height; // rect.width /= width; // rect.height /= height; // rects[i] = rect; // } // return rects; //} internal bool Build() { System.Diagnostics.Debug.Assert(ConfigData.Size > 0); TexID = null; TexWidth = TexHeight = 0; TexUvWhitePixel = new ImVec2(0, 0); ClearTexData(); int total_glyph_count = 0; int total_glyph_range_count = 0; for (int input_i = 0; input_i < ConfigData.Size; input_i++) { ImFontConfig cfg = ConfigData[input_i]; System.Diagnostics.Debug.Assert(cfg.DstFont != null && (!cfg.DstFont.IsLoaded() || cfg.DstFont.ContainerAtlas == this)); System.Diagnostics.Debug.Assert(cfg.FontData != null); // Count glyphs if (cfg.GlyphRanges == null) { cfg.GlyphRanges = GetGlyphRangesDefault(); } for (int in_range = 0; cfg.GlyphRanges[in_range] > 0 && cfg.GlyphRanges[in_range + 1] > 0; in_range += 2) { total_glyph_count += (cfg.GlyphRanges[in_range + 1] - cfg.GlyphRanges[in_range]) + 1; total_glyph_range_count++; } } // Start packing. We need a known width for the skyline algorithm. Using a cheap heuristic here to decide of width. User can override TexDesiredWidth if they wish. // After packing is done, width shouldn't matter much, but some API/GPU have texture size limitations and increasing width can decrease height. TexWidth = (TexDesiredWidth > 0) ? TexDesiredWidth : (total_glyph_count > 4000) ? 4096 : (total_glyph_count > 2000) ? 2048 : (total_glyph_count > 1000) ? 1024 : 512; TexHeight = 0; int max_tex_height = 1024 * 32; var spc = new MaxRectsBinPack(TexWidth, max_tex_height, false); ImVector <ImFontPackingRect> rects = new ImVector <ImFontPackingRect>(); RenderCustomTexData(spc, 0, rects); // First font pass: pack all glyphs (no rendering at this point, we are working with rectangles in an infinitely tall texture at this point) for (int input_i = 0; input_i < ConfigData.Size; input_i++) { ImFontConfig cfg = ConfigData[input_i]; cfg.Face.SetPixelSizes((uint)(cfg.SizePixels * cfg.OversampleH), (uint)(cfg.SizePixels * cfg.OversampleV)); for (int in_range = 0; cfg.GlyphRanges[in_range] > 0 && cfg.GlyphRanges[in_range + 1] > 0; in_range += 2) { var glyphs = new List <ImRect>((cfg.GlyphRanges[in_range + 1] - cfg.GlyphRanges[in_range]) + 1); var packedGlyphs = new List <ImRect>((cfg.GlyphRanges[in_range + 1] - cfg.GlyphRanges[in_range]) + 1); for (var range = cfg.GlyphRanges[in_range]; range <= cfg.GlyphRanges[in_range + 1]; range++) { char c = (char)range; uint glyphIndex = cfg.Face.GetCharIndex(c); cfg.Face.LoadGlyph(glyphIndex, LoadFlags.Default, LoadTarget.Normal); //added padding to keep from bleeding glyphs.Add(new ImRect(0, 0, (int)cfg.Face.Glyph.Metrics.Width + 2, (int)cfg.Face.Glyph.Metrics.Height + 2)); spc.Insert((int)cfg.Face.Glyph.Metrics.Width + 2, (int)cfg.Face.Glyph.Metrics.Height + 2, MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit); } spc.Insert(glyphs, packedGlyphs, MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit); System.Diagnostics.Debug.Assert(glyphs.Count == packedGlyphs.Count); for (var i = 0; i < glyphs.Count; i++) { var c = (cfg.GlyphRanges[in_range] + i); var g = glyphs[i]; var pg = packedGlyphs[i]; var was_packed = pg.width > 0 && pg.height > 0; var r = new ImFontPackingRect() { id = c, x = (int)pg.x + 1, y = (int)pg.y + 1, w = (int)pg.width, h = (int)pg.height, was_packed = was_packed }; if (was_packed) { TexHeight = ImGui.Max(TexHeight, r.y + r.h); } rects.push_back(r); } } } // Create texture TexHeight = ImGui.UpperPowerOfTwo(TexHeight); TexPixelsAlpha8 = new byte[TexWidth * TexHeight]; for (int input_i = 0; input_i < ConfigData.Size; input_i++) { ImFontConfig cfg = ConfigData[input_i]; ImFont dst_font = cfg.DstFont; int unscaled_ascent = cfg.Face.Ascender, unscaled_descent = cfg.Face.Descender; float font_scale = cfg.SizePixels / (unscaled_ascent - unscaled_descent); //taken from stbtt_ScaleForPixelHeight var max_height = cfg.Face.Height * font_scale; float ascent = unscaled_ascent * font_scale; float descent = unscaled_descent * font_scale; if (!cfg.MergeMode) { dst_font.ContainerAtlas = this; dst_font.ConfigData = cfg; dst_font.ConfigDataCount = 0; dst_font.FontSize = cfg.SizePixels; dst_font.Ascent = ascent; dst_font.Descent = descent; dst_font.Glyphs.resize(0); } dst_font.ConfigDataCount++; float off_y = (cfg.MergeMode && cfg.MergeGlyphCenterV) ? (ascent - dst_font.Ascent) * 0.5f : 0.0f; //render for (var i = 0; i < rects.Size; i++) { var rect = rects[i]; //if (rect.id > 0 /*&& rect.was_packed*/) { var codepoint = (ushort)rect.id; if (cfg.MergeMode && dst_font.HasGlyph((char)codepoint)) { continue; } uint glyphIndex = cfg.Face.GetCharIndex(codepoint); cfg.Face.LoadGlyph(glyphIndex, LoadFlags.Render, LoadTarget.Normal); cfg.Face.Glyph.RenderGlyph(RenderMode.Normal); var bmp = cfg.Face.Glyph.Bitmap; for (var x = 0; x < bmp.Width; x++) { for (var y = 0; y < bmp.Rows; y++) { TexPixelsAlpha8[(rect.x + x) + ((rect.y + y) * TexWidth)] = bmp.BufferData[x + y * bmp.Pitch]; } } } } cfg.Face.SetPixelSizes((uint)cfg.SizePixels, (uint)cfg.SizePixels); //need to calculate origin/baseline var top = 0f; var bot = 0f; for (var i = 0; i < rects.Size; i++) { var rect = rects[i]; //if (rect.id > 0 /*&& rect.was_packed*/) { var codepoint = (ushort)rect.id; if (cfg.MergeMode && dst_font.HasGlyph((char)codepoint)) { continue; } uint glyphIndex = cfg.Face.GetCharIndex(codepoint); cfg.Face.LoadGlyph(glyphIndex, LoadFlags.ComputeMetrics, LoadTarget.Normal); var glyphTop = (float)cfg.Face.Glyph.Metrics.HorizontalBearingY; var glyphBot = (float)(cfg.Face.Glyph.Metrics.Height - cfg.Face.Glyph.Metrics.HorizontalBearingY); if (glyphTop > top) { top = glyphTop; } if (glyphBot > bot) { bot = glyphBot; } } } //dst_font.FallbackGlyph = null; // Always clear fallback so FindGlyph can return NULL. It will be set again in BuildLookupTable() for (var i = 0; i < rects.Size; i++) { var rect = rects[i]; //if (rect.id > 0 /*&& rect.was_packed*/) { var codepoint = (ushort)rect.id; if (cfg.MergeMode && dst_font.HasGlyph((char)codepoint)) { continue; } uint glyphIndex = cfg.Face.GetCharIndex(codepoint); cfg.Face.LoadGlyph(glyphIndex, LoadFlags.ComputeMetrics, LoadTarget.Normal); dst_font.Glyphs.resize(dst_font.Glyphs.Size + 1); var glyph = dst_font.Glyphs[dst_font.Glyphs.Size - 1]; glyph.Codepoint = codepoint; glyph.X0 = (int)cfg.Face.Glyph.Metrics.HorizontalBearingX + (float)cfg.Face.Glyph.BitmapLeft; glyph.X1 = (int)cfg.Face.Glyph.Metrics.Width + glyph.X0; glyph.Y0 = top - (float)cfg.Face.Glyph.Metrics.HorizontalBearingY; glyph.Y1 = glyph.Y0 + (float)cfg.Face.Glyph.Metrics.Height; glyph.U0 = rect.x / (float)TexWidth; glyph.V0 = rect.y / (float)TexHeight; glyph.U1 = (rect.x + rect.w) / (float)TexWidth; glyph.V1 = (rect.y + rect.h) / (float)TexHeight; glyph.XAdvance = ((int)cfg.Face.Glyph.Metrics.HorizontalAdvance + cfg.GlyphExtraSpacing.x); // Bake spacing into XAdvance if (cfg.PixelSnapH) { glyph.XAdvance = (int)(glyph.XAdvance + 0.5f); } dst_font.Glyphs[dst_font.Glyphs.Size - 1] = glyph; } } cfg.DstFont.BuildLookupTable(); } // Cleanup temporaries //ImGui::MemFree(buf_packedchars); //ImGui::MemFree(buf_ranges); //ImGui::MemFree(tmp_array); // Render into our custom data block RenderCustomTexData(spc, 1, rects); return(true); }