File: bdfresize.c

package info (click to toggle)
bdfresize 1.5-11
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 316 kB
  • sloc: ansic: 1,594; sh: 330; makefile: 14
file content (474 lines) | stat: -rw-r--r-- 10,585 bytes parent folder | download | duplicates (8)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 * $Id: bdfresize.c,v 1.5 2000/12/12 11:18:14 kazuhiko Exp $
 *
 * Copyright 1988, 1992 Hiroto Kagotani
 * Copyright 2000 Kazuhiko
 * Copyright 2000 Masao Uebayashi
 *
 * 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, 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.
 */ 

#include	<stdio.h>
#include	<stdlib.h>
#include	<ctype.h>
#include	<string.h>
#include	"def.h"
#include	"getopt.h"

int main(int argc, char *argv[]);
static int	processHeader(void);
static void	processFooter(void);
static void	processProperties(void);
static void	wresize(char *prop);
static void	hresize(char *prop);
static void	yresize(char *prop);
static void	startfont(void);
static void	font(void);
static void	resize_xlfd(char *xlfd);
static int	isnum(char *p);
static void	size(void);
static void	fontboundingbox(void);
static int	chars(void);
static void	endfont(void);
static void	usage(void);
static void	version(void);

int	numerator_x = 1, denominator_x = 1;
int	numerator_y = 1, denominator_y = 1;
int	blackness = 4;
char	linebuf[BUFSIZE];

static int	line;

int
main(int argc, char *argv[])
{
  int	c, err = 0;
  int	charcount;
  while ((c = getopt(argc, argv, "vb:w:h:f:")) != EOF) {
    switch (c)  {
    case 'v':
      version();
      exit(0);
      break;
    case 'b':
      blackness = atoi(optarg);
      if (blackness <= 0) err ++;
      break;
    case 'w':
      numerator_x = atoi(optarg);
      denominator_x = strchr(optarg,'/')
	? atoi(strchr(optarg,'/')+1) : 1;
      if (numerator_x <= 0 || denominator_x <= 0) err ++;
      break;
    case 'h':
      numerator_y = atoi(optarg);
      denominator_y = strchr(optarg,'/')
	? atoi(strchr(optarg,'/')+1) : 1;
      if (numerator_y <= 0 || denominator_y <= 0) err ++;
      break;
    case 'f':
      numerator_x = numerator_y = atoi(optarg);
      denominator_x = denominator_y = strchr(optarg,'/')
	? atoi(strchr(optarg,'/')+1) : 1;
      if (numerator_x <= 0 || denominator_x <= 0) err ++;
      break;
    case '?':
      err ++;
      break;
    }
  }
  if (err) {
    version();
    usage();
    exit(1);
  }
  if (optind < argc && freopen(argv[optind], "r", stdin) == NULL) {
    perror(argv[optind]);
    exit(1);
  }
  charcount = processHeader();
  for (; charcount > 0; charcount --) {
    processChar();
  }
  processFooter();
  exit(0);
  /* NOTREACHED */
}

static int
processHeader(void)
{
  startfont();
  font();
  size();
  fontboundingbox();
  processProperties();
  return chars();
}

static void
processFooter(void)
{
  endfont();
}

static void
processProperties(void)
{
  int	arg;

  get_line();
  if (beginwith(linebuf, "STARTPROPERTIES")) { /* optional */
    if (sscanf(linebuf, "STARTPROPERTIES %d", &arg) != 1
	|| arg < 0) {
      error("STARTPROPERTIES has illegal or no arguments\n");
    }
    put_line();
    for (; arg > 0; arg --) {
      get_line();
      if (beginwith(linebuf, "FONT_ASCENT")) {
	hresize("FONT_ASCENT");
      } else if (beginwith(linebuf, "FONT_DESCENT")) {
	yresize("FONT_DESCENT");
      } else if (beginwith(linebuf, "PIXEL_SIZE")) {
	hresize("PIXEL_SIZE");
      } else if (beginwith(linebuf, "POINT_SIZE")) {
	hresize("POINT_SIZE");
      } else if (beginwith(linebuf, "AVERAGE_WIDTH")) {
	wresize("AVERAGE_WIDTH");
      }
      put_line();
    }
    get_line();
    if (!beginwith(linebuf, "ENDPROPERTIES")) {
      error("ENDPROPERTIES expected\n");
    }
    put_line();
    get_line();
  }
}

static void
wresize(char *prop)
{
  int	arg;
  char	form[100];
  char	errbuf[100];
  sprintf(form, "%s %%d", prop);
  if (sscanf(linebuf, form, &arg) != 1) {
    sprintf(errbuf, "%s has illegal or no arguments\n", prop);
    error(errbuf);
  }
  sprintf(linebuf, "%s %d", prop, WRESIZE(arg));
}

static void
hresize(char *prop)
{
  int	arg;
  char	form[100];
  char	errbuf[100];
  sprintf(form, "%s %%d", prop);
  if (sscanf(linebuf, form, &arg) != 1) {
    sprintf(errbuf, "%s has illegal or no arguments\n", prop);
    error(errbuf);
  }
  sprintf(linebuf, "%s %d", prop, HRESIZE(arg));
}

static void
yresize(char *prop)
{
  int	arg;
  char	form[100];
  char	errbuf[100];
  sprintf(form, "%s %%d", prop);
  if (sscanf(linebuf, form, &arg) != 1) {
    sprintf(errbuf, "%s has illegal or no arguments\n", prop);
    error(errbuf);
  }
  sprintf(linebuf, "%s %d", prop, YRESIZE(arg));
}

static void
startfont(void)
{
  char	arg[BUFSIZE];

  get_line();
  if (!beginwith(linebuf, "STARTFONT")) {
    error("STARTFONT expected\n");
  }
  if (sscanf(linebuf, "STARTFONT %s", arg) != 1) {
    error("STARTFONT has illegal or no arguments\n");
  }
  if (strcmp(arg, "2.1")) {
    error("STARTFONT version error\n");
  }
  put_line();
  sprintf(linebuf,
	  "COMMENT This font was automatically resized by bdfresize-%s",
	  VERSION);
  put_line();
  sprintf(linebuf,
	  "COMMENT   mag_x = %d/%d, mag_y = %d/%d, blackness = %d",
	  numerator_x, denominator_x, numerator_y, denominator_y, blackness);
  put_line();
}

static void
font(void)
{
  char *cp;
  get_line();
  if (!beginwith(linebuf, "FONT")) {
    error("FONT expected\n");
  }
  cp = linebuf + 4;
  while (*cp && isspace((int)*cp)) cp++;
  if (!*cp) {
    error("FONT has no arguments\n");
  }
  if (*cp == '+' || *cp == '-') { /* xlfd */
    resize_xlfd(cp);
  }
  put_line();
}

static void
resize_xlfd(char *xlfd)
{
  char	buf[BUFSIZE];
  char	*p, *q;

  buf[0] = '\0';

  if (!(q = strchr(p = xlfd, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* XFontNameRegistry */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* FOUNDRY */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* FAMILY_NAME */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* WEIGHT_NAME */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* SLANT */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* SETWIDTH_NAME */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* ADD_STYLE_NAME */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  if (!isnum(p)) { fputs(p, stdout); putchar('-'); goto parseerror; }
  sprintf(buf + strlen(buf), "%d", HRESIZE(atoi(p))); strcat(buf, "-");
  /* PIXEL_SIZE */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  if (!isnum(p)) { fputs(p, stdout); putchar('-'); goto parseerror; }
  sprintf(buf + strlen(buf), "%d", HRESIZE(atoi(p))); strcat(buf, "-");
  /* POINT_SIZE */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* RESOLUTION_X */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* RESOLUTION_Y */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* SPACING */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  if (!isnum(p)) { fputs(p, stdout); putchar('-'); goto parseerror; }
  sprintf(buf + strlen(buf), "%d", WRESIZE(atoi(p))); strcat(buf, "-");
  /* AVERAGE_WIDTH */

  if (!(q = strchr(p = q + 1, '-'))) goto parseerror;
  *q = '\0';
  strcat(buf, p); strcat(buf, "-");
  /* CHARSET_REGISTRY */

  if ((q = strchr(p = q + 1, '-'))) goto parseerror;
  strcat(buf, p);
  /* CHARSET_ENCODING */

  strcpy(xlfd, buf);
  return;

 parseerror:
  fprintf(stderr, "Cannot parse XLFD '%s'\n", xlfd);
  strcat(buf, p);			/* rest */

  strcpy(xlfd, buf);
  return;
}

static int
isnum(char *p)
{
  if (!*p) return 0;
  while (*p && isdigit((int)*p)) {
    p ++;
  }
  if (*p) return 0;

  return 1;
}

static void
size(void)
{
  int	arg1, arg2, arg3;
  int	newsize;

  get_line();
  if (!beginwith(linebuf, "SIZE")) {
    error("SIZE expected\n");
  }
  if (sscanf(linebuf, "SIZE %d %d %d", &arg1, &arg2, &arg3) != 3
      || arg1 <= 0 || arg2 <= 0 || arg3 <= 0) {
    error("SIZE has illegal arguments\n");
  }
  newsize = HRESIZE(arg1);
  sprintf(linebuf, "SIZE %d %d %d", newsize, arg2, arg3);
  put_line();
}

static void
fontboundingbox(void)
{
  int	arg1, arg2, arg3, arg4;
  int	new1, new2, new3, new4;

  get_line();
  if (!beginwith(linebuf, "FONTBOUNDINGBOX")) {
    error("FONTBOUNDINGBOX expected\n");
  }
  if (sscanf(linebuf, "FONTBOUNDINGBOX %d %d %d %d",
	     &arg1, &arg2, &arg3, &arg4) != 4
      || arg1 <= 0 || arg2 <= 0) {
    error("FONTBOUNDINGBOX has illegal arguments\n");
  }
  new1 = WRESIZE(arg1);
  new2 = HRESIZE(arg2);
  new3 = XRESIZE(arg3);
  new4 = YRESIZE(arg4);
  sprintf(linebuf, "FONTBOUNDINGBOX %d %d %d %d", new1,new2,new3,new4);
  put_line();
}

static int
chars(void)
{
  int	arg;

  if (!beginwith(linebuf, "CHARS")) {
    error("CHARS expected\n");
  }
  if (sscanf(linebuf, "CHARS %d", &arg) != 1 || arg < 0) {
    error("CHARS has illegal or no arguments\n");
  }
  put_line();
  return arg;
}

static void
endfont(void)
{
  get_line();
  if (!beginwith(linebuf, "ENDFONT")) {
    error("ENDFONT expected\n");
  }
  put_line();
}

void
get_line(void)
{
  while (fgets(linebuf, sizeof(linebuf), stdin)) {
    line ++;
    linebuf[strlen(linebuf)-1] = '\0';
    if (linebuf[0] == '\0' || beginwith(linebuf, "COMMENT")) {
      put_line();
    } else {
      return;
    }
  }
  error("EOF detected\n");
}

void
put_line(void)
{
  puts(linebuf);
}

int
beginwith(char *l, char *s)
{
  return !strncmp(l, s, strlen(s));
}

void error(char *fmt)
{
  char	buf[100];
  strcat(buf, "bdfresize: line %d: ");
  strcat(buf, fmt);
  fprintf(stderr, buf, line);
  exit(1);
}

static void
usage(void)
{
  fprintf(stderr, "usage: bdfresize [-options] [bdffile]\n");
  fprintf(stderr, "  -v : show version\n");
  fprintf(stderr, "  -b <number> : blackness (default = 4)\n");
  fprintf(stderr, "  -f <factor> : \n");
  fprintf(stderr, "  -w <factor> : (width only)\n");
  fprintf(stderr, "  -h <factor> : (height only)\n");
  fprintf(stderr, "    (<factor> ::= digits | digits/digits)\n");
}

static void
version(void)
{
  fprintf(stderr, "bdfresize: version %s\n", VERSION);
}