DemotivatorGen: minor refactoring and optimization

This commit is contained in:
Slavasil 2025-04-20 00:50:24 +03:00
parent 7dbb6fa8f5
commit fab11e9c90

View File

@ -125,7 +125,7 @@ public class DemotivatorGen
g.DrawImage(picture, contX + bt + pad, contY + bt + pad, contWidth - bt * 2 - pad * 2, contHeight - bt * 2 - pad * 2);
}
float titleY = contY + contHeight + capSp;// + style.TitleFont.GetHeight();
float titleY = contY + contHeight + capSp;
foreach (string titleLine in titles[j])
{
float titleX = currentOrigin.X + (availableWidth - g.MeasureString(titleLine, style.TitleFont).Width) / 2f;
@ -133,11 +133,10 @@ public class DemotivatorGen
titleY += titleFontHeight;
}
float subtitleY = titleY/* - titleFontHeight*/ + capSp;
float subtitleY = titleY + capSp;
string[] subtitleLines = subtitles[j];
if (subtitleLines.Length > 0)
{
// subtitleY += subtitleFontHeight;
foreach (string subtitleLine in subtitleLines)
{
float subtitleX = currentOrigin.X + (availableWidth - measureSubtitleString(subtitleLine)) / 2f;
@ -176,34 +175,32 @@ public class DemotivatorGen
actualWidth = Math.Max(width - free, actualWidth);
rawPosition += word.Length + 1;
words--;
continue; // TODO заменить if на else-if чтобы убрать continue
}
if (wordWidth <= width)
else if (wordWidth <= width)
{
if (!trailingReturn)
wrappedText.Append("\n");
wrappedText.Append('\n');
wrappedText.Append(word);
wrappedText.Append(" ");
wrappedText.Append(' ');
trailingReturn = false;
free = width - wordWidth;
rawPosition += word.Length + 1;
words--;
continue;
}
if (wordWidth <= maxWidth)
else if (wordWidth <= maxWidth)
{
actualWidth = Math.Max(actualWidth, wordWidth);
if (!trailingReturn)
wrappedText.Append("\n");
wrappedText.Append('\n');
wrappedText.Append(word);
wrappedText.Append("\n");
wrappedText.Append('\n');
trailingReturn = true;
free = width;
rawPosition += word.Length + 1;
words--;
continue;
}
else
{
// TODO заменить на что-то поэффективнее
float substrWidth = 0;
int substrLength = word.Length;
@ -218,14 +215,15 @@ public class DemotivatorGen
substrWidth += charWidth;
}
if (!trailingReturn)
wrappedText.Append("\n");
wrappedText.Append('\n');
wrappedText.Append(word.Substring(0, substrLength));
wrappedText.Append("\n");
wrappedText.Append('\n');
trailingReturn = true;
free = width;
rawPosition += substrLength;
actualWidth = maxWidth;
}
}
return new WordWrapResult()
{
lines = wrappedText.ToString().Split('\n'),