// Convert from FT_FaceInfo to FaceInfo
        FaceInfo GetFaceInfo(FT_FaceInfo ft_face, int scaleFactor)
        {
            FaceInfo face = new FaceInfo();

            face.Name = ft_face.name;
            face.PointSize = (float)ft_face.pointSize / scaleFactor;
            face.Padding = 0; // ft_face.padding / scaleFactor;
            face.LineHeight = ft_face.lineHeight / scaleFactor;
            face.Baseline = 0;
            face.Ascender = ft_face.ascender / scaleFactor;
            face.Descender = ft_face.descender / scaleFactor;
            face.CenterLine = ft_face.centerLine / scaleFactor;
            face.Underline = ft_face.underline / scaleFactor;
            face.UnderlineThickness = ft_face.underlineThickness == 0 ? 5 : ft_face.underlineThickness / scaleFactor; // Set Thickness to 5 if TTF value is Zero.
            face.SuperscriptOffset = face.Ascender;
            face.SubscriptOffset = face.Underline;
            face.SubSize = 0.5f;
            //face.CharacterCount = ft_face.characterCount;
            face.AtlasWidth = ft_face.atlasWidth / scaleFactor;
            face.AtlasHeight = ft_face.atlasHeight / scaleFactor;

            return face;
        }
        void DrawControls()
        {
            GUILayout.BeginVertical();
            GUILayout.Label("<b>TextMeshPro - Font Asset Creator</b>", TMP_UIStyleManager.Section_Label, GUILayout.Width(300));
            GUILayout.Label("Font Settings", TMP_UIStyleManager.Section_Label, GUILayout.Width(300));

            GUILayout.BeginVertical(TMP_UIStyleManager.TextureAreaBox, GUILayout.Width(300));
            EditorGUIUtility.LookLikeControls(120f, 160f);

            // FONT TTF SELECTION
            font_TTF = EditorGUILayout.ObjectField("Font Source", font_TTF, typeof(Font), false, GUILayout.Width(290)) as Font;

            // FONT SIZING
            if (FontSizingOption_Selection == 0)
            {
                FontSizingOption_Selection = EditorGUILayout.Popup("Font Size", FontSizingOption_Selection, FontSizingOptions, GUILayout.Width(290));
            }
            else
            {
                EditorGUIUtility.LookLikeControls(120f, 40f);
                GUILayout.BeginHorizontal(GUILayout.Width(290));
                FontSizingOption_Selection = EditorGUILayout.Popup("Font Size", FontSizingOption_Selection, FontSizingOptions, GUILayout.Width(225));
                font_size = EditorGUILayout.IntField(font_size);
                GUILayout.EndHorizontal();
            }

            EditorGUIUtility.LookLikeControls(120f, 160f);

            
            // FONT PADDING
            font_padding = EditorGUILayout.IntField("Font Padding", font_padding, GUILayout.Width(290));
            font_padding = (int)Mathf.Clamp(font_padding, 0f, 64f);

            // FONT PACKING METHOD SELECTION
            m_fontPackingSelection = (FontPackingModes)EditorGUILayout.EnumPopup("Packing Method", m_fontPackingSelection, GUILayout.Width(225));
            
            //font_renderingMode = (FontRenderingMode)EditorGUILayout.EnumPopup("Rendering Mode", font_renderingMode, GUILayout.Width(290));

            // FONT ATLAS RESOLUTION SELECTION
            GUILayout.BeginHorizontal(GUILayout.Width(290));
            GUI.changed = false;
            EditorGUIUtility.LookLikeControls(120f, 40f);

            GUILayout.Label("Atlas Resolution:", GUILayout.Width(116));
            font_atlas_width = EditorGUILayout.IntPopup(font_atlas_width, FontResolutionLabels, FontAtlasResolutions); //, GUILayout.Width(80));
            font_atlas_height = EditorGUILayout.IntPopup(font_atlas_height, FontResolutionLabels, FontAtlasResolutions); //, GUILayout.Width(80));

            GUILayout.EndHorizontal();


            // FONT CHARACTER SET SELECTION
            GUI.changed = false;
            font_CharacterSet_Selection = EditorGUILayout.Popup("Character Set", font_CharacterSet_Selection, FontCharacterSets, GUILayout.Width(290));
            if (GUI.changed)
            {
                characterSequence = "";
                //Debug.Log("Resetting Sequence!");
            }

            switch (font_CharacterSet_Selection)
            {
                case 0: // ASCII
                    //characterSequence = "32 - 126, 130, 132 - 135, 139, 145 - 151, 153, 155, 161, 166 - 167, 169 - 174, 176, 181 - 183, 186 - 187, 191, 8210 - 8226, 8230, 8240, 8242 - 8244, 8249 - 8250, 8252 - 8254, 8260, 8286";
                    characterSequence = "32 - 126, 8230";
                    break;

                case 1: // EXTENDED ASCII
                    characterSequence = "32 - 126, 160 - 255, 8210 - 8226, 8230, 8240, 8242 - 8244, 8249 - 8250, 8252, 8254, 8260, 8286, 8364";
                    break;

                case 2: // Lowercase
                    characterSequence = "32 - 64, 91 - 126";
                    break;

                case 3: // Uppercase
                    characterSequence = "32 - 96, 123 - 126";
                    break;

                case 4: // Numbers & Symbols
                    characterSequence = "32 - 64, 91 - 96, 123 - 126";
                    break;

                case 5: // Custom Range
                    GUILayout.BeginHorizontal(GUILayout.Width(290));
                    GUILayout.Label("Custom Range (Dec)", GUILayout.Width(116));

                    // Filter out unwanted characters.
                    char chr = Event.current.character;
                    if ((chr < '0' || chr > '9') && (chr < ',' || chr > '-'))
                    {
                        Event.current.character = '\0';
                    }
                    characterSequence = EditorGUILayout.TextArea(characterSequence, TMP_UIStyleManager.TextAreaBoxWindow, GUILayout.Height(32), GUILayout.MaxWidth(170));

                    GUILayout.EndHorizontal();
                    break;

                case 6: // Unicode HEX Range
                    GUILayout.BeginHorizontal(GUILayout.Width(290));
                    GUILayout.Label("Unicode Range (Hex)", GUILayout.Width(116));

                    // Filter out unwanted characters.
                    chr = Event.current.character;
                    if ((chr < '0' || chr > '9') && (chr < 'a' || chr > 'f') && (chr < 'A' || chr > 'F') && (chr < ',' || chr > '-'))
                    {
                        Event.current.character = '\0';
                    }
                    characterSequence = EditorGUILayout.TextArea(characterSequence, TMP_UIStyleManager.TextAreaBoxWindow, GUILayout.Height(32), GUILayout.MaxWidth(170));

                    GUILayout.EndHorizontal();
                    break;


                case 7: // Custom Characters
                    GUILayout.BeginHorizontal(GUILayout.Width(290));

                    GUILayout.Label("Custom Characters", GUILayout.Width(116));
                    characterSequence = EditorGUILayout.TextArea(characterSequence, TMP_UIStyleManager.TextAreaBoxWindow, GUILayout.Height(32), GUILayout.MaxWidth(170));
                    GUILayout.EndHorizontal();
                    break;

                case 8: // Character List from File
                    characterList = EditorGUILayout.ObjectField("Character File", characterList, typeof(TextAsset), false, GUILayout.Width(290)) as TextAsset;
                    if (characterList != null)
                    {
                        characterSequence = characterList.text;
                    }
                    break;
            }

            EditorGUIUtility.LookLikeControls(120f, 40f);

            // FONT STYLE SELECTION
            GUILayout.BeginHorizontal(GUILayout.Width(290));
            font_style = (FaceStyles)EditorGUILayout.EnumPopup("Font Style:", font_style, GUILayout.Width(225));
            font_style_mod = EditorGUILayout.IntField((int)font_style_mod);
            GUILayout.EndHorizontal();

            // Render Mode Selection   
            font_renderMode = (RenderModes)EditorGUILayout.EnumPopup("Font Render Mode:", font_renderMode, GUILayout.Width(290));

            includeKerningPairs = EditorGUILayout.Toggle("Get Kerning Pairs?", includeKerningPairs, GUILayout.MaxWidth(290));

            EditorGUIUtility.LookLikeControls(120f, 160f);

            GUILayout.Space(20);

            GUI.enabled = font_TTF == null || isProcessing ? false : true;    // Enable Preview if we are not already rendering a font.
            if (GUILayout.Button("Generate Font Atlas", GUILayout.Width(290)) && characterSequence.Length != 0 && GUI.enabled)
            {
                if (font_TTF != null)
                {
                    int error_Code;

                    error_Code = TMPro_FontPlugin.Initialize_FontEngine(); // Initialize Font Engine
                    if (error_Code != 0)
                    {
                        if (error_Code == 99)
                        {
                            //Debug.Log("Font Library was already initialized!");
                            error_Code = 0;
                        }
                        else
                            Debug.Log("Error Code: " + error_Code + "  occurred while Initializing the FreeType Library.");
                    }

                    string fontPath = AssetDatabase.GetAssetPath(font_TTF); // Get file path of TTF Font.

                    if (error_Code == 0)
                    {
                        error_Code = TMPro_FontPlugin.Load_TrueType_Font(fontPath); // Load the selected font.

                        if (error_Code != 0)
                        {
                            if (error_Code == 99)
                            {
                                //Debug.Log("Font was already loaded!");
                                error_Code = 0;
                            }
                            else
                                Debug.Log("Error Code: " + error_Code + "  occurred while Loading the font.");
                        }
                    }

                    if (error_Code == 0)
                    {
                        if (FontSizingOption_Selection == 0) font_size = 72; // If Auto set size to 72 pts.

                        error_Code = TMPro_FontPlugin.FT_Size_Font(font_size); // Load the selected font and size it accordingly.
                        if (error_Code != 0)
                            Debug.Log("Error Code: " + error_Code + "  occurred while Sizing the font.");
                    }

                    // Define an array containing the characters we will render.
                    if (error_Code == 0)
                    {
                        int[] character_Set = null;
                        if (font_CharacterSet_Selection == 7 || font_CharacterSet_Selection == 8)
                        {
                            List<int> char_List = new List<int>();
                            
                            for (int i = 0; i < characterSequence.Length; i++)
                            {
                                // Check to make sure we don't include duplicates
                                if (char_List.FindIndex(item => item == characterSequence[i]) == -1)
                                    char_List.Add(characterSequence[i]);
                                else
                                {
                                    //Debug.Log("Character [" + characterSequence[i] + "] is a duplicate.");
                                }
                            }

                            character_Set = char_List.ToArray();
                        }
                        else if (font_CharacterSet_Selection == 6)
                        {
                            character_Set = ParseHexNumberSequence(characterSequence);
                        }
                        else
                        {
                            character_Set = ParseNumberSequence(characterSequence);
                        }

                        m_character_Count = character_Set.Length;

                        m_texture_buffer = new byte[font_atlas_width * font_atlas_height];

                        m_font_faceInfo = new FT_FaceInfo();

                        m_font_glyphInfo = new FT_GlyphInfo[m_character_Count];

                        int padding = font_padding;

                        bool autoSizing = FontSizingOption_Selection == 0 ? true : false;

                        float strokeSize = font_style_mod;
                        if (font_renderMode == RenderModes.DistanceField16) strokeSize = font_style_mod * 16;
                        if (font_renderMode == RenderModes.DistanceField32) strokeSize = font_style_mod * 32;
                        
                        isProcessing = true;
                        
                        ThreadPool.QueueUserWorkItem(SomeTask =>
                        {
                            isRenderingDone = false;
                            
                            error_Code = TMPro_FontPlugin.Render_Characters(m_texture_buffer, font_atlas_width, font_atlas_height, padding, character_Set, m_character_Count, font_style, strokeSize, autoSizing, font_renderMode,(int)m_fontPackingSelection, ref m_font_faceInfo, m_font_glyphInfo);
                            isRenderingDone = true;
                            //Debug.Log("Font Rendering is completed.");
                        });
                        
                        previewSelection = PreviewSelectionTypes.PreviewFont;
                        
                    }
                }
            }


            // FONT RENDERING PROGRESS BAR
            GUILayout.Space(1);
            progressRect = GUILayoutUtility.GetRect(288, 20, TMP_UIStyleManager.TextAreaBoxWindow, GUILayout.Width(288), GUILayout.Height(20));

            GUI.BeginGroup(progressRect);
            GUI.DrawTextureWithTexCoords(new Rect(2, 0, 288, 20), TMP_UIStyleManager.progressTexture, new Rect(1 - m_renderingProgress, 0, 1, 1));
            GUI.EndGroup();


            // FONT STATUS & INFORMATION
            GUISkin skin = GUI.skin;
            GUI.skin = TMP_UIStyleManager.TMP_GUISkin;

            GUILayout.Space(5);
            GUILayout.BeginVertical(TMP_UIStyleManager.TextAreaBoxWindow);
            output_ScrollPosition = EditorGUILayout.BeginScrollView(output_ScrollPosition, GUILayout.Height(145));
            EditorGUILayout.LabelField(output_feedback, TMP_UIStyleManager.Label);
            EditorGUILayout.EndScrollView();
            GUILayout.EndVertical();

            GUI.skin = skin;

            GUILayout.Space(10);


            // SAVE TEXTURE & CREATE and SAVE FONT XML FILE
            GUI.enabled = m_font_Atlas != null ? true : false;    // Enable Save Button if font_Atlas is not Null.
            if (GUILayout.Button("Save TextMeshPro Font Asset", GUILayout.Width(290)) && GUI.enabled)
            {
                string filePath = string.Empty;

                if (font_renderMode < RenderModes.DistanceField16) // == RenderModes.HintedSmooth || font_renderMode == RenderModes.RasterHinted)
                {
                    filePath = EditorUtility.SaveFilePanel("Save TextMesh Pro! Font Asset File", new FileInfo(AssetDatabase.GetAssetPath(font_TTF)).DirectoryName, font_TTF.name, "asset");

                    if (filePath.Length == 0)
                        return;

                    Save_Normal_FontAsset(filePath);
                }
                else if (font_renderMode >= RenderModes.DistanceField16)
                {
                    filePath = EditorUtility.SaveFilePanel("Save TextMesh Pro! Font Asset File", new FileInfo(AssetDatabase.GetAssetPath(font_TTF)).DirectoryName, font_TTF.name + " SDF", "asset");

                    if (filePath.Length == 0)
                        return;

                    Save_SDF_FontAsset(filePath);
                }

            }

            GUI.enabled = true; // Re-enable GUI

            GUILayout.Space(5);

            GUILayout.EndVertical();

            GUILayout.Space(25);

            /*
            // GENERATE DISTANCE FIELD TEXTURE
            GUILayout.Label("Distance Field Options", SectionLabel, GUILayout.Width(300));

            GUILayout.BeginVertical(textureAreaBox, GUILayout.Width(300));

            GUILayout.Space(5);


            font_spread = EditorGUILayout.IntField("Spread", font_spread, GUILayout.Width(280));
            font_scaledownFactor = EditorGUILayout.IntField("Scale down factor", font_scaledownFactor, GUILayout.Width(280));
            if (GUI.changed)
            {
                EditorPrefs.SetInt("Font_Spread", font_spread);
                EditorPrefs.SetInt("Font_ScaleDownFactor", font_scaledownFactor);
            }

            GUILayout.Space(20);

            GUI.enabled = m_font_Atlas != null ? true : false;    // Enable Save Button if font_Atlas is not Null.
            if (GUILayout.Button("Preview Distance Field Font Atlas", GUILayout.Width(290)))
            {

                if (m_font_Atlas != null && isProcessing == false)
                {
                    // Generate Distance Field	                 
                    int width = m_font_Atlas.width;
                    int height = m_font_Atlas.height;
                    Color[] colors = m_font_Atlas.GetPixels(); // Should modify this to use Color32 instead

                    isProcessing = true;

                    ThreadPool.QueueUserWorkItem(SomeTask => { TMPro_DistanceTransform.Generate(colors, width, height, font_spread, font_scaledownFactor); });

                    previewSelection = PreviewSelectionTypes.PreviewDistanceField;
                }
            }

            GUILayout.Space(1);

            progressRect = GUILayoutUtility.GetRect(290, 20, textAreaBox, GUILayout.Width(290), GUILayout.Height(20));

            GUI.BeginGroup(progressRect);

            GUI.DrawTextureWithTexCoords(new Rect(0, 0, 290, 20), progressTexture, new Rect(1 - ProgressPercentage, 0, 1, 1));
            GUI.EndGroup();

            //GUILayout.Space(5);

            GUI.enabled = m_destination_Atlas != null ? true : false;    // Enable Save Button if font_Atlas is not Null.
            if (GUILayout.Button("Save TextMeshPro (SDF) Font Asset", GUILayout.Width(290)))
            {
                string filePath = EditorUtility.SaveFilePanel("Save TextMesh Pro! Font Asset File", new FileInfo(AssetDatabase.GetAssetPath(font_TTF)).DirectoryName, font_TTF.name + " SDF", "asset");

                if (filePath.Length == 0)
                    return;

                Save_SDF_FontAsset(filePath);

            } 
                         
            GUILayout.EndVertical();
            */
             
            // Figure out the size of the current UI Panel
            Rect rect = EditorGUILayout.GetControlRect(false, 5);
            if (Event.current.type == EventType.Repaint)
                m_UI_Panel_Size = rect;

            GUILayout.EndVertical();
        }
 public static extern int Render_Characters(byte[] buffer, int buffer_width, int buffer_height, int character_padding, int[] asc_set, int char_count, FaceStyles style, float style_mod, bool autoSize, RenderModes renderMode, int method, ref FT_FaceInfo fontData, FT_GlyphInfo[] Output);