aboutsummaryrefslogtreecommitdiffstats
path: root/libparsepcf_get_metrics.c
blob: 8994405d62d52fafba33bad9d606661c78d22a75 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


#define PARSE_INT8(TEXT) ((int16_t)((int)*(const uint8_t *)(TEXT) - 128))


int
libparsepcf_get_metrics(const void *file, size_t size,
                        const struct libparsepcf_table *table,
                        struct libparsepcf_metrics *metrics,
                        size_t first, size_t count)
{
	const char *text = file;
	size_t pos, i;
	int msb = table->format & LIBPARSEPCF_BYTE;
	int compressed = table->format & LIBPARSEPCF_COMPRESSED_METRICS;

	(void) size;

	pos = table->offset + (compressed ? 6 : 8);
	pos += first * (compressed ? 5 : 12);

	if (compressed) {
		for (i = 0; i < count; i++, pos += 5) {
			metrics[i].left_side_bearing  = PARSE_INT8(&text[pos + 0]);
			metrics[i].right_side_bearing = PARSE_INT8(&text[pos + 1]);
			metrics[i].character_width    = PARSE_INT8(&text[pos + 2]);
			metrics[i].character_ascent   = PARSE_INT8(&text[pos + 3]);
			metrics[i].character_descent  = PARSE_INT8(&text[pos + 4]);
			metrics[i].character_attributes = 0;
		}
	} else {
		for (i = 0; i < count; i++, pos += 12) {
			metrics[i].left_side_bearing    = PARSE_INT16(&text[pos + 0], msb);
			metrics[i].right_side_bearing   = PARSE_INT16(&text[pos + 2], msb);
			metrics[i].character_width      = PARSE_INT16(&text[pos + 4], msb);
			metrics[i].character_ascent     = PARSE_INT16(&text[pos + 6], msb);
			metrics[i].character_descent    = PARSE_INT16(&text[pos + 8], msb);
			metrics[i].character_attributes = PARSE_UINT16(&text[pos + 10], msb);
		}
	}

	for (i = 0; i < count; i++) {
		if (metrics[i].left_side_bearing > metrics[i].right_side_bearing ||
		    (int32_t)metrics[i].character_ascent < -(int32_t)metrics[i].character_descent)
			goto ebfont;
	}

	return 0;

ebfont:
	errno = EBFONT;
	return -1;
}