summaryrefslogtreecommitdiff
path: root/src/cli.c
blob: 5ec85ff6ddf3204b71af232ee3b22a2eaba20166 (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
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/stat.h>

#include "context.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)
{
	ttf2f_ctx ctx;
	int fail;
	ttf2f_result err = TTF2F_RESULT_OK;

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

	memset(&ctx, 0, sizeof(ctx));

	ft_init();

	if ((err = glyph_load_list()) != TTF2F_RESULT_OK)
		goto error_out;

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

	ctx.nglyphs = count_glyphs();

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

	for (size_t i = 0; i != ctx.nglyphs; i++) {
		struct glyph *g = &ctx.glyphs[i];

		g->code = -1;
	}

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

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

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

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

	glmetrics(ctx.glyphs, progress);

	mkdir(argv[2], 0755);

	if ((err = intmetrics_write(argv[2], argv[2], ctx.glyphs, ctx.nglyphs,
		ctx.metrics, progress)) != TTF2F_RESULT_OK) goto error_out;

	if ((err = outlines_write(argv[2], argv[2], ctx.glyphs, ctx.nglyphs,
		ctx.metrics, progress)) != TTF2F_RESULT_OK) goto error_out;

	if ((err = encoding_write(argv[2], argv[2], ctx.glyphs, ctx.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(ctx.metrics);
	free(ctx.glyphs);

	close_font();

	ft_fini();
	glyph_destroy_list();

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