-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
189 lines (167 loc) · 6.07 KB
/
main.cpp
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
185
186
187
188
189
/*
Irrtum
Copyright (C) 2011-2015 kahrl <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "common.h"
#include "irrtum.h"
#include "intervallist.h"
#include "color.h"
#include "cmake_config.h"
#include <popt.h>
static int version(Irrtum& irrtum)
{
cout << "irrtum " << VERSION_STRING << endl;
bool good = true;
if (irrtum.initLibpng())
{
cout << "libpng version: " << irrtum.getLibpngVersion() << endl;
}
else
{
cerr << "Unable to initialize libpng: " << irrtum.getLastError() << endl;
good = false;
}
if (irrtum.initFreetype())
{
cout << "freetype version: " << irrtum.getFreetypeVersion() << endl;
}
else
{
cerr << "Unable to initialize freetype: " << irrtum.getLastError() << endl;
good = false;
}
return (good ? 0 : 1);
}
int main(int argc, char *argv[])
{
Irrtum irrtum;
float opt_size = 16;
u32 opt_color = 0xffffffUL;
float opt_dpi = 72;
int opt_outwidth = 0;
int opt_outheight = 0;
IntervalList opt_ranges;
struct poptOption poptopts[] = {
{"size", 's', POPT_ARG_FLOAT, &opt_size, 0, "Set font size in points", "POINTS"},
{"color", 'c', POPT_ARG_STRING, 0, 'c', "Set output color", "NAME|RRGGBB"},
{"dpi", 'd', POPT_ARG_FLOAT, &opt_dpi, 0, "Set DPI value", "DPI"},
{"outwidth", 'w', POPT_ARG_INT, &opt_outwidth, 0, "Set width of output image", "PIXELS"},
{"outheight", 'h', POPT_ARG_INT, &opt_outheight, 0, "Set height of output image. Ignored if --outwidth is not set.", "PIXELS"},
{"range", 'r', POPT_ARG_STRING, 0, 'r', "Add character range", "START-END"},
{"version", 'V', 0, 0, 'V', "Display version number and exit", 0},
POPT_AUTOHELP
POPT_TABLEEND
};
poptContext poptcon = poptGetContext("irrtum",
argc, const_cast<const char**>(argv), poptopts, 0);
poptSetOtherOptionHelp(poptcon, "[OPTION...] FILE...");
int rc;
while ((rc = poptGetNextOpt(poptcon)) > 0) {
if (rc == 'c') {
const char *color_str = poptGetOptArg(poptcon);
if (!parseColorString(color_str, &opt_color)) {
cerr << color_str << ": invalid color (must be a color name or RRGGBB)" << endl;
return 1;
}
} else if (rc == 'r') {
const char *range_str = poptGetOptArg(poptcon);
s32 from, to;
if (opt_ranges.parseInterval(range_str, from, to)) {
opt_ranges.addInterval(from, to);
} else {
cerr << range_str << ": invalid range (must be FROM-TO)" << endl;
return 1;
}
} else if (rc == 'V') {
return version(irrtum);
}
}
if (rc < -1) {
/* an error occurred during option processing */
cerr << poptBadOption(poptcon, POPT_BADOPTION_NOALIAS) << ": "
<< poptStrerror(rc) << endl;
return 1;
}
// Clean character range
if (opt_ranges.isEmpty())
{
opt_ranges.addInterval(32, 255);
}
if (opt_ranges.getMin() < IRRTUM_CHAR_MIN)
{
cerr << "warning: character ranges below " << IRRTUM_CHAR_MIN << " are ignored" << endl;
opt_ranges.removeBelow(IRRTUM_CHAR_MIN);
}
if (opt_ranges.getMax() > IRRTUM_CHAR_MAX)
{
cerr << "warning: character ranges above " << IRRTUM_CHAR_MAX << " are ignored" << endl;
opt_ranges.removeAbove(IRRTUM_CHAR_MAX);
}
if (opt_ranges.isEmpty())
{
cerr << "error: all specified character ranges are ignored, exiting" << endl;
return 1;
}
if (!irrtum.initLibpng())
{
cerr << "Unable to initialize libpng: " << irrtum.getLastError() << endl;
return 1;
}
if (!irrtum.initFreetype())
{
cerr << "Unable to initialize freetype: " << irrtum.getLastError() << endl;
return 1;
}
irrtum.setColor(opt_color);
irrtum.setCharacterRanges(opt_ranges);
const char* filename = poptGetArg(poptcon);
if (!filename)
{
cerr << "irrtum: no input files" << endl;
return 1;
}
while (filename)
{
// stage 1: loading the font face
cerr << "Loading font face: " << filename << endl;
if (!irrtum.loadFace(filename, opt_size, opt_dpi))
{
cerr << filename << ": Unable to load font: " << irrtum.getLastError() << endl;
return 1;
}
// stage 2: building the layout
if (!irrtum.layout(opt_outwidth, opt_outheight))
{
cerr << filename << ": Unable to create layout: " << irrtum.getLastError() << endl;
return 1;
}
// stage 3: drawing an intermediate grayscale bitmap
if (!irrtum.drawGrayscaleBitmap())
{
cerr << filename << ": Unable to draw bitmap font: " << irrtum.getLastError() << endl;
return 1;
}
// stage 4: converting to ARGB and writing the PNG file
std::string outputFilename = irrtum.getOutputFilename(filename);
cout << "Writing " << irrtum.getLayoutWidth() << "x" << irrtum.getLayoutHeight() << " PNG image: " << outputFilename << endl;
if (!irrtum.outputPNG(outputFilename))
{
cerr << outputFilename << ": Unable to write PNG: " << irrtum.getLastError() << endl;
return 1;
}
filename = poptGetArg(poptcon);
}
poptFreeContext(poptcon);
return 0;
}