/// <summary> /// Loads a font from a file and adds it to the <see cref="Fonts"/> collection. /// </summary> /// <param name="font">The font file to load.</param> /// <returns>A master font that you can generate a usable font from.</returns> public static FontMaster LoadFont(string font) { //if (!File.Exists(font)) //{ // font = Path.Combine(Path.Combine(Path.GetDirectoryName(Path.GetFullPath(font)), "fonts"), Path.GetFileName(font)); // if (!File.Exists(font)) // throw new Exception($"Font does not exist: {font}"); //} //FontPathHint = Path.GetDirectoryName(Path.GetFullPath(font)); try { FontMaster masterFont = SadConsole.Serializer.Load <FontMaster>(font, false); if (Fonts.ContainsKey(masterFont.Name)) { Fonts.Remove(masterFont.Name); } Fonts.Add(masterFont.Name, masterFont); return(masterFont); } catch (System.Runtime.Serialization.SerializationException) { throw; } }
private void Initialize(FontMaster masterFont, FontSizes fontMultiple) { Master = masterFont; FontImage = masterFont.Image; MaxGlyphIndex = masterFont.Rows * masterFont.Columns - 1; switch (fontMultiple) { case FontSizes.Quarter: Size = new Point((int)(masterFont.GlyphWidth * 0.25), (int)(masterFont.GlyphHeight * 0.25)); break; case FontSizes.Half: Size = new Point((int)(masterFont.GlyphWidth * 0.5), (int)(masterFont.GlyphHeight * 0.5)); break; case FontSizes.One: Size = new Point(masterFont.GlyphWidth, masterFont.GlyphHeight); break; case FontSizes.Two: Size = new Point(masterFont.GlyphWidth * 2, masterFont.GlyphHeight * 2); break; case FontSizes.Three: Size = new Point(masterFont.GlyphWidth * 3, masterFont.GlyphHeight * 3); break; case FontSizes.Four: Size = new Point(masterFont.GlyphWidth * 4, masterFont.GlyphHeight * 4); break; default: break; } if (Size.X == 0 || Size.Y == 0) { throw new ArgumentException($"This font cannot use size {fontMultiple.ToString()}, at least one axis is 0.", "fontMultiple"); } SizeMultiple = fontMultiple; Name = masterFont.Name; GlyphRects = masterFont.GlyphIndexRects; SolidGlyphIndex = masterFont.SolidGlyphIndex; Rows = masterFont.Rows; Columns = masterFont.Columns; }
public static IEnumerable <GlyphDefinition> ListGlyphDefinitions(this FontMaster _thisFM, string basename, SpriteEffects?mirrorMode = null) { int? frameIndex = null; string currentName; List <GlyphDefinition> outList = new List <GlyphDefinition>(); bool useMirror = mirrorMode.HasValue; while (_thisFM.HasGlyphDefinition(currentName = basename + frameIndex)) { GlyphDefinition glyph = _thisFM.GetGlyphDefinition(currentName); yield return(mirrorMode.HasValue ? new GlyphDefinition(glyph.Glyph, mirrorMode.Value) : glyph); frameIndex = (frameIndex == null ? 1 : ++frameIndex); } }
internal Font(FontMaster masterFont, FontSizes fontMultiple) { Initialize(masterFont, fontMultiple); }
private void Initialize(FontMaster masterFont, FontSizes fontMultiple) { FontImage = masterFont.Image; MaxGlyphIndex = masterFont.Rows * masterFont.Columns - 1; switch (fontMultiple) { case FontSizes.Quarter: Size = new Point((int)(masterFont.GlyphWidth * 0.25), (int)(masterFont.GlyphHeight * 0.25)); break; case FontSizes.Half: Size = new Point((int)(masterFont.GlyphWidth * 0.5), (int)(masterFont.GlyphHeight * 0.5)); break; case FontSizes.One: Size = new Point(masterFont.GlyphWidth, masterFont.GlyphHeight); break; case FontSizes.Two: Size = new Point(masterFont.GlyphWidth * 2, masterFont.GlyphHeight * 2); break; case FontSizes.Three: Size = new Point(masterFont.GlyphWidth * 3, masterFont.GlyphHeight * 3); break; case FontSizes.Four: Size = new Point(masterFont.GlyphWidth * 4, masterFont.GlyphHeight * 4); break; default: break; } if (Size.X == 0 || Size.Y == 0) throw new ArgumentException($"This font cannot use size {fontMultiple.ToString()}, at least one axis is 0.", "fontMultiple"); SizeMultiple = fontMultiple; Name = masterFont.Name; GlyphIndexRects = masterFont.GlyphIndexRects; SolidGlyphIndex = masterFont.SolidGlyphIndex; Rows = masterFont.Rows; Columns = masterFont.Columns; }