-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
184 lines (124 loc) · 6.17 KB
/
Program.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using CommandLine;
#nullable enable
namespace Bitmapper
{
[Verb("image", HelpText = "convert an image")]
class ImageOptions
{
[Option('p', "image-path", Default = true, Required = true, HelpText = "Image file to be processed.")]
public string ImagePath { get; set; }
[Option('o', "outpath-path", Required = true, HelpText = "Path to output file")]
public string OutFilePath { get; set; }
[Option('t', "template", Required = false, HelpText = "Path to handebar template to be used")]
public string TemplateFilePath { get; set; }
[Option('b', "bias", Required = false, HelpText = "Luminance Bias .default is 128")]
public byte? Bias { get; set; }
}
[Verb("font", HelpText = "convert a font")]
class FontOptions
{
[Option('p', "font-path", SetName = "fontName", HelpText = "Font file to be processed.")]
public string FontPath { get; set; }
[Option('n', "font-name", SetName = "fontName", HelpText = "Name of font already installed on system to be processed.")]
public string FontName { get; set; }
[Option('s', "size", Required = true, HelpText = "Size")]
public byte Size { get; set; }
[Option('o', "output-file", Required = true, HelpText = "Path to output file")]
public string OutputFile { get; set; }
[Option('t', "template", Required = false, HelpText = "Path to handebar template to be used")]
public string TemplateFilePath { get; set; }
[Option('c', "char-path", Required = false, HelpText = "Path to file with all chars")]
public string CharSetPath { get; set; }
[Option('b', "bias", Required = false, HelpText = "Luminance Bias .default is 128")]
public byte? Bias { get; set; }
}
[Verb("fonts", HelpText = "convert multiple font")]
class FontsOptions
{
[Option('p', "font-path", SetName = "fontName", HelpText = "Font file to be processed.")]
public string FontPath { get; set; }
[Option('s', "sizes", Required = true, HelpText = "font size sepeated by ,")]
public string Sizes { get; set; }
[Option('o', "outpath-path", Required = true, HelpText = "Path to output file")]
public string OutFilePath { get; set; }
[Option('t', "template", Required = false, HelpText = "Path to handebar template to be used")]
public string TemplateFilePath { get; set; }
[Option('c', "char-path", Required = false, HelpText = "Path to file with all chars")]
public string CharSetPath { get; set; }
[Option('b', "bias", Required = false, HelpText = "Luminance Bias .default is 128")]
public byte? Bias { get; set; }
}
class Program
{
const string imageConvertTemplateFilePath = "./templates/image-template.handlebars";
const string fontConvertTemplateFilePath = "./templates/font-template.handlebars";
public static int Main(string[] args)
{
try
{
return Parser.Default.ParseArguments<ImageOptions, FontOptions, FontsOptions>(args)
.MapResult(
(ImageOptions opts) => RunConvertImage(opts),
(FontOptions opts) => RunConvertFont(opts),
(FontsOptions opts) => RunConvertFonts(opts),
errs => 1);
}
catch (Exception e)
{
Console.Error.WriteLine(e.ToString());
return -1;
}
}
private static int RunConvertFont(FontOptions opts)
{
FontMap fontmap = null;
if (!string.IsNullOrWhiteSpace(opts.FontName))
{
fontmap = FontMap.FromSystemFont(opts.FontName, opts.Size, opts.CharSetPath, opts.Bias);
}
else if (!string.IsNullOrWhiteSpace(opts.FontPath))
{
fontmap = FontMap.FromFontFile(opts.FontPath, opts.Size, opts.CharSetPath, opts.Bias);
}
else
{
throw new ApplicationException("Invalid font specification");
}
var template = new Template(opts.TemplateFilePath ?? fontConvertTemplateFilePath);
template.RenderTemplate(fontmap, opts.OutputFile);
Console.WriteLine($"Rendered output to file {opts.OutputFile}");
return 0;
}
private static int RunConvertFonts(FontsOptions opts)
{
var sizes = opts.Sizes.Split(',').Select(s => Convert.ToInt32(s));
//TODO: filtes out unsupported extensions
//var vaildExtensions = "ttf;woff;woff2".Split(";");
//foreach (var fontfile in Directory.EnumerateFiles(opts.FontPath).Where(p => vaildExtensions.Contains(Path.GetExtension(p))))
foreach (var fontfile in Directory.EnumerateFiles(opts.FontPath))
{
foreach (var size in sizes)
{
var fontmap = FontMap.FromFontFile(fontfile, size, opts.CharSetPath, opts.Bias);
var outputFileNAme =Path.Combine(opts.OutFilePath,$"{Path.GetFileNameWithoutExtension(fontfile)}-{size}.c");
var template = new Template(opts.TemplateFilePath ?? fontConvertTemplateFilePath);
template.RenderTemplate(fontmap, outputFileNAme);
Console.WriteLine($"Rendered output to file {outputFileNAme}");
}
}
return 0;
}
private static int RunConvertImage(ImageOptions opts)
{
var bitmap = Bitmap.FromPath(opts.ImagePath, opts.Bias);
var template = new Template(opts.TemplateFilePath ?? imageConvertTemplateFilePath);
template.RenderTemplate(new
{
ImageName = Path.GetFileNameWithoutExtension(opts.ImagePath),
Bitmap = bitmap
}, opts.OutFilePath);
Console.WriteLine($"Rendered output to file {opts.OutFilePath}");
return 0;
}
}
}