From 0bfb97ad8270facd741de16e03f9035a38a38c25 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 7 Feb 2021 11:22:32 +0100 Subject: [PATCH] Revert optimization of CalcPixelWidth --- src/ui/Logic/TextWidth.cs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/ui/Logic/TextWidth.cs b/src/ui/Logic/TextWidth.cs index 91cf30c15..2e8220768 100644 --- a/src/ui/Logic/TextWidth.cs +++ b/src/ui/Logic/TextWidth.cs @@ -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; } } }