Exemplo n.º 1
0
 static TexFontSigned Convert(TexAsset asset)
 {
     if (asset.type == TexAssetType.FontSigned)
     {
         return((TexFontSigned)asset);
     }
     else
     {
         var n = ScriptableObject.CreateInstance <TexFontSigned>();
         asset.ImportDictionary();
         EditorJsonUtility.FromJsonOverwrite(EditorJsonUtility.ToJson(asset), n);
         n.rawpath = AssetDatabase.GetAssetPath(((TexFont)asset).asset);
         var main = TEXPreference.main;
         main.fontnames[asset.id] = main.fonts[Array.IndexOf(main.fonts, asset)] = n;
         AssetDatabase.CreateAsset(n, AssetDatabase.GetAssetPath(asset));
         return(n);
     }
 }
Exemplo n.º 2
0
        public static void CreateSDFAsset(TexAsset asset)
        {
            switch (asset.type)
            {
            case TexAssetType.Font:
                var font = ((TexFont)asset).asset;
                CreateSDFAsset(asset, font, AssetDatabase.GetAssetPath(font));
                break;

            case TexAssetType.Sprite:
                // WTF?
                break;

            case TexAssetType.FontSigned:
                var path = ((TexFontSigned)asset).rawpath;
                CreateSDFAsset(asset, AssetDatabase.LoadAssetAtPath <Font>(path), path);
                break;
            }
        }
Exemplo n.º 3
0
        private static TexAsset CreateAndRecover <T>(string metaPath) where T : TexAsset
        {
            TexAsset metadata = ScriptableObject.CreateInstance <T>();

            {
                // it may saved in another format. try to recover.
                var mt2 = AssetDatabase.LoadAssetAtPath <TexAsset>(metaPath);
                if (mt2)
                {
                    EditorJsonUtility.FromJsonOverwrite(EditorJsonUtility.ToJson(mt2), metadata);
                    AssetDatabase.DeleteAsset(metaPath);
                }
                else
                {
                    metadata.ImportCharacters(TexCharPresets.legacyChars);
                }
            }
            return(metadata);
        }
Exemplo n.º 4
0
        public static void SetupGUI(TexAsset asset)
        {
            EditorGUI.BeginDisabledGroup(onRendering);
            GUILayout.BeginHorizontal();
            GUI.changed = false;

            GUILayout.Label("Atlas Resolution", GUILayout.Width(EditorGUIUtility.labelWidth));
            _bufferWidth  = EditorGUILayout.IntPopup(_bufferWidth, FontResolutionLabels, FontAtlasResolutions);  //, GUILayout.Width(80));
            _bufferHeight = EditorGUILayout.IntPopup(_bufferHeight, FontResolutionLabels, FontAtlasResolutions); //, GUILayout.Width(80));

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Padding", GUILayout.Width(EditorGUIUtility.labelWidth));
            _padding = EditorGUILayout.IntSlider(_padding, 1, 10);
            GUILayout.EndHorizontal();

            _render = (RenderModes)EditorGUILayout.EnumPopup(_render);
            if (GUILayout.Button("Render"))
            {
                CreateSDFAsset(asset);
            }
            EditorGUI.BeginDisabledGroup(onRendering || asset.type != TexAssetType.FontSigned);
            GUI.backgroundColor = Color.red;

            if (GUILayout.Button("Delete SDF Asset"))
            {
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(((TexFontSigned)asset).asset));
                asset = Deconvert((TexFontSigned)asset);
            }

            GUI.backgroundColor = Color.white;
            EditorGUI.EndDisabledGroup();
            {
                EditorGUILayout.Space();
                if (GUILayout.Button("Render All Fonts"))
                {
                    onRenderingBatch = (EditorUtility.DisplayDialogComplex("Confirm Action", "Are you sure? This will take few moments.\nAnd what will you do with existing SDF Asset?",
                                                                           "Overwrite", "Cancel", "Skip (Faster)"));
                    onRenderingBatch = onRenderingBatch == 1 ? 0 : (onRenderingBatch == 0 ? 1 : 2);
                    if (onRenderingBatch > 0)
                    {
                        DoBatchRendering();
                    }
                }
                EditorGUI.BeginDisabledGroup(onRendering);
                GUI.backgroundColor = new Color(0.5f, 0, 0.5f);
                if (GUILayout.Button("Delete All SDF Asset"))
                {
                    if (EditorUtility.DisplayDialog("Confirm Deletion", "Are you sure you want to delete ALL SDF Font Asset?", "Yes", "No"))
                    {
                        var fonts = TEXPreference.main.fonts;
                        for (int i = 0; i < fonts.Length; i++)
                        {
                            EditorUtility.DisplayProgressBar("Please wait", "Reimporting Fonts...", i / (float)fonts.Length);
                            if (fonts[i].type == TexAssetType.FontSigned)
                            {
                                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(((TexFontSigned)fonts[i]).asset));
                                Deconvert((TexFontSigned)fonts[i]);
                            }
                        }
                        TEXPreference.main.RebuildMaterial();
                        EditorUtility.ClearProgressBar();
                    }
                }
                GUI.backgroundColor = Color.white;
                EditorGUI.EndDisabledGroup();
            }

            EditorGUI.EndDisabledGroup();
            if (onRendering)
            {
                var prog = TMPro_FontPlugin.Check_RenderProgress();
                EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(), prog, prog.ToString("P"));
                if (onRenderingBatch > 0)
                {
                    if (onRenderingBatch == 3)
                    {
                        EditorGUILayout.HelpBox("Will be stopped after current rendering is done", MessageType.Info);
                    }
                    else
                    {
                        GUI.backgroundColor = Color.yellow;
                        if (GUILayout.Button("Cancel"))
                        {
                            onRenderingBatch = 3;
                        }
                        GUI.backgroundColor = Color.white;
                    }
                }
            }
        }
Exemplo n.º 5
0
        public static void CreateSDFAsset(TexAsset asset, Font font, string fontPath)
        {
            // initialize
            CheckErr(TMPro_FontPlugin.Initialize_FontEngine());
            CheckErr(TMPro_FontPlugin.Load_TrueType_Font(fontPath));
            CheckErr(TMPro_FontPlugin.FT_Size_Font(72));

            _buffers = new byte[_bufferWidth * _bufferHeight];
            var charSet = new int[asset.chars.Length];

            _glyphsInfo = new FT_GlyphInfo[asset.chars.Length];
            _atlasInfo  = null;

            for (int i = 0; i < asset.chars.Length; i++)
            {
                charSet[i] = asset.chars[i].characterIndex;
            }

            float strokeSize = _styleStrokeSize;

            if (_render == RenderModes.DistanceField16)
            {
                strokeSize = _styleStrokeSize * 16;
            }
            if (_render == RenderModes.DistanceField32)
            {
                strokeSize = _styleStrokeSize * 32;
            }

            ThreadPool.QueueUserWorkItem(SomeTask =>
            {
                try
                {
                    onRendering = true;

                    CheckErr(TMPro_FontPlugin.Render_Characters(_buffers, _bufferWidth, _bufferHeight, _padding,
                                                                charSet, charSet.Length, _style, strokeSize, true, _render, _optimized ? 4 : 0,
                                                                ref _faceInfo, _glyphsInfo));

                    EditorApplication.delayCall += delegate()
                    {
                        try
                        {
                            OnFinished(Convert(asset));
                        }
                        catch (Exception ex)
                        {
                            Debug.LogException(ex);
                            Debug.LogWarningFormat("Failed to create SDF File for {0}.ttf. Consider delete and rerender again", asset.id);
                        }
                        finally
                        {
                            hasRendered = false;

                            if (onRendering)
                            {
                                //CheckErr(TMPro_FontPlugin.Destroy_FontEngine());
                                onRendering = false;
                            }

                            if (onRenderingBatch > 0)
                            {
                                isRenderingBatchFinished = true;
                            }
                        }
                    };
                    Debug.LogFormat("Font Rendering of {0}.ttf has completed.", asset.id);
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                    Debug.LogWarningFormat("Failed to create Render {0}.ttf. Consider rerender again", asset.id);
                }
                finally
                {
                    if (onRendering)
                    {
                        //CheckErr(TMPro_FontPlugin.Destroy_FontEngine());
                        onRendering = false;
                    }
                }
            });
        }
Exemplo n.º 6
0
        static void LoadPrimaryDefinitionSubset <T>(TEXPreference pref, List <TexAsset> fontList, string folderPath, string typeStr, TexAssetType typeEnum, int mode) where T : TexAsset
        {
            if (!AssetDatabase.IsValidFolder(folderPath))
            {
                return;
            }
            string[] customF = AssetDatabase.FindAssets(typeStr, new string[] { folderPath });
            for (int i = 0; i < customF.Length; i++)
            {
                if (fontList.Count >= 31)
                {
                    Debug.LogWarning("Font/Sprite database count are beyond 31, ignoring any assets after " + fontList[fontList.Count - 1].id);
                    break;
                }
                string realPath = AssetDatabase.GUIDToAssetPath(customF[i]);
                string id       = Path.GetFileNameWithoutExtension(realPath).ToLower();

                if (fontList.Any((x) => x.id == id))
                {
                    continue;
                }

                if (!isNameValid(id))
                {
                    // We show this for information purpose, since *-Regular or *_ is very common mistake
                    // We are not showing this for frequent update, since this behavior is 'intended' for giving an alternative styling
                    if (id.Contains("-Regular") || id.Substring(id.Length - 1) == "_")
                    {
                        Debug.LogWarning("File " + id + " is ignored since it has invalid character(s) in its name");
                    }
                    continue;
                }

                UpdateProgress(mode, id, i, customF.Length);

                string   json     = null;
                var      metaPath = pref.MainFolderPath + resourceFontMetaPath + id + ".asset";
                TexAsset metadata = AssetDatabase.LoadAssetAtPath <T>(metaPath);

#if TEXDRAW_TMP
                string sdfPath = pref.MainFolderPath + "/Fonts/TMPro/" + id + ".asset";
                if (typeEnum == TexAssetType.Font && File.Exists(sdfPath))
                {
                    metadata = AssetDatabase.LoadAssetAtPath <TexFontSigned>(metaPath);
                    if (!metadata)
                    {
                        metadata = CreateAndRecover <TexFontSigned>(metaPath);
                        ((TexFontSigned)metadata).rawpath = realPath;
                        AssetDatabase.CreateAsset(metadata, metaPath);
                    }
                    realPath = sdfPath;
                }
#endif
                if (!metadata)
                {
                    metadata = CreateAndRecover <T>(metaPath);
                    AssetDatabase.CreateAsset(metadata, metaPath);
                }
                else
                {
                    json = JsonUtility.ToJson(metadata);
                }

                metadata.id    = id;
                metadata.index = fontList.Count;
                metadata.ImportAsset(realPath);
                metadata.ImportDictionary();

                if (json != JsonUtility.ToJson(metadata))
                {
                    // this is necessary to avoid messing with Git version control
                    EditorUtility.SetDirty(metadata);
                }

                fontList.Add(metadata);
            }
        }