Skip to content

Commit a2e0141

Browse files
committed
razorify html templates
1 parent a5786e3 commit a2e0141

File tree

11 files changed

+304
-191
lines changed

11 files changed

+304
-191
lines changed

CrossStitchProject/CrossStitchProject.csproj

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<DefineConstants>DEBUG;TRACE</DefineConstants>
2828
<ErrorReport>prompt</ErrorReport>
2929
<WarningLevel>4</WarningLevel>
30+
<LangVersion>7.3</LangVersion>
3031
</PropertyGroup>
3132
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3233
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -38,10 +39,16 @@
3839
<WarningLevel>4</WarningLevel>
3940
</PropertyGroup>
4041
<ItemGroup>
42+
<Reference Include="RazorEngine, Version=3.10.0.0, Culture=neutral, PublicKeyToken=9ee697374c7e744a, processorArchitecture=MSIL">
43+
<HintPath>..\packages\RazorEngine.3.10.0\lib\net45\RazorEngine.dll</HintPath>
44+
</Reference>
4145
<Reference Include="System" />
4246
<Reference Include="System.Configuration" />
4347
<Reference Include="System.Core" />
4448
<Reference Include="System.Web" />
49+
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
50+
<HintPath>..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll</HintPath>
51+
</Reference>
4552
<Reference Include="System.Xml.Linq" />
4653
<Reference Include="System.Data.DataSetExtensions" />
4754
<Reference Include="Microsoft.CSharp" />
@@ -54,15 +61,16 @@
5461
</ItemGroup>
5562
<ItemGroup>
5663
<Compile Include="CrossStitcher.cs" />
57-
<Compile Include="Floss.cs" />
64+
<Compile Include="Models\Floss.cs" />
5865
<Compile Include="Form1.cs">
5966
<SubType>Form</SubType>
6067
</Compile>
6168
<Compile Include="Form1.Designer.cs">
6269
<DependentUpon>Form1.cs</DependentUpon>
6370
</Compile>
6471
<Compile Include="Helpers\ColorMath.cs" />
65-
<Compile Include="HtmlWriter.cs" />
72+
<Compile Include="PatternWriter.cs" />
73+
<Compile Include="Models\PatternChunk.cs" />
6674
<Compile Include="Program.cs" />
6775
<Compile Include="Properties\AssemblyInfo.cs" />
6876
<EmbeddedResource Include="Form1.resx">
@@ -84,6 +92,7 @@
8492
<None Include="NotoColorEmoji.ttf">
8593
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
8694
</None>
95+
<None Include="packages.config" />
8796
<None Include="Properties\Settings.settings">
8897
<Generator>SettingsSingleFileGenerator</Generator>
8998
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -98,10 +107,10 @@
98107
<None Include="App.config" />
99108
</ItemGroup>
100109
<ItemGroup>
101-
<Content Include="legend_template.html">
110+
<Content Include="Templates\legend_razor.html">
102111
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
103112
</Content>
104-
<Content Include="template.html">
113+
<Content Include="Templates\pattern_razor.html">
105114
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
106115
</Content>
107116
</ItemGroup>

CrossStitchProject/CrossStitcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Reflection;
66
using CrossStitchProject.Helpers;
7+
using CrossStitchProject.Models;
78

89
namespace CrossStitchProject
910
{
@@ -25,7 +26,7 @@ public CrossStitcher(Bitmap b, bool isColorPattern, bool shouldDitherImage, stri
2526
public void GenerateCrossStitch()
2627
{
2728
var csBitmap = GenerateStitchBitmap();
28-
var htmlWriter = new HtmlWriter
29+
var htmlWriter = new PatternWriter
2930
(csBitmap,FlossDict,IsColorPattern,ProjectName);
3031
htmlWriter.BuildAndSavePattern();
3132
}

CrossStitchProject/HtmlWriter.cs

Lines changed: 0 additions & 152 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace CrossStitchProject
1+
namespace CrossStitchProject.Models
22
{
3-
internal class Floss
3+
public class Floss
44
{
55
public string DMC { get; set; }
66
public string Symbol { get; set; }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace CrossStitchProject.Models
9+
{
10+
public class PatternChunk
11+
{
12+
public bool IsColorPattern { get; set; }
13+
public Bitmap Chunk { get; set; }
14+
public Dictionary<Color, Floss> ColorToFlossDict { get; set; }
15+
}
16+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Drawing;
5+
using System.Drawing.Imaging;
6+
using System.IO;
7+
using RazorEngine;
8+
using RazorEngine.Templating;
9+
using System.Linq;
10+
using RazorEngine.Configuration;
11+
using CrossStitchProject.Models;
12+
using System.Threading.Tasks;
13+
14+
namespace CrossStitchProject
15+
{
16+
public class PatternWriter
17+
{
18+
private readonly List<Bitmap> _imageChunks = new List<Bitmap>();
19+
private readonly Dictionary<Color, Floss> _color2Floss;
20+
private readonly bool _isColored;
21+
private const decimal DefaultChunkWidth = 50.0M;
22+
private const decimal DefaultChunkHeight = 60.0M;
23+
private static string _outputPath;
24+
public List<string> PatternChunks;
25+
public string PatternLegend;
26+
27+
public PatternWriter(Bitmap b, Dictionary<Color, Floss> c2F, bool colored, string outDir)
28+
{
29+
PatternChunks = new List<string>();
30+
ChunkifyImage(b);
31+
32+
_isColored = colored;
33+
_color2Floss = c2F;
34+
35+
var pictureFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
36+
if (string.IsNullOrEmpty(outDir)) { outDir = "MyCrossStitchPattern"; }
37+
_outputPath = Path.Combine(pictureFolder, outDir);
38+
39+
if (!Directory.Exists(_outputPath)) { Directory.CreateDirectory(_outputPath); }
40+
}
41+
public void BuildAndSavePattern()
42+
{
43+
var task = Task.Factory.StartNew(() => GetDistinctFlossesInImage());
44+
BuildCrossStitchPattern();
45+
GenerateHtmlLegend(task.Result);
46+
for (var i = 0; i < PatternChunks.Count; i++)
47+
{
48+
Save(PatternChunks[i],$"chunk{i+1}.html");
49+
}
50+
Save(PatternLegend,"legend.html");
51+
}
52+
53+
private void ChunkifyImage(Bitmap b)
54+
{
55+
var numHorizontalChunks = (int)Math.Ceiling(b.Width / DefaultChunkWidth);
56+
var numVerticalChunks = (int)Math.Ceiling(b.Height / DefaultChunkHeight);
57+
for (var y = 0; y < numVerticalChunks; y++)
58+
for (var x = 0; x < numHorizontalChunks; x++)
59+
{
60+
var widthLeft = b.Width - (x + 1) * DefaultChunkWidth;
61+
var heightLeft = b.Height - (y + 1) * DefaultChunkHeight;
62+
var curChunkWidth = (int)(widthLeft < 0 ? widthLeft + DefaultChunkWidth: DefaultChunkWidth);
63+
var curChunkHeight = (int)(heightLeft < 0 ? heightLeft + DefaultChunkHeight: DefaultChunkHeight);
64+
var chunk = b.Clone(
65+
new Rectangle(x * (int) DefaultChunkWidth, y * (int) DefaultChunkHeight, curChunkWidth,
66+
curChunkHeight), PixelFormat.Format32bppArgb);
67+
_imageChunks.Add(chunk);
68+
}
69+
}
70+
private void Save(string outString, string filename)
71+
{
72+
File.WriteAllText(Path.Combine(_outputPath, filename), outString);
73+
Process.Start(_outputPath);
74+
}
75+
76+
private void GenerateHtmlLegend(HashSet<Floss> flosses)
77+
{
78+
var model = flosses.ToList();
79+
var template = File.ReadAllText("Templates/legend_razor.html");
80+
var result = Engine.Razor.RunCompile(template,"legendKey", null, model);
81+
PatternLegend = result;
82+
}
83+
private void BuildCrossStitchPattern()
84+
{
85+
foreach (var chunk in _imageChunks)
86+
{
87+
var template = File.ReadAllText("Templates/pattern_razor.html");
88+
var model = new PatternChunk { Chunk = chunk, ColorToFlossDict = _color2Floss, IsColorPattern = _isColored };
89+
//Engine.Razor = RazorEngineService.Create(new TemplateServiceConfiguration { Debug = true });
90+
var result = Engine.Razor.RunCompile(template,"patternKey", null, model);
91+
PatternChunks.Add(result);
92+
}
93+
}
94+
private HashSet<Floss> GetDistinctFlossesInImage()
95+
{
96+
var flossesUsed = new HashSet<Floss>();
97+
foreach (var chunk in _imageChunks)
98+
for (var y = 0; y < chunk.Height; y++)
99+
for (var x = 0; x < chunk.Width; x++)
100+
{
101+
flossesUsed.Add(_color2Floss[chunk.GetPixel(x, y)]);
102+
}
103+
return flossesUsed;
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)