Added bold+alignment options to image export

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@965 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2012-02-05 20:32:30 +00:00
parent 2fcb3fbe65
commit 1995aee7ec
7 changed files with 151 additions and 46 deletions

View File

@ -259,7 +259,7 @@ namespace Nikse.SubtitleEdit.Forms
italicFromStart = i == 0;
if (sb.Length > 0)
{
TextDraw.DrawText(font, sf, path, sb, isItalic, left, top, ref newLine, addX, leftMargin);
TextDraw.DrawText(font, sf, path, sb, isItalic, false, left, top, ref newLine, addX, leftMargin);
addX = 0;
}
isItalic = true;
@ -271,7 +271,7 @@ namespace Nikse.SubtitleEdit.Forms
addX = 0;
else
addX = italicSpacing;
TextDraw.DrawText(font, sf, path, sb, isItalic, left, top, ref newLine, addX, leftMargin);
TextDraw.DrawText(font, sf, path, sb, isItalic, false, left, top, ref newLine, addX, leftMargin);
addX = 1;
if (_subtitleFontName.StartsWith("Arial"))
addX = 3;
@ -285,7 +285,7 @@ namespace Nikse.SubtitleEdit.Forms
else
addX = italicSpacing;
TextDraw.DrawText(font, sf, path, sb, isItalic, left, top, ref newLine, addX, leftMargin);
TextDraw.DrawText(font, sf, path, sb, isItalic, false, left, top, ref newLine, addX, leftMargin);
addX = 0;
top += lineHeight;
@ -310,7 +310,7 @@ namespace Nikse.SubtitleEdit.Forms
addX = 0;
else
addX = italicSpacing;
TextDraw.DrawText(font, sf, path, sb, isItalic, left, top, ref newLine, addX, leftMargin);
TextDraw.DrawText(font, sf, path, sb, isItalic, false, left, top, ref newLine, addX, leftMargin);
}
if (_borderWidth > 0)

View File

@ -34,7 +34,8 @@
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.groupBoxImageSettings = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.checkBoxBold = new System.Windows.Forms.CheckBox();
this.labelResolution = new System.Windows.Forms.Label();
this.comboBoxResolution = new System.Windows.Forms.ComboBox();
this.comboBoxHAlign = new System.Windows.Forms.ComboBox();
this.labelHorizontalAlign = new System.Windows.Forms.Label();
@ -106,7 +107,8 @@
//
this.groupBoxImageSettings.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBoxImageSettings.Controls.Add(this.label1);
this.groupBoxImageSettings.Controls.Add(this.checkBoxBold);
this.groupBoxImageSettings.Controls.Add(this.labelResolution);
this.groupBoxImageSettings.Controls.Add(this.comboBoxResolution);
this.groupBoxImageSettings.Controls.Add(this.comboBoxHAlign);
this.groupBoxImageSettings.Controls.Add(this.labelHorizontalAlign);
@ -128,15 +130,25 @@
this.groupBoxImageSettings.TabStop = false;
this.groupBoxImageSettings.Text = "Image settings";
//
// label1
// checkBoxBold
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(10, 81);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(51, 13);
this.label1.TabIndex = 26;
this.label1.Text = "Video res";
this.label1.Visible = false;
this.checkBoxBold.AutoSize = true;
this.checkBoxBold.Location = new System.Drawing.Point(276, 91);
this.checkBoxBold.Name = "checkBoxBold";
this.checkBoxBold.Size = new System.Drawing.Size(47, 17);
this.checkBoxBold.TabIndex = 27;
this.checkBoxBold.Text = "Bold";
this.checkBoxBold.UseVisualStyleBackColor = true;
this.checkBoxBold.CheckedChanged += new System.EventHandler(this.checkBoxBold_CheckedChanged);
//
// labelResolution
//
this.labelResolution.AutoSize = true;
this.labelResolution.Location = new System.Drawing.Point(10, 81);
this.labelResolution.Name = "labelResolution";
this.labelResolution.Size = new System.Drawing.Size(51, 13);
this.labelResolution.TabIndex = 26;
this.labelResolution.Text = "Video res";
//
// comboBoxResolution
//
@ -144,10 +156,14 @@
this.comboBoxResolution.FormattingEnabled = true;
this.comboBoxResolution.Items.AddRange(new object[] {
"1080p (1920x1080)",
"1440x1080",
"720p (1280x720)",
"960x720",
"480p (848x480)",
"PAL (720x576)",
"NTSC (720x480)"});
"NTSC (720x480)",
"640×352",
"640×272"});
this.comboBoxResolution.Location = new System.Drawing.Point(100, 78);
this.comboBoxResolution.Name = "comboBoxResolution";
this.comboBoxResolution.Size = new System.Drawing.Size(121, 21);
@ -159,12 +175,12 @@
this.comboBoxHAlign.FormattingEnabled = true;
this.comboBoxHAlign.Items.AddRange(new object[] {
"Left",
"Center"});
"Center",
"Right"});
this.comboBoxHAlign.Location = new System.Drawing.Point(100, 105);
this.comboBoxHAlign.Name = "comboBoxHAlign";
this.comboBoxHAlign.Size = new System.Drawing.Size(121, 21);
this.comboBoxHAlign.TabIndex = 23;
this.comboBoxHAlign.Visible = false;
this.comboBoxHAlign.SelectedIndexChanged += new System.EventHandler(this.comboBoxHAlign_SelectedIndexChanged);
//
// labelHorizontalAlign
@ -175,7 +191,6 @@
this.labelHorizontalAlign.Size = new System.Drawing.Size(30, 13);
this.labelHorizontalAlign.TabIndex = 22;
this.labelHorizontalAlign.Text = "Align";
this.labelHorizontalAlign.Visible = false;
//
// checkBoxAntiAlias
//
@ -440,7 +455,8 @@
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.ComboBox comboBoxHAlign;
private System.Windows.Forms.Label labelHorizontalAlign;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label labelResolution;
private System.Windows.Forms.ComboBox comboBoxResolution;
private System.Windows.Forms.CheckBox checkBoxBold;
}
}

View File

@ -24,10 +24,12 @@ namespace Nikse.SubtitleEdit.Forms
public Color SubtitleColor { get; set; }
public string SubtitleFontName { get; set; }
public float SubtitleFontSize { get; set; }
public bool SubtitleFontBold { get; set; }
public Color BorderColor { get; set; }
public float BorderWidth { get; set; }
public bool AntiAlias { get; set; }
public bool AlignLeft { get; set; }
public bool AlignRight { get; set; }
public byte[] Buffer { get; set; }
public int ScreenWidth { get; set; }
public int ScreenHeight { get; set; }
@ -38,6 +40,7 @@ namespace Nikse.SubtitleEdit.Forms
Color _subtitleColor = Color.White;
string _subtitleFontName = "Verdana";
float _subtitleFontSize = 75.0f;
bool _subtitleFontBold = false;
Color _borderColor = Color.Black;
float _borderWidth = 2.0f;
bool _isLoading = true;
@ -84,10 +87,12 @@ namespace Nikse.SubtitleEdit.Forms
SubtitleColor = _subtitleColor,
SubtitleFontName = _subtitleFontName,
SubtitleFontSize = _subtitleFontSize,
SubtitleFontBold = _subtitleFontBold,
BorderColor = _borderColor,
BorderWidth = _borderWidth,
AntiAlias = checkBoxAntiAlias.Checked,
AlignLeft = comboBoxHAlign.SelectedIndex == 0,
AlignRight = comboBoxHAlign.SelectedIndex == 2,
ScreenWidth = screenWidth,
ScreenHeight = screenHeight,
Bitmap = null,
@ -134,25 +139,45 @@ namespace Nikse.SubtitleEdit.Forms
int width = 1920;
int height = 1080;
if (comboBoxResolution.SelectedIndex == 1)
{
width = 1440;
height = 1080;
}
else if (comboBoxResolution.SelectedIndex == 2)
{
width = 1280;
height = 720;
}
else if (comboBoxResolution.SelectedIndex == 2)
else if (comboBoxResolution.SelectedIndex == 3)
{
width = 960;
height = 720;
}
else if (comboBoxResolution.SelectedIndex == 4)
{
width = 848;
height = 480;
}
else if (comboBoxResolution.SelectedIndex == 3)
else if (comboBoxResolution.SelectedIndex == 5)
{
width = 720;
height = 576;
}
else if (comboBoxResolution.SelectedIndex == 4)
else if (comboBoxResolution.SelectedIndex == 6)
{
width = 720;
height = 480;
}
else if (comboBoxResolution.SelectedIndex == 7)
{
width = 640;
height = 352;
}
else if (comboBoxResolution.SelectedIndex == 8)
{
width = 640;
height = 272;
}
FileStream binarySubtitleFile = null;
VobSubWriter vobSubWriter = null;
@ -316,6 +341,7 @@ namespace Nikse.SubtitleEdit.Forms
_borderColor = panelBorderColor.BackColor;
_subtitleFontName = comboBoxSubtitleFont.SelectedItem.ToString();
_subtitleFontSize = float.Parse(comboBoxSubtitleFontSize.SelectedItem.ToString());
_subtitleFontBold = checkBoxBold.Checked;
_borderWidth = float.Parse(comboBoxBorderWidth.SelectedItem.ToString());
}
@ -330,12 +356,14 @@ namespace Nikse.SubtitleEdit.Forms
{
MakeBitmapParameter mbp = new MakeBitmapParameter();
mbp.AlignLeft = comboBoxHAlign.SelectedIndex == 0;
mbp.AlignRight = comboBoxHAlign.SelectedIndex == 2;
mbp.AntiAlias = checkBoxAntiAlias.Checked;
mbp.BorderWidth = _borderWidth;
mbp.BorderColor = _borderColor;
mbp.SubtitleFontName = _subtitleFontName;
mbp.SubtitleColor = _subtitleColor;
mbp.SubtitleFontSize = _subtitleFontSize;
mbp.SubtitleFontBold = _subtitleFontBold;
mbp.P = new Paragraph(text, 0, 0);
var bmp = GenerateImageFromTextWithStyle(mbp);
@ -348,6 +376,32 @@ namespace Nikse.SubtitleEdit.Forms
return bmp;
}
private static float MeasureText(Font font, string text, bool bold)
{
var sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
var path = new GraphicsPath();
var sb = new StringBuilder(text);
bool isItalic = false;
bool newLine = false;
int addX = 0;
int leftMargin = 0;
TextDraw.DrawText(font, sf, path, sb, isItalic, bold, 0, 0, ref newLine, addX, leftMargin);
float width = 0;
int index = path.PathPoints.Length - 30;
if (index < 0)
index = 0;
for (int i = index; i < path.PathPoints.Length; i++)
{
if (path.PathPoints[i].X > width)
width = path.PathPoints[i].X;
}
return width;
}
private static Bitmap GenerateImageFromTextWithStyle(MakeBitmapParameter parameter)
{
string text = parameter.P.Text;
@ -367,7 +421,10 @@ namespace Nikse.SubtitleEdit.Forms
Font font;
try
{
font = new Font(parameter.SubtitleFontName, parameter.SubtitleFontSize);
var fontStyle = FontStyle.Regular;
if (parameter.SubtitleFontBold)
fontStyle = FontStyle.Bold;
font = new Font(parameter.SubtitleFontName, parameter.SubtitleFontSize, fontStyle);
}
catch (Exception exception)
{
@ -390,13 +447,14 @@ namespace Nikse.SubtitleEdit.Forms
var lefts = new List<float>();
foreach (string line in text.Replace("<i>", string.Empty).Replace("</i>", string.Empty).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
if (parameter.AlignLeft) //comboBoxHAlign.SelectedIndex == 0) // left
if (parameter.AlignLeft)
lefts.Add(5);
else if (parameter.AlignRight)
lefts.Add((float)(bmp.Width - (MeasureText(font, line, parameter.SubtitleFontBold) + 15)));
else
lefts.Add((float)(bmp.Width - g.MeasureString(line, font).Width * 0.8+15) / 2);
}
if (parameter.AntiAlias)
{
g.TextRenderingHint = TextRenderingHint.AntiAlias;
@ -428,7 +486,7 @@ namespace Nikse.SubtitleEdit.Forms
italicFromStart = i == 0;
if (sb.Length > 0)
{
TextDraw.DrawText(font, sf, path, sb, isItalic, left, top, ref newLine, addX, leftMargin);
TextDraw.DrawText(font, sf, path, sb, isItalic, parameter.SubtitleFontBold, left, top, ref newLine, addX, leftMargin);
addX = 0;
firstLinePart = false;
}
@ -441,7 +499,7 @@ namespace Nikse.SubtitleEdit.Forms
addX = 0;
else
addX = italicSpacing;
TextDraw.DrawText(font, sf, path, sb, isItalic, left, top, ref newLine, addX, leftMargin);
TextDraw.DrawText(font, sf, path, sb, isItalic, parameter.SubtitleFontBold, left, top, ref newLine, addX, leftMargin);
firstLinePart = false;
addX = 1;
if (parameter.SubtitleFontName.StartsWith("Arial"))
@ -456,7 +514,7 @@ namespace Nikse.SubtitleEdit.Forms
else
addX = italicSpacing;
TextDraw.DrawText(font, sf, path, sb, isItalic, left, top, ref newLine, addX, leftMargin);
TextDraw.DrawText(font, sf, path, sb, isItalic, parameter.SubtitleFontBold, left, top, ref newLine, addX, leftMargin);
firstLinePart = true;
addX = 0;
@ -482,7 +540,7 @@ namespace Nikse.SubtitleEdit.Forms
addX = 0;
else
addX = italicSpacing;
TextDraw.DrawText(font, sf, path, sb, isItalic, left, top, ref newLine, addX, leftMargin);
TextDraw.DrawText(font, sf, path, sb, isItalic, parameter.SubtitleFontBold, left, top, ref newLine, addX, leftMargin);
}
if (parameter.BorderWidth > 0)
@ -522,11 +580,23 @@ namespace Nikse.SubtitleEdit.Forms
labelSubtitleFontSize.Text = Configuration.Settings.Language.ExportPngXml.FontSize;
buttonColor.Text = Configuration.Settings.Language.ExportPngXml.FontColor;
checkBoxAntiAlias.Text = Configuration.Settings.Language.ExportPngXml.AntiAlias;
checkBoxBold.Text = Configuration.Settings.Language.General.Bold;
buttonBorderColor.Text = Configuration.Settings.Language.ExportPngXml.BorderColor;
labelBorderWidth.Text = Configuration.Settings.Language.ExportPngXml.BorderWidth;
buttonExport.Text = Configuration.Settings.Language.ExportPngXml.ExportAllLines;
buttonCancel.Text = Configuration.Settings.Language.General.OK;
labelImageResolution.Text = string.Empty;
labelHorizontalAlign.Text = Configuration.Settings.Language.ExportPngXml.Align;
if (Configuration.Settings.Language.ExportPngXml.Left != null &&
Configuration.Settings.Language.ExportPngXml.Center != null &&
Configuration.Settings.Language.ExportPngXml.Right != null)
{
comboBoxHAlign.Items.Clear();
comboBoxHAlign.Items.Add(Configuration.Settings.Language.ExportPngXml.Left);
comboBoxHAlign.Items.Add(Configuration.Settings.Language.ExportPngXml.Center);
comboBoxHAlign.Items.Add(Configuration.Settings.Language.ExportPngXml.Right);
}
subtitleListView1.InitializeLanguage(Configuration.Settings.Language.General, Configuration.Settings);
Utilities.InitializeSubtitleFont(subtitleListView1);
subtitleListView1.AutoSizeAllColumns(this);
@ -541,7 +611,7 @@ namespace Nikse.SubtitleEdit.Forms
if (exportType == "VOBSUB")
{
comboBoxBorderWidth.SelectedIndex = 3;
comboBoxResolution.SelectedIndex = 3;
comboBoxResolution.SelectedIndex = 5;
}
foreach (var x in FontFamily.Families)
@ -646,5 +716,10 @@ namespace Nikse.SubtitleEdit.Forms
subtitleListView1_SelectedIndexChanged(null, null);
}
private void checkBoxBold_CheckedChanged(object sender, EventArgs e)
{
subtitleListView1_SelectedIndexChanged(null, null);
}
}
}

View File

@ -745,17 +745,17 @@ namespace Nikse.SubtitleEdit.Forms
index = SubtitleListview1.SelectedItems[0].Index;
SetWaveFormPosition(audioVisualizer.StartPositionSeconds, seconds, index);
if (mediaPlayer.VideoPlayer != null && mediaPlayer.VideoPlayer is Nikse.SubtitleEdit.Logic.VideoPlayers.LibVlc11xDynamic) //TODO: Fix this hack!
{
double newPosition = seconds - 0.015;
for (int i = 0; i < 200; i++)
{
if (mediaPlayer.CurrentPosition > seconds)
mediaPlayer.CurrentPosition = newPosition;
System.Threading.Thread.Sleep(1);
Application.DoEvents();
}
}
//if (mediaPlayer.VideoPlayer != null && mediaPlayer.VideoPlayer is Nikse.SubtitleEdit.Logic.VideoPlayers.LibVlc11xDynamic) //TODO: Fix this hack!
//{
// double newPosition = seconds - 0.015;
// for (int i = 0; i < 200; i++)
// {
// if (mediaPlayer.CurrentPosition > seconds)
// mediaPlayer.CurrentPosition = newPosition;
// System.Threading.Thread.Sleep(1);
// Application.DoEvents();
// }
//}
timerWaveForm.Start();
}

View File

@ -346,6 +346,11 @@ namespace Nikse.SubtitleEdit.Logic
FontFamily = "Font family",
FontSize = "Font size",
XImagesSavedInY = "{0} images saved in {1}",
VideoResolution = "Video res",
Align = "Align",
Left = "Left",
Center = "Center",
Right = "Right",
};
FindDialog = new LanguageStructure.FindDialog

View File

@ -269,6 +269,11 @@
public string AntiAlias { get; set; }
public string ExportAllLines { get; set; }
public string XImagesSavedInY { get; set; }
public string VideoResolution { get; set; }
public string Align { get; set; }
public string Left { get; set; }
public string Right { get; set; }
public string Center { get; set; }
}
public class FindDialog

View File

@ -6,7 +6,7 @@ namespace Nikse.SubtitleEdit.Logic
public static class TextDraw
{
public static void DrawText(Font font, StringFormat sf, System.Drawing.Drawing2D.GraphicsPath path, StringBuilder sb, bool isItalic, float left, float top, ref bool newLine, float addX, float leftMargin)
public static void DrawText(Font font, StringFormat sf, System.Drawing.Drawing2D.GraphicsPath path, StringBuilder sb, bool isItalic, bool isBold, float left, float top, ref bool newLine, float addX, float leftMargin)
{
PointF next = new PointF(left, top);
@ -30,10 +30,14 @@ namespace Nikse.SubtitleEdit.Logic
newLine = false;
}
if (isItalic)
path.AddString(sb.ToString(), font.FontFamily, (int)System.Drawing.FontStyle.Italic, font.Size, next, sf);
else
path.AddString(sb.ToString(), font.FontFamily, 0, font.Size, next, sf);
var fontStyle = FontStyle.Regular;
if (isItalic && isBold)
fontStyle = FontStyle.Italic | FontStyle.Bold;
else if (isItalic)
fontStyle = FontStyle.Italic;
else if (isBold)
fontStyle = FontStyle.Bold;
path.AddString(sb.ToString(), font.FontFamily, (int)fontStyle, font.Size, next, sf);
sb.Length = 0;
}