This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BingoCard.cs
140 lines (126 loc) · 4.98 KB
/
BingoCard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using PdfSharpCore.Drawing;
using PdfSharpCore.Fonts;
using PdfSharpCore.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Frixu.BingoGen
{
/// <summary>Represents a 5x5 bingo card.</summary>
public class BingoCard
{
static BingoCard()
{
GlobalFontSettings.FontResolver = new BingoFontResolver();
}
private PdfDocument InternalDocument { get; }
private PdfPage Page { get; }
private XGraphics Gfx { get; }
private XFont Font { get; }
private XFont TitleFont { get; }
private readonly Dictionary<string, int> Words;
private readonly Random RNG;
private const double FontRegularSize = 16;
private const double FontTitleSize = 40;
private const double LineHeight = 1.15;
private const double TableBorder = 3.0;
/// <summary>Creates a new bingo card.</summary>
/// <param name="title">Card title. Will display at the top of the card.</param>
/// <param name="author">Card author. Will appear in PDF metadata.</param>
/// <param name="creator">Card creator. Will appear in PDF metadata.</param>
public BingoCard(string title = "Bingo!", string author = "BingoGen", string creator = "BingoGen")
{
RNG = new Random();
Words = new Dictionary<string, int>();
InternalDocument = new PdfDocument();
InternalDocument.Info.Title = title;
InternalDocument.Info.Author = author;
InternalDocument.Info.Creator = creator;
Page = InternalDocument.AddPage();
Page.TrimMargins.All = XUnit.FromCentimeter(2);
Gfx = XGraphics.FromPdfPage(Page);
Font = new XFont("OpenSans", FontRegularSize, XFontStyle.Regular);
TitleFont = new XFont("OpenSans", FontTitleSize, XFontStyle.Bold);
}
/// <summary>Adds word to this card.</summary>
public void AddWord(string word) => Words.Add(word, RNG.Next(10000));
/// <summary>Compiles word list into a PDF document.</summary>
public BingoCard Create()
{
var shuffledWords = Words.OrderBy(w => w.Value).Select(w => w.Key).ToList();
Gfx.DrawString
(
text: InternalDocument.Info.Title,
font: TitleFont,
brush: XBrushes.Black,
layoutRectangle: new XRect(0, 0, Page.Width, Page.Height.Value / 10),
format: XStringFormats.Center
);
//Which word (from the shuffled list) should we use now?
var index = 0;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
//Don't print anything in the middle cell
if(i == 2 && j == 2) continue;
var parts = shuffledWords[index++].Split(@"\n");
for(var partIndex = 0; partIndex < parts.Length; partIndex++)
{
Gfx.DrawString
(
text: parts[partIndex],
font: Font,
brush: XBrushes.Black,
layoutRectangle: new XRect
(
Page.Width.Value / 5 * i,
Page.Height.Value / 8 * (2 + j)
- XUnit.FromPoint(FontRegularSize) * ((parts.Length - 1) * 0.5 - partIndex) * LineHeight,
Page.Width.Value / 5,
Page.Height.Value / 8
),
format: XStringFormats.Center
);
}
}
}
//Draw table borders
var pen = new XPen(XColors.Black, TableBorder);
for (var i = 0; i <= 5; i++)
{
Gfx.DrawLine
(
pen,
Page.Width.Value / 5 * i,
Page.Height.Value / 8 * 2,
Page.Width.Value / 5 * i,
Page.Height.Value / 8 * 7
);
Gfx.DrawLine
(
pen,
0,
Page.Height.Value / 8 * (2 + i),
Page.Width,
Page.Height.Value / 8 * (2 + i)
);
}
return this;
}
/// <summary>Saves resulting PDF document to disk.</summary>
/// <param name="filename">Output file.</param>
public void Save(string filename = "bingo.pdf")
{
try
{
InternalDocument.Save(filename);
}
catch(Exception e)
{
Console.Error.WriteLine($"Cannot save to {filename}.");
Console.Error.WriteLine(e.ToString());
}
}
}
}