File: rdfontfile.c

package info (click to toggle)
wily 0.13.41-7.3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,560 kB
  • ctags: 3,426
  • sloc: ansic: 25,364; perl: 580; makefile: 448; sh: 415; python: 30; exp: 17
file content (135 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download | duplicates (12)
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
#include <u.h>
#include <libc.h>
#include <libg.h>
#include "libgint.h"
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

static char*
skip(char *s)
{
	while (*s==' ' || *s=='\n' || *s=='\t')
		s++;
	return s;
}

Font *
rdfontfile(char *name, int ldepth)
{
	Font *fnt;
	Cachesubf *c;
	int fd, i;
	char *buf, *s, *t;
	struct stat sbuf;
	unsigned long min, max;

	fd = open(name, O_RDONLY);
	if (fd < 0)
		return 0;
	if (fstat(fd, &sbuf) < 0)
	{
    Err0:
		close(fd);
		return 0;
	}
	buf = (char *)malloc(sbuf.st_size+1);
	if (buf == 0)
		goto Err0;
	buf[sbuf.st_size] = 0;
	i = read(fd, buf, sbuf.st_size);
	close(fd);
	if (i != sbuf.st_size)
	{
    Err1:
		free(buf);
		return 0;
	}

	s = buf;
	fnt = (Font *)malloc(sizeof(Font));
	if (fnt == 0)
		goto Err1;
	memset((void*)fnt, 0, sizeof(Font));
	fnt->name = (char *)malloc(strlen(name)+1);
	if (fnt->name==0)
	{
    Err2:
		free(fnt->name);
		free(fnt);
		goto Err1;
	}
	strcpy(fnt->name, name);
	fnt->height = strtol(s, &s, 0);
	s = skip(s);
	fnt->ascent = strtol(s, &s, 0);
	s = skip(s);
	if (fnt->height<=0 || fnt->ascent<=0)
		goto Err2;
	fnt->width = 0;
	fnt->ldepth = ldepth;

	fnt->id = 0;
	fnt->nsubf = 0;
	fnt->subf = 0;

	do {
		min = strtol(s, &s, 0);
		s = skip(s);
		max = strtol(s, &s, 0);
		s = skip(s);
		if(*s==0 || min>=65536 || max>=65536 || min>max)
		{
    Err3:
			ffree(fnt);
			return 0;
		}
		if (fnt->subf)
			fnt->subf = (Cachesubf *)realloc(fnt->subf, (fnt->nsubf+1)*sizeof(Cachesubf));
		else
			fnt->subf = (Cachesubf *)malloc(sizeof(Cachesubf));
		if (fnt->subf == 0)
		{
			/* realloc manual says fnt->subf may have been destroyed */
			fnt->nsubf = 0;
			goto Err3;
		}
		c = &fnt->subf[fnt->nsubf];
		c->min = min;
		c->max = max;
		t = s;
		while (*s && *s!=' ' && *s!='\n' && *s!='\t')
			s++;
		*s++ = 0;
		c->f = (Subfont *)0;
		c->name = (char *)malloc(strlen(t)+1);
		if (c->name == 0)
		{
			free(c);
			goto Err3;
		}
		strcpy(c->name, t);
		s = skip(s);
		fnt->nsubf++;
	} while(*s);
	free(buf);
	return fnt;
}

void
ffree(Font *f)
{
	int i;
	Cachesubf *c;
	unsigned char *b;

	for (i=0; i<f->nsubf; i++){
		c = f->subf+i;
		if (c->f)
			subffree(c->f);
		free(c->name);
		free(c);
	}
	free(f->subf);
	free(f);
}