Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DrawMultiLinedText: add an option to provide an image as background #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions barraider-sdtools/Tools/GraphicsTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public static Image CreateOpacityImage(Image image, float opacity)
/// <summary>
/// Generates one (or more) images where each one has a few letters drawn on them based on the parameters. You can set number of letters and number of lines per key.
/// Use expandToNextImage to decide if you want only one Image returned or multiple if text is too long for one key
/// Will generate a plain background of the provided color.
/// </summary>
/// <param name="text"></param>
/// <param name="currentTextPosition"></param>
Expand All @@ -172,8 +173,55 @@ public static Image CreateOpacityImage(Image image, float opacity)
/// <param name="textColor"></param>
/// <param name="expandToNextImage"></param>
/// <param name="keyDrawStartingPosition"></param>
/// <returns></returns>
public static Image[] DrawMultiLinedText(string text, int currentTextPosition, int lettersPerLine, int numberOfLines, Font font, Color backgroundColor, Color textColor, bool expandToNextImage, PointF keyDrawStartingPosition)
/// <returns>A list of images generated</returns>
public static Image[] DrawMultiLinedText(string text, int currentTextPosition, int lettersPerLine,
int numberOfLines, Font font, Color backgroundColor, Color textColor, bool expandToNextImage,
PointF keyDrawStartingPosition)
{
return DrawMultiLinedTextCommon(text, currentTextPosition, lettersPerLine, numberOfLines, font, backgroundColor, textColor, expandToNextImage, keyDrawStartingPosition);
}

/// <summary>
/// Generates one (or more) images where each one has a few letters drawn on them based on the parameters. You can set number of letters and number of lines per key.
/// Use expandToNextImage to decide if you want only one Image returned or multiple if text is too long for one key
/// Will use the provided background image to generate the text on top of.
/// </summary>
/// <param name="text"></param>
/// <param name="currentTextPosition"></param>
/// <param name="lettersPerLine"></param>
/// <param name="numberOfLines"></param>
/// <param name="font"></param>
/// <param name="backgroundPath"></param>
/// <param name="textColor"></param>
/// <param name="expandToNextImage"></param>
/// <param name="keyDrawStartingPosition"></param>
/// <returns>A list of images generated.</returns>
public static Image[] DrawMultiLinedTextWithBackground(string text, int currentTextPosition, int lettersPerLine,
int numberOfLines, Font font, String backgroundPath, Color textColor, bool expandToNextImage,
PointF keyDrawStartingPosition)
{
Image myBackground = Image.FromFile(backgroundPath);
return DrawMultiLinedTextCommon(text, currentTextPosition, lettersPerLine, numberOfLines, font, Color.Transparent, textColor, expandToNextImage, keyDrawStartingPosition, myBackground);
}

/// <summary>
/// Centralize the code to generate images with text wrapped:
/// Generates one (or more) images where each one has a few letters drawn on them based on the parameters. You can set number of letters and number of lines per key.
/// Use expandToNextImage to decide if you want only one Image returned or multiple if text is too long for one key
/// Use the background image or generate a plain background of the provided color.
/// </summary>
/// <param name="text"></param>
/// <param name="currentTextPosition"></param>
/// <param name="lettersPerLine"></param>
/// <param name="numberOfLines"></param>
/// <param name="font"></param>
/// <param name="backgroundColor"></param>
/// <param name="textColor"></param>
/// <param name="expandToNextImage"></param>
/// <param name="keyDrawStartingPosition"></param>
/// <param name="background"></param>
/// <returns>A list of images generated.</returns>
private static Image[] DrawMultiLinedTextCommon(string text, int currentTextPosition, int lettersPerLine, int numberOfLines, Font font, Color backgroundColor, Color textColor, bool expandToNextImage, PointF keyDrawStartingPosition, Image background = null)
{
float currentWidth = keyDrawStartingPosition.X;
float currentHeight = keyDrawStartingPosition.Y;
Expand All @@ -182,9 +230,17 @@ public static Image[] DrawMultiLinedText(string text, int currentTextPosition, i
Bitmap img = Tools.GenerateGenericKeyImage(out Graphics graphics);
images.Add(img);

// Draw Background
var bgBrush = new SolidBrush(backgroundColor);
graphics.FillRectangle(bgBrush, 0, 0, img.Width, img.Height);
if (background != null)
{
// Use the provided background
graphics.DrawImage(background, 0, 0, img.Width, img.Height);
}
else
{
// Draw Background
var bgBrush = new SolidBrush(backgroundColor);
graphics.FillRectangle(bgBrush, 0, 0, img.Width, img.Height);
}

float lineHeight = img.Height / numberOfLines;
if (numberOfLines == 1)
Expand All @@ -203,7 +259,7 @@ public static Image[] DrawMultiLinedText(string text, int currentTextPosition, i
{
if (expandToNextImage)
{
images.AddRange(DrawMultiLinedText(text, letter, lettersPerLine, numberOfLines, font, backgroundColor, textColor, expandToNextImage, keyDrawStartingPosition));
images.AddRange(DrawMultiLinedTextCommon(text, letter, lettersPerLine, numberOfLines, font, backgroundColor, textColor, expandToNextImage, keyDrawStartingPosition, background));
}
break;
}
Expand Down