Beispiel #1
0
        public static GPK Load(String filename)
        {
            GPK rv = new GPK(filename);

            ZIP z = new ZIP(filename);
            z.Open();

            String type = z.ExtractFileToString("Type.txt");
            switch (type.ToUpperInvariant().Trim())
            {
                case "GFX":
                    rv.PackageType = GPKType.Graphics;
                    break;
                case "FNT":
                    rv.PackageType = GPKType.Font;
                    break;
            }

            foreach(ZipStorer.ZipFileEntry zf in z.ListFiles().Where(x => x.FilenameInZip != "Type.txt"))
            {
                MemoryStream ms = new MemoryStream();
                if(z.ExtractFileToStream(zf, ref ms))
                {
                    int code = -1;
                    String name = String.Empty;
                    if (zf.FilenameInZip.Contains("_"))
                    {
                        code = int.Parse(zf.FilenameInZip.Substring(0, zf.FilenameInZip.IndexOf('_')));
                        name = zf.FilenameInZip.Substring(zf.FilenameInZip.IndexOf('_') + 1);
                    }
                    else
                    {
                        code = int.Parse(zf.FilenameInZip.Substring(0, zf.FilenameInZip.IndexOf(".")));
                        name = zf.FilenameInZip;
                    }
                    GPBitmap bmp = new GPBitmap()
                    {
                        Code = code,
                        Name = name,
                        Filename = zf.FilenameInZip,
                        Bitmap = Image.FromStream(ms)
                    };
                    bmp.Name = bmp.Name.Substring(0, bmp.Name.LastIndexOf('.'));
                    bmp.Size = bmp.Bitmap.Size;
                    ms.Position = 0;
                    PNG p = new PNG(ms);
                    String tmp = String.Empty;
                    foreach (PNG.PNGChunk c in p.Chunks.Where(x => x.Type == "tEXt"))
                    {
                        switch (c.ToString().Split('=')[0])
                        {
                            case "Description":
                                bmp.Description = c.ToString().Substring(c.ToString().IndexOf('=') + 1);
                                break;
                            case "ControlPoints":
                                foreach (String cp in c.ToString().Substring(c.ToString().IndexOf('=') + 1).Split('|'))
                                    bmp.ControlPoints.Add(int.Parse(cp.Split('=')[0]), new Point(int.Parse(cp.Split('=')[1].Split(',')[0]), int.Parse(cp.Split('=')[1].Split(',')[1])));
                                break;
                            case "OffsetPos":
                                tmp = c.ToString().Substring(c.ToString().IndexOf('=') + 1);
                                bmp.OffsetPosition = new Point(int.Parse(tmp.Split(',')[0]), int.Parse(tmp.Split(',')[1]));
                                break;
                            case "OffsetSize":
                                tmp = c.ToString().Substring(c.ToString().IndexOf('=') + 1);
                                bmp.OffsetSize = new Size(int.Parse(tmp.Split(',')[0]), int.Parse(tmp.Split(',')[1]));
                                break;
                        }
                    }
                    if (bmp.ControlPoints.Count == 0)
                        bmp.ControlPoints.Add(0, new Point(bmp.Bitmap.Size.Width / 2, bmp.Bitmap.Size.Height / 2));
                    bmp.Center = bmp.ControlPoints[0];
                    ms.Close(); ms.Dispose();
                    rv.Bitmaps.Add(bmp);
                }
            }

            z.Close();
            rv._filename = filename;
            return rv;
        }
        public static void Convert(String src, String dst)
        {
            FileInfo fi = new FileInfo(src);
            FileInfo fiDst = new FileInfo(dst);

            List<String> Lfiles = new List<String>();
            String tmpFolder = Path.GetTempPath() + fi.Name + DateTime.Now.Ticks.ToString() + "\\";
            String outFolder = tmpFolder + "output\\";
            if (!Directory.Exists(outFolder))
                Directory.CreateDirectory(outFolder);

            ZIP z = new ZIP();
            try
            {
                z = new ZIP(fi.FullName);
                z.Open();

                font f = Xml.Deserialize<font>("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + z.ExtractFileToString("font.fnt"));

                foreach (fontPage p in f.pages)
                    z.ExtractFile(p.file, tmpFolder + p.file);

                foreach (fontCharsChar c in f.chars.@char)
                {
                    if (c.width == 0 && c.height == 0)
                        continue;

                    String outFile = outFolder + c.id.ToString().PadLeft(3, '0') + ".png";
                    Rectangle rCrop = new Rectangle(c.x, c.y, c.width, c.height);
                    using (Bitmap bSrc = Image.FromFile(tmpFolder + f.pages[c.page].file) as Bitmap)
                    {
                        using (Bitmap bDst = new Bitmap(c.width, c.height))
                        {
                            using (Graphics g = Graphics.FromImage(bDst))
                                g.DrawImage(bSrc, new Rectangle(0, 0, bDst.Width, bDst.Height), rCrop, GraphicsUnit.Pixel);
                            bDst.Save(outFile);
                        }
                    }
                    PNG p = new PNG(outFile);
                    p.SetTextChunk("Description", c.id.ToString());
                    p.SetTextChunk("OffsetPos", "0,0");
                    p.SetTextChunk("OffsetSize", c.xoffset + "," + c.yoffset);
                    p.Save(outFile);
                    Lfiles.Add(outFile);
                }
                z.Close();
            }
            catch (Exception)
            {
                z.Close();
            }

            using (StreamWriter sw = new StreamWriter(outFolder + "\\Type.txt"))
                sw.WriteLine("FNT");
            Lfiles.Add(outFolder + "\\Type.txt");

            ZIP fnt = new ZIP(dst);
            fnt.Files.AddRange(Lfiles);
            fnt.Save();
            fnt.Close();

            if (Directory.Exists(tmpFolder))
                Directory.Delete(tmpFolder, true);
        }