Ejemplo n.º 1
0
        // Creates a font atlas from FntFile
        public static void CreateFontAtlas(FntFile fntFile, Color backColor, Color textColor, out Texture2D atlasTextureOut, out Rect[] atlasRectsOut)
        {
            const int atlasDim = 256;

            // Create atlas colors array
            Color32[] atlasColors = new Color32[atlasDim * atlasDim];

            // Add Daggerfall glyphs
            int xpos = 0, ypos = 0;
            Rect[] rects = new Rect[FntFile.MaxGlyphCount];
            for (int i = 0; i < FntFile.MaxGlyphCount; i++)
            {
                // Get glyph colors
                Rect rect;
                Color32[] glyphColors = GetGlyphColors(fntFile, i, backColor, textColor, out rect);

                // Offset pixel rect
                rect.x += xpos;
                rect.y += ypos;

                // Flip pixel rect top-bottom
                float top = rect.yMin;
                float bottom = rect.yMax;
                rect.yMin = bottom;
                rect.yMax = top;

                // Convert to UV coords and store
                rect.xMin /= atlasDim;
                rect.yMin /= atlasDim;
                rect.xMax /= atlasDim;
                rect.yMax /= atlasDim;
                rects[i] = rect;

                // Insert into atlas
                InsertColors(
                    ref glyphColors,
                    ref atlasColors,
                    xpos,
                    ypos,
                    FntFile.GlyphFixedDimension,
                    FntFile.GlyphFixedDimension,
                    atlasDim,
                    atlasDim);

                // Offset position
                xpos += FntFile.GlyphFixedDimension;
                if (xpos >= atlasDim)
                {
                    xpos = 0;
                    ypos += FntFile.GlyphFixedDimension;
                }
            }

            // Create texture from colors array
            atlasTextureOut = MakeTexture2D(ref atlasColors, atlasDim, atlasDim, TextureFormat.ARGB32, false);
            atlasRectsOut = rects;
        }
Ejemplo n.º 2
0
        // Gets a glyph from FntFile as Color32 array
        public static Color32[] GetGlyphColors(FntFile fntFile, int index, Color backColor, Color textColor, out Rect sizeOut)
        {
            // Get actual glyph rect
            sizeOut = new Rect(0, 0, fntFile.GetGlyphWidth(index), fntFile.FixedHeight);

            // Get glyph byte data as color array
            byte[] data = fntFile.GetGlyphPixels(index);
            Color32[] colors = new Color32[data.Length];
            for (int y = 0; y < FntFile.GlyphFixedDimension; y++)
            {
                for (int x = 0; x < FntFile.GlyphFixedDimension; x++)
                {
                    int pos = y * FntFile.GlyphFixedDimension + x;
                    if (data[pos] > 0)
                        colors[pos] = textColor;
                    else
                        colors[pos] = backColor;
                }
            }

            return colors;
        }
        // Gets proportial-width glyph data from FntFile as Color32 array
        public static Color32[] GetProportionalGlyphColors(FntFile fntFile, int index, Color backColor, Color textColor, bool invertY = false)
        {
            // Get actual glyph dimensions
            int width = fntFile.GetGlyphWidth(index);
            int height = fntFile.FixedHeight;
            Color32[] colors = new Color32[width * height];

            // Get glyph byte data as color array
            byte[] data = fntFile.GetGlyphPixels(index);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int srcPos = y * FntFile.GlyphFixedDimension + x;
                    int dstPos = (invertY) ? (height - 1 - y) * width + x : y * width + x;
                    colors[dstPos] = (data[srcPos] > 0) ? textColor : backColor;
                }
            }

            return colors;
        }