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