public virtual object Clone() { var tm = new TextMeasurement(); tm.CopyFrom(this); return(tm); }
static TextMeasurement charRectangle(this IntPtr hdc, string text, Rectangle container, string wholeText = null, Point?point = null, bool adjustByPoint = false, DrawTextFlags?flags = null) { if (string.IsNullOrEmpty(text)) { return(TextMeasurement.Default); } TextMeasurement measurement = new TextMeasurement(); Rectangle textBound; wholeText = (wholeText ?? text); var location = container.Location; var measureWholeText = point == null; measurement.UserPoint = point ?? Point.Empty; textBound = hdc.textBound(wholeText, container, flags: flags); var rect = textBound; var p = point ?? new Point(container.Right, container.Y); if (!measureWholeText) { if (p.X > textBound.Right) { p.X = textBound.Right; } else if (p.X < textBound.Left) { p.X = textBound.X; } } var charIndex = 0; var result = hdc.charRectangle(text, ref p, rect, flags, measureWholeText); charIndex = Math.Max(0, result.Item2); var rectangles = result.Item1; measurement.Bounds = rectangles[0]; measurement.TextBounds = (measureWholeText) ? rectangles[1] : textBound; rectangles[1] = measurement.TextBounds; if (!measureWholeText && adjustByPoint && charIndex > 0) { float middle = (float)measurement.Bounds.Left + measurement.Bounds.Width / 2; if (p.X > middle - 1) { Rectangle r; Dimension r1 = measurement.TextBounds; var newresult = hdc.charBound(text, charIndex + 2, ref r1, (int)flags.getMeasureFlag(false), out r); if (!newresult.Equals(measurement.Bounds) && newresult.X > measurement.Bounds.X) { charIndex++; measurement.Bounds = newresult; } } } if (measurement.Bounds.Size.Width <= 0) { measurement.Bounds = new Rectangle(measurement.Bounds.Location, new Size(2, 2)); } measurement.CharIndex = charIndex; measurement.Char = '\0'; measurement.Char = text[Math.Min(charIndex, text.Length - 1)]; return(measurement); }