summaryrefslogtreecommitdiff
path: root/rufl_test.c
blob: a72017f8c052cb7af5a47102e45dd5c131d8529b (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
135
136
137
138
139
140
141
/*
 * This file is part of RUfl
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license
 * Copyright 2006 James Bursa <james@semichrome.net>
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "rufl.h"


static void try(rufl_code code, const char *context);
static int move_to(os_coord *to, void *user);
static int line_to(os_coord *to, void *user);
static int cubic_to(os_coord *control1, os_coord *control2, os_coord *to,
		void *user);
static void callback(void *context,
		const char *font_name, unsigned int font_size,
		const char *s8, unsigned short *s16, unsigned int n,
		int x, int y);


int main(void)
{
	char utf8_test[] = "Hello,	world! ὕαλον "
			"Uherské Hradiště. 𐀀";
	int width;
	size_t char_offset;
	int x;
	int actual_x;
	struct rufl_decomp_funcs funcs = { move_to, line_to, cubic_to };
	int bbox[4];

	try(rufl_init(), "rufl_init");
	rufl_dump_state();
	try(rufl_paint("NewHall", rufl_WEIGHT_400, 240,
			utf8_test, sizeof utf8_test - 1,
			1200, 1000, 0), "rufl_paint");
	try(rufl_width("NewHall", rufl_WEIGHT_400, 240,
			utf8_test, sizeof utf8_test - 1,
			&width), "rufl_width");
	printf("width: %i\n", width);
	for (x = 0; x < width + 100; x += 100) {
		try(rufl_x_to_offset("NewHall", rufl_WEIGHT_400, 240,
				utf8_test, sizeof utf8_test - 1,
				x, &char_offset, &actual_x),
				"rufl_x_to_offset");
		printf("x to offset: %i -> %i %i \"%s\"\n", x, actual_x,
				char_offset, utf8_test + char_offset);
		try(rufl_split("NewHall", rufl_WEIGHT_400, 240,
				utf8_test, sizeof utf8_test - 1,
				x, &char_offset, &actual_x),
				"rufl_split");
		printf("split: %i -> %i %i \"%s\"\n", x, actual_x,
				char_offset, utf8_test + char_offset);
	}
	try(rufl_decompose_glyph("Homerton", rufl_WEIGHT_400, 1280,
				"A", 1, &funcs, 0),
				"rufl_decompose_glyph");
	try(rufl_paint_callback("NewHall", rufl_WEIGHT_400, 240,
			utf8_test, sizeof utf8_test - 1,
			1200, 1000, callback, 0), "rufl_paint_callback");
	try(rufl_font_bbox("NewHall", rufl_WEIGHT_400, 240, bbox),
			"rufl_font_bbox");
	printf("bbox: %i %i %i %i\n", bbox[0], bbox[1], bbox[2], bbox[3]);
	rufl_quit();

	return 0;
}


void try(rufl_code code, const char *context)
{
	if (code == rufl_OK)
		return;
	else if (code == rufl_OUT_OF_MEMORY)
		printf("error: %s: out of memory\n", context);
	else if (code == rufl_FONT_MANAGER_ERROR)
		printf("error: %s: Font Manager error %x %s\n", context,
				rufl_fm_error->errnum,
				rufl_fm_error->errmess);
	else if (code == rufl_FONT_NOT_FOUND)
		printf("error: %s: font not found\n", context);
	else if (code == rufl_IO_ERROR)
		printf("error: %s: io error: %i %s\n", context, errno,
				strerror(errno));
	else if (code == rufl_IO_EOF)
		printf("error: %s: eof\n", context);
	else
		printf("error: %s: unknown error\n", context);
	rufl_quit();
	exit(1);
}


int move_to(os_coord *to, void *user)
{
	printf("Move to (%d,%d)\n", to->x, to->y);

	return 0;
}


int line_to(os_coord *to, void *user)
{
	printf("Line to (%d,%d)\n", to->x, to->y);

	return 0;
}


int cubic_to(os_coord *control1, os_coord *control2, os_coord *to,
		void *user)
{
	printf("Bezier to (%d,%d),(%d,%d),(%d,%d)\n",
			control1->x, control1->y,
			control2->x, control2->y,
			to->x, to->y);

	return 0;
}


void callback(void *context,
		const char *font_name, unsigned int font_size,
		const char *s8, unsigned short *s16, unsigned int n,
		int x, int y)
{
	printf("callback: \"%s\", %u, ", font_name, font_size);
	if (s8)
		printf("s8 \"%.*s\" ", n, s8);
	else {
		printf("s16 \"");
		for (unsigned int i = 0; i != n; i++)
			printf("%x ", (unsigned int) s16[i]);
		printf("\" ");
	}
	printf("%i %i\n", x, y);
}