public static unsafe Font AddFontFromFileTTF(this FontAtlas atlas, string fileName, float pixelSize, FontConfig config, char[] glyphRanges)
        {
            NativeFontAtlas *atlasPtr = ImGui.GetIO().GetNativePointer()->FontAtlas;
            IntPtr           cfgPtr   = Marshal.AllocHGlobal(Marshal.SizeOf(config));

            Marshal.StructureToPtr(config, cfgPtr, false);
            fixed(char *glyphPtr = &glyphRanges[0])
            {
                NativeFont *fontPtr =
                    ImGuiNative.ImFontAtlas_AddFontFromFileTTF(atlasPtr, fileName, pixelSize, cfgPtr, glyphPtr);

                return(new Font(fontPtr));
            }
        }
예제 #2
0
        private unsafe void SetFonts()
        {
            var folder = "fonts";
            var files  = Directory.GetFiles(folder);

            if (!(Directory.Exists(folder) && files.Length > 0))
            {
                return;
            }

            var fontsForLoad = new List <(string, int)>();

            if (files.Contains($"{folder}\\config.ini"))
            {
                var lines = File.ReadAllLines($"{folder}\\config.ini");

                foreach (var line in lines)
                {
                    var split = line.Split(':');
                    fontsForLoad.Add(($"{folder}\\{split[0]}.ttf", int.Parse(split[1])));
                }
            }

            var          sm   = new ImFontAtlas();
            ImFontAtlas *some = &sm;

            var imFontAtlasGetGlyphRangesCyrillic = ImGuiNative.ImFontAtlas_GetGlyphRangesCyrillic(some);

            fonts["Default:13"] = new FontContainer(ImGuiNative.ImFontAtlas_AddFontDefault(some, null),
                                                    "Default", 13);

            foreach (var tuple in fontsForLoad)
            {
                var bytes = Encoding.UTF8.GetBytes(tuple.Item1);

                fixed(byte *f = &bytes[0])
                {
                    fonts[$"{tuple.Item1.Replace(".ttf", "").Replace("fonts\\", "")}:{tuple.Item2}"] =
                        new FontContainer(
                            ImGuiNative.ImFontAtlas_AddFontFromFileTTF(some, f, tuple.Item2, null,
                                                                       imFontAtlasGetGlyphRangesCyrillic), tuple.Item1, tuple.Item2);
                }
            }

            Settings.Font.Values = new List <string>(fonts.Keys);
        }
예제 #3
0
        public static void AddFontFromFileTTF(string filename, float sizePixels, FontConfig config, char[] glyphRanges)
        {
            IO io = ImGui.GetIO();

            config.OversampleH        = 1;
            config.OversampleV        = 1;
            config.RasterizerMultiply = 1;
            IntPtr cnfPtr = Marshal.AllocHGlobal(Marshal.SizeOf <FontConfig>());

            Marshal.StructureToPtr(config, cnfPtr, false);

            unsafe
            {
                NativeFontAtlas *atlas = io.GetNativePointer()->FontAtlas;
                fixed(char *glyphs = &glyphRanges[0])
                {
                    ImGuiNative.ImFontAtlas_AddFontFromFileTTF(atlas, filename, sizePixels, cnfPtr,
                                                               glyphs);
                }
            }
        }
예제 #4
0
        private unsafe void CreateFontsTexture(RenderContext rc)
        {
            IO io = ImGui.GetIO();

            if (!string.IsNullOrEmpty(Preferences.Instance.MenuFont))
            {
                if (File.Exists(Preferences.Instance.MenuFont))
                {
                    float fontSize = Math.Max(4, Math.Min(48, Preferences.Instance.FontSize));
                    var   font     = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(io.GetNativePointer()->FontAtlas, Preferences.Instance.MenuFont, fontSize, IntPtr.Zero, null);
                }
                else
                {
                    Console.WriteLine("Font listed in preferences doesn't exist: " + Preferences.Instance.MenuFont);
                }
            }
            else
            {
                ImGui.LoadDefaultFont();
            }

            // Build
            _textureData = io.FontAtlas.GetTexDataAsRGBA32();
            int[] pixels = new int[_textureData.Width * _textureData.Height];
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = ((int *)_textureData.Pixels)[i];
            }

            _fontTexture = new RawTextureDataArray <int>(pixels, _textureData.Width, _textureData.Height, _textureData.BytesPerPixel, PixelFormat.R8_G8_B8_A8);

            // Store our identifier
            io.FontAtlas.SetTexID(_fontAtlasID);

            // Cleanup (don't clear the input data if you want to append new fonts later)
            io.FontAtlas.ClearTexData();
        }
예제 #5
0
        public Font AddFontFromFileTTF(string fileName, float pixelSize)
        {
            NativeFont *nativeFontPtr = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(_atlasPtr, fileName, pixelSize, IntPtr.Zero, null);

            return(new Font(nativeFontPtr));
        }