internal java.awt.geom.Rectangle2D GetStringBounds(String aString, System.Drawing.Graphics g) { // TODO (KR) Could replace with System.Windows.Forms.TextRenderer#MeasureText (to skip creating Graphics) // // From .NET Framework Class Library documentation for Graphics#MeasureString: // // To obtain metrics suitable for adjacent strings in layout (for // example, when implementing formatted text), use the // MeasureCharacterRanges method or one of the MeasureString // methods that takes a StringFormat, and pass GenericTypographic. // Also, ensure the TextRenderingHint for the Graphics is // AntiAlias. // // TODO (KR) Consider implementing with one of the Graphics#MeasureString methods that takes a StringFormat. // TODO (KR) Consider implementing with Graphics#MeasureCharacterRanges(). if (aString.Length == 0) { SizeF size = g.MeasureString(aString, GetNetFont(), Int32.MaxValue, StringFormat.GenericTypographic); return new java.awt.geom.Rectangle2D.Float(0, 0, size.Width, size.Height); } else { StringFormat format = new StringFormat(StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap); format.Trimming = StringTrimming.None; format.SetMeasurableCharacterRanges(new CharacterRange[] { new CharacterRange(0, aString.Length) }); Region[] regions = g.MeasureCharacterRanges(aString, GetNetFont(), new RectangleF(0, 0, int.MaxValue, int.MaxValue), format); SizeF size = regions[0].GetBounds(g).Size; regions[0].Dispose(); return new java.awt.geom.Rectangle2D.Float(0, -getAscent(), size.Width, size.Height); } }
private System.Drawing.Point GetCharSize(System.Drawing.Graphics CurGraphics) { // DrawString doesn't actually print where you tell it to but instead consistently prints // with an offset. This is annoying when the other draw commands do not print with an offset // this method returns a point defining the offset so we can take it off the printstring command. System.Drawing.CharacterRange[] characterRanges = { new System.Drawing.CharacterRange(0, 1) }; System.Drawing.RectangleF layoutRect = new System.Drawing.RectangleF (0, 0, 100, 100); System.Drawing.StringFormat stringFormat = new System.Drawing.StringFormat(); stringFormat.SetMeasurableCharacterRanges(characterRanges); System.Drawing.Region[] stringRegions = new System.Drawing.Region[1]; stringRegions = CurGraphics.MeasureCharacterRanges( "A", this.Font, layoutRect, stringFormat); System.Drawing.RectangleF measureRect1 = stringRegions[0].GetBounds (CurGraphics); return new System.Drawing.Point ((int) (measureRect1.Width + 0.5), (int) (measureRect1.Height + 0.5)); }
private System.Drawing.Point GetDrawStringOffset(System.Drawing.Graphics CurGraphics, System.Int32 X, System.Int32 Y, System.Char CurChar) { //prntSome.printSome("GetDrawStringOffset"); // DrawString doesn't actually print where you tell it to but instead consistently prints // with an offset. This is annoying when the other draw commands do not print with an offset // this method returns a point defining the offset so we can take it off the printstring command. System.Drawing.CharacterRange[] characterRanges = { new System.Drawing.CharacterRange(0, 1) }; System.Drawing.RectangleF layoutRect = new System.Drawing.RectangleF (X, Y, 100, 100); System.Drawing.StringFormat stringFormat = new System.Drawing.StringFormat(); stringFormat.SetMeasurableCharacterRanges(characterRanges); System.Drawing.Region[] stringRegions = new System.Drawing.Region[1]; stringRegions = CurGraphics.MeasureCharacterRanges( CurChar.ToString (), this.Font, layoutRect, stringFormat); System.Drawing.RectangleF measureRect1 = stringRegions[0].GetBounds (CurGraphics); return new System.Drawing.Point ((int) (measureRect1.X + 0.5), (int) (measureRect1.Y + 0.5)); }
private static RectangleF MeasureRectangle(System.Drawing.Graphics graphics, int fontSize, System.Drawing.Font font, string s) { var stringFormat = new StringFormat(StringFormatFlags.NoWrap); stringFormat.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, 1) }); var ranges = graphics.MeasureCharacterRanges(s, font, new RectangleF(0, 0, 10 * fontSize, 10 * fontSize), stringFormat); var rect = ranges[0].GetBounds(graphics); return rect; }