summaryrefslogtreecommitdiff
path: root/fitz/res_text.c
blob: 5564bb22284cc6fe4507cf660e6b54f1f0250bf9 (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
#include "fitz.h"

fz_text *
fz_newtext(fz_font *font)
{
	fz_text *text;

	text = fz_malloc(sizeof(fz_text));
	text->font = fz_keepfont(font);
	text->trm = fz_identity();
	text->len = 0;
	text->cap = 0;
	text->els = nil;

	return text;
}

void
fz_freetext(fz_text *text)
{
	fz_dropfont(text->font);
	fz_free(text->els);
	fz_free(text);
}

fz_text *
fz_clonetext(fz_text *old)
{
	fz_text *text;

	text = fz_malloc(sizeof(fz_text));
	text->font = fz_keepfont(old->font);
	text->trm = old->trm;
	text->len = old->len;
	text->cap = text->len;
	text->els = fz_malloc(text->len * sizeof(fz_textel));
	memcpy(text->els, old->els, text->len * sizeof(fz_textel));

	return text;
}

fz_rect
fz_boundtext(fz_text *text, fz_matrix ctm)
{
	fz_matrix trm;
	fz_rect bbox;
	fz_rect fbox;
	int i;

	if (text->len == 0)
		return fz_emptyrect;

	/* find bbox of glyph origins in ctm space */

	bbox.x0 = bbox.x1 = text->els[0].x;
	bbox.y0 = bbox.y1 = text->els[0].y;

	for (i = 1; i < text->len; i++)
	{
		bbox.x0 = MIN(bbox.x0, text->els[i].x);
		bbox.y0 = MIN(bbox.y0, text->els[i].y);
		bbox.x1 = MAX(bbox.x1, text->els[i].x);
		bbox.y1 = MAX(bbox.y1, text->els[i].y);
	}

	bbox = fz_transformrect(ctm, bbox);

	/* find bbox of font in trm * ctm space */

	trm = fz_concat(text->trm, ctm);
	trm.e = 0;
	trm.f = 0;

	fbox.x0 = text->font->bbox.x0 * 0.001;
	fbox.y0 = text->font->bbox.y0 * 0.001;
	fbox.x1 = text->font->bbox.x1 * 0.001;
	fbox.y1 = text->font->bbox.y1 * 0.001;

	fbox = fz_transformrect(trm, fbox);

	/* expand glyph origin bbox by font bbox */

	bbox.x0 += fbox.x0;
	bbox.y0 += fbox.y0;
	bbox.x1 += fbox.x1;
	bbox.y1 += fbox.y1;

	return bbox;
}

static void
fz_growtext(fz_text *text, int n)
{
	if (text->len + n < text->cap)
		return;
	while (text->len + n > text->cap)
		text->cap = text->cap + 36;
	text->els = fz_realloc(text->els, sizeof (fz_textel) * text->cap);
}

void
fz_addtext(fz_text *text, int gid, int ucs, float x, float y)
{
	fz_growtext(text, 1);
	text->els[text->len].ucs = ucs;
	text->els[text->len].gid = gid;
	text->els[text->len].x = x;
	text->els[text->len].y = y;
	text->len++;
}

void fz_debugtext(fz_text *text, int indent)
{
	int i, n;
	for (i = 0; i < text->len; i++)
	{
		for (n = 0; n < indent; n++)
			putchar(' ');
		if (text->els[i].ucs >= 32 && text->els[i].ucs < 128)
			printf("<g ucs=\"%c\" gid=\"%d\" x=\"%g\" y=\"%g\" />\n",
				text->els[i].ucs, text->els[i].gid, text->els[i].x, text->els[i].y);
		else
			printf("<g ucs=\"U+%04X\" gid=\"%d\" x=\"%g\" y=\"%g\" />\n",
				text->els[i].ucs, text->els[i].gid, text->els[i].x, text->els[i].y);
	}
}