Skip to content

Commit 5196639

Browse files
committed
More HtmlWriter refactoring
1 parent 6fa77d4 commit 5196639

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

CrossStitchProject/HtmlWriter.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ internal class HtmlWriter
1515
private readonly List<Bitmap> _imageChunks = new List<Bitmap>();
1616
private readonly Dictionary<Color, FlossInfo> _color2Floss;
1717
private readonly bool _isColored;
18-
private const decimal ChunkWidth = 50.0M;
19-
private const decimal ChunkHeight = 60.0M;
18+
private const decimal DefaultChunkWidth = 50.0M;
19+
private const decimal DefaultChunkHeight = 60.0M;
2020
private static string _outputPath;
2121
private const string LegendTableStart = "<table><thead><tr><th>Floss</th><th>Symbol</th></tr></thead><tbody>";
2222
private const string LegendTableEnd = "</tbody></table>";
@@ -37,31 +37,35 @@ public HtmlWriter(Bitmap b, Dictionary<Color, FlossInfo> c2F, bool colored, stri
3737

3838
private void ChunkifyImage(Bitmap b)
3939
{
40-
var numHorizontalChunks = (int)Math.Ceiling(b.Width / ChunkWidth);
41-
var numVerticalChunks = (int)Math.Ceiling(b.Height / ChunkHeight);
40+
var numHorizontalChunks = (int)Math.Ceiling(b.Width / DefaultChunkWidth);
41+
var numVerticalChunks = (int)Math.Ceiling(b.Height / DefaultChunkHeight);
4242
for (var y = 0; y < numVerticalChunks; y++)
4343
{
4444
for (var x = 0; x < numHorizontalChunks; x++)
4545
{
46-
var wLeft = b.Width - (x + 1) * ChunkWidth;
47-
var hLeft = b.Height - (y + 1) * ChunkHeight;
48-
var width = (int)(wLeft < 0 ? wLeft + ChunkWidth: ChunkWidth);
49-
var height = (int)(hLeft < 0 ? hLeft + ChunkHeight: ChunkHeight);
50-
_imageChunks.Add(b.Clone(new Rectangle(x * (int)ChunkWidth, y * (int)ChunkHeight, width, height), PixelFormat.Format32bppArgb));
46+
var widthLeft = b.Width - (x + 1) * DefaultChunkWidth;
47+
var heightLeft = b.Height - (y + 1) * DefaultChunkHeight;
48+
var curChunkWidth = (int)(widthLeft < 0 ? widthLeft + DefaultChunkWidth: DefaultChunkWidth);
49+
var curChunkHeight = (int)(heightLeft < 0 ? heightLeft + DefaultChunkHeight: DefaultChunkHeight);
50+
var chunk = b.Clone(
51+
new Rectangle(x * (int) DefaultChunkWidth, y * (int) DefaultChunkHeight, curChunkWidth,
52+
curChunkHeight), PixelFormat.Format32bppArgb);
53+
_imageChunks.Add(chunk);
5154
}
5255
}
5356

5457
}
55-
//TODO: this is really dumb
56-
private void Save(StringBuilder outString, string filename="chunk",int fileNo=-1)
58+
private void Save(StringBuilder outString, string filename)
5759
{
58-
File.WriteAllText(
59-
filename == "chunk"
60-
? Path.Combine(_outputPath, $"chunk{fileNo}.html")
61-
: Path.Combine(_outputPath, filename), outString.ToString());
60+
File.WriteAllText(Path.Combine(_outputPath, filename), outString.ToString());
6261
Process.Start(_outputPath);
6362
}
6463

64+
private void SaveChunk(StringBuilder outString, int fileNo)
65+
{
66+
File.WriteAllText(Path.Combine(_outputPath, $"chunk{fileNo}.html"), outString.ToString());
67+
}
68+
6569
private void GenerateHtmlLegend(IReadOnlyList<FlossInfo> flossInfos)
6670
{
6771
var tableEnded = false;
@@ -73,7 +77,7 @@ private void GenerateHtmlLegend(IReadOnlyList<FlossInfo> flossInfos)
7377

7478
htmlString.AppendLine($"<tr><td>{flossInfos[i].FlossId}</td><td>{flossInfos[i].FlossSymbol}</td></tr>");
7579

76-
if (i > 0 && (i+1) % ChunkWidth == 0)
80+
if (i > 0 && (i+1) % DefaultChunkWidth == 0)
7781
{
7882
htmlString.AppendLine(LegendTableEnd);
7983
htmlString.AppendLine(LegendTableStart);
@@ -89,11 +93,11 @@ private void GenerateHtmlLegend(IReadOnlyList<FlossInfo> flossInfos)
8993
}
9094
public void GenerateHtml()
9195
{
92-
var distinctFlossesInImage = GenerateCrossStitch();
96+
var distinctFlossesInImage = GenerateCrossStitchPattern();
9397
GenerateHtmlLegend(distinctFlossesInImage.ToList());
9498
}
9599

96-
public HashSet<FlossInfo> GenerateCrossStitch()
100+
public HashSet<FlossInfo> GenerateCrossStitchPattern()
97101
{
98102
var chunkCount = 0;
99103
var flossesInImage = new HashSet<FlossInfo>();
@@ -135,7 +139,7 @@ public HashSet<FlossInfo> GenerateCrossStitch()
135139

136140
}
137141
htmlString.AppendLine("</body></html>");
138-
Save(htmlString,fileNo:chunkCount);
142+
SaveChunk(htmlString,chunkCount);
139143
chunkCount++;
140144
}
141145
return flossesInImage;

0 commit comments

Comments
 (0)