Revert optimization of CalcPixelWidth

This commit is contained in:
Nikolaj Olsson 2021-02-07 11:22:32 +01:00
parent 3ec673c5a6
commit 0bfb97ad82

View File

@ -1,14 +1,11 @@
using Nikse.SubtitleEdit.Core.Common;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Nikse.SubtitleEdit.Logic
{
public static class TextWidth
{
private static readonly Graphics Graphics = Graphics.FromHwnd(IntPtr.Zero);
private static readonly object GdiLock = new object();
public static int CalcPixelWidth(string text)
{
if (string.IsNullOrEmpty(text))
@ -18,15 +15,12 @@ namespace Nikse.SubtitleEdit.Logic
using (var measureFont = new Font(Configuration.Settings.General.MeasureFontName, Configuration.Settings.General.MeasureFontSize, Configuration.Settings.General.MeasureFontBold ? FontStyle.Bold : FontStyle.Regular))
{
lock (GdiLock)
{
// MeasureString adds padding, se we'll calculate the length of 2x the text +
// padding, and substract the length of 1x the text + padding.
// I.e. [testtest] - [test] = length of 'test' without padding.
var measuredWidth = Graphics.MeasureString(text, measureFont).Width;
var measuredDoubleWidth = Graphics.MeasureString(text + text, measureFont).Width;
return (int)Math.Round(measuredDoubleWidth - measuredWidth);
}
// MeasureString adds padding, se we'll calculate the length of 2x the text +
// padding, and substract the length of 1x the text + padding.
// I.e. [testtest] - [test] = length of 'test' without padding.
int measuredWidth = TextRenderer.MeasureText(text, measureFont, Size.Empty, TextFormatFlags.NoPadding).Width;
int measuredDoubleWidth = TextRenderer.MeasureText(text + text, measureFont, Size.Empty, TextFormatFlags.NoPadding).Width;
return measuredDoubleWidth - measuredWidth;
}
}
}