mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-25 20:52:44 +01:00
Revert optimization of CalcPixelWidth
This commit is contained in:
parent
3ec673c5a6
commit
0bfb97ad82
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user