Skip to content

Commit

Permalink
Version 2.1
Browse files Browse the repository at this point in the history
Added background image alignment
Resize previews from screensize, instead of rendering inside bounds of preview graphics.
Include versionnumber in config form title
Updated version to 2.1
  • Loading branch information
AMSoftwareNL committed Sep 23, 2019
1 parent 6db111e commit 09f145c
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 136 deletions.
3 changes: 3 additions & 0 deletions AMSoftware.Quotes/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<setting name="BackgroundImagePath" serializeAs="String">
<value />
</setting>
<setting name="BackgroundAlignment" serializeAs="String">
<value>0</value>
</setting>
</AMSoftware.Quotes.Properties.Settings>
</userSettings>
</configuration>
211 changes: 113 additions & 98 deletions AMSoftware.Quotes/ConfigForm.Designer.cs

Large diffs are not rendered by default.

31 changes: 28 additions & 3 deletions AMSoftware.Quotes/ConfigForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public partial class ConfigForm : Form
public ConfigForm()
{
InitializeComponent();
this.Text = $"{Application.ProductName} [{Application.ProductVersion}]";

_configSettings = Settings.Default;

Expand All @@ -51,6 +52,17 @@ public ConfigForm()

backgroundColorTextBox.Text = $"{_configSettings.BackgroundColor.Name}";
backgroundImageTextBox.Text = _configSettings.BackgroundImagePath;

backgroundAlignmentComboBox.DataSource = new ArrayList()
{
Tuple.Create<BackgroundAlignment,string>(BackgroundAlignment.Fit, "Fit"),
Tuple.Create<BackgroundAlignment,string>(BackgroundAlignment.Stretch, "Stretch"),
Tuple.Create<BackgroundAlignment,string>(BackgroundAlignment.Center, "Center"),
Tuple.Create<BackgroundAlignment, string>(BackgroundAlignment.Tile, "Tile"),
};
backgroundAlignmentComboBox.DisplayMember = "Item2";
backgroundAlignmentComboBox.ValueMember = "Item1";
backgroundAlignmentComboBox.SelectedValue = (BackgroundAlignment)_configSettings.BackgroundAlignment;
}

private void FontButton_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -88,6 +100,18 @@ private void AlignmentComboBox_SelectedIndexChanged(object sender, EventArgs e)
}
}

private void BackgroundAlignmentComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (backgroundAlignmentComboBox.SelectedValue != null)
{
if (backgroundAlignmentComboBox.SelectedValue is BackgroundAlignment)
{
_configSettings.BackgroundAlignment = (int)backgroundAlignmentComboBox.SelectedValue;
applyButton.Enabled = true;
}
}
}

private void PathButton_Click(object sender, EventArgs e)
{
openFileDialog.Filter = "Quotes XML|*.xml|All files|*.*";
Expand Down Expand Up @@ -117,7 +141,7 @@ private void BackgroundColorButton_Click(object sender, EventArgs e)

private void BackgroundImageButton_Click(object sender, EventArgs e)
{
openFileDialog.Filter = "Bitmap files|*.bmp;*.dib|JPEG|*.jpg;*.jpeg;*.jpe;*.jfif|GIF|*.gif|PNG|*.png|All Picture Files|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.png|All files|*.*";
openFileDialog.Filter = "All Picture Files|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.png|Bitmap files|*.bmp;*.dib|JPEG|*.jpg;*.jpeg;*.jpe;*.jfif|GIF|*.gif|PNG|*.png|All files|*.*";
openFileDialog.Title = "Select background image";

openFileDialog.FileName = _configSettings.BackgroundImagePath;
Expand Down Expand Up @@ -181,9 +205,10 @@ private void PreviewPanel_Paint(object sender, PaintEventArgs e)
TextAlignment = (TextAlignment)_configSettings.TextAlignment,
TextShrinkToFit = true,
BackgroundColor = _configSettings.BackgroundColor,
BackgroundImagePath = _configSettings.BackgroundImagePath
BackgroundImagePath = _configSettings.BackgroundImagePath,
BackgroundAlignment = (BackgroundAlignment)_configSettings.BackgroundAlignment,
});
renderer.Render(q, g);
renderer.Render(q, g, Screen.FromControl(this).Bounds);
}
}

Expand Down
5 changes: 3 additions & 2 deletions AMSoftware.Quotes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static void RunPreview(int parentHwndArgument)

using (Graphics g = Graphics.FromHwnd(new IntPtr(parentHwndArgument)))
{
renderer.Render(quote, g);
renderer.Render(quote, g, Screen.FromHandle(new IntPtr(parentHwndArgument)).Bounds);
}
}

Expand All @@ -109,7 +109,8 @@ private static RenderSettings InitRenderSettings()
TextAlignment = (TextAlignment)Settings.Default.TextAlignment,
TextShrinkToFit = Settings.Default.TextShrinkToFit,
BackgroundColor = Settings.Default.BackgroundColor,
BackgroundImagePath = Settings.Default.BackgroundImagePath
BackgroundImagePath = Settings.Default.BackgroundImagePath,
BackgroundAlignment = (BackgroundAlignment)Settings.Default.BackgroundAlignment
};
}

Expand Down
4 changes: 2 additions & 2 deletions AMSoftware.Quotes/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ You should have received a copy of the GNU Affero General Public License
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.1.*")]
[assembly: AssemblyFileVersion("2.1.0.0")]
14 changes: 13 additions & 1 deletion AMSoftware.Quotes/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions AMSoftware.Quotes/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
<Setting Name="BackgroundImagePath" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="BackgroundAlignment" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
</Settings>
</SettingsFile>
164 changes: 142 additions & 22 deletions AMSoftware.Quotes/QuoteRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,39 @@ public QuoteRenderer(RenderSettings settings)

public void Render(Quote q, Graphics g)
{
g.Clear(_settings.BackgroundColor);
RectangleF clipBounds = g.VisibleClipBounds;

if (!string.IsNullOrWhiteSpace(_settings.BackgroundImagePath))
{
try
{
Image backgroundImage = Image.FromFile(_settings.BackgroundImagePath);
g.DrawImage(backgroundImage, g.VisibleClipBounds);
}
catch { }
}
RenderBackground(g, clipBounds);
RenderText(q, g, clipBounds);

if (q == null)
{
g.Flush(FlushIntention.Flush);
return;
}
g.Flush(FlushIntention.Flush);
}

public void Render(Quote q, Graphics g, RectangleF measureBounds)
{
g.ScaleTransform(
g.VisibleClipBounds.Width / measureBounds.Width,
g.VisibleClipBounds.Height / measureBounds.Height,
MatrixOrder.Append);

RenderBackground(g, measureBounds);
RenderText(q, g, measureBounds);

g.Flush(FlushIntention.Flush);
}

private void RenderText(Quote q, Graphics g, RectangleF displayBounds)
{
if (q == null) return;

// Set a margin of 5%
float marginLeft = g.VisibleClipBounds.Width * 0.05F;
float marginTop = g.VisibleClipBounds.Height * 0.05F;
float marginLeft = displayBounds.Width * 0.05F;
float marginTop = displayBounds.Height * 0.05F;
RectangleF layoutArea = new RectangleF(
marginLeft,
marginTop,
g.VisibleClipBounds.Width - (2.0F * marginLeft),
g.VisibleClipBounds.Height - (2.0F * marginTop)
displayBounds.Width - (2.0F * marginLeft),
displayBounds.Height - (2.0F * marginTop)
);

// Print author and year of quote.
Expand Down Expand Up @@ -90,8 +97,121 @@ public void Render(Quote q, Graphics g)
DrawQuoteText(q.QuoteText, g, textFont, _settings.TextColor, layoutArea);
}
}
}

g.Flush(FlushIntention.Flush);
private void RenderBackground(Graphics g, RectangleF displayBounds)
{
g.Clear(_settings.BackgroundColor);

if (!string.IsNullOrWhiteSpace(_settings.BackgroundImagePath))
{
try
{
Image backgroundImage = Image.FromFile(_settings.BackgroundImagePath);

switch (_settings.BackgroundAlignment)
{
case BackgroundAlignment.Fit:
DrawBackgroundFitted(g, backgroundImage, displayBounds);
break;
case BackgroundAlignment.Stretch:
DrawBackgroundStretched(g, backgroundImage, displayBounds);
break;
case BackgroundAlignment.Center:
DrawBackgroundCentered(g, backgroundImage, displayBounds);
break;
case BackgroundAlignment.Tile:
DrawBackgroundTiled(g, backgroundImage, displayBounds);
break;
default:
break;
}
}
catch { }
}
}

private static void DrawBackgroundFitted(Graphics g, Image backgroundImage, RectangleF displayBounds)
{
float ratioScreen = displayBounds.Width / displayBounds.Height;
float ratioImage = (float)backgroundImage.Width / (float)backgroundImage.Height;

if (ratioScreen > ratioImage)
{
float width = displayBounds.Height * ratioImage;
RectangleF destRectangle = new RectangleF(
(displayBounds.Width - width) / 2, 0,
width, displayBounds.Height);

g.DrawImage(backgroundImage, destRectangle);
}
else
{
float height = displayBounds.Width * ratioImage;
RectangleF destRectangle = new RectangleF(
0, (displayBounds.Height - height) / 2,
displayBounds.Width, height);

g.DrawImage(backgroundImage, destRectangle);
}
}

private static void DrawBackgroundStretched(Graphics g, Image backgroundImage, RectangleF displayBounds)
{
float ratioScreen = displayBounds.Width / displayBounds.Height;
float ratioImage = (float)backgroundImage.Width / (float)backgroundImage.Height;

if (ratioScreen > ratioImage)
{
float height = backgroundImage.Width / ratioScreen;
RectangleF srcRectangle = new RectangleF(
0, (backgroundImage.Height - height) / 2,
backgroundImage.Width, height);

g.DrawImage(backgroundImage, displayBounds, srcRectangle, GraphicsUnit.Pixel);
}
else
{
float width = backgroundImage.Height / ratioScreen;
RectangleF srcRectangle = new RectangleF(
(backgroundImage.Width - width) / 2, 0,
width, backgroundImage.Height);

g.DrawImage(backgroundImage, displayBounds, srcRectangle, GraphicsUnit.Pixel);
}
}

private static void DrawBackgroundTiled(Graphics g, Image backgroundImage, RectangleF displayBounds)
{
Rectangle srcRectangle = new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height);

int tileCountWidth = (int)Math.Ceiling(displayBounds.Width / srcRectangle.Width);
int tileCountHeight = (int)Math.Ceiling(displayBounds.Height / srcRectangle.Height);

int offsetX = (int)Math.Round(((tileCountWidth * srcRectangle.Width) - displayBounds.Width) / 2);
int offsetY = (int)Math.Round(((tileCountHeight * srcRectangle.Height) - displayBounds.Height) / 2); ;

for (int y = 0; y < tileCountHeight; y++)
{
for (int x = 0; x < tileCountWidth; x++)
{
Rectangle destRectangle = new Rectangle((x * srcRectangle.Width) - offsetX, (y * srcRectangle.Height) - offsetY, srcRectangle.Width, srcRectangle.Height);
g.DrawImageUnscaledAndClipped(backgroundImage, destRectangle);
}
}
}

private static void DrawBackgroundCentered(Graphics g, Image backgroundImage, RectangleF displayBounds)
{
Rectangle destRectangle = new Rectangle()
{
Width = backgroundImage.Width,
Height = backgroundImage.Height,
X = (int)Math.Round((displayBounds.Width - backgroundImage.Width) / 2),
Y = (int)Math.Round((displayBounds.Height - backgroundImage.Height) / 2)
};

g.DrawImageUnscaledAndClipped(backgroundImage, destRectangle);
}

private static Font GetFittedFont(string text, Graphics g, Font textFont, RectangleF area)
Expand All @@ -105,11 +225,11 @@ private static Font GetFittedFont(string text, Graphics g, Font textFont, Rectan

if (shrinkFactorHeight < shrinkFactorWidth)
{
return new Font(textFont.FontFamily, textFont.Size * shrinkFactorHeight);
return new Font(textFont.FontFamily, textFont.Size * shrinkFactorHeight, textFont.Style);
}
else
{
return new Font(textFont.FontFamily, textFont.Size * shrinkFactorWidth);
return new Font(textFont.FontFamily, textFont.Size * shrinkFactorWidth, textFont.Style);
}

}
Expand Down
1 change: 1 addition & 0 deletions AMSoftware.Quotes/RenderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ internal class RenderSettings
public TextAlignment TextAlignment { get; set; }
public bool TextShrinkToFit { get; set; }
public string BackgroundImagePath { get; set; }
public BackgroundAlignment BackgroundAlignment { get; internal set; }
}
}
Loading

0 comments on commit 09f145c

Please sign in to comment.