Implemented switches for fixed image width and text alignment

This commit is contained in:
Bailey Eaton 2023-06-02 13:37:27 +10:00
parent c663731818
commit 8579fb6932
3 changed files with 51 additions and 4 deletions

View File

@ -50,8 +50,18 @@ namespace TED.DrawModes
var scaledWorkingAreaWidth = primaryAreaRect.X / scaleX;
var scaledWorkingAreaHeight = primaryAreaRect.Y / scaleY;
// Calculate the maximum width of all lines
var maxWidth = graphics.MeasureString(Options.Lines.Max(l => l), font).Width;
var maxWidth = 0f;
if(Options.FixedWidth > 0)
{
// Use the fixed width.
maxWidth = Options.FixedWidth;
}
else
{
// Calculate the maximum width based on the longest line.
maxWidth = Options.Lines.Select(line => new SizeF(graphics.MeasureString(line, font).Width, 0))
.Max(size => size.Width);
}
// Calculate the positions of the text and the image
var textX = scaledWorkingAreaWidth + Screen.PrimaryScreen.WorkingArea.Width - maxWidth - Options.PaddingHorizontal;
@ -80,8 +90,12 @@ namespace TED.DrawModes
{
var line = Options.Lines[i];
var format = new StringFormat() { Alignment = Options.TextAlignment };
var lineHeight = graphics.MeasureString(line, font).Height;
var textRect = new RectangleF(textX, textY, maxWidth, lineHeight);
// Draw the line
graphics.DrawString(line, font, new SolidBrush(textColor), new PointF(textX, textY));
graphics.DrawString(line, font, new SolidBrush(textColor), textRect, format);
// Move the text cursor down to the next line
textY += graphics.MeasureString(line, font).Height;

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Drawing;
using TED.Utils;
namespace TED.Program
@ -17,6 +18,8 @@ namespace TED.Program
internal readonly string LightImagePath;
internal readonly string DarkImagePath;
internal readonly List<string> Lines;
internal int FixedWidth;
internal StringAlignment TextAlignment;
internal readonly bool Debug;
internal readonly bool AdaptiveImageMode;
@ -37,6 +40,8 @@ namespace TED.Program
Tokenizer.ReplaceTokens("DEVICE NAME: @machineName"),
Tokenizer.ReplaceTokens("OS: @os"),
},
-1,
StringAlignment.Near,
false);
return _default;
@ -49,7 +54,7 @@ namespace TED.Program
internal Options(int paddingHorizontal, int paddingVertical,
int lineSpacing, int fontSize, string fontName,
string imagePath, string lightImagePath,
string darkImagePath, List<string> lines,
string darkImagePath, List<string> lines, int fixedWidth, StringAlignment textAlignment,
bool debug)
{
PaddingHorizontal = paddingHorizontal;
@ -62,6 +67,8 @@ namespace TED.Program
DarkImagePath = darkImagePath;
Lines = lines;
Debug = debug;
FixedWidth = fixedWidth;
TextAlignment = textAlignment;
AdaptiveImageMode = !string.IsNullOrEmpty(LightImagePath) && !string.IsNullOrEmpty(DarkImagePath);
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
@ -36,6 +37,7 @@ namespace TED.Program
var imagePath = GetArgument(args, new string[] { "-image", "-i" }, Options.Default.ImagePath);
var darkImagePath = GetArgument(args, new string[] { "-darkimage", "-di" }, Options.Default.DarkImagePath);
var lightImagePath = GetArgument(args, new string[] { "-lightimage", "-li" }, Options.Default.LightImagePath);
var alignment = GetArgument(args, new string[] { "-align", "-a" }, "left");
var lines = Options.Default.Lines;
if (!bool.TryParse(GetArgument(args, new string[] { "-debug", "-d" }, Options.Default.Debug.ToString()), out bool debug))
@ -63,6 +65,28 @@ namespace TED.Program
paddingVertical = Options.Default.PaddingVertical;
}
if (!int.TryParse(GetArgument(args, new string[] { "-width", "-w" }, Options.Default.FixedWidth.ToString()), out int fixedWidth))
{
fixedWidth = Options.Default.FixedWidth;
}
var alignmentOption = StringAlignment.Near;
switch(alignment.ToLower())
{
case "left":
alignmentOption = StringAlignment.Near;
break;
case "center":
alignmentOption = StringAlignment.Center;
break;
case "right":
alignmentOption = StringAlignment.Far;
break;
default:
alignmentOption = StringAlignment.Near;
break;
}
if (args.Any(arg => arg.Contains("-line")))
{
lines.Clear();
@ -99,6 +123,8 @@ namespace TED.Program
lightImagePath,
darkImagePath,
lines,
fixedWidth,
alignmentOption,
debug
);
}