Skip to content

Commit

Permalink
Merge pull request #16 from Amomum/master
Browse files Browse the repository at this point in the history
Added 6px font for enumeration
  • Loading branch information
rumkit committed Aug 30, 2018
2 parents 763585a + 7719041 commit 4c50d56
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
75 changes: 58 additions & 17 deletions Pixie/PatternGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
using System.Drawing.Text;
using System.Xml;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Pixie
{
Expand Down Expand Up @@ -80,9 +84,13 @@ private void FillBackground(Bitmap pattern)
private void DrawHorizontalLines(Bitmap pattern)
{
var g = Graphics.FromImage(pattern);
for (var i = _settings.SymbolHeight; i < pattern.Height; i += _settings.SymbolHeight + _settings.DelimeterHeight)
for (var i = _settings.SymbolHeight + _settings.DelimeterHeight/2; i < pattern.Height; i += _settings.SymbolHeight + _settings.DelimeterHeight)
{
g.DrawLine(new Pen(_delimeterColor, _settings.DelimeterHeight), 0, i, pattern.Width, i);
var pen = new Pen(_delimeterColor, _settings.DelimeterHeight)
{
Alignment = PenAlignment.Outset
};
g.DrawLine(pen, 0, i, pattern.Width, i);
}

}
Expand All @@ -94,12 +102,42 @@ private void DrawHorizontalLines(Bitmap pattern)
private void DrawVerticalLines(Bitmap pattern)
{
var g = Graphics.FromImage(pattern);
for (var i = _settings.SymbolWidth; i < pattern.Width; i += _settings.SymbolWidth + _settings.DelimeterWidth)
for (var i = _settings.SymbolWidth + _settings.DelimeterWidth/2; i < pattern.Width; i += _settings.SymbolWidth + _settings.DelimeterWidth)
{
g.DrawLine(new Pen(_delimeterColor, _settings.DelimeterWidth), i, 0, i, pattern.Height);
var pen = new Pen(_delimeterColor, _settings.DelimeterWidth)
{
Alignment = PenAlignment.Right
};
g.DrawLine(pen, i, 0, i, pattern.Height);
}
}

/// <summary>
/// Loads custom font from resource and returns it
/// </summary>
/// <param name="resourceName">name of resource, containing font</param>
/// <param name="size">font size</param>
private Font GetCustomFont(string resourceName, float size)
{
var assembly = Assembly.GetExecutingAssembly();

// create private font collection object
PrivateFontCollection pfc = new PrivateFontCollection();

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
var buffer = new byte[stream.Length];
var result = stream.Read(buffer, 0, buffer.Length);
// create an unsafe memory block for the font data
System.IntPtr data = Marshal.AllocCoTaskMem(buffer.Length);
// copy the bytes to the unsafe memory block
Marshal.Copy(buffer, 0, data, buffer.Length);
// pass the font to the font collection
pfc.AddMemoryFont(data, buffer.Length);
}
return new Font(pfc.Families[0], size);
}

/// <summary>
/// Draws line and column numbers
/// </summary>
Expand All @@ -108,40 +146,43 @@ private void DrawVerticalLines(Bitmap pattern)
private void Enumerate(Bitmap pattern, EnumerationStyle enumerationStyle)
{
// 72 pixels in one pt
const int PixelsPerPoint = 72;
const int pixelsPerPoint = 72;
const string fontName = "Pixie.Resources.XpaiderPE.TTF";
if (enumerationStyle == EnumerationStyle.None)
return;

var graphics = Graphics.FromImage(pattern);

// rows and column numbers will be 0.75 of symbol size
var fontSize = (float)(_settings.SymbolHeight * 0.75 * PixelsPerPoint / pattern.VerticalResolution);
// align vertically in center
int topPadding = _settings.SymbolHeight / 4 - 1;
var fontSize = (float)(_settings.SymbolHeight * 0.75 * pixelsPerPoint / pattern.VerticalResolution);

var font = new Font(FontFamily.GenericMonospace, fontSize);
var font = GetCustomFont(fontName, fontSize);
var brush = new SolidBrush(_delimeterColor);

// select string format specifier based on enumeration style
var numbersStyle = enumerationStyle == EnumerationStyle.Hex ? "X" : "D2";

// make numbers more readable on low resolutions
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
// we use 6px sized font so we don't need any anti-aliasing
graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;

var rowHeight = _settings.SymbolHeight + _settings.DelimeterHeight;
var columnWidth = _settings.SymbolWidth + _settings.DelimeterWidth;
// Enumerate rows
for (int rowHeight = _settings.SymbolHeight + _settings.DelimeterHeight, rowNumber = 0, i = rowHeight;
for (int rowNumber = 0, i = rowHeight;
i < pattern.Height;
i += rowHeight)
i += rowHeight, rowNumber++)
{
graphics.DrawString(rowNumber++.ToString(numbersStyle), font, brush, 0, i + topPadding);
graphics.DrawString(rowNumber.ToString(numbersStyle), font, brush,
new RectangleF(0 - 1,i - 1,_settings.SymbolWidth, _settings.SymbolHeight));
}

// Enumerate columns
for (int columnWidth = _settings.SymbolWidth + _settings.DelimeterWidth, columnNumber = 0, i = columnWidth;
for (int columnNumber = 0, i = columnWidth;
i < pattern.Width;
i += columnWidth)
i += columnWidth, columnNumber++)
{
graphics.DrawString(columnNumber++.ToString(numbersStyle), font, brush, i, topPadding);
graphics.DrawString(columnNumber.ToString(numbersStyle), font, brush,
new RectangleF(i - 1,0 - 1,_settings.SymbolWidth, _settings.SymbolHeight));
}
}

Expand Down
10 changes: 10 additions & 0 deletions Pixie/Pixie.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.Net.Compilers.2.9.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.2.9.0\build\Microsoft.Net.Compilers.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -13,6 +14,8 @@
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -77,8 +80,15 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
<EmbeddedResource Include="Resources\XpaiderPE.TTF" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.2.9.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.2.9.0\build\Microsoft.Net.Compilers.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Binary file added Pixie/Resources/XpaiderPE.TTF
Binary file not shown.
1 change: 1 addition & 0 deletions Pixie/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Net.Compilers" version="2.9.0" targetFramework="net46" developmentDependency="true" />
<package id="CommandLineParser" version="2.3.0" targetFramework="net46" />
</packages>

0 comments on commit 4c50d56

Please sign in to comment.