Example #1
0
        static void Vf3ToXml(Options options)
        {
            Dictionary <char, char> charMap = null;

            using (Stream cStream = File.OpenRead(options.Charlist))
            {
                charMap = LanguageUtility.GetDecodeCharMapFromStream(cStream);
            }

            using (Stream s = File.OpenRead(options.Source))
            {
                FontFile font = new FontFile(s);

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent       = true;
                settings.IndentChars  = "\t";
                settings.NewLineChars = "\r\n";

                using (XmlWriter xml = XmlWriter.Create(options.Output, settings))
                {
                    xml.WriteStartDocument();
                    xml.WriteStartElement("font");
                    xml.WriteAttributeString("id", font.Header.ID.ToString());
                    xml.WriteAttributeString("version", font.Header.Version.ToString());
                    xml.WriteAttributeString("first_ascii", font.Header.FirstAscii.ToString());
                    xml.WriteAttributeString("width", font.Header.Width.ToString());
                    xml.WriteAttributeString("height", font.Header.Height.ToString());
                    xml.WriteAttributeString("render_height", font.Header.RenderHeight.ToString());
                    xml.WriteAttributeString("baseline_offset", font.Header.BaselineOffset.ToString());
                    xml.WriteAttributeString("character_spacing", font.Header.CharacterSpacing.ToString());
                    xml.WriteAttributeString("vertical_offset", font.Header.VerticalOffset.ToString());
                    xml.WriteAttributeString("peg_name", font.Header.PegName);
                    xml.WriteAttributeString("bitmap_name", font.Header.BitmapName);

                    xml.WriteStartElement("characters");

                    for (int i = 0; i < font.Characters.Count; i++)
                    {
                        FontCharacter c = font.Characters[i];
                        int           u = font.U[i];
                        int           v = font.V[i];

                        xml.WriteStartElement("character");

                        int  charValue  = font.Header.FirstAscii + i;
                        char actualChar = DecodeChar(font.Header, charMap, i);

                        xml.WriteAttributeString("spacing", c.Spacing.ToString());
                        xml.WriteAttributeString("byte_width", c.ByteWidth.ToString());
                        xml.WriteAttributeString("offset", c.Offset.ToString());
                        xml.WriteAttributeString("kerning_entry", c.KerningEntry.ToString());
                        xml.WriteAttributeString("user_data", c.UserData.ToString());
                        xml.WriteAttributeString("u", u.ToString());
                        xml.WriteAttributeString("v", v.ToString());
                        xml.WriteAttributeString("char_value", charValue.ToString());
                        xml.WriteAttributeString("actual_char", actualChar.ToString());
                        xml.WriteEndElement(); // character
                    }

                    xml.WriteEndElement(); // characters

                    xml.WriteStartElement("kerning_pairs");

                    for (int i = 0; i < font.KerningPairs.Count; i++)
                    {
                        FontKerningPair pair = font.KerningPairs[i];

                        xml.WriteStartElement("kerning_pair");

                        char char1 = DecodeChar(font.Header, charMap, pair.Char1);
                        char char2 = DecodeChar(font.Header, charMap, pair.Char2);

                        xml.WriteAttributeString("char1", char1.ToString());
                        xml.WriteAttributeString("char2", char2.ToString());
                        xml.WriteAttributeString("offset", pair.Offset.ToString());
                        xml.WriteAttributeString("padding", pair.Padding.ToString());

                        xml.WriteEndElement(); // kerning_pair
                    }

                    xml.WriteEndElement(); // kerning_pairs

                    xml.WriteEndElement(); // font
                    xml.WriteEndDocument();
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            Options options = null;

            try
            {
                options = CommandLine.Parse <Options>();
            }
            catch (CommandLineException exception)
            {
                Console.WriteLine(exception.ArgumentHelp.Message);
                Console.WriteLine();
                Console.WriteLine(exception.ArgumentHelp.GetHelpText(Console.BufferWidth));

#if DEBUG
                Console.ReadLine();
#endif
                return;
            }

            if (options.Output == null)
            {
                options.Output = "output";
            }

            if (!Directory.Exists(options.Output))
            {
                Directory.CreateDirectory(options.Output);
            }

            FontFile font = null;

            using (Stream s = File.OpenRead(options.Source))
            {
                font = new FontFile(s);
            }

            Dictionary <char, char> charMap = new Dictionary <char, char>();
            using (Stream s = File.OpenRead(options.Charmap))
            {
                charMap = LanguageUtility.GetDecodeCharMapFromStream(s);
            }

            string indicatedPegPath = Path.Combine(Path.GetDirectoryName(options.Source), font.Header.BitmapName);

            string[] pegExtensions = new string[]
            {
                ".cpeg_pc",
                ".cvbm_pc"
            };

            string[] gpegExtensions = new string[]
            {
                ".gpeg_pc",
                ".gvbm_pc"
            };

            bool   foundPeg = false;
            string pegPath  = null;
            string gpegPath = null;
            for (int i = 0; i < pegExtensions.Length; i++)
            {
                string pegExtension  = pegExtensions[i];
                string gpegExtension = gpegExtensions[i];
                string candidatePath = Path.ChangeExtension(indicatedPegPath, pegExtension);

                if (File.Exists(candidatePath))
                {
                    foundPeg = true;
                    pegPath  = candidatePath;
                    gpegPath = Path.ChangeExtension(pegPath, gpegExtension);
                    break;
                }
            }

            if (!foundPeg)
            {
                Console.WriteLine("Couldn't find {0}! Extension may be \".cpeg_pc\" or \".cvbm_pc\".", indicatedPegPath);
                return;
            }


            Bitmap fontBitmap = null;


            using (Stream pegStream = File.OpenRead(pegPath))
            {
                PegFile peg = new PegFile(pegStream);

                PegEntry entry = null;

                foreach (PegEntry e in peg.Entries)
                {
                    if (e.Filename == font.Header.BitmapName)
                    {
                        entry = e;
                        break;
                    }
                }

                if (entry == null)
                {
                    Console.WriteLine("Couldn't find bitmap {0} in font peg!", font.Header.BitmapName);
                    return;
                }

                byte[] bitmapData = null;

                using (Stream gpegStream = File.OpenRead(gpegPath))
                {
                    bitmapData = entry.GetData(gpegStream);
                }

                byte[] uncompressed = null;

                switch (entry.Data.BitmapFormat)
                {
                case PegBitmapFormat.D3DFMT_DXT3:
                    uncompressed = ManagedSquish.Squish.DecompressImage(bitmapData, entry.Data.Width, entry.Data.Height, ManagedSquish.SquishFlags.Dxt3);
                    break;

                case PegBitmapFormat.D3DFMT_DXT5:
                    uncompressed = ManagedSquish.Squish.DecompressImage(bitmapData, entry.Data.Width, entry.Data.Height, ManagedSquish.SquishFlags.Dxt5);
                    break;

                default: throw new Exception();
                }

                fontBitmap = new Bitmap(entry.Data.Width, entry.Data.Height, PixelFormat.Format32bppArgb);
                BitmapData data = fontBitmap.LockBits(new Rectangle(0, 0, fontBitmap.Width, fontBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                Marshal.Copy(uncompressed, 0, data.Scan0, uncompressed.Length);
                fontBitmap.UnlockBits(data);
            }

            using (StreamWriter sw = new StreamWriter(Path.Combine(options.Output, "out.txt")))
            {
                for (int i = 0; i < font.Characters.Count; i++)
                {
                    FontCharacter c         = font.Characters[i];
                    int           u         = font.U[i];
                    int           v         = font.V[i];
                    int           charValue = font.Header.FirstAscii + i;

                    if (c.ByteWidth == 0)
                    {
                        continue;
                    }

                    char actualChar = '\0';
                    char rawChar    = (char)charValue;
                    if (charMap.ContainsKey(rawChar))
                    {
                        actualChar = charMap[rawChar];
                        sw.WriteLine("{0} \"{1}\"", charValue, actualChar);
                    }
                    else
                    {
                        sw.WriteLine("{0} \"\"", charValue);
                    }



                    using (Bitmap bm = new Bitmap(c.ByteWidth, font.Header.RenderHeight))
                    {
                        using (Graphics g = Graphics.FromImage(bm))
                        {
                            g.Clear(Color.Black);
                            g.DrawImage(fontBitmap, 0, 0, new Rectangle(u, v, c.ByteWidth, font.Header.RenderHeight), GraphicsUnit.Pixel);
                            g.Flush();
                        }
                        string bmName = String.Format("{0}.png", charValue);
                        string bmPath = Path.Combine(options.Output, bmName);
                        bm.Save(bmPath, ImageFormat.Png);
                    }
                }
            }
        }