SubtitleEdit/libse/TextDraw.cs

194 lines
6.4 KiB
C#
Raw Normal View History

using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
namespace Nikse.SubtitleEdit.Core
{
public static class TextDraw
{
public static double GetFontSize(double fontSize)
{
return fontSize * 0.895d; // font rendered in video players like vlc/mpv are a little smaller than .net, so we adjust font size a bit down
}
public static void DrawText(Font font, StringFormat sf, GraphicsPath path, StringBuilder sb, bool isItalic, bool isBold, bool isUnderline, float left, float top, ref bool newLine, float leftMargin, ref int pathPointsStart)
{
var next = new PointF(left, top);
if (path.PointCount > 0)
{
int k = 0;
var list = (PointF[])path.PathPoints.Clone(); // avoid using very slow path.PathPoints indexer!!!
for (int i = list.Length - 1; i >= 0; i--)
{
if (list[i].X > next.X)
2019-01-19 14:40:37 +01:00
{
next.X = list[i].X;
2019-01-19 14:40:37 +01:00
}
k++;
if (k > 60 || i <= pathPointsStart && pathPointsStart != -1)
2019-01-19 14:40:37 +01:00
{
break;
2019-01-19 14:40:37 +01:00
}
}
}
if (newLine)
{
next.X = leftMargin;
newLine = false;
pathPointsStart = path.PointCount;
}
var fontStyle = FontStyle.Regular;
if (isItalic)
2019-01-19 14:40:37 +01:00
{
fontStyle |= FontStyle.Italic;
2019-01-19 14:40:37 +01:00
}
if (isBold)
2019-01-19 14:40:37 +01:00
{
fontStyle |= FontStyle.Bold;
2019-01-19 14:40:37 +01:00
}
if (isUnderline)
2019-01-19 14:40:37 +01:00
{
fontStyle |= FontStyle.Underline;
2019-01-19 14:40:37 +01:00
}
2019-11-15 11:00:31 +01:00
var fontSize = (float)GetFontSize(font.Size);
try
{
path.AddString(sb.ToString(), font.FontFamily, (int)fontStyle, fontSize, next, sf);
}
catch
{
fontStyle = FontStyle.Regular;
try
{
path.AddString(sb.ToString(), font.FontFamily, (int)fontStyle, fontSize, next, sf);
}
catch
{
path.AddString(sb.ToString(), new FontFamily("Arial"), (int)fontStyle, fontSize, next, sf);
}
}
sb.Length = 0;
}
public static float MeasureTextWidth(Font font, string text, bool bold)
{
using (var sf = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near })
{
using (var path = new GraphicsPath())
{
var sb = new StringBuilder(text);
bool newLine = false;
const int leftMargin = 0;
int pathPointsStart = -1;
DrawText(font, sf, path, sb, false, bold, false, 0, 0, ref newLine, leftMargin, ref pathPointsStart);
if (path.PathData.Points.Length == 0)
{
return 0;
}
float width = 0;
var list = (PointF[])path.PathPoints.Clone(); // avoid using very slow path.PathPoints indexer!!!
int max = list.Length;
if (max <= 500)
{
2019-09-17 04:11:35 +02:00
for (int i = 0; i < max; i++)
2019-01-19 14:40:37 +01:00
{
if (list[i].X > width)
{
width = list[i].X;
}
2019-01-19 14:40:37 +01:00
}
return width;
}
int interval = 1;
if (max > 1500)
{
interval = 5;
}
else if (max > 1000)
{
interval = 3;
}
2019-09-17 04:11:35 +02:00
else
{
interval = 2;
}
2019-09-17 04:11:35 +02:00
for (int i = 0; i < max; i += interval)
{
if (list[i].X > width)
2019-01-19 14:40:37 +01:00
{
width = list[i].X;
2019-01-19 14:40:37 +01:00
}
}
if (list[1].X > width)
{
width = list[1].X;
}
if (list[2].X > width)
{
width = list[2].X;
}
if (list[max - 1].X > width)
{
width = list[max - 1].X;
}
if (list[max - 2].X > width)
{
width = list[max - 2].X;
}
return width;
}
}
}
public static float MeasureTextHeight(Font font, string text, bool bold)
{
using (var sf = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near })
{
using (var path = new GraphicsPath())
{
var sb = new StringBuilder(text);
bool newLine = false;
const int leftMargin = 0;
int pathPointsStart = -1;
DrawText(font, sf, path, sb, false, bold, false, 0, 0, ref newLine, leftMargin, ref pathPointsStart);
float height = 0;
var list = (PointF[])path.PathPoints.Clone(); // avoid using very slow path.PathPoints indexer!!!
2016-01-15 02:15:58 +01:00
int index = System.Math.Max(list.Length - 80, 0);
for (int i = index; i < list.Length; i += 2)
{
if (list[i].Y > height)
2019-01-19 14:40:37 +01:00
{
height = list[i].Y;
2019-01-19 14:40:37 +01:00
}
}
for (int i = 0; i < list.Length; i += 2)
{
if (list[i].Y > height)
2019-01-19 14:40:37 +01:00
{
height = list[i].Y;
2019-01-19 14:40:37 +01:00
}
}
return height;
}
}
}
}
2016-01-24 11:51:04 +01:00
}