/// <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")); } } }
/// <summary> /// Creates a TrueTypeFont from XML. /// </summary> /// <param name="xElement">An XElement that contains the XML representation of the TrueTypeFont.</param> /// <param name="system">The MiyagiSystem.</param> /// <returns> /// The newly created TrueTypeFont or null if the XML file is invalid. /// </returns> public static TrueTypeFont CreateFromXml(XElement xElement, MiyagiSystem system) { TrueTypeFont retValue; var xv = new XmlValidator(); if (xv.ValidateAgainstInternalSchema(xElement, "TrueTypeFont")) { // create the TrueTypeFont using the Name attribute retValue = new TrueTypeFont(xElement.Attribute("Name").Value) { FileName = (string)xElement.Element("FileName"), FontStyle = xElement.Element("FontStyle").Value.ParseEnum <SD.FontStyle>(), Resolution = (int)xElement.Element("Resolution"), Size = (int)xElement.Element("Size"), TabSize = xElement.Element("TabSize") != null ? (int)xElement.Element("TabSize") : 3, Tracking = xElement.Element("Tracking") != null ? (int)xElement.Element("Tracking") : 0, }; var leading = xElement.Element("Leading"); if (leading != null) { retValue.Leading = (int)leading; } var spaceWidth = xElement.Element("SpaceWidth"); if (spaceWidth != null) { retValue.SpaceWidth = (int)spaceWidth; } // get CodePoints IList <Range> codePoints; if (xElement.Element("CodePoints") == null) { codePoints = DefaultCodePoints; } else { codePoints = new List <Range>( from codePointNode in xElement.Descendants("CodePoint") let codePoint = new Range( (int)codePointNode.Element("First"), (int)codePointNode.Element("Last")) select codePoint); } retValue.codePoints = codePoints; CreateFont(system, retValue); } else { throw new XmlException("Invalid font XElement"); } return(retValue); }
private static void CreateFont(MiyagiSystem system, TrueTypeFont font) { if (!system.Backend.TextureExists(font.TextureName)) { font.CreateFont(system); } else { font.Leading = GlobalLeadings[font.TextureName]; font.GlyphCoordinates = GlobalGlyphCoordinates[font.TextureName]; font.TextureSize = system.Backend.GetTextureSize(font.TextureName); } }
/// <summary> /// Creates a new TrueTypeFont. /// </summary> /// <param name="system">The MiyagiSystem.</param> /// <param name="name">The name of the TrueTypeFont.</param> /// <param name="fontFileName">The filename of the font.</param> /// <param name="size">The desired size in points.</param> /// <param name="resolution">The resolution in dpi.</param> /// <param name="fontStyle">The style of the font.</param> /// <param name="codePoints">A list codepoint ranges.</param> /// <returns>The created TrueType font.</returns> public static TrueTypeFont Create(MiyagiSystem system, string name, string fontFileName, int size, int resolution, SD.FontStyle fontStyle, IList <Range> codePoints) { if (codePoints == null) { codePoints = DefaultCodePoints; } var retValue = new TrueTypeFont(name) { FileName = fontFileName, Size = size, Resolution = resolution, FontStyle = fontStyle, codePoints = codePoints, }; CreateFont(system, retValue); return(retValue); }