Ejemplo n.º 1
0
        public void Compile()
        {
            ParsedPath spriteFontFileName = Target.InputFiles.Where(f => f.Extension == ".spritefont").First();
            ParsedPath stringsFileName    = Target.InputFiles.Where(f => f.Extension == ".strings").First();
            ParsedPath xnbFileName        = Target.OutputFiles.Where(f => f.Extension == ".xnb").First();

            SpriteFontFile sff = SpriteFontFileReader.ReadFile(spriteFontFileName);
            StringsFileV1  sf  = StringsFileReaderV1.ReadFile(stringsFileName);

            HashSet <char> hs = new HashSet <char>();

            foreach (var item in sf.Strings)
            {
                for (int i = 0; i < item.Value.Length; i++)
                {
                    hs.Add(item.Value[i]);
                }
            }

            foreach (var region in sff.CharacterRegions)
            {
                for (char c = region.Start; c <= region.End; c++)
                {
                    hs.Add(c);
                }
            }

            List <char>       fontChars  = hs.OrderBy(c => c).ToList();
            FontSlant         fontSlant  = (sff.Style == SpriteFontFile.FontStyle.Italic ? FontSlant.Italic : FontSlant.Normal);
            FontWeight        fontWeight = (sff.Style == SpriteFontFile.FontStyle.Bold ? FontWeight.Bold : FontWeight.Normal);
            ParsedPath        pngFile    = xnbFileName.WithExtension(".png");
            SpriteFontContent sfc        = CreateSpriteFontContent(
                sff.FontName, sff.Size, fontSlant, fontWeight, sff.Spacing, sff.DefaultCharacter, fontChars, pngFile);

            if (!Directory.Exists(xnbFileName.VolumeAndDirectory))
            {
                Directory.CreateDirectory(xnbFileName.VolumeAndDirectory);
            }

            XnbFileWriterV5.WriteFile(sfc, xnbFileName);
        }
Ejemplo n.º 2
0
        public void Compile()
        {
            IEnumerable <ParsedPath> svgFileNames  = Target.InputFiles.Where(f => f.Extension == ".svg");
            ParsedPath            pinboardFileName = Target.InputFiles.Where(f => f.Extension == ".pinboard").First();
            ParsedPath            xnbFileName      = Target.OutputFiles.Where(f => f.Extension == ".xnb").First();
            PinboardFileV1        pinboardFile     = PinboardFileCache.Load(pinboardFileName);
            List <ImagePlacement> placements       = new List <ImagePlacement>();
            string rectangleName = this.Target.Properties.GetRequiredValue("Rectangle");

            PinboardFileV1.RectangleInfo rectInfo = pinboardFile.GetRectangleInfoByName(rectangleName);

            if (rectInfo == null)
            {
                throw new ContentFileException("Rectangle {0} not found in pinboard file {1}".CultureFormat(rectangleName, pinboardFileName));
            }

            string converterName = Target.Properties.GetOptionalValue("Converter", "Inkscape").ToLower();

            if (converterName != "inkscape" && converterName != "rsvg")
            {
                throw new ContentFileException("Unknown SVG converter '{0}'".CultureFormat(converterName));
            }

            ParsedPath combinedPngPathName = xnbFileName.WithExtension(".png");

            try
            {
                int row = 0;

                foreach (var svgFileName in svgFileNames)
                {
                    int col = 0;

                    if (rectInfo == null)
                    {
                        throw new InvalidOperationException(
                                  "Rectangle '{0}' not found in pinboard '{1}'".CultureFormat(rectangleName, pinboardFileName));
                    }

                    // TODO-johnls: This should probably go in an intermediate file directory
                    ParsedPath pngFile = xnbFileName.WithFileAndExtension(String.Format("{0}_{1}_{2}.png",
                                                                                        svgFileName.File, row, col));

                    placements.Add(new ImagePlacement(pngFile,
                                                      new Rectangle(col * rectInfo.Width, row * rectInfo.Height, rectInfo.Width, rectInfo.Height)));

                    switch (converterName)
                    {
                    default:
                    case "rsvg":
                        ImageTools.SvgToPngWithRSvg(svgFileName, pngFile, rectInfo.Width, rectInfo.Height);
                        break;

                    case "inkscape":
                        ImageTools.SvgToPngWithInkscape(svgFileName, pngFile, rectInfo.Width, rectInfo.Height);
                        break;
                    }
                }

                ImageTools.CombinePngs(placements, combinedPngPathName);

                Texture2DContent textureContent;

                ImageTools.CompressPngToTexture2DContent(
                    combinedPngPathName,
                    this.Target.Properties.GetOptionalValue("CompressionType", "None"),
                    out textureContent);

                XnbFileWriterV5.WriteFile(textureContent, xnbFileName);
            }
            finally
            {
                foreach (var placement in placements)
                {
                    if (File.Exists(placement.ImageFile))
                    {
                        File.Delete(placement.ImageFile);
                    }
                }

                if (File.Exists(combinedPngPathName))
                {
                    File.Delete(combinedPngPathName);
                }
            }
        }