Esempio n. 1
0
        /// <summary>
        /// Converts a TrueType to an image font.
        /// </summary>
        /// <param name="outputPath">The output path.</param>
        /// <param name="ttfFileName">The name of the TTF file.</param>
        /// <param name="fontStyle">The font style.</param>
        /// <param name="fontSize">Size of the font.</param>
        /// <param name="fontResolution">The font resolution.</param>
        /// <param name="leading">The leading.</param>
        /// <param name="tracking">The tracking.</param>
        /// <param name="spaceWidth">The width of the space character.</param>
        /// <param name="codePoints">The code points.</param>
        /// <remarks></remarks>
        public static void TrueTypeToImageFont(string outputPath, string ttfFileName, SD.FontStyle fontStyle, int fontSize, int fontResolution, int leading = 0, int tracking = 0, int spaceWidth = 0, IList <Range> codePoints = null)
        {
            if (codePoints == null)
            {
                codePoints = DefaultCodePoints;
            }

            using (var pfc = new PrivateFontCollection())
            {
                pfc.AddFontFile(ttfFileName);
                var glyphs = new Dictionary <char, RectangleF>();
                using (var bitmap = TrueTypeFont.CreateFontBitmap(pfc.Families[0], fontStyle, fontSize, fontResolution, ref leading, codePoints, glyphs))
                {
                    string name      = Path.GetFileName(ttfFileName) + "_" + fontStyle + "_" + fontSize + "_" + fontResolution;
                    var    imagefont = new ImageFont(name, glyphs, name + ".png", leading, tracking, new Size(bitmap.Size.Width, bitmap.Size.Height));

                    if (!Directory.Exists(outputPath))
                    {
                        Directory.CreateDirectory(outputPath);
                    }

                    imagefont.ToXElement().Save(Path.Combine(outputPath, name + ".xml"));
                    bitmap.Save(Path.Combine(outputPath, name + ".png"));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new Font for an image font.
        /// </summary>
        /// <param name="system">The MiyagiSystem.</param>
        /// <param name="name">The name of the ImageFont.</param>
        /// <param name="fontFileName">The filename of the ImageFont.</param>
        /// <param name="glyphCoordinates">The glyph coordinates of the ImageFont.</param>
        /// <returns>The newly created ImageFont.</returns>
        public static ImageFont Create(MiyagiSystem system, string name, string fontFileName, IDictionary <char, RectangleF> glyphCoordinates)
        {
            ImageFont retValue = new ImageFont(name)
            {
                FileName         = fontFileName,
                GlyphCoordinates = glyphCoordinates
            };

            retValue.CreateFont(system);
            return(retValue);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a ImageFont from XML.
        /// </summary>
        /// <param name="xElement">An XElement that contains the XML representation of the ImageFont.</param>
        /// <param name="system">The MiyagiSystem.</param>
        /// <returns>The newly created ImageFont or null if the XML file is invalid.</returns>
        public static ImageFont CreateFromXml(XElement xElement, MiyagiSystem system)
        {
            ImageFont retValue = null;

            var xv = new XmlValidator();

            if (xv.ValidateAgainstInternalSchema(xElement, "ImageFont"))
            {
                // create the ImageFont using the Name attribute
                retValue = new ImageFont(xElement.Attribute("Name").Value)
                {
                    FileName         = (string)xElement.Element("FileName"),
                    TabSize          = xElement.Element("TabSize") != null ? (int)xElement.Element("TabSize") : 3,
                    Tracking         = xElement.Element("Tracking") != null ? (int)xElement.Element("Tracking") : 0,
                    GlyphCoordinates = GetGlyphCoordinates(xElement)
                };

                var leading = xElement.Element("Leading");
                if (leading != null)
                {
                    retValue.Leading = (int)leading;
                }

                var spaceWidth = xElement.Element("SpaceWidth");
                if (leading != null)
                {
                    retValue.SpaceWidth = (int)spaceWidth;
                }

                retValue.CreateFont(system);
            }
            else
            {
                throw new XmlException("Invalid font XElement");
            }

            return(retValue);
        }