Skip to content

Commit

Permalink
Add option to lock aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
plamf committed May 4, 2021
1 parent 7c7e954 commit d6390fe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 39 deletions.
51 changes: 32 additions & 19 deletions ImageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,60 @@ namespace SourceCodePosterizer
{
public class ImageHandler
{
public static Image CreateImage(string text, string title, string foregroundColor, string backgroundColor, int fontsize, int borderThickness)
public static Image CreateImage(string text, Options options)
{
var cc = new ColorConverter();
var font = new Font("Lucida Console", fontsize, FontStyle.Regular);
var bgColor = (Color)cc.ConvertFromString(backgroundColor);
var fgColor = (Color)cc.ConvertFromString(foregroundColor);
var hasTitle = title != string.Empty;
var font = new Font("Lucida Console", options.FontSize, FontStyle.Regular);
var bgColor = (Color) cc.ConvertFromString(options.BackgroundColor);
var fgColor = (Color) cc.ConvertFromString(options.ForegroundColor);
var padding = 100;
var textBrush = new SolidBrush(fgColor);

Image img = new Bitmap(1, 1);
// Prepare a dummy image
var img = new Bitmap(1, 1);
var drawing = Graphics.FromImage(img);
var textSize = drawing.MeasureString(text, font);

// Free up memory used by the dummy image
img.Dispose();
drawing.Dispose();

img = new Bitmap((int) textSize.Width + (padding * 2), (int) textSize.Height + (padding * (hasTitle ? 3:2)));
// Create an image with the right size to fit the text
img = options.LockAspect ? CreateBitmapByAspect(textSize, padding) : CreateBitmapByTextsize(textSize, padding);

// Draw the text and colorize the image
drawing = Graphics.FromImage(img);
drawing.Clear(bgColor);

Brush textBrush = new SolidBrush(fgColor);
drawing.DrawString(text, font, textBrush, padding, padding * (hasTitle ? 2 : 0));
drawing.DrawString(text, font, textBrush, padding, padding * 2);
drawing.Save();

if (hasTitle)
{
AddTitle(img, title, textBrush, fontsize);
}
AddTitle(img, options.Title, textBrush, options.FontSize);

if (borderThickness > 0)
{
AddBorder(img, borderThickness, fgColor);
}
if (options.BorderThickness > 0) AddBorder(img, options.BorderThickness, fgColor);

textBrush.Dispose();
drawing.Dispose();

return img;
}

private static Bitmap CreateBitmapByTextsize(SizeF textSize, int padding)
{
var width = (int) textSize.Width + padding * 2;
var height = (int) textSize.Height + padding * 3;

return new Bitmap(width, height);
}

private static Bitmap CreateBitmapByAspect(SizeF textSize, int padding)
{
const float aspectRatio = 1.414F;
var width = (int)textSize.Width + padding * 2;
var aspectCorrectedHeight = width * aspectRatio;

return new Bitmap(width, (int)aspectCorrectedHeight);
}

private static void AddBorder(Image img, int thickness, Color color)
{
using (var g = Graphics.FromImage(img))
Expand All @@ -66,7 +80,6 @@ private static void AddTitle(Image img, string title, Brush brush, int fontsize)
}
}


public static void SaveImage(string filePath, string filename, Image poster)
{
var codecInfo = GetEncoderInfo("image/png");
Expand Down
6 changes: 5 additions & 1 deletion Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class Options

[Option('u', "border", Required = false,
HelpText = "Frames the image with a border in the same color as the foreground. Default width: 0")]
public int Border { get; set; } = 0;
public int BorderThickness { get; set; } = 0;

[Option('a', "lock-aspect", Required = false,
HelpText = "Locks the generated image in an 1:414 aspect ratio.")]
public bool LockAspect { get; set; }
}
}
25 changes: 10 additions & 15 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ private static void Main(string[] args)
{
#if DEBUG
// A helper to quickly generate different images during debugging
var foreground = "FFFFFF";
var background = "000000";
args = new[] {
var foreground = "#FFFFFF";
var background = "#000000";
args = new[]
{
$"-p{new DirectoryInfo(Directory.GetCurrentDirectory()).Parent?.Parent?.Parent?.FullName}",
$"-f{foreground}",
$"-b{background}",
$"-u{80}",
"-y*.cs",
"-l100"
"-a",
"-l110"
};
#endif

Expand All @@ -34,21 +36,14 @@ private static void RunOptions(Options opts)
{
if (Directory.Exists(opts.FilePath))
{
if (!opts.ForegroundColor.StartsWith("#"))
{
opts.ForegroundColor = opts.ForegroundColor.Insert(0, "#");
}
if (!opts.BackgroundColor.StartsWith("#"))
{
opts.BackgroundColor = opts.BackgroundColor.Insert(0, "#");
}
if (!opts.ForegroundColor.StartsWith("#")) opts.ForegroundColor = opts.ForegroundColor.Insert(0, "#");
if (!opts.BackgroundColor.StartsWith("#")) opts.BackgroundColor = opts.BackgroundColor.Insert(0, "#");

ProcessDirectory(opts.FilePath, opts.Filetypes);

var minifiedText = TextHandler.MinifyText(_rawText, opts.TextCase);
var formattedText = TextHandler.FormatText(minifiedText, opts.LineLength);
var poster = ImageHandler.CreateImage(formattedText,opts.Title, opts.ForegroundColor, opts.BackgroundColor,
opts.FontSize, opts.Border);
var poster = ImageHandler.CreateImage(formattedText, opts);

ImageHandler.SaveImage(opts.FilePath, opts.Title, poster);
}
Expand Down Expand Up @@ -76,7 +71,7 @@ private static void ProcessDirectory(string targetDirectory, string filetypes)
ProcessDirectory(subdirectory, filetypes);
}

public static string[] GetFiles(string path, string searchPattern)
private static string[] GetFiles(string path, string searchPattern)
{
var searchPatterns = searchPattern.Split(',');
var files = new List<string>();
Expand Down
10 changes: 6 additions & 4 deletions TextHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public static string MinifyText(string rawText, string textcase)
.Replace("\r", string.Empty)
.Replace(" ", string.Empty);

if (textcase.Equals("Upper"))
result = result.ToUpper();
if (textcase.Equals("Lower"))
result = result.ToLower();
result = textcase switch
{
"Upper" => result.ToUpper(),
"Lower" => result.ToLower(),
_ => result
};

return result;
}
Expand Down

0 comments on commit d6390fe

Please sign in to comment.