/// <summary> /// Creates a bitmap font by loading an OS font, and drawing it to /// a bitmap to use as a Surface object. You should only use this method /// if writing a driver. /// </summary> /// <param name="options"></param> /// <returns></returns> public static BitmapFontImpl ConstructFromOSFont(BitmapFontOptions options) { System.Drawing.FontStyle drawingStyle = System.Drawing.FontStyle.Regular; if ((options.FontStyle & FontStyle.Bold) > 0) { drawingStyle |= System.Drawing.FontStyle.Bold; } if ((options.FontStyle & FontStyle.Italic) > 0) { drawingStyle |= System.Drawing.FontStyle.Italic; } if ((options.FontStyle & FontStyle.Strikeout) > 0) { drawingStyle |= System.Drawing.FontStyle.Strikeout; } if ((options.FontStyle & FontStyle.Underline) > 0) { drawingStyle |= System.Drawing.FontStyle.Underline; } Drawing.Font font = new Drawing.Font(options.FontFamily, options.SizeInPoints, drawingStyle); Drawing.Bitmap bmp; FontMetrics glyphs; ICharacterRenderer rend = options.UseTextRenderer ? (ICharacterRenderer) new TextRend(font) : (ICharacterRenderer) new GraphicsRend(font); MakeBitmap(options, rend, out bmp, out glyphs); //bmp.Save("testfont.png", Drawing.Imaging.ImageFormat.Png); string tempFile = System.IO.Path.GetTempFileName() + ".png"; bmp.Save(tempFile, Drawing.Imaging.ImageFormat.Png); bmp.Dispose(); Surface surf = new Surface(tempFile); System.IO.File.Delete(tempFile); return(new BitmapFontImpl(surf, glyphs)); }
public CharacterCreationMenu(IContentChest contentChest, IPlayerMaker playerMaker, IViewPortManager viewPortManager, IKeyboardDispatcher keyboardDispatcher, IUserInterface userInterface, IOptionsManager optionsManager, IContentLoader <IReadOnlyCollection <Hair> > hairContentLoader, IContentLoader <IReadOnlyCollection <Head> > headContentLoader, IContentLoader <IReadOnlyCollection <Eyes> > eyeContentLoader, IContentLoader <AsepriteSpriteMap> spriteMapLoader, ICharacterRenderer characterRenderer) { _contentChest = contentChest; _playerMaker = playerMaker; _viewPortPortManager = viewPortManager; _keyboardDispatcher = keyboardDispatcher; _userInterface = userInterface; _optionsManager = optionsManager; _hairContentLoader = hairContentLoader; _headContentLoader = headContentLoader; _eyeContentLoader = eyeContentLoader; _spriteMapLoader = spriteMapLoader; _characterRenderer = characterRenderer; }
public CharacterControl(ICharacterRenderProperties initialProperties, ICharacterRendererFactory characterRendererFactory) { _characterRenderer = characterRendererFactory.CreateCharacterRenderer(initialProperties); }
private static void MakeBitmap(BitmapFontOptions options, ICharacterRenderer rend, out Drawing.Bitmap bmp, out FontMetrics glyphs) { Size bitmapSize = new Size(256, 64); bmp = new System.Drawing.Bitmap(bitmapSize.Width, bitmapSize.Height); Drawing.Graphics g = Drawing.Graphics.FromImage(bmp); Drawing.Font font = rend.Font; g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit; glyphs = new FontMetrics(); const int bitmapPadding = 2; int x = rend.Padding, y = 2; int height = 0; char lastChar = ' '; // first measure the required height of the image. foreach (BitmapFontOptions.CharacterRange range in options.CharacterRanges) { for (char i = range.StartChar; i <= range.EndChar; i++) { Size sourceSize = rend.MeasureText(g, i.ToString()); if (options.CreateBorder) { sourceSize.Width += 2; sourceSize.Height += 2; } int thisWidth = sourceSize.Width + bitmapPadding; x += thisWidth; if (height < sourceSize.Height) { height = sourceSize.Height; } if (x > bitmapSize.Width) { x = 1 + thisWidth; y += height + bitmapPadding + 1; height = 0; } glyphs[i] = new GlyphMetrics(new Rectangle(0, 0, sourceSize.Width, sourceSize.Height)); lastChar = i; } } y += glyphs[lastChar].Height; if (y > bitmapSize.Height) { while (y > bitmapSize.Height) { bitmapSize.Height *= 2; } g.Dispose(); bmp.Dispose(); bmp = new System.Drawing.Bitmap(bitmapSize.Width, bitmapSize.Height); g = Drawing.Graphics.FromImage(bmp); } Drawing.Bitmap borderBmp = new System.Drawing.Bitmap(bmp.Width, bmp.Height); Drawing.Graphics borderG = Drawing.Graphics.FromImage(borderBmp); x = rend.Padding; y = 2; height = 0; Drawing.Color borderColor = System.Drawing.Color.FromArgb( options.BorderColor.A, options.BorderColor.R, options.BorderColor.G, options.BorderColor.B); foreach (BitmapFontOptions.CharacterRange range in options.CharacterRanges) { for (char i = range.StartChar; i <= range.EndChar; i++) { if (x + glyphs[i].Width > bitmapSize.Width) { x = rend.Padding; y += height + bitmapPadding + 1; height = 0; } if (options.CreateBorder) { rend.DrawText(borderG, i.ToString(), new Point(x, y + 1), borderColor); rend.DrawText(borderG, i.ToString(), new Point(x + 2, y + 1), borderColor); rend.DrawText(borderG, i.ToString(), new Point(x + 1, y), borderColor); rend.DrawText(borderG, i.ToString(), new Point(x + 1, y + 2), borderColor); rend.DrawText(g, i.ToString(), new Point(x + 1, y + 1), System.Drawing.Color.White); if (font.SizeInPoints >= 14.0) { glyphs[i].LeftOverhang = 1; } glyphs[i].RightOverhang = 1; } else { rend.DrawText(g, i.ToString(), new Point(x, y), System.Drawing.Color.White); } glyphs[i].SourceRect = new Rectangle( new Point(x, y), glyphs[i].Size); x += glyphs[i].Width + bitmapPadding; if (height < glyphs[i].Height) { height = glyphs[i].Height; } } } g.Dispose(); // do post processing of chars. PostProcessFont(options, bmp); // place the chars on the border image borderG.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height)); bmp.Dispose(); borderG.Dispose(); bmp = borderBmp; }
public CharacterPreview(ICharacterRenderer characterRenderer, Vector2 position) { _characterRenderer = characterRenderer; Position = position; }