Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="glyphInfo"></param>
        public void AddGlyphInfo(TMP_Glyph[] glyphInfo)
        {
            m_glyphInfoList = new List <TMP_Glyph>();
            int characterCount = glyphInfo.Length;

            m_fontInfo.CharacterCount = characterCount;
            m_characterSet            = new int[characterCount];

            for (int i = 0; i < characterCount; i++)
            {
                TMP_Glyph g = new TMP_Glyph();
                g.id       = glyphInfo[i].id;
                g.x        = glyphInfo[i].x;
                g.y        = glyphInfo[i].y;
                g.width    = glyphInfo[i].width;
                g.height   = glyphInfo[i].height;
                g.xOffset  = glyphInfo[i].xOffset;
                g.yOffset  = (glyphInfo[i].yOffset);
                g.xAdvance = glyphInfo[i].xAdvance;
                g.scale    = 1;

                m_glyphInfoList.Add(g);

                // While iterating through list of glyphs, find the Descender & Ascender for this GlyphSet.
                //m_fontInfo.Ascender = Mathf.Max(m_fontInfo.Ascender, glyphInfo[i].yOffset);
                //m_fontInfo.Descender = Mathf.Min(m_fontInfo.Descender, glyphInfo[i].yOffset - glyphInfo[i].height);
                //Debug.Log(m_fontInfo.Ascender + "  " + m_fontInfo.Descender);
                m_characterSet[i] = g.id; // Add Character ID to Array to make it easier to get the kerning pairs.
            }

            // Sort List by ID.
            m_glyphInfoList = m_glyphInfoList.OrderBy(s => s.id).ToList();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Function to create a deep copy of a GlyphInfo.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TMP_Glyph Clone(TMP_Glyph source)
        {
            TMP_Glyph copy = new TMP_Glyph();

            copy.id       = source.id;
            copy.x        = source.x;
            copy.y        = source.y;
            copy.width    = source.width;
            copy.height   = source.height;
            copy.xOffset  = source.xOffset;
            copy.yOffset  = source.yOffset;
            copy.xAdvance = source.xAdvance;
            copy.scale    = source.scale;

            return(copy);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        public void ReadFontDefinition()
        {
            //Debug.Log("Reading Font Definition for " + this.name + ".");
            // Make sure that we have a Font Asset file assigned.
            if (m_fontInfo == null)
            {
                return;
            }

            // Check Font Asset type
            //Debug.Log(name + "   " + fontAssetType);

            // Create new instance of GlyphInfo Dictionary for fast access to glyph info.
            m_characterDictionary = new Dictionary <int, TMP_Glyph>();
            for (int i = 0; i < m_glyphInfoList.Count; i++)
            {
                TMP_Glyph glyph = m_glyphInfoList[i];

                if (!m_characterDictionary.ContainsKey(glyph.id))
                {
                    m_characterDictionary.Add(glyph.id, glyph);
                }

                // Compatibility
                if (glyph.scale == 0)
                {
                    glyph.scale = 1;
                }
            }


            //Debug.Log("PRE: BaseLine:" + m_fontInfo.Baseline + "  Ascender:" + m_fontInfo.Ascender + "  Descender:" + m_fontInfo.Descender); // + "  Centerline:" + m_fontInfo.CenterLine);

            TMP_Glyph temp_charInfo = new TMP_Glyph();

            // Add Character (10) LineFeed, (13) Carriage Return & Space (32) to Dictionary if they don't exists.
            if (m_characterDictionary.ContainsKey(32))
            {
                m_characterDictionary[32].width   = m_characterDictionary[32].xAdvance; // m_fontInfo.Ascender / 5;
                m_characterDictionary[32].height  = m_fontInfo.Ascender - m_fontInfo.Descender;
                m_characterDictionary[32].yOffset = m_fontInfo.Ascender;
                m_characterDictionary[32].scale   = 1;
            }
            else
            {
                //Debug.Log("Adding Character 32 (Space) to Dictionary for Font (" + m_fontInfo.Name + ").");
                temp_charInfo          = new TMP_Glyph();
                temp_charInfo.id       = 32;
                temp_charInfo.x        = 0;
                temp_charInfo.y        = 0;
                temp_charInfo.width    = m_fontInfo.Ascender / 5;
                temp_charInfo.height   = m_fontInfo.Ascender - m_fontInfo.Descender;
                temp_charInfo.xOffset  = 0;
                temp_charInfo.yOffset  = m_fontInfo.Ascender;
                temp_charInfo.xAdvance = m_fontInfo.PointSize / 4;
                temp_charInfo.scale    = 1;
                m_characterDictionary.Add(32, temp_charInfo);
            }

            // Add Non-Breaking Space (160)
            if (!m_characterDictionary.ContainsKey(160))
            {
                temp_charInfo = TMP_Glyph.Clone(m_characterDictionary[32]);
                m_characterDictionary.Add(160, temp_charInfo);
            }

            // Add Zero Width Space (8203)
            if (!m_characterDictionary.ContainsKey(8203))
            {
                temp_charInfo          = TMP_Glyph.Clone(m_characterDictionary[32]);
                temp_charInfo.width    = 0;
                temp_charInfo.xAdvance = 0;
                m_characterDictionary.Add(8203, temp_charInfo);
            }

            //Add Zero Width no-break space (8288)
            if (!m_characterDictionary.ContainsKey(8288))
            {
                temp_charInfo          = TMP_Glyph.Clone(m_characterDictionary[32]);
                temp_charInfo.width    = 0;
                temp_charInfo.xAdvance = 0;
                m_characterDictionary.Add(8288, temp_charInfo);
            }

            // Add Linefeed (10)
            if (m_characterDictionary.ContainsKey(10) == false)
            {
                //Debug.Log("Adding Character 10 (Linefeed) to Dictionary for Font (" + m_fontInfo.Name + ").");

                temp_charInfo          = new TMP_Glyph();
                temp_charInfo.id       = 10;
                temp_charInfo.x        = 0;  // m_characterDictionary[32].x;
                temp_charInfo.y        = 0;  // m_characterDictionary[32].y;
                temp_charInfo.width    = 10; // m_characterDictionary[32].width;
                temp_charInfo.height   = m_characterDictionary[32].height;
                temp_charInfo.xOffset  = 0;  // m_characterDictionary[32].xOffset;
                temp_charInfo.yOffset  = m_characterDictionary[32].yOffset;
                temp_charInfo.xAdvance = 0;
                temp_charInfo.scale    = 1;
                m_characterDictionary.Add(10, temp_charInfo);

                if (!m_characterDictionary.ContainsKey(13))
                {
                    m_characterDictionary.Add(13, temp_charInfo);
                }
            }

            // Add Tab Character to Dictionary. Tab is Tab Size * Space Character Width.
            if (m_characterDictionary.ContainsKey(9) == false)
            {
                //Debug.Log("Adding Character 9 (Tab) to Dictionary for Font (" + m_fontInfo.Name + ").");

                temp_charInfo          = new TMP_Glyph();
                temp_charInfo.id       = 9;
                temp_charInfo.x        = m_characterDictionary[32].x;
                temp_charInfo.y        = m_characterDictionary[32].y;
                temp_charInfo.width    = m_characterDictionary[32].width * tabSize + (m_characterDictionary[32].xAdvance - m_characterDictionary[32].width) * (tabSize - 1);
                temp_charInfo.height   = m_characterDictionary[32].height;
                temp_charInfo.xOffset  = m_characterDictionary[32].xOffset;
                temp_charInfo.yOffset  = m_characterDictionary[32].yOffset;
                temp_charInfo.xAdvance = m_characterDictionary[32].xAdvance * tabSize;
                temp_charInfo.scale    = 1;
                m_characterDictionary.Add(9, temp_charInfo);
            }

            // Centerline is located at the center of character like { or in the middle of the lowercase o.
            //m_fontInfo.CenterLine = m_characterDictionary[111].yOffset - m_characterDictionary[111].height * 0.5f;

            // Tab Width is using the same xAdvance as space (32).
            m_fontInfo.TabWidth = m_characterDictionary[9].xAdvance;

            // Set Cap Height
            if (m_fontInfo.CapHeight == 0 && m_characterDictionary.ContainsKey(72))
            {
                m_fontInfo.CapHeight = m_characterDictionary[72].yOffset;
            }

            // Adjust Font Scale for compatibility reasons
            if (m_fontInfo.Scale == 0)
            {
                m_fontInfo.Scale = 1.0f;
            }

            // Set Strikethrough Offset (if needed)
            if (m_fontInfo.strikethrough == 0)
            {
                m_fontInfo.strikethrough = m_fontInfo.CapHeight / 2.5f;
            }

            // Set Padding value for legacy font assets.
            if (m_fontInfo.Padding == 0)
            {
                if (material.HasProperty(ShaderUtilities.ID_GradientScale))
                {
                    m_fontInfo.Padding = material.GetFloat(ShaderUtilities.ID_GradientScale) - 1;
                }
            }

            // Populate Dictionary with Kerning Information
            m_kerningDictionary = new Dictionary <int, KerningPair>();
            List <KerningPair> pairs = m_kerningInfo.kerningPairs;

            //Debug.Log(m_fontInfo.Name + " has " + pairs.Count +  " Kerning Pairs.");
            for (int i = 0; i < pairs.Count; i++)
            {
                KerningPair pair = pairs[i];

                // Convert legacy kerning data
                if (pair.xOffset != 0)
                {
                    pairs[i].ConvertLegacyKerningData();
                }

                KerningPairKey uniqueKey = new KerningPairKey(pair.firstGlyph, pair.secondGlyph);

                if (m_kerningDictionary.ContainsKey((int)uniqueKey.key) == false)
                {
                    m_kerningDictionary.Add((int)uniqueKey.key, pair);
                }
                else
                {
                    if (!TMP_Settings.warningsDisabled)
                    {
                        Debug.LogWarning("Kerning Key for [" + uniqueKey.ascii_Left + "] and [" + uniqueKey.ascii_Right + "] already exists.");
                    }
                }
            }


            // Compute Hashcode for the font asset name
            hashCode = TMP_TextUtilities.GetSimpleHashCode(this.name);

            // Compute Hashcode for the material name
            materialHashCode = TMP_TextUtilities.GetSimpleHashCode(material.name);

            // Unload font atlas texture
            //ShaderUtilities.GetShaderPropertyIDs();
            //Resources.UnloadAsset(material.GetTexture(ShaderUtilities.ID_MainTex));

            // Initialize Font Weights if needed
            //InitializeFontWeights();
        }
Exemplo n.º 4
0
        private static TMP_FontAsset SearchForGlyphInternal(List <TMP_FontAsset> fonts, int character, out TMP_Glyph glyph)
        {
            glyph = null;

            if (fonts != null && fonts.Count > 0)
            {
                for (int i = 0; i < fonts.Count; i++)
                {
                    TMP_FontAsset fontAsset = SearchForGlyphInternal(fonts[i], character, out glyph);

                    if (fontAsset != null)
                    {
                        return(fontAsset);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        private static TMP_FontAsset SearchForGlyphInternal(TMP_FontAsset font, int character, out TMP_Glyph glyph)
        {
            glyph = null;

            if (font == null)
            {
                return(null);
            }

            if (font.characterDictionary.TryGetValue(character, out glyph))
            {
                return(font);
            }
            else if (font.fallbackFontAssets != null && font.fallbackFontAssets.Count > 0)
            {
                for (int i = 0; i < font.fallbackFontAssets.Count && glyph == null; i++)
                {
                    TMP_FontAsset temp = font.fallbackFontAssets[i];
                    if (temp == null)
                    {
                        continue;
                    }

                    int id = temp.GetInstanceID();

                    // Skip over the fallback font asset in the event it is null or if already searched.
                    if (k_searchedFontAssets.Contains(id))
                    {
                        continue;
                    }

                    // Add to list of font assets already searched.
                    k_searchedFontAssets.Add(id);

                    temp = SearchForGlyphInternal(temp, character, out glyph);

                    if (temp != null)
                    {
                        return(temp);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Search through the given list of fonts and their possible fallbacks for the specified character.
 /// </summary>
 /// <param name="fonts"></param>
 /// <param name="character"></param>
 /// <param name="glyph"></param>
 /// <returns></returns>
 public static TMP_FontAsset SearchForGlyph(List <TMP_FontAsset> fonts, int character, out TMP_Glyph glyph)
 {
     return(SearchForGlyphInternal(fonts, character, out glyph));
 }
Exemplo n.º 7
0
        /// <summary>
        /// Search through the given font and its fallbacks for the specified character.
        /// </summary>
        /// <param name="font">The font asset to search for the given character.</param>
        /// <param name="character">The character to find.</param>
        /// <param name="glyph">out parameter containing the glyph for the specified character (if found).</param>
        /// <returns></returns>
        public static TMP_FontAsset SearchForGlyph(TMP_FontAsset font, int character, out TMP_Glyph glyph)
        {
            if (k_searchedFontAssets == null)
            {
                k_searchedFontAssets = new List <int>();
            }

            k_searchedFontAssets.Clear();

            return(SearchForGlyphInternal(font, character, out glyph));
        }