Factor out a few functions (GetCover was tooooo big)

Make it return a bitmap (preparation for next step)
This commit is contained in:
Flamefire committed 2014-09-25 19:59:16 +02:00
1 parent 2a79f778e6
commit aff7f42c68
2 files changed
+56 -38

No files matched your search

+9 -10
View File
@@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using VocaluxeLib;
using VocaluxeLib.Draw;
@@ -100,19 +101,17 @@ namespace Vocaluxe.Base
public static CTextureRef GenerateCover(string text, ESongSorting sorting, CCategory category)
{
CSong firstSong = category.GetSong(0);
CTextureRef cover = _NoCoverThemes[_SongSortingToType(sorting)].GetCover(text, firstSong != null ? Path.Combine(firstSong.Folder, firstSong.CoverFileName) : null);
if (cover == null)
cover = _NoCoverThemes[_DefaultTypeName].GetCover(text, firstSong != null ? Path.Combine(firstSong.Folder, firstSong.CoverFileName) : null);
if (cover == null)
cover = NoCover;
else
{
Bitmap coverBmp = _NoCoverThemes[_SongSortingToType(sorting)].GetCover(text, firstSong != null ? Path.Combine(firstSong.Folder, firstSong.CoverFileName) : null);
if (coverBmp == null)
coverBmp = _NoCoverThemes[_DefaultTypeName].GetCover(text, firstSong != null ? Path.Combine(firstSong.Folder, firstSong.CoverFileName) : null);
if (coverBmp == null)
return NoCover;
CTextureRef texture = CDraw.AddTexture(coverBmp);
lock (_Covers)
{
_Covers.Add(text, cover);
_Covers.Add(text, texture);
}
}
return cover;
return texture;
}
/// <summary>
+47 -28
View File
@@ -25,7 +25,6 @@ using System.Drawing.Text;
using System.IO;
using Vocaluxe.Base.Fonts;
using VocaluxeLib;
using VocaluxeLib.Draw;
using System.Linq;
namespace Vocaluxe.Base
@@ -149,43 +148,25 @@ namespace Vocaluxe.Base
}
}
public CTextureRef GetCover(string text, string firstCoverPath)
private void _DrawBackground(Graphics g, Bitmap bmpBackground, String firstCoverPath)
{
if (!_Valid)
return null;
text = CLanguage.Translate(_Text.Replace("%TEXT%", text));
using (Bitmap bmpImage = new Bitmap(_Image))
using (Bitmap bmp = new Bitmap(bmpImage.Width, bmpImage.Height, PixelFormat.Format32bppArgb))
using (Graphics g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Clear(_BGColor.AsColor());
ImageAttributes ia = null;
if (_ShowFirstCover && !String.IsNullOrEmpty(firstCoverPath) && File.Exists(firstCoverPath))
{
using (Bitmap bmp2 = new Bitmap(firstCoverPath))
g.DrawImage(bmp2, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp2.Width, bmp2.Height, GraphicsUnit.Pixel);
g.DrawImage(bmp2, bmpBackground.GetRect(), 0, 0, bmp2.Width, bmp2.Height, GraphicsUnit.Pixel);
ColorMatrix cm = new ColorMatrix {Matrix33 = _ImageAlpha};
ia = new ImageAttributes();
ia.SetColorMatrix(cm);
}
g.DrawImage(bmpImage, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia);
g.DrawImage(bmpBackground, bmpBackground.GetRect(), 0, 0, bmpBackground.Width, bmpBackground.Height, GraphicsUnit.Pixel, ia);
}
if (text != "")
private void _DrawText(Graphics g, Size bmpSize, CFont font, List<CTextElement> elements)
{
IEnumerable<string> textParts = _SplitText(text);
CFont font = new CFont(_Font, _Style, bmp.Height - _TextMarginTop - _TextMarginBottom);
Font fo = CFonts.GetSystemFont(font);
List<CTextElement> elements = textParts.Select(line => new CTextElement(line, g, fo)).ToList();
float factor = _DistributeText(elements, bmp.Width, bmp.Height);
foreach (CTextElement element in elements)
element.AdjustSize(factor);
font.Height *= factor / (1f + CFonts.GetOutlineSize(font)); //Adjust for outline size
fo = CFonts.GetSystemFont(font);
float maxHeight = elements.Select(el => el.Height).Max();
int lineCount = elements.Last().Line + 1;
@@ -201,7 +182,7 @@ namespace Vocaluxe.Base
{
pen.LineJoin = LineJoin.Round;
pen.Alignment = PenAlignment.Outset;
float top = (bmp.Height - _TextMarginBottom - _TextMarginTop - maxHeight * lineCount) / 2 + _TextMarginTop;
float top = (bmpSize.Height - _TextMarginBottom - _TextMarginTop - maxHeight * lineCount) / 2 + _TextMarginTop;
int nextLineEl = 0;
for (int i = 0; i < lineCount; i++)
{
@@ -218,13 +199,13 @@ namespace Vocaluxe.Base
{
//Center Text if this is the only line or the middle line
float width = _GetWidth(elements, firstEl, nextLineEl - 1);
left = (bmp.Width - _TextMarginLeft - _TextMarginRight - width) / 2 + _TextMarginLeft;
left = (bmpSize.Width - _TextMarginLeft - _TextMarginRight - width) / 2 + _TextMarginLeft;
}
else if (i == lineCount - 1)
{
//Place last line at right
float width = _GetWidth(elements, firstEl, nextLineEl - 1);
left = bmp.Width - width - _TextMarginRight;
left = bmpSize.Width - width - _TextMarginRight;
}
else
left = _TextMarginLeft;
@@ -236,9 +217,47 @@ namespace Vocaluxe.Base
g.FillPath(new SolidBrush(_TextColor.AsColor()), path);
}
}
return CDraw.AddTexture(bmp);
public Bitmap GetCover(string text, string firstCoverPath)
{
if (!_Valid)
return null;
text = CLanguage.Translate(_Text.Replace("%TEXT%", text));
using (Bitmap bmpImage = new Bitmap(_Image))
{
Bitmap bmp = new Bitmap(bmpImage.Width, bmpImage.Height, PixelFormat.Format32bppArgb);
try
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
_DrawBackground(g, bmpImage, firstCoverPath);
if (text != "")
{
CFont font = new CFont(_Font, _Style, bmp.Height - _TextMarginTop - _TextMarginBottom);
Font fo = CFonts.GetSystemFont(font);
IEnumerable<string> textParts = _SplitText(text);
List<CTextElement> elements = textParts.Select(line => new CTextElement(line, g, fo)).ToList();
float factor = _DistributeText(elements, bmp.Width, bmp.Height);
foreach (CTextElement element in elements)
element.AdjustSize(factor);
font.Height *= factor / (1f + CFonts.GetOutlineSize(font)); //Adjust for outline size
_DrawText(g, bmp.GetSize(), font, elements);
}
}
return bmp;
}
catch (Exception)
{
bmp.Dispose();
}
}
return null;
}
/// <summary>
/// Distributes the text among max. 3 lines maximizing the height of the line<br />