private float measureSpace() { //hack to measure the size of spaces. For some reason MeasureString returns bad results when string ends with a space. IFont font = _fonts.LoadFont(_config.Font.FontFamily, _config.Font.SizeInPoints * _scaleUp.X, _config.Font.Style); return(font.MeasureString(" a").Width - font.MeasureString("a").Width); }
private void CenterText(IFont font, int y, string text, Color color, Color borderColor) { Size size = font.MeasureString(text); int x = (Coordinates.Width - size.Width) / 2; DrawBorderedText(font, x, y, text, color, borderColor); }
private void CenterText(IFont font, int y, string text, Color color) { Size size = font.MeasureString(text); int x = (Coordinates.Width - size.Width) / 2; font.Color = color; font.DrawText(spriteBatch, x, y, text); }
private void FontTests(IFont font, out Rectangle drawRect) { Point drawPoint = new Point(10, 10); Size fontsize = font.MeasureString(text); drawRect = new Rectangle(drawPoint, fontsize); font.DrawText(drawPoint, text); }
public void DrawString(string text, IFont font, Color col, PointF pt, Color?backColor = null) { Point ptd = ImgToDisp(pt); if (backColor != null) { var size = font.MeasureString(text); Drawing.DrawRectangle(Buf, Bw, Bh, ptd.X, ptd.Y, ptd.X + size.Width, ptd.Y + size.Height, backColor.Value.ToArgb(), true); } font.DrawString(text, Buf, Bw, Bh, ptd.X, ptd.Y, col); }
public static void Show(Point location, string text, string caption = "Information") { var strSize = _font.MeasureString(text); Modals.Add(new MessageBox( new Rectangle( location.X, location.Y, Math.Max((int)strSize.X + Margin * 2, 100), Math.Max((int)strSize.Y + Ui.CaptionHeight + ButtonHeight + 8 + Margin, Ui.CaptionHeight + ButtonHeight + Margin * 2)), caption, text)); }
public static void Show(Point location, string text, Action onYes, string caption = "Are you sure?") { var strSize = _font.MeasureString(text); Modals.Add(new ConfirmationBox( new Rectangle( location.X, location.Y, Math.Max((int)strSize.X + Margin * 2, 150), Math.Max((int)strSize.Y + Ui.CaptionHeight + ButtonHeight + 8 + Margin, Ui.CaptionHeight + ButtonHeight + Margin * 2)), caption, text, onYes)); }
public static void Init(IFont font, int w, int h) { Font = font; _fontChars = ((TextureFont)font).GetFrames(); W = w; H = h; CharSize = Font.MeasureString(Font.DefaultCharacter.ToString()); Camera = new Camera((int)(W * CharSize.X), (int)(H * CharSize.Y)); //Camera.ClearBackground = false; }
public void Run(string[] args) { ChangeDisplayWindow(3); Surface mySurface = new Surface("Images/pointer.png"); Input.Unhandled.KeyDown += Keyboard_KeyDown; Input.Unhandled.MouseMove += (sender, e) => mousePosition = e.MousePosition; IFont font = Font.AgateSans; Size bottomSize = font.MeasureString(bottomText); Size topSize = font.MeasureString(topText + "z\nz"); // Run the program while the window is open. while (AgateApp.IsAlive) { var mouseText = topText + $"Resolution: {currentResolution}\nMouse: {mousePosition}"; Display.BeginFrame(); Display.Clear(Color.DarkGreen); font.DrawText(0, Display.CurrentWindow.Height - bottomSize.Height, bottomText); Display.Primitives.FillRect(Color.Maroon, new Rectangle(0, 0, Display.CurrentWindow.Width, topSize.Height)); font.DrawText(mouseText); mySurface.Draw(mousePosition.X, mousePosition.Y); Display.EndFrame(); AgateApp.KeepAlive(); } mySurface.Dispose(); wind.Dispose(); }
/// <summary> /// Creates the initial font bitmap. This is simply a long thin strip of all glyphs in a row /// </summary> /// <param name="font">The <see cref="IFont"/> object to build the initial bitmap from</param> /// <param name="maxSize">The maximum glyph size of the font</param> /// <param name="initialMargin">The initial bitmap margin (used for all four sides)</param> /// <param name="glyphs">A collection of <see cref="QFontGlyph"/>s corresponding to the initial bitmap</param> /// <param name="renderHint">The font rendering hint to use</param> /// <returns></returns> private Bitmap CreateInitialBitmap(IFont font, SizeF maxSize, int initialMargin, out QFontGlyph[] glyphs, TextGenerationRenderHint renderHint) { glyphs = new QFontGlyph[_charSet.Length]; int spacing = (int)Math.Ceiling(maxSize.Width) + 2 * initialMargin; Bitmap bmp = new Bitmap(spacing * _charSet.Length, (int)Math.Ceiling(maxSize.Height) + 2 * initialMargin, PixelFormat.Format24bppRgb); Graphics graph = Graphics.FromImage(bmp); switch (renderHint) { case TextGenerationRenderHint.SizeDependent: graph.TextRenderingHint = font.Size <= 12.0f ? TextRenderingHint.ClearTypeGridFit : TextRenderingHint.AntiAlias; break; case TextGenerationRenderHint.AntiAlias: graph.TextRenderingHint = TextRenderingHint.AntiAlias; break; case TextGenerationRenderHint.AntiAliasGridFit: graph.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; break; case TextGenerationRenderHint.ClearTypeGridFit: graph.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; break; case TextGenerationRenderHint.SystemDefault: graph.TextRenderingHint = TextRenderingHint.SystemDefault; break; } // enable high quality graphics graph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graph.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; int xOffset = initialMargin; for (int i = 0; i < _charSet.Length; i++) { var offset = font.DrawString("" + _charSet[i], graph, Brushes.White, xOffset, initialMargin); var charSize = font.MeasureString("" + _charSet[i], graph); glyphs[i] = new QFontGlyph(0, new Rectangle(xOffset - initialMargin + offset.X, initialMargin + offset.Y, (int)charSize.Width + initialMargin * 2, (int)charSize.Height + initialMargin * 2), 0, _charSet[i]); xOffset += (int)charSize.Width + initialMargin * 2; } graph.Flush(); graph.Dispose(); return(bmp); }
//these do not affect the actual width of glyphs (we measure widths pixel-perfectly ourselves), but is used to detect whether a font is monospaced private List <SizeF> GetGlyphSizes(IFont font) { // We add padding to the returned sizes measured by MeasureString, because on some platforms (*cough*Mono*cough) this method // can return unreliable information. Without padding, this leads to some glyphs not fitting on the generated // texture page when we precisely measure their bounds // Hopefully a padding of 5 is enough, however may need to increase this? // For now we scale it with font size int padding = 5 + (int)(0.1 * font.Size); Bitmap bmp = new Bitmap(512, 512, PixelFormat.Format24bppRgb); Graphics graph = Graphics.FromImage(bmp); List <SizeF> sizes = new List <SizeF>(); foreach (char t in _charSet) { var charSize = font.MeasureString("" + t, graph); sizes.Add(new SizeF(charSize.Width + padding, charSize.Height + padding)); } graph.Dispose(); bmp.Dispose(); return(sizes); }
public ContextMenu(IEnumerable <MenuItem> items, bool visible = true) : base(null, null, visible) { BorderSize = 1; var menuItems = items as MenuItem[] ?? items.ToArray(); var maxX = (int)menuItems.Max(m => _font.MeasureString(m.Text).X); var yPerItem = ListBox <MenuItem> .ItemHeight; var visibleItems = Math.Min(MaxItems, menuItems.Length); var rect = new Rectangle(0, 0, maxX + Margin * 2 + Scroll.Width, visibleItems * yPerItem + Margin * 2); SetRect(rect); _listBox = new ListBox <MenuItem>(null, null, rect, false, 1); _listBox.AddRange(menuItems, item => item.Text); _listBox.OnSelect = item => { if (item.OnClick != null) { Shortcuts.SetToolSelect(); Close(); } _listBox.Unselect(); item.OnClick?.Invoke(); }; }
void CalculateBounds() { var size = LabelFont.MeasureString(Message); _bounds = size + (new Vector2(Padding, Padding) * 2); }
private void AutoSize(string text) { Vector2 v = _font.MeasureString(text, 1.0f); // using scale of 1.0f Size = new Size((int)v.X, (int)v.Y); }
public Size MeasureString(string text, IFont font) { return(font.MeasureString(text)); }
static Rectangle? GetMaximumCharacterExtents( Graphics g, IFont font, Char ch ) { SizeF size = font.MeasureString( g, ch.ToString() ); return new Rectangle( 0, 0, (int)Math.Ceiling( size.Width ), (int)Math.Ceiling( size.Height ) ); }
public void Run(string[] args) { using (var wind = new DisplayWindowBuilder(args) .BackbufferSize(800, 600) .QuitOnClose() .Build()) { DisplayWindow fullWind = null; FontSurface bitmapFontSurface = FontSurface.BitmapMonospace("lotafont.png", new Size(16, 16)); Font bitmapFont = new FontBuilder("lotafont").AddFontSurface( new FontSettings(16, FontStyles.None), bitmapFontSurface).Build(); int frame = 0; while (AgateApp.IsAlive) { Display.BeginFrame(); Display.Clear(Color.DarkGray); IFont font = Font.AgateSans; font.Size = 12; // test the color changing font.Color = Color.LightGreen; font.DrawText(20, 150, "This is regular green text."); font.Color = Color.White; // test display alignment property Point textPoint = new Point(100, 50); string text = string.Format("This text is centered on {0},{1}.", textPoint.X, textPoint.Y); Size textSize = font.MeasureString(text); // draw a box around where the text should be displayed. Display.Primitives.DrawRect(Color.Gray, new Rectangle(textPoint.X - textSize.Width / 2, textPoint.Y - textSize.Height / 2, textSize.Width, textSize.Height)); font.DisplayAlignment = OriginAlignment.Center; font.DrawText(textPoint, text); font.DisplayAlignment = OriginAlignment.TopLeft; // test text scaling font.Size = 24; text = "This text is twice as big."; textPoint = new Point(50, 75); textSize = font.MeasureString(text); // draw a box with the same size the text should appear as Display.Primitives.DrawRect(Color.White, new Rectangle(textPoint, textSize)); font.DrawText(textPoint, text); font.Size = 12; // this draws a white background behind the text we want to Display. text = "F2: Toggle VSync F5: Toggle Windowed / Fullscreen "; text += "FPS: " + Display.FramesPerSecond.ToString("0.00") + " "; if (AgateApp.IsActive) { text += "Active"; } else { text += "Not Active"; } // figure out how big the displayed text will be textSize = font.MeasureString(text); // draw the white background Display.Primitives.FillRect(Color.White, new Rectangle(new Point(0, 0), textSize)); // draw the text on top of the background font.Color = Color.Black; font.DrawText(text); // supplying no position arguments defaults to (0, 0) // draw something which moves to let us know the program is running Display.Primitives.FillRect(Color.Red, new Rectangle( 10, 200, 70 + (int)(50 * Math.Cos(frame / 10.0)), 50)); // do some bitmap font stuff bitmapFont.DrawText(10, 350, "THIS IS BITMAP FONT TEXT."); bitmapFont.Color = Color.Red; bitmapFont.DrawText(10, 366, "THIS IS RED TEXT."); bitmapFont.Color = Color.White; bitmapFont.Size = 32; bitmapFont.DrawText(10, 382, "THIS IS BIGG."); Display.Primitives.FillRect(Color.Blue, new Rectangle(95, 425, 10, 10)); bitmapFont.TextAlignment = OriginAlignment.Center; bitmapFont.DrawText(100, 430, "CHECK"); bitmapFont.TextAlignment = OriginAlignment.TopLeft; Display.Primitives.FillRect(Color.Green, new Rectangle(-10, -10, 20, 20)); // and we're done. Display.EndFrame(); AgateApp.KeepAlive(); frame++; // toggle full screen if the user pressed F5; if (Input.Unhandled.Keys[KeyCode.F5]) { System.Diagnostics.Debug.Print("IsFullscreen: {0}", Display.CurrentWindow.IsFullScreen); if (Display.CurrentWindow.IsFullScreen == false) { fullWind = DisplayWindow.CreateFullScreen("Font Tester", 800, 600); } else { fullWind.Dispose(); Display.RenderTarget = wind.FrameBuffer; } Input.Unhandled.Keys.ReleaseAll(); System.Diagnostics.Debug.Print("IsFullscreen: {0}", Display.CurrentWindow.IsFullScreen); } else if (Input.Unhandled.Keys[KeyCode.F2]) { Display.RenderState.WaitForVerticalBlank = !Display.RenderState.WaitForVerticalBlank; Input.Unhandled.Keys.Release(KeyCode.F2); } else if (Input.Unhandled.Keys[KeyCode.Escape]) { Display.Dispose(); return; } } } }
public override void Update() { if (_typeAlarm.Update()) { TextPtr += 1; try { if (Text[TextPtr] == ' ') { TextPtr += 1; } if (Text[TextPtr] == Environment.NewLine[0]) { TextPtr += Environment.NewLine.Length; } if (TextPtr >= Text.Length) { throw new Exception(); } } catch (Exception) { _typeAlarm.Active = false; TextPtr = Text.Length - 1; _delayAlarm.Set(1 + Text.Length * 0.1); } } var str = ""; if (!_dead) { str = Text.Substring(0, TextPtr + 1); } _targetTextSize = _font.MeasureString(str); _textSize.X += (float)GameCntrl.Time((_targetTextSize.X - _textSize.X) / _textRubberBand); _textSize.Y += (float)GameCntrl.Time((_targetTextSize.Y - _textSize.Y) / _textRubberBand); _targetPos = Test.RoundVector2(Owner.Position + MainOffset); _pos.X += (float)GameCntrl.Time((_targetPos.X - _pos.X) / _posRubberBand); _pos.Y += (float)GameCntrl.Time((_targetPos.Y - _pos.Y) / _posRubberBand); if ((_targetPos - _pos).Length() > _maxBubbleDist) { var e = _targetPos - _pos; e.Normalize(); _pos = _targetPos + e * _maxBubbleDist; } if (_delayAlarm.Update()) { //Objects.Destroy(this); _dead = true; _textRubberBand = 2f / 60f; } if (_dead && _textSize.X < 4 && _textSize.Y < 4) { Objects.Destroy(this); } var phaseAdd = GameCntrl.Time(_wiggleysSpd); for (var i = 0; i < _wiggleys.Length; i += 1) { _wiggleysPhase[i] += phaseAdd; if (_wiggleysPhase[i] > Math.PI * 2) { _wiggleysPhase[i] -= Math.PI * 2; } _wiggleys[i] = new Vector2( (float)Math.Cos(_wiggleysPhase[i]), (float)Math.Sin(_wiggleysPhase[i]) ) * _wiggleysR; } }