// 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);
        }
Exemplo n.º 2
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;
        }
Exemplo n.º 3
0
        static void Convert(RootCommandOptions options)
        {
            foreach (var file in options.Files)
            {
                var filePath = file.ToString(); // Returns the original path supplied to FileInfo.

                // Convert from XML
                if (filePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
                {
                    // XML to MTX
                    if (string.Equals(options.Format, "mtx", StringComparison.OrdinalIgnoreCase) ||
                        (options.Format is null && filePath.EndsWith(".mtx.xml", StringComparison.OrdinalIgnoreCase)))
                    {
                        var destinationPath = options.Output is not null
                            ? options.Output.FullName
                            : Path.ChangeExtension(filePath, null); // Removes the ".xml" part of the extension.

                        MtxEncoding encoding;
                        if (options.Fpd is not null)
                        {
                            var fpdFile = new FpdFile(options.Fpd.ToString());
                            encoding = new CharacterMapMtxEncoding(fpdFile.Characters);
                        }
                        else if (options.Fnt is not null)
                        {
                            var fntFile = new FntFile(options.Fnt.ToString());
                            encoding = new CharacterMapMtxEncoding(fntFile.Characters);
                        }
                        else
                        {
                            encoding = new Utf16MtxEncoding();
                        }

                        var serializedSource   = File.ReadAllText(filePath);
                        var deserializedSource = Utf8XmlSerializer.Deserialize <MtxSerializable>(serializedSource)
                                                 ?? throw new NullValueException();
                        var destination = new MtxFile(deserializedSource, encoding);

                        destination.Save(destinationPath);
                    }

                    // XML to CNVRS-TEXT
                    else if (string.Equals(options.Format, "cnvrs-text", StringComparison.OrdinalIgnoreCase) ||
                             (options.Format is null && filePath.EndsWith(".cnvrs-text.xml", StringComparison.OrdinalIgnoreCase)))
                    {
                        var destinationPath = options.Output is not null
                            ? options.Output.FullName
                            : Path.ChangeExtension(filePath, null); // Removes the ".xml" part of the extension.

                        var serializedSource   = File.ReadAllText(filePath);
                        var deserializedSource = Utf8XmlSerializer.Deserialize <CnvrsTextSerializable>(serializedSource)
                                                 ?? throw new NullValueException();
                        var destination = new CnvrsTextFile(deserializedSource);

                        destination.Save(destinationPath);
                    }
                }
 internal FntSerializable(FntFile fntFile)
 {
     Width      = fntFile.Width;
     Height     = fntFile.Height;
     Characters = fntFile.Entries.Select(x => new Char
     {
         Character = x.Key,
         Width     = x.Value.Width,
     })
                  .ToList();
 }
Exemplo n.º 5
0
        // 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);
        }
Exemplo n.º 6
0
        public void OnExecute()
        {
            // Don't execute if we are showing help.
            if (ShowHelp)
            {
                return;
            }

            MtxEncoding encoding = null;
            var         version  = Version ?? "auto";

            if (version == "auto")
            {
                if (FntPath != null || FpdPath != null)
                {
                    version = "1";
                }
                else
                {
                    version = "2";
                }
            }
            if (version == "1")
            {
                if (FntPath != null)
                {
                    if (!File.Exists(FntPath))
                    {
                        Error(string.Format(ErrorMessages.FileDoesNotExist, FntPath));
                        return;
                    }

                    var fntFile = new FntFile(FntPath);
                    encoding = new CharacterMapMtxEncoding(fntFile.Characters);
                }
                else if (FpdPath != null)
                {
                    if (!File.Exists(FpdPath))
                    {
                        Error(string.Format(ErrorMessages.FileDoesNotExist, FpdPath));
                        return;
                    }

                    var fpdFile = new FpdFile(FpdPath);
                    encoding = new CharacterMapMtxEncoding(fpdFile.Characters);
                }
                else
                {
                    Error(ErrorMessages.FntOrFpdOptionMustBeSet);
                    return;
                }
            }
            else if (version == "2")
            {
                encoding = new Utf16MtxEncoding();
            }

            foreach (var file in Files)
            {
                if (!File.Exists(file))
                {
                    Error(string.Format(ErrorMessages.FileDoesNotExist, file));
                    continue;
                }

                var format = Format ?? "auto";
                if (format == "auto")
                {
                    var extension = Path.GetExtension(file);

                    if (extension.Equals(".mtx", StringComparison.OrdinalIgnoreCase))
                    {
                        format = "json";
                    }
                    else if (extension.Equals(".json", StringComparison.OrdinalIgnoreCase))
                    {
                        format = "mtx";
                    }
                    else
                    {
                        Error(string.Format(ErrorMessages.CouldNotDetermineOutputFormat, file));
                        continue;
                    }
                }

                if (format == "json")
                {
                    var outputFilename = Output ?? Path.ChangeExtension(file, ".json");

                    try
                    {
                        var mtxFile = new MtxFile(file, encoding);
                        var mtxJson = new MtxJson
                        {
                            Has64BitOffsets = mtxFile.Has64BitOffsets,
                            Entries         = mtxFile.Entries,
                        };
                        JsonFileSerializer.Serialize(outputFilename, mtxJson);
                    }
                    catch (Exception e)
                    {
                        Error(e.Message);
                    }
                }
                else if (format == "mtx")
                {
                    var outputFilename = Output ?? Path.ChangeExtension(file, ".mtx");

                    try
                    {
                        var mtxJson = JsonFileSerializer.Deserialize <MtxJson>(file);
                        var mtxFile = new MtxFile(mtxJson.Entries, encoding, mtxJson.Has64BitOffsets ? true : Has64BitOffsets);
                        mtxFile.Save(outputFilename);
                    }
                    catch (Exception e)
                    {
                        Error(e.Message);
                    }
                }
            }
        }