Skip to content

Commit

Permalink
WinForms: Fix text measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jan 14, 2024
1 parent a86427c commit 1172c4f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
7 changes: 3 additions & 4 deletions NAPS2.Lib.WinForms/EtoForms/WinForms/WinFormsEtoPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public override void ConfigureImageButton(Button button, bool big)
}

var imageWidth = native.Image!.Width;
using var g = native.CreateGraphics();
var textWidth = (int) g.MeasureString(native.Text, native.Font).Width;
var textWidth = WF.TextRenderer.MeasureText(native.Text, native.Font).Width;
native.AutoSize = false;

if (big)
Expand Down Expand Up @@ -187,8 +186,8 @@ public override SizeF GetWrappedSize(Control control, int defaultWidth)
{
if (control.ControlObject is WF.Label label)
{
using var g = label.CreateGraphics();
return g.MeasureString(label.Text, label.Font, defaultWidth).ToEto();
return WF.TextRenderer.MeasureText(label.Text, label.Font, new SD.Size(defaultWidth, int.MaxValue),
WF.TextFormatFlags.WordBreak).ToEto();
}
return base.GetWrappedSize(control, defaultWidth);
}
Expand Down
2 changes: 1 addition & 1 deletion NAPS2.Lib.WinForms/EtoForms/WinForms/WinFormsListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void CustomRenderItem(object? sender, DrawListViewItemEventArgs e)
// to have room for the page numbers, and the selection rectangle has a completely different style to
// encompass the page numbers too.
string label = $"{e.ItemIndex + 1} / {_view.Items.Count}";
SizeF textSize = e.Graphics.MeasureString(label, _view.Font);
SizeF textSize = TextRenderer.MeasureText(label, _view.Font);
int textOffset = (int) (textSize.Height + PageNumberTextPadding);

float scaleHeight = (float) (ImageSize - textOffset) / image.Height;
Expand Down
5 changes: 3 additions & 2 deletions NAPS2.Lib.WinForms/Util/StringWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NAPS2.Util;

Expand All @@ -8,7 +9,7 @@ namespace NAPS2.Util;
/// </summary>
public class StringWrapper
{
public string Wrap(string text, int maxWidth, Graphics drawingGraphics, Font drawingFont)
public string Wrap(string text, int maxWidth, Font drawingFont)
{
var result = new StringBuilder();
var parts = new Queue<string>(text.Split(' '));
Expand All @@ -19,7 +20,7 @@ public string Wrap(string text, int maxWidth, Graphics drawingGraphics, Font dra
{
nextParts.Add(parts.Dequeue());
} while (
parts.Count > 0 && drawingGraphics.MeasureString(string.Join(" ", nextParts.Concat(new[] { parts.Peek() })).Replace("&", ""),
parts.Count > 0 && TextRenderer.MeasureText(string.Join(" ", nextParts.Concat(new[] { parts.Peek() })).Replace("&", ""),
drawingFont).Width < maxWidth);
result.Append(string.Join(" ", nextParts));
if (parts.Count > 0)
Expand Down
5 changes: 2 additions & 3 deletions NAPS2.Lib.WinForms/WinForms/ToolStripDoubleButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ public override Size GetPreferredSize(Size constrainingSize)

private int MeasureTextWidth(string text, ref bool wrap)
{
using var g = Graphics.FromImage(new Bitmap(1, 1));
var width = (int) Math.Ceiling(g.MeasureString(text, Font).Width);
var width = TextRenderer.MeasureText(text, Font).Width;
if (MaxTextWidth > 0 && width > MaxTextWidth)
{
var words = text.Split(' ');
for (int i = 1; i < words.Length; i++)
{
var left = string.Join(" ", words.Take(words.Length - i));
var right = string.Join(" ", words.Skip(words.Length - i));
var wrappedWidth = (int) Math.Ceiling(g.MeasureString(left + "\n" + right, Font).Width);
var wrappedWidth = TextRenderer.MeasureText(left + "\n" + right, Font).Width;
if (wrappedWidth < width)
{
width = wrappedWidth;
Expand Down
7 changes: 2 additions & 5 deletions NAPS2.Lib.WinForms/WinForms/ToolbarFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ public void RelayoutToolbar(ToolStrip tStrip)
{
if (tStrip.Parent == null) return;
// Resize and wrap text as necessary
using (var g = tStrip.CreateGraphics())
foreach (var btn in tStrip.Items.OfType<ToolStripItem>())
{
foreach (var btn in tStrip.Items.OfType<ToolStripItem>())
{
btn.Text = _stringWrapper.Wrap(btn.Text ?? "", 80, g, btn.Font);
}
btn.Text = _stringWrapper.Wrap(btn.Text ?? "", 80, btn.Font);
}
ResetToolbarMargin(tStrip);
// Recalculate sizes
Expand Down

0 comments on commit 1172c4f

Please sign in to comment.