示例#1
0
        /// <summary>
        /// Pack glyphs in the given atlas size.
        /// </summary>
        /// <param name="glyphsToAdd">Glyphs to pack in atlas.</param>
        /// <param name="glyphsAdded">Glyphs packed in atlas.</param>
        /// <param name="padding">The padding between glyphs.</param>
        /// <param name="packingMode">The packing algorithm used to pack the glyphs.</param>
        /// <param name="renderMode">The glyph rendering mode.</param>
        /// <param name="width">The width of the target atlas texture.</param>
        /// <param name="height">The height of the target atlas texture.</param>
        /// <param name="freeGlyphRects">List of GlyphRects representing the available space in the atlas.</param>
        /// <param name="usedGlyphRects">List of GlyphRects representing the occupied space in the atlas.</param>
        /// <returns></returns>
        internal static bool TryPackGlyphsInAtlas(List <Glyph> glyphsToAdd, List <Glyph> glyphsAdded, int padding, GlyphPackingMode packingMode, GlyphRenderMode renderMode, int width, int height, List <GlyphRect> freeGlyphRects, List <GlyphRect> usedGlyphRects)
        {
            // Determine potential total allocations required for glyphs and glyph rectangles.
            int glyphsToAddCount   = glyphsToAdd.Count;
            int glyphsAddedCount   = glyphsAdded.Count;
            int freeGlyphRectCount = freeGlyphRects.Count;
            int usedGlyphRectCount = usedGlyphRects.Count;
            int totalCount         = glyphsToAddCount + glyphsAddedCount + freeGlyphRectCount + usedGlyphRectCount;

            // Make sure marshaling arrays allocations are appropriate.
            if (s_GlyphMarshallingStruct_IN.Length < totalCount || s_GlyphMarshallingStruct_OUT.Length < totalCount || s_FreeGlyphRects.Length < totalCount || s_UsedGlyphRects.Length < totalCount)
            {
                int newSize = Mathf.NextPowerOfTwo(totalCount + 1);
                s_GlyphMarshallingStruct_IN  = new GlyphMarshallingStruct[newSize];
                s_GlyphMarshallingStruct_OUT = new GlyphMarshallingStruct[newSize];
                s_FreeGlyphRects             = new GlyphRect[newSize];
                s_UsedGlyphRects             = new GlyphRect[newSize];
            }

            s_GlyphLookupDictionary.Clear();

            // Copy glyph data into appropriate marshaling array.
            for (int i = 0; i < totalCount; i++)
            {
                if (i < glyphsToAddCount)
                {
                    GlyphMarshallingStruct glyphStruct = new GlyphMarshallingStruct(glyphsToAdd[i]);

                    s_GlyphMarshallingStruct_IN[i] = glyphStruct;

                    // Add reference to glyph in lookup dictionary
                    if (s_GlyphLookupDictionary.ContainsKey(glyphStruct.index) == false)
                    {
                        s_GlyphLookupDictionary.Add(glyphStruct.index, glyphsToAdd[i]);
                    }
                }

                if (i < glyphsAddedCount)
                {
                    GlyphMarshallingStruct glyphStruct = new GlyphMarshallingStruct(glyphsAdded[i]);

                    s_GlyphMarshallingStruct_OUT[i] = glyphStruct;

                    // Add reference to glyph in lookup dictionary
                    if (s_GlyphLookupDictionary.ContainsKey(glyphStruct.index) == false)
                    {
                        s_GlyphLookupDictionary.Add(glyphStruct.index, glyphsAdded[i]);
                    }
                }

                if (i < freeGlyphRectCount)
                {
                    s_FreeGlyphRects[i] = freeGlyphRects[i];
                }

                if (i < usedGlyphRectCount)
                {
                    s_UsedGlyphRects[i] = usedGlyphRects[i];
                }
            }

            bool allGlyphsIncluded = TryPackGlyphsInAtlas_Internal(s_GlyphMarshallingStruct_IN, ref glyphsToAddCount, s_GlyphMarshallingStruct_OUT, ref glyphsAddedCount,
                                                                   padding, packingMode, renderMode, width, height,
                                                                   s_FreeGlyphRects, ref freeGlyphRectCount, s_UsedGlyphRects, ref usedGlyphRectCount);

            // Clear lists and / or re-allocate arrays.
            glyphsToAdd.Clear();
            glyphsAdded.Clear();
            freeGlyphRects.Clear();
            usedGlyphRects.Clear();

            // Copy marshaled glyph data back into the appropriate lists.
            for (int i = 0; i < totalCount; i++)
            {
                if (i < glyphsToAddCount)
                {
                    GlyphMarshallingStruct glyphStruct = s_GlyphMarshallingStruct_IN[i];
                    Glyph glyph = s_GlyphLookupDictionary[glyphStruct.index];

                    // Note: In theory, only new glyphRect x and y need to be copied.
                    glyph.metrics    = glyphStruct.metrics;
                    glyph.glyphRect  = glyphStruct.glyphRect;
                    glyph.scale      = glyphStruct.scale;
                    glyph.atlasIndex = glyphStruct.atlasIndex;

                    glyphsToAdd.Add(glyph);
                }

                if (i < glyphsAddedCount)
                {
                    GlyphMarshallingStruct glyphStruct = s_GlyphMarshallingStruct_OUT[i];
                    Glyph glyph = s_GlyphLookupDictionary[glyphStruct.index];

                    glyph.metrics    = glyphStruct.metrics;
                    glyph.glyphRect  = glyphStruct.glyphRect;
                    glyph.scale      = glyphStruct.scale;
                    glyph.atlasIndex = glyphStruct.atlasIndex;

                    glyphsAdded.Add(glyph);
                }

                if (i < freeGlyphRectCount)
                {
                    freeGlyphRects.Add(s_FreeGlyphRects[i]);
                }

                if (i < usedGlyphRectCount)
                {
                    usedGlyphRects.Add(s_UsedGlyphRects[i]);
                }
            }

            return(allGlyphsIncluded);
        }
示例#2
0
 internal extern static void RenderBufferToTexture(Texture2D srcTexture, int padding, GlyphRenderMode renderMode, Texture2D dstTexture);
示例#3
0
        /// <summary>
        /// Try to pack the given glyph into the given texture width and height.
        /// </summary>
        /// <param name="glyph">The glyph to try to pack.</param>
        /// <param name="padding">The padding between this glyph and other glyphs.</param>
        /// <param name="packingMode">The packing algorithm used to pack the glyphs.</param>
        /// <param name="renderMode">The glyph rendering mode.</param>
        /// <param name="width">The width of the target atlas texture.</param>
        /// <param name="height">The height of the target atlas texture.</param>
        /// <param name="freeGlyphRects">List of GlyphRects representing the available space in the atlas.</param>
        /// <param name="usedGlyphRects">List of GlyphRects representing the occupied space in the atlas.</param>
        /// <returns></returns>
        internal static bool TryPackGlyphInAtlas(Glyph glyph, int padding, GlyphPackingMode packingMode, GlyphRenderMode renderMode, int width, int height, List <GlyphRect> freeGlyphRects, List <GlyphRect> usedGlyphRects)
        {
            GlyphMarshallingStruct glyphStruct = new GlyphMarshallingStruct(glyph);

            int freeGlyphRectCount = freeGlyphRects.Count;
            int usedGlyphRectCount = usedGlyphRects.Count;
            int totalGlyphRects    = freeGlyphRectCount + usedGlyphRectCount;

            // Make sure marshalling arrays allocations are appropriate.
            if (s_FreeGlyphRects.Length < totalGlyphRects || s_UsedGlyphRects.Length < totalGlyphRects)
            {
                int newSize = Mathf.NextPowerOfTwo(totalGlyphRects + 1);
                s_FreeGlyphRects = new GlyphRect[newSize];
                s_UsedGlyphRects = new GlyphRect[newSize];
            }

            // Copy glyph rect data to marshalling arrays.
            int glyphRectCount = Mathf.Max(freeGlyphRectCount, usedGlyphRectCount);

            for (int i = 0; i < glyphRectCount; i++)
            {
                if (i < freeGlyphRectCount)
                {
                    s_FreeGlyphRects[i] = freeGlyphRects[i];
                }

                if (i < usedGlyphRectCount)
                {
                    s_UsedGlyphRects[i] = usedGlyphRects[i];
                }
            }

            if (TryPackGlyphInAtlas_Internal(ref glyphStruct, padding, packingMode, renderMode, width, height, s_FreeGlyphRects, ref freeGlyphRectCount, s_UsedGlyphRects, ref usedGlyphRectCount))
            {
                // Copy new glyph position to source glyph.
                glyph.glyphRect = glyphStruct.glyphRect;

                freeGlyphRects.Clear();
                usedGlyphRects.Clear();

                // Copy marshalled glyph rect data
                glyphRectCount = Mathf.Max(freeGlyphRectCount, usedGlyphRectCount);
                for (int i = 0; i < glyphRectCount; i++)
                {
                    if (i < freeGlyphRectCount)
                    {
                        freeGlyphRects.Add(s_FreeGlyphRects[i]);
                    }

                    if (i < usedGlyphRectCount)
                    {
                        usedGlyphRects.Add(s_UsedGlyphRects[i]);
                    }
                }

                return(true);
            }

            return(false);
        }
示例#4
0
 private static extern int RenderGlyphToTexture_Internal_Injected(ref GlyphMarshallingStruct glyphStruct, int padding, GlyphRenderMode renderMode, Texture2D texture);
示例#5
0
        void DrawGlyph(uint glyphIndex, Rect position, SerializedProperty property)
        {
            // Get a reference to the serialized object which can either be a TMP_FontAsset or FontAsset.
            SerializedObject so = property.serializedObject;

            if (so == null)
            {
                return;
            }

            if (m_GlyphLookupDictionary == null)
            {
                m_GlyphLookupDictionary = new Dictionary <uint, GlyphProxy>();
                FontAssetEditorUtilities.PopulateGlyphProxyLookupDictionary(so, m_GlyphLookupDictionary);
            }

            // Try getting a reference to the glyph for the given glyph index.
            if (!m_GlyphLookupDictionary.TryGetValue(glyphIndex, out GlyphProxy glyph))
            {
                return;
            }

            // Get the atlas index of the glyph and lookup its atlas texture
            SerializedProperty atlasTextureProperty = so.FindProperty("m_AtlasTextures");
            Texture2D          atlasTexture         = atlasTextureProperty.GetArrayElementAtIndex(glyph.atlasIndex).objectReferenceValue as Texture2D;

            if (atlasTexture == null)
            {
                return;
            }

            Material mat;

            GlyphRenderMode atlasRenderMode = (GlyphRenderMode)so.FindProperty("m_AtlasRenderMode").intValue;
            int             padding         = so.FindProperty("m_AtlasPadding").intValue;

            if (((GlyphRasterModes)atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
            {
                mat = FontAssetEditor.internalBitmapMaterial;

                if (mat == null)
                {
                    return;
                }

                mat.mainTexture = atlasTexture;
            }
            else
            {
                mat = FontAssetEditor.internalSDFMaterial;

                if (mat == null)
                {
                    return;
                }

                mat.mainTexture = atlasTexture;
                mat.SetFloat(TextShaderUtilities.ID_GradientScale, padding + 1);
            }

            // Draw glyph from atlas texture.
            Rect glyphDrawPosition = new Rect(position.x, position.y + 2, position.width, position.height);

            GlyphRect glyphRect    = glyph.glyphRect;
            int       glyphOriginX = glyphRect.x - padding;
            int       glyphOriginY = glyphRect.y - padding;
            int       glyphWidth   = glyphRect.width + padding * 2;
            int       glyphHeight  = glyphRect.height + padding * 2;

            SerializedProperty faceInfoProperty = so.FindProperty("m_FaceInfo");
            float ascentLine  = faceInfoProperty.FindPropertyRelative("m_AscentLine").floatValue;
            float descentLine = faceInfoProperty.FindPropertyRelative("m_DescentLine").floatValue;

            float normalizedHeight = ascentLine - descentLine;
            float scale            = glyphDrawPosition.width / normalizedHeight;

            // Compute the normalized texture coordinates
            Rect texCoords = new Rect((float)glyphOriginX / atlasTexture.width, (float)glyphOriginY / atlasTexture.height, (float)glyphWidth / atlasTexture.width, (float)glyphHeight / atlasTexture.height);

            if (Event.current.type == EventType.Repaint)
            {
                glyphDrawPosition.x     += -(glyphWidth * scale / 2 - padding * scale);
                glyphDrawPosition.y     += glyphDrawPosition.height - glyph.metrics.horizontalBearingY * scale;
                glyphDrawPosition.width  = glyphWidth * scale;
                glyphDrawPosition.height = glyphHeight * scale;

                // Could switch to using the default material of the font asset which would require passing scale to the shader.
                Graphics.DrawTexture(glyphDrawPosition, atlasTexture, texCoords, 0, 0, 0, 0, new Color(1f, 1f, 1f), mat);
            }
        }
示例#6
0
 extern static int RenderGlyphToTexture_Internal(GlyphMarshallingStruct glyphStruct, int padding, GlyphRenderMode renderMode, Texture2D texture);
示例#7
0
        internal static bool TryPackGlyphInAtlas(Glyph glyph, int padding, GlyphPackingMode packingMode, GlyphRenderMode renderMode, int width, int height, List <GlyphRect> freeGlyphRects, List <GlyphRect> usedGlyphRects)
        {
            GlyphMarshallingStruct glyphMarshallingStruct = new GlyphMarshallingStruct(glyph);
            int  count  = freeGlyphRects.Count;
            int  count2 = usedGlyphRects.Count;
            int  num    = count + count2;
            bool flag   = FontEngine.s_FreeGlyphRects.Length < num || FontEngine.s_UsedGlyphRects.Length < num;

            if (flag)
            {
                int num2 = Mathf.NextPowerOfTwo(num + 1);
                FontEngine.s_FreeGlyphRects = new GlyphRect[num2];
                FontEngine.s_UsedGlyphRects = new GlyphRect[num2];
            }
            int num3 = Mathf.Max(count, count2);

            for (int i = 0; i < num3; i++)
            {
                bool flag2 = i < count;
                if (flag2)
                {
                    FontEngine.s_FreeGlyphRects[i] = freeGlyphRects[i];
                }
                bool flag3 = i < count2;
                if (flag3)
                {
                    FontEngine.s_UsedGlyphRects[i] = usedGlyphRects[i];
                }
            }
            bool flag4 = FontEngine.TryPackGlyphInAtlas_Internal(ref glyphMarshallingStruct, padding, packingMode, renderMode, width, height, FontEngine.s_FreeGlyphRects, ref count, FontEngine.s_UsedGlyphRects, ref count2);
            bool result;

            if (flag4)
            {
                glyph.glyphRect = glyphMarshallingStruct.glyphRect;
                freeGlyphRects.Clear();
                usedGlyphRects.Clear();
                num3 = Mathf.Max(count, count2);
                for (int j = 0; j < num3; j++)
                {
                    bool flag5 = j < count;
                    if (flag5)
                    {
                        freeGlyphRects.Add(FontEngine.s_FreeGlyphRects[j]);
                    }
                    bool flag6 = j < count2;
                    if (flag6)
                    {
                        usedGlyphRects.Add(FontEngine.s_UsedGlyphRects[j]);
                    }
                }
                result = true;
            }
            else
            {
                result = false;
            }
            return(result);
        }
示例#8
0
        internal static bool TryAddGlyphToTexture(uint glyphIndex, int padding, GlyphPackingMode packingMode, List <GlyphRect> freeGlyphRects, List <GlyphRect> usedGlyphRects, GlyphRenderMode renderMode, Texture2D texture, out Glyph glyph)
        {
            int  count  = freeGlyphRects.Count;
            int  count2 = usedGlyphRects.Count;
            int  num    = count + count2;
            bool flag   = FontEngine.s_FreeGlyphRects.Length < num || FontEngine.s_UsedGlyphRects.Length < num;

            if (flag)
            {
                int num2 = Mathf.NextPowerOfTwo(num + 1);
                FontEngine.s_FreeGlyphRects = new GlyphRect[num2];
                FontEngine.s_UsedGlyphRects = new GlyphRect[num2];
            }
            int num3 = Mathf.Max(count, count2);

            for (int i = 0; i < num3; i++)
            {
                bool flag2 = i < count;
                if (flag2)
                {
                    FontEngine.s_FreeGlyphRects[i] = freeGlyphRects[i];
                }
                bool flag3 = i < count2;
                if (flag3)
                {
                    FontEngine.s_UsedGlyphRects[i] = usedGlyphRects[i];
                }
            }
            GlyphMarshallingStruct glyphStruct;
            bool flag4 = FontEngine.TryAddGlyphToTexture_Internal(glyphIndex, padding, packingMode, FontEngine.s_FreeGlyphRects, ref count, FontEngine.s_UsedGlyphRects, ref count2, renderMode, texture, out glyphStruct);
            bool result;

            if (flag4)
            {
                glyph = new Glyph(glyphStruct);
                freeGlyphRects.Clear();
                usedGlyphRects.Clear();
                num3 = Mathf.Max(count, count2);
                for (int j = 0; j < num3; j++)
                {
                    bool flag5 = j < count;
                    if (flag5)
                    {
                        freeGlyphRects.Add(FontEngine.s_FreeGlyphRects[j]);
                    }
                    bool flag6 = j < count2;
                    if (flag6)
                    {
                        usedGlyphRects.Add(FontEngine.s_UsedGlyphRects[j]);
                    }
                }
                result = true;
            }
            else
            {
                glyph  = null;
                result = false;
            }
            return(result);
        }
示例#9
0
        internal static bool TryAddGlyphsToTexture(List <Glyph> glyphsToAdd, List <Glyph> glyphsAdded, int padding, GlyphPackingMode packingMode, List <GlyphRect> freeGlyphRects, List <GlyphRect> usedGlyphRects, GlyphRenderMode renderMode, Texture2D texture)
        {
            Profiler.BeginSample("FontEngine.TryAddGlyphsToTexture");
            int  count = glyphsToAdd.Count;
            int  num   = 0;
            bool flag  = FontEngine.s_GlyphMarshallingStruct_IN.Length < count || FontEngine.s_GlyphMarshallingStruct_OUT.Length < count;

            if (flag)
            {
                int  newSize = Mathf.NextPowerOfTwo(count + 1);
                bool flag2   = FontEngine.s_GlyphMarshallingStruct_IN.Length < count;
                if (flag2)
                {
                    Array.Resize <GlyphMarshallingStruct>(ref FontEngine.s_GlyphMarshallingStruct_IN, newSize);
                }
                bool flag3 = FontEngine.s_GlyphMarshallingStruct_OUT.Length < count;
                if (flag3)
                {
                    Array.Resize <GlyphMarshallingStruct>(ref FontEngine.s_GlyphMarshallingStruct_OUT, newSize);
                }
            }
            int  count2 = freeGlyphRects.Count;
            int  count3 = usedGlyphRects.Count;
            int  num2   = count2 + count3 + count;
            bool flag4  = FontEngine.s_FreeGlyphRects.Length < num2 || FontEngine.s_UsedGlyphRects.Length < num2;

            if (flag4)
            {
                int  newSize2 = Mathf.NextPowerOfTwo(num2 + 1);
                bool flag5    = FontEngine.s_FreeGlyphRects.Length < num2;
                if (flag5)
                {
                    Array.Resize <GlyphRect>(ref FontEngine.s_FreeGlyphRects, newSize2);
                }
                bool flag6 = FontEngine.s_UsedGlyphRects.Length < num2;
                if (flag6)
                {
                    Array.Resize <GlyphRect>(ref FontEngine.s_UsedGlyphRects, newSize2);
                }
            }
            FontEngine.s_GlyphLookupDictionary.Clear();
            int  num3  = 0;
            bool flag7 = true;

            while (flag7)
            {
                flag7 = false;
                bool flag8 = num3 < count;
                if (flag8)
                {
                    Glyph glyph = glyphsToAdd[num3];
                    FontEngine.s_GlyphMarshallingStruct_IN[num3] = new GlyphMarshallingStruct(glyph);
                    FontEngine.s_GlyphLookupDictionary.Add(glyph.index, glyph);
                    flag7 = true;
                }
                bool flag9 = num3 < count2;
                if (flag9)
                {
                    FontEngine.s_FreeGlyphRects[num3] = freeGlyphRects[num3];
                    flag7 = true;
                }
                bool flag10 = num3 < count3;
                if (flag10)
                {
                    FontEngine.s_UsedGlyphRects[num3] = usedGlyphRects[num3];
                    flag7 = true;
                }
                num3++;
            }
            bool result = FontEngine.TryAddGlyphsToTexture_Internal_MultiThread(FontEngine.s_GlyphMarshallingStruct_IN, ref count, FontEngine.s_GlyphMarshallingStruct_OUT, ref num, padding, packingMode, FontEngine.s_FreeGlyphRects, ref count2, FontEngine.s_UsedGlyphRects, ref count3, renderMode, texture);

            glyphsToAdd.Clear();
            glyphsAdded.Clear();
            freeGlyphRects.Clear();
            usedGlyphRects.Clear();
            num3  = 0;
            flag7 = true;
            while (flag7)
            {
                flag7 = false;
                bool flag11 = num3 < count;
                if (flag11)
                {
                    uint index = FontEngine.s_GlyphMarshallingStruct_IN[num3].index;
                    glyphsToAdd.Add(FontEngine.s_GlyphLookupDictionary[index]);
                    flag7 = true;
                }
                bool flag12 = num3 < num;
                if (flag12)
                {
                    uint  index2 = FontEngine.s_GlyphMarshallingStruct_OUT[num3].index;
                    Glyph glyph2 = FontEngine.s_GlyphLookupDictionary[index2];
                    glyph2.atlasIndex = FontEngine.s_GlyphMarshallingStruct_OUT[num3].atlasIndex;
                    glyph2.scale      = FontEngine.s_GlyphMarshallingStruct_OUT[num3].scale;
                    glyph2.glyphRect  = FontEngine.s_GlyphMarshallingStruct_OUT[num3].glyphRect;
                    glyph2.metrics    = FontEngine.s_GlyphMarshallingStruct_OUT[num3].metrics;
                    glyphsAdded.Add(glyph2);
                    flag7 = true;
                }
                bool flag13 = num3 < count2;
                if (flag13)
                {
                    freeGlyphRects.Add(FontEngine.s_FreeGlyphRects[num3]);
                    flag7 = true;
                }
                bool flag14 = num3 < count3;
                if (flag14)
                {
                    usedGlyphRects.Add(FontEngine.s_UsedGlyphRects[num3]);
                    flag7 = true;
                }
                num3++;
            }
            Profiler.EndSample();
            return(result);
        }
示例#10
0
 private static extern int RenderGlyphsToTexture_Internal(GlyphMarshallingStruct[] glyphs, int glyphCount, int padding, GlyphRenderMode renderMode, Texture2D texture);
示例#11
0
        internal static FontEngineError RenderGlyphsToSharedTexture(List <Glyph> glyphs, int padding, GlyphRenderMode renderMode)
        {
            int  count = glyphs.Count;
            bool flag  = FontEngine.s_GlyphMarshallingStruct_IN.Length < count;

            if (flag)
            {
                int num = Mathf.NextPowerOfTwo(count + 1);
                FontEngine.s_GlyphMarshallingStruct_IN = new GlyphMarshallingStruct[num];
            }
            for (int i = 0; i < count; i++)
            {
                FontEngine.s_GlyphMarshallingStruct_IN[i] = new GlyphMarshallingStruct(glyphs[i]);
            }
            return((FontEngineError)FontEngine.RenderGlyphsToSharedTexture_Internal(FontEngine.s_GlyphMarshallingStruct_IN, count, padding, renderMode));
        }
示例#12
0
 private static int RenderGlyphToTexture_Internal(GlyphMarshallingStruct glyphStruct, int padding, GlyphRenderMode renderMode, Texture2D texture)
 {
     return(FontEngine.RenderGlyphToTexture_Internal_Injected(ref glyphStruct, padding, renderMode, texture));
 }
示例#13
0
        internal static bool TryPackGlyphsInAtlas(List <Glyph> glyphsToAdd, List <Glyph> glyphsAdded, int padding, GlyphPackingMode packingMode, GlyphRenderMode renderMode, int width, int height, List <GlyphRect> freeGlyphRects, List <GlyphRect> usedGlyphRects)
        {
            int  count  = glyphsToAdd.Count;
            int  count2 = glyphsAdded.Count;
            int  count3 = freeGlyphRects.Count;
            int  count4 = usedGlyphRects.Count;
            int  num    = count + count2 + count3 + count4;
            bool flag   = FontEngine.s_GlyphMarshallingStruct_IN.Length < num || FontEngine.s_GlyphMarshallingStruct_OUT.Length < num || FontEngine.s_FreeGlyphRects.Length < num || FontEngine.s_UsedGlyphRects.Length < num;

            if (flag)
            {
                int num2 = Mathf.NextPowerOfTwo(num + 1);
                FontEngine.s_GlyphMarshallingStruct_IN  = new GlyphMarshallingStruct[num2];
                FontEngine.s_GlyphMarshallingStruct_OUT = new GlyphMarshallingStruct[num2];
                FontEngine.s_FreeGlyphRects             = new GlyphRect[num2];
                FontEngine.s_UsedGlyphRects             = new GlyphRect[num2];
            }
            FontEngine.s_GlyphLookupDictionary.Clear();
            for (int i = 0; i < num; i++)
            {
                bool flag2 = i < count;
                if (flag2)
                {
                    GlyphMarshallingStruct glyphMarshallingStruct = new GlyphMarshallingStruct(glyphsToAdd[i]);
                    FontEngine.s_GlyphMarshallingStruct_IN[i] = glyphMarshallingStruct;
                    bool flag3 = !FontEngine.s_GlyphLookupDictionary.ContainsKey(glyphMarshallingStruct.index);
                    if (flag3)
                    {
                        FontEngine.s_GlyphLookupDictionary.Add(glyphMarshallingStruct.index, glyphsToAdd[i]);
                    }
                }
                bool flag4 = i < count2;
                if (flag4)
                {
                    GlyphMarshallingStruct glyphMarshallingStruct2 = new GlyphMarshallingStruct(glyphsAdded[i]);
                    FontEngine.s_GlyphMarshallingStruct_OUT[i] = glyphMarshallingStruct2;
                    bool flag5 = !FontEngine.s_GlyphLookupDictionary.ContainsKey(glyphMarshallingStruct2.index);
                    if (flag5)
                    {
                        FontEngine.s_GlyphLookupDictionary.Add(glyphMarshallingStruct2.index, glyphsAdded[i]);
                    }
                }
                bool flag6 = i < count3;
                if (flag6)
                {
                    FontEngine.s_FreeGlyphRects[i] = freeGlyphRects[i];
                }
                bool flag7 = i < count4;
                if (flag7)
                {
                    FontEngine.s_UsedGlyphRects[i] = usedGlyphRects[i];
                }
            }
            bool result = FontEngine.TryPackGlyphsInAtlas_Internal(FontEngine.s_GlyphMarshallingStruct_IN, ref count, FontEngine.s_GlyphMarshallingStruct_OUT, ref count2, padding, packingMode, renderMode, width, height, FontEngine.s_FreeGlyphRects, ref count3, FontEngine.s_UsedGlyphRects, ref count4);

            glyphsToAdd.Clear();
            glyphsAdded.Clear();
            freeGlyphRects.Clear();
            usedGlyphRects.Clear();
            for (int j = 0; j < num; j++)
            {
                bool flag8 = j < count;
                if (flag8)
                {
                    GlyphMarshallingStruct glyphMarshallingStruct3 = FontEngine.s_GlyphMarshallingStruct_IN[j];
                    Glyph glyph = FontEngine.s_GlyphLookupDictionary[glyphMarshallingStruct3.index];
                    glyph.metrics    = glyphMarshallingStruct3.metrics;
                    glyph.glyphRect  = glyphMarshallingStruct3.glyphRect;
                    glyph.scale      = glyphMarshallingStruct3.scale;
                    glyph.atlasIndex = glyphMarshallingStruct3.atlasIndex;
                    glyphsToAdd.Add(glyph);
                }
                bool flag9 = j < count2;
                if (flag9)
                {
                    GlyphMarshallingStruct glyphMarshallingStruct4 = FontEngine.s_GlyphMarshallingStruct_OUT[j];
                    Glyph glyph2 = FontEngine.s_GlyphLookupDictionary[glyphMarshallingStruct4.index];
                    glyph2.metrics    = glyphMarshallingStruct4.metrics;
                    glyph2.glyphRect  = glyphMarshallingStruct4.glyphRect;
                    glyph2.scale      = glyphMarshallingStruct4.scale;
                    glyph2.atlasIndex = glyphMarshallingStruct4.atlasIndex;
                    glyphsAdded.Add(glyph2);
                }
                bool flag10 = j < count3;
                if (flag10)
                {
                    freeGlyphRects.Add(FontEngine.s_FreeGlyphRects[j]);
                }
                bool flag11 = j < count4;
                if (flag11)
                {
                    usedGlyphRects.Add(FontEngine.s_UsedGlyphRects[j]);
                }
            }
            return(result);
        }
示例#14
0
 private static extern bool TryPackGlyphInAtlas_Internal(ref GlyphMarshallingStruct glyph, int padding, GlyphPackingMode packingMode, GlyphRenderMode renderMode, int width, int height, [Out] GlyphRect[] freeGlyphRects, ref int freeGlyphRectCount, [Out] GlyphRect[] usedGlyphRects, ref int usedGlyphRectCount);
示例#15
0
 extern static bool TryPackGlyphsInAtlas_Internal([Out] GlyphMarshallingStruct[] glyphsToAdd, ref int glyphsToAddCount, [Out] GlyphMarshallingStruct[] glyphsAdded, ref int glyphsAddedCount,
                                                  int padding, GlyphPackingMode packingMode, GlyphRenderMode renderMode, int width, int height,
                                                  [Out] GlyphRect[] freeGlyphRects, ref int freeGlyphRectCount, [Out] GlyphRect[] usedGlyphRects, ref int usedGlyphRectCount);
示例#16
0
 private static extern bool TryAddGlyphsToTexture_Internal_MultiThread([Out] GlyphMarshallingStruct[] glyphsToAdd, ref int glyphsToAddCount, [Out] GlyphMarshallingStruct[] glyphsAdded, ref int glyphsAddedCount, int padding, GlyphPackingMode packingMode, [Out] GlyphRect[] freeGlyphRects, ref int freeGlyphRectCount, [Out] GlyphRect[] usedGlyphRects, ref int usedGlyphRectCount, GlyphRenderMode renderMode, Texture2D texture);
示例#17
0
        /// <summary>
        /// Render and add glyph to the provided texture.
        /// </summary>
        /// <param name="glyph">The Glyph that should be added into the provided texture.</param>
        /// <param name="padding">The padding value around the glyph.</param>
        /// <param name="renderMode">The Rendering Mode for the Glyph.</param>
        /// <param name="texture">The Texture to which the glyph should be added.</param>
        /// <returns>Returns a value of zero if the glyph was successfully added to the texture.</returns>
        internal static FontEngineError RenderGlyphToTexture(Glyph glyph, int padding, GlyphRenderMode renderMode, Texture2D texture)
        {
            GlyphMarshallingStruct glyphStruct = new GlyphMarshallingStruct(glyph);

            return((FontEngineError)RenderGlyphToTexture_Internal(glyphStruct, padding, renderMode, texture));
        }
示例#18
0
        internal static bool TryAddGlyphsToTexture(List <uint> glyphIndexes, int padding, GlyphPackingMode packingMode, List <GlyphRect> freeGlyphRects, List <GlyphRect> usedGlyphRects, GlyphRenderMode renderMode, Texture2D texture, out Glyph[] glyphs)
        {
            Profiler.BeginSample("FontEngine.TryAddGlyphsToTexture");
            glyphs = null;
            bool flag = glyphIndexes == null || glyphIndexes.Count == 0;
            bool result;

            if (flag)
            {
                Profiler.EndSample();
                result = false;
            }
            else
            {
                int  count = glyphIndexes.Count;
                bool flag2 = FontEngine.s_GlyphIndexes_MarshallingArray_A == null || FontEngine.s_GlyphIndexes_MarshallingArray_A.Length < count;
                if (flag2)
                {
                    bool flag3 = FontEngine.s_GlyphIndexes_MarshallingArray_A == null;
                    if (flag3)
                    {
                        FontEngine.s_GlyphIndexes_MarshallingArray_A = new uint[count];
                    }
                    else
                    {
                        int num = Mathf.NextPowerOfTwo(count + 1);
                        FontEngine.s_GlyphIndexes_MarshallingArray_A = new uint[num];
                    }
                }
                int  count2 = freeGlyphRects.Count;
                int  count3 = usedGlyphRects.Count;
                int  num2   = count2 + count3 + count;
                bool flag4  = FontEngine.s_FreeGlyphRects.Length < num2 || FontEngine.s_UsedGlyphRects.Length < num2;
                if (flag4)
                {
                    int num3 = Mathf.NextPowerOfTwo(num2 + 1);
                    FontEngine.s_FreeGlyphRects = new GlyphRect[num3];
                    FontEngine.s_UsedGlyphRects = new GlyphRect[num3];
                }
                bool flag5 = FontEngine.s_GlyphMarshallingStruct_OUT.Length < count;
                if (flag5)
                {
                    int num4 = Mathf.NextPowerOfTwo(count + 1);
                    FontEngine.s_GlyphMarshallingStruct_OUT = new GlyphMarshallingStruct[num4];
                }
                int num5 = FontEngineUtilities.MaxValue(count2, count3, count);
                for (int i = 0; i < num5; i++)
                {
                    bool flag6 = i < count;
                    if (flag6)
                    {
                        FontEngine.s_GlyphIndexes_MarshallingArray_A[i] = glyphIndexes[i];
                    }
                    bool flag7 = i < count2;
                    if (flag7)
                    {
                        FontEngine.s_FreeGlyphRects[i] = freeGlyphRects[i];
                    }
                    bool flag8 = i < count3;
                    if (flag8)
                    {
                        FontEngine.s_UsedGlyphRects[i] = usedGlyphRects[i];
                    }
                }
                bool flag9  = FontEngine.TryAddGlyphsToTexture_Internal(FontEngine.s_GlyphIndexes_MarshallingArray_A, padding, packingMode, FontEngine.s_FreeGlyphRects, ref count2, FontEngine.s_UsedGlyphRects, ref count3, renderMode, texture, FontEngine.s_GlyphMarshallingStruct_OUT, ref count);
                bool flag10 = FontEngine.s_Glyphs == null || FontEngine.s_Glyphs.Length <= count;
                if (flag10)
                {
                    FontEngine.s_Glyphs = new Glyph[Mathf.NextPowerOfTwo(count + 1)];
                }
                FontEngine.s_Glyphs[count] = null;
                freeGlyphRects.Clear();
                usedGlyphRects.Clear();
                num5 = FontEngineUtilities.MaxValue(count2, count3, count);
                for (int j = 0; j < num5; j++)
                {
                    bool flag11 = j < count;
                    if (flag11)
                    {
                        FontEngine.s_Glyphs[j] = new Glyph(FontEngine.s_GlyphMarshallingStruct_OUT[j]);
                    }
                    bool flag12 = j < count2;
                    if (flag12)
                    {
                        freeGlyphRects.Add(FontEngine.s_FreeGlyphRects[j]);
                    }
                    bool flag13 = j < count3;
                    if (flag13)
                    {
                        usedGlyphRects.Add(FontEngine.s_UsedGlyphRects[j]);
                    }
                }
                glyphs = FontEngine.s_Glyphs;
                Profiler.EndSample();
                result = flag9;
            }
            return(result);
        }
示例#19
0
 extern static int RenderGlyphsToTextureBuffer_Internal(GlyphMarshallingStruct[] glyphs, int glyphCount, int padding, GlyphRenderMode renderMode, [Out] byte[] texBuffer, int texWidth, int texHeight);
        void DrawGlyph(Rect position, SerializedProperty property)
        {
            // Serialized object can either be a FontAsset or a TMP_FontAsset
            SerializedObject so = property.serializedObject;

            if (so == null)
            {
                return;
            }

            // Search the glyph table for the glyph referenced by this character
            SerializedProperty glyphTableProperty = property.serializedObject.FindProperty("m_GlyphTable");
            int glyphIndex = property.FindPropertyRelative("m_GlyphIndex").intValue;

            // Find the element index for this glyph in the glyph table.
            int elementIndex = GetElementIndex(glyphTableProperty, glyphIndex);

            if (elementIndex == -1)
            {
                return;
            }

            SerializedProperty glyphProperty = glyphTableProperty.GetArrayElementAtIndex(elementIndex);

            // Get reference to atlas texture.
            int atlasIndex = glyphProperty.FindPropertyRelative("m_AtlasIndex").intValue;
            SerializedProperty atlasTextureProperty = so.FindProperty("m_AtlasTextures");
            Texture2D          atlasTexture         = atlasTextureProperty.GetArrayElementAtIndex(atlasIndex).objectReferenceValue as Texture2D;

            if (atlasTexture == null)
            {
                return;
            }

            Material mat;

            GlyphRenderMode atlasRenderMode = (GlyphRenderMode)so.FindProperty("m_AtlasRenderMode").intValue;
            int             atlasPadding    = so.FindProperty("m_AtlasPadding").intValue;

            if (((GlyphRasterModes)atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
            {
                mat = FontAssetEditor.internalBitmapMaterial;

                if (mat == null)
                {
                    return;
                }

                mat.mainTexture = atlasTexture;
                mat.SetColor("_Color", Color.white);
            }
            else
            {
                mat = FontAssetEditor.internalSDFMaterial;

                if (mat == null)
                {
                    return;
                }

                mat.mainTexture = atlasTexture;
                mat.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + 1);
            }

            // Draw glyph
            Rect glyphDrawPosition = new Rect(position.x, position.y, 48, 58);

            SerializedProperty glyphRectProperty = glyphProperty.FindPropertyRelative("m_GlyphRect");

            int padding   = atlasPadding;
            int padding2X = padding * 2;

            int glyphOriginX = glyphRectProperty.FindPropertyRelative("m_X").intValue - padding;
            int glyphOriginY = glyphRectProperty.FindPropertyRelative("m_Y").intValue - padding;
            int glyphWidth   = glyphRectProperty.FindPropertyRelative("m_Width").intValue + padding2X;
            int glyphHeight  = glyphRectProperty.FindPropertyRelative("m_Height").intValue + padding2X;

            SerializedProperty faceInfoProperty = so.FindProperty("m_FaceInfo");
            float ascentLine  = faceInfoProperty.FindPropertyRelative("m_AscentLine").floatValue;
            float descentLine = faceInfoProperty.FindPropertyRelative("m_DescentLine").floatValue;

            float normalizedHeight = ascentLine - descentLine;
            float scale            = glyphDrawPosition.width / normalizedHeight;

            // Compute the normalized texture coordinates
            Rect texCoords = new Rect((float)glyphOriginX / atlasTexture.width, (float)glyphOriginY / atlasTexture.height, (float)glyphWidth / atlasTexture.width, (float)glyphHeight / atlasTexture.height);

            if (Event.current.type == EventType.Repaint)
            {
                glyphDrawPosition.x     += (glyphDrawPosition.width - glyphWidth * scale) / 2;
                glyphDrawPosition.y     += (glyphDrawPosition.height - glyphHeight * scale) / 2;
                glyphDrawPosition.width  = glyphWidth * scale;
                glyphDrawPosition.height = glyphHeight * scale;

                // Could switch to using the default material of the font asset which would require passing scale to the shader.
                Graphics.DrawTexture(glyphDrawPosition, atlasTexture, texCoords, 0, 0, 0, 0, new Color(1f, 1f, 1f), mat);
            }
        }