SubtitleEdit/libse/TextSplitResult.cs

103 lines
3.5 KiB
C#
Raw Normal View History

2019-11-08 13:09:48 +01:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Nikse.SubtitleEdit.Core
{
public class TextSplitResult
{
public List<string> Lines { get; set; }
2019-11-09 16:02:12 +01:00
public List<float> LengthPixels { get; set; }
public List<int> LengthCharacters { get; set; }
public bool IsBottomHeavy => LengthPixels[1] + 2 > LengthPixels[0]; // allow a small diff of 2 pixels
public static float SpaceLengthPixels { get; set; }
public double TotalLength => Lines.Sum(p => p.Length);
public double TotalLengthPixels => LengthPixels.Sum(p => p) - SpaceLengthPixels;
2019-11-08 13:09:48 +01:00
2020-03-01 19:29:27 +01:00
private static readonly Graphics Graphics = Graphics.FromHwnd(IntPtr.Zero);
private static readonly Font DefaultFont = SystemFonts.DefaultFont;
private static readonly object GdiLock = new object();
2020-03-01 15:50:39 +01:00
2019-11-09 16:02:12 +01:00
public TextSplitResult(List<string> lines)
2019-11-08 13:09:48 +01:00
{
2019-11-09 16:02:12 +01:00
Lines = lines;
LengthPixels = new List<float>();
if (Configuration.Settings.Tools.AutoBreakUsePixelWidth)
2019-11-08 13:09:48 +01:00
{
2020-07-13 12:58:06 +02:00
if (Lines[0].Length > 1000)
{
2020-07-13 12:58:06 +02:00
SpaceLengthPixels = Lines[0].Length * 7;
}
2020-07-13 12:58:06 +02:00
else
{
lock (GdiLock)
{
2020-07-13 17:52:15 +02:00
var lineOneWidth = Graphics.MeasureString(Lines[0], DefaultFont).Width;
LengthPixels.Add(lineOneWidth);
2020-07-13 12:58:06 +02:00
}
}
if (Lines[1].Length > 1000)
{
2020-07-13 12:58:06 +02:00
SpaceLengthPixels = Lines[1].Length * 7;
}
else
{
lock (GdiLock)
{
var lineTwoWidth = Graphics.MeasureString(Lines[1], DefaultFont).Width;
LengthPixels.Add(lineTwoWidth);
}
}
2020-03-01 15:50:39 +01:00
if (Math.Abs(SpaceLengthPixels) < 0.01)
{
lock (GdiLock)
{
SpaceLengthPixels = Graphics.MeasureString(" ", DefaultFont).Width;
}
2019-11-08 13:09:48 +01:00
}
}
2019-11-09 16:02:12 +01:00
LengthCharacters = new List<int>();
foreach (var line in lines)
{
LengthCharacters.Add(line.Length);
}
2019-11-08 13:09:48 +01:00
}
public bool IsLineLengthOkay(int singleLineMaxLength)
{
return Lines[0].Length <= singleLineMaxLength && Lines[1].Length <= singleLineMaxLength;
}
public double DiffFromAverage()
{
var avg = TotalLength / Lines.Count;
return Lines.Sum(line => Math.Abs(avg - line.Length));
}
public double DiffFromAveragePixel()
2019-11-09 16:02:12 +01:00
{
var avg = TotalLengthPixels / Lines.Count;
return LengthPixels.Sum(w => Math.Abs(avg - w));
}
public double DiffFromAveragePixelBottomHeavy()
2019-11-08 13:09:48 +01:00
{
var avg = TotalLengthPixels / Lines.Count;
double diff = 0;
double bottomHeavyPercentageFactor = 0;
if (Configuration.Settings.Tools.AutoBreakPreferBottomHeavy)
{
bottomHeavyPercentageFactor = Configuration.Settings.Tools.AutoBreakPreferBottomPercent / 100.0;
}
var bottomDiffPixels = avg * bottomHeavyPercentageFactor;
2019-11-09 16:02:12 +01:00
diff += Math.Abs(avg - bottomDiffPixels - LengthPixels[0]);
diff += Math.Abs(avg + bottomDiffPixels - LengthPixels[1]);
return diff;
2019-11-08 13:09:48 +01:00
}
}
}