public static void AddToFontAtlas(ImFontAtlasPtr fontAtlas, byte mergeMode, float size, float minAdvanceX) { var assembly = typeof(ForkAwesomeIconFont).Assembly; using var stream = assembly.GetManifestResourceStream("zzre.core.assets.forkawesome-webfont.ttf"); if (stream == null) { throw new FileNotFoundException("Could not find embedded ForkAwesome font"); } var data = new byte[stream.Length]; stream.Read(data, 0, data.Length); stream.Close(); var fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); fontConfig->MergeMode = mergeMode; fontConfig->GlyphMinAdvanceX = minAdvanceX; fontConfig->FontDataOwnedByAtlas = 0; fixed(byte *fontPtr = data) { fontAtlas.AddFontFromMemoryTTF(new IntPtr(fontPtr), data.Length, size, fontConfig, GlyphRangePtr.Value); } ImGuiNative.ImFontConfig_destroy(fontConfig); }
public CFontProvider(ImFontAtlasPtr fontAtlasPtr, Action fontAddedCallback) { m_managedFontAtlas = fontAtlasPtr; // Add our default font in different sizes to provide best quality with different scaling ImFontPtr defaultSmall = fontAtlasPtr.AddFontFromFileTTF(DEFAULT_FONT_PATH, 15.0f); ImFontPtr defaultMedium = fontAtlasPtr.AddFontFromFileTTF(DEFAULT_FONT_PATH, 30.0f); ImFontPtr defaultBig = fontAtlasPtr.AddFontFromFileTTF(DEFAULT_FONT_PATH, 60.0f); m_defaultFont.Add(defaultSmall); m_defaultFont.Add(defaultMedium); m_defaultFont.Add(defaultBig); m_fontAddedCallback = fontAddedCallback; }
void RebuildFonts() { ImGuiIOPtr io = ImGui.GetIO(); // Default font ImFontAtlasPtr fonts = io.Fonts; fonts.Clear(); using (ImFontConfig config = new ImFontConfig()) { config.SizePixels = 13.0f * uiScale; // ImGui default font size is 13 pixels config.OversampleH = 1; fonts.AddFontDefault(config); } #if !UNITY_ANDROID // Icon font if (iconFont.Length > 0) { using (ImFontConfig config = new ImFontConfig()) { config.MergeMode = true; if (iconFontMinAdvanceX > 0) { config.GlyphMinAdvanceX = iconFontMinAdvanceX; config.GlyphMaxAdvanceX = iconFontMinAdvanceX; } config.PixelSnapH = true; config.OversampleH = 1; ushort rangeMin = ushort.Parse(iconRangeMin, NumberStyles.HexNumber); ushort rangeMax = ushort.Parse(iconRangeMax, NumberStyles.HexNumber); if (rangeMin != 0 && rangeMax != 0) { ushort[] icon_ranges = { rangeMin, rangeMax, 0 }; ImGui.AddFontFromFileTTF(fonts, Application.streamingAssetsPath + "/" + iconFont, iconFontPixelSize * uiScale, config, icon_ranges); } } } // Custom fonts imFonts = new ImFontPtr[customFonts.Length]; for (int i = 0; i < customFonts.Length; i++) { using (ImFontConfig config = new ImFontConfig()) { config.OversampleH = 1; config.OversampleV = 1; imFonts[i] = fonts.AddFontFromFileTTF(Application.streamingAssetsPath + "/" + customFonts[i], customFontPixelSize * uiScale, config); } } #endif if (useFreetype) { // Freetype rasterizer ImGui.BuildFontAtlas(fonts, freetype_flags); } else { fonts.Build(); } RecreateFontTexture(); }
public unsafe IconFont(GraphicsDevice device, Action <ImFontAtlasPtr> addFontToAtlas) { var atlas = new ImFontAtlasPtr(ImGuiNative.ImFontAtlas_ImFontAtlas()); addFontToAtlas(atlas); atlas.Flags = ImFontAtlasFlags.NoMouseCursors; if (!atlas.Build()) { throw new InvalidProgramException("Building ImFontAtlas failed"); } if (atlas.Fonts.Size != 1) { throw new InvalidProgramException("ImFontAtlas does not contain a font after building"); } var font = *(ImFont **)atlas.Fonts.Data.ToPointer(); if (font->Glyphs.Size <= 0) { throw new InvalidProgramException("ImFont does not contain at least one glyph after building"); } IntPtr texPixels; int texWidth, texHeight; atlas.GetTexDataAsAlpha8(out texPixels, out texWidth, out texHeight); Texture = device.ResourceFactory.CreateTexture(new TextureDescription( width: (uint)texWidth, height: (uint)texHeight, format: PixelFormat.R8_UNorm, usage: TextureUsage.Sampled, type: TextureType.Texture2D, depth: 1, mipLevels: 1, arrayLayers: 1)); Texture.Name = "IconFontTexture"; device.UpdateTexture(Texture, texPixels, (uint)(texWidth * texHeight * 1), 0, 0, 0, (uint)texWidth, (uint)texHeight, 1, 0, 0); var glyphsPtr = (ImFontGlyph *)font->Glyphs.Data.ToPointer(); var glyphs = new Dictionary <string, Rect>(); for (int i = 0; i < font->Glyphs.Size; i++) { var glyph = new ImFontGlyphPtr(glyphsPtr + i); var uvMin = new Vector2(glyph.U0, glyph.V1); // switch v to prevent vflip var uvMax = new Vector2(glyph.U1, glyph.V0); var codepoint = (int)(glyph.Codepoint); glyphs[char.ConvertFromUtf32(codepoint)] = new Rect((uvMin + uvMax) / 2, uvMax - uvMin); } Glyphs = glyphs; atlas.Destroy(); Sampler = device.ResourceFactory.CreateSampler(new SamplerDescription( addressModeU: SamplerAddressMode.Border, addressModeV: SamplerAddressMode.Border, addressModeW: SamplerAddressMode.Border, filter: SamplerFilter.MinLinear_MagLinear_MipLinear, comparisonKind: null, maximumAnisotropy: 0, minimumLod: 0, maximumLod: 1, lodBias: 0, borderColor: SamplerBorderColor.TransparentBlack )); }
public static bool BuildFontAtlas(ImFontAtlasPtr atlas, RasterizerFlags extra_flags) { return(ImFreetypeNative.frBuildFontAtlas(atlas.NativePtr, (uint)extra_flags)); }
public static void Init() { if (_Initialized) { return; } _Initialized = true; IntPtr context = ImGui.CreateContext(); ImGui.SetCurrentContext(context); ImGuiIOPtr io = ImGui.GetIO(); io.KeyMap[(int)ImGuiKey.Tab] = (int)SDL.SDL_Keycode.SDLK_TAB; io.KeyMap[(int)ImGuiKey.LeftArrow] = (int)SDL.SDL_Scancode.SDL_SCANCODE_LEFT; io.KeyMap[(int)ImGuiKey.RightArrow] = (int)SDL.SDL_Scancode.SDL_SCANCODE_RIGHT; io.KeyMap[(int)ImGuiKey.UpArrow] = (int)SDL.SDL_Scancode.SDL_SCANCODE_UP; io.KeyMap[(int)ImGuiKey.DownArrow] = (int)SDL.SDL_Scancode.SDL_SCANCODE_DOWN; io.KeyMap[(int)ImGuiKey.PageUp] = (int)SDL.SDL_Scancode.SDL_SCANCODE_PAGEUP; io.KeyMap[(int)ImGuiKey.PageDown] = (int)SDL.SDL_Scancode.SDL_SCANCODE_PAGEDOWN; io.KeyMap[(int)ImGuiKey.Home] = (int)SDL.SDL_Scancode.SDL_SCANCODE_HOME; io.KeyMap[(int)ImGuiKey.End] = (int)SDL.SDL_Scancode.SDL_SCANCODE_END; io.KeyMap[(int)ImGuiKey.Delete] = (int)SDL.SDL_Keycode.SDLK_DELETE; io.KeyMap[(int)ImGuiKey.Backspace] = (int)SDL.SDL_Keycode.SDLK_BACKSPACE; io.KeyMap[(int)ImGuiKey.Enter] = (int)SDL.SDL_Keycode.SDLK_RETURN; io.KeyMap[(int)ImGuiKey.Escape] = (int)SDL.SDL_Keycode.SDLK_ESCAPE; io.KeyMap[(int)ImGuiKey.A] = (int)SDL.SDL_Keycode.SDLK_a; io.KeyMap[(int)ImGuiKey.C] = (int)SDL.SDL_Keycode.SDLK_c; io.KeyMap[(int)ImGuiKey.V] = (int)SDL.SDL_Keycode.SDLK_v; io.KeyMap[(int)ImGuiKey.X] = (int)SDL.SDL_Keycode.SDLK_x; io.KeyMap[(int)ImGuiKey.Y] = (int)SDL.SDL_Keycode.SDLK_y; io.KeyMap[(int)ImGuiKey.Z] = (int)SDL.SDL_Keycode.SDLK_z; //io.GetClipboardTextFn((userData) => SDL.SDL_GetClipboardText()); //io.SetSetClipboardTextFn((userData, text) => SDL.SDL_SetClipboardText(text)); unsafe { string rf = "Resources"; ImFontAtlasPtr fontAtlas = ImGui.GetIO().Fonts; var builder = new ImFontGlyphRangesBuilderPtr(ImGuiNative.ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder()); builder.AddText("Ωων"); //builder.AddRanges(fontAtlas.GetGlyphRangesDefault()); builder.BuildRanges(out ImVector ranges); fontAtlas.AddFontFromFileTTF(Path.Combine(rf, "ProggyClean.ttf"), 13); ImFontConfig * rawPtr = ImGuiNative.ImFontConfig_ImFontConfig(); ImFontConfigPtr config = new ImFontConfigPtr(rawPtr); config.MergeMode = true; fontAtlas.AddFontFromFileTTF(Path.Combine(rf, "DejaVuSans.ttf"), 13, config, ranges.Data); } if (io.Fonts.Fonts.Size == 0) { io.Fonts.AddFontDefault(); } }