toolbox

Check-in [74db56ab65]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:townsign: add scale factor and country road number
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 74db56ab65473c7648dc47bf93bb6e1caedcd0f7d19ba0f6ffdefef01896c224
User & Date: jef 2019-08-23 18:44:30
Context
2019-08-23
18:49
townsign: add check on write() call check-in: bddff63dab user: jef tags: trunk
18:44
townsign: add scale factor and country road number check-in: 74db56ab65 user: jef tags: trunk
2019-08-06
07:33
arte7dump: improve example in man page check-in: 92ffe56f12 user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to doc/townsign.md.

12
13
14
15
16
17
18










19
20
21
22
23
24
25
26
27
*sign.png*

## DESCRIPTION

**townsign**
generates a french town sign in PNG.











## DEPENDENCIES

**townsign**
depends on
magick(1).

## AUTHOR

Gerome Fournier <[jef@foutaise.org](mailto:jef@foutaise.org)>.







>
>
>
>
>
>
>
>
>
>









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
*sign.png*

## DESCRIPTION

**townsign**
generates a french town sign in PNG.

## OPTIONS

**-r** *number*

> Specify country road number.

**-s** *factor*

> Specify scale factor. Default 1.

## DEPENDENCIES

**townsign**
depends on
magick(1).

## AUTHOR

Gerome Fournier <[jef@foutaise.org](mailto:jef@foutaise.org)>.

Changes to src/townsign/townsign.1.

8
9
10
11
12
13
14







15
16
17
18
19
20
.Nm
.Ar town_name
>
.Ar sign.png
.Sh DESCRIPTION
.Nm
generates a french town sign in PNG.







.Sh DEPENDENCIES
.Nm
depends on
.Xr magick 1 .
.Sh AUTHOR
.An Gerome Fournier Aq Mt jef@foutaise.org .







>
>
>
>
>
>
>






8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
.Nm
.Ar town_name
>
.Ar sign.png
.Sh DESCRIPTION
.Nm
generates a french town sign in PNG.
.Sh OPTIONS
.Bl -tag -width indent
.It Fl r Ar number
Specify country road number.
.It Fl s Ar factor
Specify scale factor. Default 1.
.El
.Sh DEPENDENCIES
.Nm
depends on
.Xr magick 1 .
.Sh AUTHOR
.An Gerome Fournier Aq Mt jef@foutaise.org .

Changes to src/townsign/townsign.c.


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
..
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

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include "xmalloc.h"
#include "font.h"
#include "die.h"

#define PROGNAME "townsign"

struct {
	int fontsize;
	int kerning;
	int hpadding;
	int wpadding;
	int borderwidth;
	char *bordercolor;


} prefs = {
	.fontsize = 96,
	.kerning = 10,
	.hpadding = 20,
	.wpadding = 50,
	.borderwidth = 15,
	.bordercolor = "#d40000"
};




















void
usage()
{
	fprintf(stderr,
		"French town sign generator\n"
		"Usage:\n"
		"	%s town_name > sign.png\n"
		"Options:\n"
		"	-h : display this help and exit\n", PROGNAME);


	exit(1);
}











































char *
font_file()
{
	char *filename;
	int fd;

................................................................................
	if (write(fd, font_ttf, font_ttf_len) != font_ttf_len)
		die("write error:");
	close(fd);

	return filename;
}

void






















































































townsign(const char *town)
{
	char *ttffile, *cmdline = NULL;

	if (strchr(town, '"') != NULL)
		die("double quotes not allowed in town name");


	ttffile = font_file();

	xasprintf(&cmdline,
		"magick -font %s -pointsize %d -kerning %d label:\"%s\" png:- "
		"| magick png:- -bordercolor white -border %dx%d png:- "
		"| magick png:- -gravity center -extent \"%%[fx:w/h<2?h*2:w]x\" png:- "
		"| magick png:- -bordercolor \"%s\" -border %d png:- "
		"| magick png:- \\( +clone -alpha extract "
			"\\( -size %dx%d xc:black -draw \"fill white circle %d,%d,%d,0\" -write mpr:arc +delete \\) "
			"\\( mpr:arc \\) -gravity northwest -composite "
			"\\( mpr:arc -flip \\) -gravity southwest -composite "
			"\\( mpr:arc -flop \\) -gravity northeast -composite "
			"\\( mpr:arc -rotate 180 \\) -gravity southeast -composite \\) "
			"-alpha off -compose CopyOpacity -composite png:-",
		ttffile, prefs.fontsize, prefs.kerning, town,
		prefs.wpadding, prefs.hpadding,
		prefs.bordercolor, prefs.borderwidth,
		prefs.borderwidth, prefs.borderwidth, prefs.borderwidth, prefs.borderwidth, prefs.borderwidth);


	if (system(cmdline) == -1) {
		unlink(ttffile);
		die("magick error");
	}

	unlink(ttffile);
	xfree(cmdline);
	xfree(ttffile);
}



























int
main(int argc, char *argv[])
{

	int ch;

	while ((ch = getopt(argc, argv, "h")) != -1) {
		switch (ch) {
		case 'h':
			usage();
			break;






		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		die("wrong number of arguments");

	townsign(argv[0]);
















	return EXIT_SUCCESS;
}
>











|




|
|
>
>
|




|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









|
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|




>

>

|
|
|
|
|





|
|
|
|
|
>
>








|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>


|




>
>
>
>
>
>








|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



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
...
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
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include "xmalloc.h"
#include "font.h"
#include "die.h"

#define PROGNAME "townsign"

struct townprefs {
	int fontsize;
	int kerning;
	int hpadding;
	int wpadding;
	int border_width;
	char *border_color;
};

struct townprefs townprefs = {
	.fontsize = 96,
	.kerning = 10,
	.hpadding = 20,
	.wpadding = 50,
	.border_width = 15,
	.border_color = "#d40000"
};

struct roadprefs {
	int fontsize;
	int interword_spacing;
	int border_width;
	int bottom_margin;
	char *background;
};

struct roadprefs roadprefs = {
	.fontsize = 64,
	.interword_spacing = 10,
	.border_width = 12,
	.bottom_margin = 3,
	.background = "#f7b113"
};

float sflag = 1.0;
int rflag = -1;

void
usage()
{
	fprintf(stderr,
		"French town sign generator\n"
		"Usage:\n"
		"	%s town_name > sign.png\n"
		"Options:\n"
		"	-h          : display this help and exit\n"
		"	-r <number> : country road number\n"
		"	-s <factor> : scale factor\n", PROGNAME);
	exit(1);
}

float
parse_scale(const char *str)
{
	int nparsed;
	float scale;

	if (sscanf(str, "%f%n", &scale, &nparsed) != 1
			||(size_t) nparsed != strlen(str)
			|| scale <= 0)
		die("invalid scale value '%s'", str);

	return scale;
}

int
parse_road_number(const char *str)
{
	int nparsed, road_number;

	if (sscanf(str, "%d%n", &road_number, &nparsed) != 1
			||(size_t) nparsed != strlen(str)
			|| road_number <= 0)
		die("invalid road value '%s'", str);

	return road_number;
}

void
cat_file(const char *file)
{
	int fd;
	char buff[4096];
	ssize_t nread;

	fd = open(file, O_RDONLY);
	if (fd == -1)
		die("catfile error");
	while ((nread = read(fd, buff, sizeof(buff))) > 0)
		write(1, buff, nread);
	close(fd);
}

char *
font_file()
{
	char *filename;
	int fd;

................................................................................
	if (write(fd, font_ttf, font_ttf_len) != font_ttf_len)
		die("write error:");
	close(fd);

	return filename;
}


char *
tmp_file()
{
	char *tmpfile;
	int fd;

	tmpfile = xstrdup("/tmp/townsign.XXXXXX");
	fd = mkstemp(tmpfile);
	if (fd == -1)
		die("unable to create temp file:");
	close(fd);

	return tmpfile;
}

/*
 * Road sign routines
 */

struct roadprefs
roadprefs_scale(struct roadprefs prefs, float scale)
{
	prefs.fontsize *= scale;
	prefs.interword_spacing *= scale;
	prefs.border_width *= scale;
	prefs.bottom_margin *= scale;

	return prefs;
}

char *
road_sign(int road)
{
	char *ttffile, *roadfile, *cmdline = NULL;

	roadfile = tmp_file();
	ttffile = font_file();
	roadprefs = roadprefs_scale(roadprefs, sflag);
	xasprintf(&cmdline,
		"magick -background \"%s\" -font %s -pointsize %d "
		"-interword-spacing %d label:\"D %d\" -trim "
		"-bordercolor \"%s\" -border %d "
		"\\( +clone -alpha extract "
		"\\( -size %dx%d xc:black -draw \"fill white circle %d,%d,%d,0\" -write mpr:arc +delete \\) "
		"\\( mpr:arc \\) -gravity northwest -composite "
		"\\( mpr:arc -flip \\) -gravity southwest -composite "
		"\\( mpr:arc -flop \\) -gravity northeast -composite "
		"\\( mpr:arc -rotate 180 \\) -gravity southeast -composite \\) "
		"-alpha off -compose CopyOpacity -composite "
		"-background none -gravity south -splice 0x%d png:%s",
		roadprefs.background, ttffile, roadprefs.fontsize,
		roadprefs.interword_spacing, road,
		roadprefs.background, roadprefs.border_width,
		roadprefs.border_width, roadprefs.border_width,
		roadprefs.border_width, roadprefs.border_width,
		roadprefs.border_width, roadprefs.bottom_margin,
		roadfile);
	if (system(cmdline) == -1) {
		unlink(ttffile);
		die("magick error");
	}

	unlink(ttffile);
	xfree(cmdline);
	xfree(ttffile);

	return roadfile;
}

/*
 * Town sign routines
 */

struct townprefs
prefs_scale(struct townprefs prefs, float scale)
{
	prefs.fontsize *= scale;
	prefs.kerning *= scale;
	prefs.hpadding *= scale;
	prefs.wpadding *= scale;
	prefs.border_width *= scale;

	return prefs;
}

char *
town_sign(const char *town)
{
	char *ttffile, *townfile, *cmdline = NULL;

	if (strchr(town, '"') != NULL)
		die("double quotes not allowed in town name");

	townfile = tmp_file();
	ttffile = font_file();
	townprefs = prefs_scale(townprefs, sflag);
	xasprintf(&cmdline,
		"magick -font %s -pointsize %d -kerning %d label:\"%s\" "
		"-bordercolor white -border %dx%d "
		"-gravity center -extent \"%%[fx:w/h<2?h*2:w]x\" "
		"-bordercolor \"%s\" -border %d "
		"\\( +clone -alpha extract "
			"\\( -size %dx%d xc:black -draw \"fill white circle %d,%d,%d,0\" -write mpr:arc +delete \\) "
			"\\( mpr:arc \\) -gravity northwest -composite "
			"\\( mpr:arc -flip \\) -gravity southwest -composite "
			"\\( mpr:arc -flop \\) -gravity northeast -composite "
			"\\( mpr:arc -rotate 180 \\) -gravity southeast -composite \\) "
			"-alpha off -compose CopyOpacity -composite png:%s",
		ttffile, townprefs.fontsize, townprefs.kerning, town,
		townprefs.wpadding, townprefs.hpadding,
		townprefs.border_color, townprefs.border_width,
		townprefs.border_width, townprefs.border_width,
		townprefs.border_width, townprefs.border_width,
		townprefs.border_width, townfile);
	if (system(cmdline) == -1) {
		unlink(ttffile);
		die("magick error");
	}

	unlink(ttffile);
	xfree(cmdline);
	xfree(ttffile);

	return townfile;
}

/*
 * Road & Town sign routines
 */

char *
road_town_sign(const char *road_file, const char *town_file)
{
	char *tmp, *cmdline = NULL;

	tmp = tmp_file();
	xasprintf(&cmdline,
		"magick convert -background none %s %s -gravity center -append png:%s",
		road_file, town_file, tmp);
	if (system(cmdline) == -1)
		die("magick error");
	xfree(cmdline);

	return tmp;
}

/*
 * Main
 */

int
main(int argc, char *argv[])
{
	char *rfile, *tfile, *rtfile;
	int ch;

	while ((ch = getopt(argc, argv, "hr:s:")) != -1) {
		switch (ch) {
		case 'h':
			usage();
			break;
		case 'r':
			rflag = parse_road_number(optarg);
			break;
		case 's':
			sflag = parse_scale(optarg);
			break;
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		die("wrong number of arguments");

	tfile = town_sign(argv[0]);

	if (rflag > 0) {
		rfile = road_sign(rflag);
		rtfile = road_town_sign(rfile, tfile);
		cat_file(rtfile);
		unlink(rfile);
		unlink(rtfile);
		xfree(rfile);
		xfree(rtfile);
		return EXIT_SUCCESS;
	}

	cat_file(tfile);
	unlink(tfile);
	xfree(tfile);

	return EXIT_SUCCESS;
}