summaryrefslogtreecommitdiff
path: root/src/cli.c
blob: 1002a94e693cb81c5a8cd8d6c62f8fd1b347223c (plain)
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
#include <stdio.h>
#include <stdlib.h>

#include <sys/stat.h>

#include "encoding.h"
#include "fm.h"
#include "ft.h"
#include "glyph.h"
#include "glyphs.h"
#include "intmetrics.h"
#include "outlines.h"
#include "utils.h"

static void progress(int value)
{
	UNUSED(value);
}

void ttf2f_poll(int active)
{
	UNUSED(active);
}

int main(int argc, char **argv)
{
	int fail;
	ttf2f_result err = TTF2F_RESULT_OK;
	int nglyphs;
	struct glyph *glist;
	struct font_metrics *metrics;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s <input.ttf> <output>\n", argv[0]);
		return 1;
	}

	ft_init();
	load_glyph_list();

	fail = open_font(argv[1]);
	if (fail) {
		fprintf(stderr, "ERROR: Failed opening font %s\n", argv[1]);
		return 1;
	}

	nglyphs = count_glyphs();

	glist = calloc(nglyphs, sizeof(struct glyph));
	if (glist == NULL) {
		fprintf(stderr, "ERROR: insufficient memory for glyphs\n");
		return 1;
	}

	for (int i = 0; i != nglyphs; i++) {
		struct glyph *g = &glist[i];

		g->code = -1;
	}

	metrics = calloc(1, sizeof(struct font_metrics));
	if (metrics == NULL) {
		fprintf(stderr, "ERROR: insufficient memory for font metrics\n");
		return 1;
	}

	fail = fnmetrics(metrics);
	if (fail) {
		fprintf(stderr, "ERROR: failed reading font metrics\n");
		return 1;
	}

	fail = glenc(glist);
	if (fail) {
		fprintf(stderr, "ERROR: failed reading glyph encoding\n");
		return 1;
	}

	fail = glnames(glist);
	if (fail) {
		fprintf(stderr, "ERROR: failed reading glyph names\n");
		return 1;
	}

	glmetrics(glist, progress);

	mkdir(argv[2], 0755);

	if ((err = write_intmetrics(argv[2], argv[2], glist, nglyphs,
		metrics, progress)) != TTF2F_RESULT_OK) goto error_out;

	if ((err = write_outlines(argv[2], argv[2], glist, nglyphs,
		metrics, progress)) != TTF2F_RESULT_OK) goto error_out;

	if ((err = encoding_write(argv[2], argv[2], glist, nglyphs,
		0, progress)) != TTF2F_RESULT_OK) goto error_out;

error_out:
	if (err != TTF2F_RESULT_OK) {
		switch (err) {
		case TTF2F_RESULT_NOMEM:
			fprintf(stderr, "ERROR: failed to allocate memory\n");
			break;
		case TTF2F_RESULT_OPEN:
			fprintf(stderr, "ERROR: failed to open output file\n");
			break;
		case TTF2F_RESULT_WRITE:
			fprintf(stderr, "ERROR: failed to write to file\n");
			break;

		case TTF2F_RESULT_OK:
			/* keeps gcc quiet, even though this will never occur.
			 * we avoid default: so we get a warning is we add more
			 */
			break;
		}
	}

	free(metrics);
	free(glist);

	close_font();

	ft_fini();
	destroy_glyphs();

	exit(err == TTF2F_RESULT_OK ? EXIT_SUCCESS : EXIT_FAILURE);
}