-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgParser.cpp
More file actions
350 lines (336 loc) · 13.7 KB
/
Copy pathArgParser.cpp
File metadata and controls
350 lines (336 loc) · 13.7 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* ArgParser.cpp
* xwall
*
* Created by Tim Medcalf on 09/02/2011.
* Copyright 2011 ErgoThis Ltd. All rights reserved.
*
*/
#include "ArgParser.h"
#include <string>
#include <iostream>
#include "Global.h"
#include <getopt.h>
#include <cstring>
#include "ColorManager.h"
#include "Cairo.h"
using namespace std;
void ArgParser::process_arguments(int argc, char * const argv[])
{
int c;
string rgba;
string s_optarg;
bool input_set = false;
bool output_set = false;
color_array newbackground;
while (1)
{
static struct option long_options[] =
{
/* These options set a flag. */
//{"verbose", no_argument, &verbose_flag, 1},
//{"brief", no_argument, &verbose_flag, 0},
/* These options don't set a flag.
We distinguish them by their indices. */
{"verbose", no_argument, 0, 'v'},
{"quiet", no_argument, 0, 'q'},
{"detail", required_argument, 0, 'd'},
{"border", required_argument, 0, 'b'},
{"wordborder", required_argument, 0, 'B'},
{"leave_common_words", no_argument, 0, 'C'},
{"format", required_argument, 0, 'f'},
{"colorletters", no_argument, 0, 'l'},
{"imagematch", required_argument, 0, 'i'},
{"width", required_argument, 0, 'w'},
{"height", required_argument, 0, 'h'},
{"number", required_argument, 0, 'n'},
{"compression", required_argument, 0, 'c'},
{"emphasis", required_argument, 0, 'e'},
{"percentvertical" , required_argument, 0, 'p'},
{"frequencylist", required_argument, 0, 'r'},
{"seed", required_argument, 0, 's'},
{"transparent", no_argument, 0, 't'},
{"extrawords", no_argument, 0, 'x'},
{"shufflemark", required_argument, 0, 'u'},
{"background", required_argument, 0, 'g'},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'I' },
{"theme", required_argument, 0, 'T'},
{"font", required_argument, 0, 'F'},
{"large_word_marker", required_argument, 0, 'L'},
{"glyph_map_width", required_argument, 0, 'W'},
{"glyph_map_height", required_argument, 0, 'H'},
{"pre_place_target", required_argument, 0, 'P'},
{"size_guestimate", required_argument, 0, 'G'},
{"output_frequency_list",no_argument, 0, 'o'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "b:B:c:Cd:e:i:f:F:g:G:h:H:lL:n:op:P:rs:u:vqw:W:xtV?T:",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
// case 0:
// /* If this option set a flag, do nothing else now. */
// if (long_options[option_index].flag != 0)
// break;
// printf ("option %s", long_options[option_index].name);
// if (optarg)
// printf (" with arg %s", optarg);
// printf ("\n");
// break;
// case 'a':
// puts ("option -a\n");
// break;
case 'b':
settings.output_border = atoi(optarg);
break;
case 'B':
settings.word_border_scale = atof(optarg);
settings.word_border = settings.word_border_scale * settings.font_size;
break;
case 'C':
settings.remove_common_words = false;
break;
case 'd':
settings.position_resolution = atof(optarg);
settings.placement_resolution = settings.font_size * settings.position_resolution;
break;
case 'l':
settings.color_individual_glyphs = true;
break;
case 'f':
s_optarg = optarg;
std::transform(s_optarg.begin(), s_optarg.end(), s_optarg.begin(),
(int(*)(int)) tolower);
if (!s_optarg.compare("svg"))
{
settings.output_format = of_SVG;
}
else if (!s_optarg.compare("pdf"))
{
settings.output_format = of_PDF;
}
else if (!s_optarg.compare("png"))
{
settings.output_format = of_PNG;
}
else
{
cout << "Invalid output image format \"" << optarg << "\". Defaulting to PNG." << endl;
settings.output_format = of_PNG;
}
break;
case 'g':
rgba = optarg;
std::transform(rgba.begin(), rgba.end(), rgba.begin(),
(int(*)(int)) tolower);
if ((rgba.find_first_of('r') != 0) ||
(rgba.find_first_of('g') < rgba.find_first_of('r') +2) ||
(rgba.find_first_of('b') < rgba.find_first_of('g')+2) ||
(rgba.find_first_of('a') < rgba.find_first_of('b')+2) ||
(rgba.find_first_of('a') > rgba.length() - 2))
{
cout << endl << " -g usage: -g r255g255b255a255" << endl;
exit(0);
}
newbackground.r = atof(rgba.substr(rgba.find_first_of('r')+1,rgba.find_first_of('g') - (rgba.find_first_of('r') +1)).c_str());
newbackground.g = atof(rgba.substr(rgba.find_first_of('g')+1,rgba.find_first_of('b') - (rgba.find_first_of('g') +1)).c_str());
newbackground.b = atof(rgba.substr(rgba.find_first_of('b')+1,rgba.find_first_of('a') - (rgba.find_first_of('b') +1)).c_str());
newbackground.a = atof(rgba.substr(rgba.find_first_of('a')+1,10).c_str());
newbackground.r /= 255;
newbackground.g /= 255;
newbackground.b /= 255;
newbackground.a /= 255;
color_manager.background_color(newbackground);
break;
case 'h':
settings.output_height = atoi(optarg);
break;
case 'i':
settings.image_match = true;
image_matcher.imagefile(optarg);
break;
case 'n':
settings.maximum_words = atoi(optarg);
break;
case 'o':
settings.frequency_list_output_file = "freq.txt";
break;
case 'r':
settings.input_type = ip_text_freq;
break;
case 's':
settings.random_seed = atoi(optarg);
srand(settings.random_seed);
break;
case 'u':
settings.shuffle_mark = atof(optarg);
break;
case 'e':
settings.emphasis = atof(optarg);
break;
case 'p':
settings.percent_vertical= atof(optarg);
break;
case 'c':
settings.frequency_spread = atof(optarg);
break;
case 'v':
settings.verbose = true;
settings.quiet = false;
break;
case 'q':
settings.verbose = false;
settings.quiet = true;
break;
case 'w':
settings.output_width = atoi(optarg);
break;
case 'L':
settings.large_word_marker = atof(optarg);
break;
case 'W':
settings.glyph_map_width = atoi(optarg);
break;
case 'H':
settings.glyph_map_height = atoi(optarg);
break;
case 'P':
settings.pre_placed_words_target = atof(optarg);
break;
case 'G':
settings.size_guestimate = atof(optarg);
break;
case 't':
settings.transparent_background = true;
break;
case 'x':
settings.allow_extra_words = true;
break;
case 'V':
display_version_number();
exit(0);
break;
case 'I':
display_version_number();
display_help();
exit(0);
break;
case 'T':
s_optarg = optarg;
std::transform(s_optarg.begin(), s_optarg.end(), s_optarg.begin(),
(int(*)(int)) tolower);
if (!s_optarg.compare("list"))
{
cout << endl << color_manager.color_schemes.size() << " themes available:" << endl << endl;
for (int i = 0; i < color_manager.color_schemes.size(); i++)
{
cout << "\"" << color_manager.color_schemes[i].name << "\"" << endl;
}
cout << endl;
exit(0);
}
else
{
if (!color_manager.set_theme(s_optarg))
{
cout << "Invalid color theme: " << s_optarg << endl;
exit(EXIT_FAILURE);
}
}
break;
case 'F':
s_optarg = optarg;
settings.font = s_optarg;
break;
case '?':
/* getopt_long already printed an error message. */
exit(EXIT_FAILURE);
break;
default:
abort ();
}
}
//deal with non-option arguments
if (optind < argc)
{
while (optind < argc)
{
string cur_parm = string(argv[optind++]);
if (!input_set)
{
settings.input = cur_parm;
input_set = true;
}
else
{
settings.output = cur_parm;
output_set = true;
}
}
}
if (!(input_set && output_set))
{
cout << "Please specify both an input and output file." << endl;
exit(EXIT_FAILURE);
}
}
void ArgParser::display_version_number()
{
cout << "XWall 0.11" << endl;
cout << "Copyright (C) 2011 ErgoThis Limited" << endl << endl;
}
void ArgParser::display_help()
{
cout <<
"usage: xwall [args] input_text_file output_image_file" << endl << endl <<
"arguments:" << endl <<
endl <<
"(examples show the default values)" << endl <<
" -b, --border : size of the border surrounding the image, e.g. -b " << settings.word_border << endl <<
" -B, --wordborder : padding around the words (relative to average word font size), e.g. -B " << settings.word_border_scale << endl <<
" -c, --compression : compress the frequency range to be no more than n e.g. -c " << settings.frequency_spread << endl <<
" -C, --leave_common_words: don't remove common words from the source text" << endl <<
" -d, --detail : the lower this number the better the cloud quality (and the longer it takes) e.g. -d " << settings.position_resolution << endl <<
" -e, --emphasis : how much do words increase in point size with frequency. a value of 1.0 will double the height " << endl <<
" for double the frequency (so, 4x the area), 0.5 will double the area. e.g. -e " << settings.emphasis << endl <<
" -f, --format : file format of output image - SVG, PDF or PNG, e.g. -f PNG" << endl <<
" -F, --font : specify font (use fc-list on command line to list font names - seems to work only " << endl <<
" if the font name has : at the end and no commas) e.g. -F \"American Typewriter\"" << endl <<
" -g, --background : override RGBA values for color theme to specfied values." << endl <<
" (a = alpha, 255 = fully opaque) e.g. -g r255g128b128a255 " << endl <<
" -h, --height : height in pixels of output image including the border, e.g. -h " << settings.output_height << endl <<
" -i, --imagematch : colour the letters of the output words (or letters, see -l) to match a PNG image, e.g. -i photo.png" << endl <<
" -l, --colorletters : make each letter a seperate color - best when combined with with -i." << endl <<
" -n, --number : number of words to include in the image (0=all), e.g. -n " << settings.maximum_words << endl <<
" -o, --output_frequency_list : save a copy of the calculated frequencies in freq.txt e.g. -n" << endl <<
" -p, --percentvertical : percentage of words to make vertical, e.g. -p " << settings.percent_vertical << endl <<
" -r, --frequencylist : signifies the input file is a list of words and frequencies in the format \"word=n\"" << endl <<
" -s, --seed : seed the random number generators with a specific value so image can be recreated with slightly " << endl <<
" different settings (otherwise the seed is taken from current system time) e.g. -s 1" << endl <<
" -t, --transparent : make the image transparent - i.e., no background will be drawn." << endl <<
" -T, --theme : specify or list color_themes. e.g. -T list or -T \"black on white\"" << endl <<
" -u, --shufflemark : specify how many of the first n words (in percentage of total size terms) are shuffled. e.g. -u " << settings.shuffle_mark << endl <<
" -v, --verbose : switch on extended console output." << endl <<
" -q, --quiet : switch off console output completely." << endl <<
" -w, --width : width in pixels of output image including the border, e.g. -w " << settings.output_width << endl <<
" -x, --extrawords : once the specifed number of words are placed fill any remaining space with any words left over." << endl <<
" -L, --large_word_marker : percentage of size range above which words are going to use the in-letter rects, e.g. -L " << settings.large_word_marker << endl <<
" -W, --glyph_map_width : how many horizontal sections to use when working out the actual area of each letter, e.g. -W " << settings.glyph_map_width << endl <<
" -H, --glyph_map_height : how many vertical sections to use when working out the actual area of each letter, e.g. -H " << settings.glyph_map_height << endl <<
" -P, --pre_place_target : what percentage of the words need to be placed before the image needs to expand? " << endl <<
" if this isn't reached - the process will start again (modifying size guestimate by 1%) e.g. -P " << settings.pre_placed_words_target << endl <<
" -G, --size_guestimate : initial guestimate of how big the initial placing area is compared to actual word area." << endl <<
" use this value to get to the pre_place_target quicker e.g. -G " << settings.size_guestimate << endl <<
endl <<
"example:" << endl <<
"xwall -vxl -w1680 -h1050 -n0 --border 25 -i dickens.png -v -x -u 96 two_cities.txt dickens_two_cities.png" << endl <<
endl <<
"contact : info@ergothis.com" << endl <<
endl;
}