summaryrefslogtreecommitdiff
path: root/render/rendertext.c
blob: 6819ccd85a90de5c1f7c9b9af3023969666aaba0 (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
#include <fitz.h>

static void blitalphaglyph(fz_pixmap *out, fz_glyph *gl, int xo, int yo)
{
	int sx, sy, dx, dy, a, b, c;

	for (sy = 0; sy < gl->h; sy++)
	{
		for (sx = 0; sx < gl->w; sx++)
		{
			dx = xo + sx + gl->lsb - out->x;
			dy = yo - sy + gl->top - out->y;

			if (dx < 0) continue;
			if (dy < 0) continue;
			if (dx >= out->w) continue;
			if (dy >= out->h) continue;

			a = gl->bitmap[sx + sy * gl->w];
			b = out->samples[dx + dy * out->w];
			c = a + fz_mul255(b, 255 - a);
			out->samples[dx + dy * out->w] = c;
		}
	}
}

static void blitcolorglyph(fz_pixmap *out, fz_glyph *gl, int xo, int yo, fz_renderer *gc)
{
	int sx, sy, dx, dy, sa, ssa;
	unsigned char *p;

	for (sy = 0; sy < gl->h; sy++)
	{
		for (sx = 0; sx < gl->w; sx++)
		{
			dy = yo - sy + gl->top - out->y;
			if (dy < 0) continue;
			if (dy >= out->h) break;

			dx = xo + sx + gl->lsb - out->x;
			if (dx < 0) continue;
			if (dx >= out->w) break;

			sa = gl->bitmap[sx + sy * gl->w];
			ssa = 255 - sa;

			p = out->samples + dx * 4 + dy * out->w * out->n;
			p[0] = sa + fz_mul255(ssa, p[0]);
			p[1] = fz_mul255(gc->r, sa) + fz_mul255(p[1], ssa);
			p[2] = fz_mul255(gc->g, sa) + fz_mul255(p[2], ssa);
			p[3] = fz_mul255(gc->b, sa) + fz_mul255(p[3], ssa);
		}
	}
}

fz_error *
fz_rendertext(fz_renderer *gc, fz_textnode *text, fz_matrix ctm)
{
	fz_error *error;
	fz_glyph gl;
	float x, y;
	int g, i, ix, iy;
	fz_matrix tm, trm;

	error = fz_newpixmap(&gc->tmp, gc->x, gc->y, gc->w, gc->h, 1);
	if (error)
		return error;

	tm = text->trm;

	for (i = 0; i < text->len; i++)
	{
		g = text->els[i].cid;
		x = text->els[i].x;
		y = text->els[i].y;

		tm.e = x;
		tm.f = y;
		trm = fz_concat(tm, ctm);

		ix = fz_floor(trm.e);
		iy = fz_floor(trm.f);

		trm.e = (trm.e - fz_floor(trm.e));
		trm.f = (trm.f - fz_floor(trm.f));

		error = fz_renderglyph(gc->cache, &gl, text->font, g, trm);
		if (error)
			return error;

		blitalphaglyph(gc->tmp, &gl, ix, iy);
	}

	return nil;
}

fz_error *
fz_rendercolortext(fz_renderer *gc, fz_textnode *text, fz_colornode *color, fz_matrix ctm)
{
	fz_error *error;
	fz_glyph gl;
	float x, y;
	int g, i, ix, iy;
	fz_matrix tm, trm;
	float rgb[3];

	fz_convertcolor(color->cs, color->samples, gc->model, rgb);
	gc->r = rgb[0] * 255;
	gc->g = rgb[1] * 255;
	gc->b = rgb[2] * 255;

	tm = text->trm;

	for (i = 0; i < text->len; i++)
	{
		g = text->els[i].cid;
		x = text->els[i].x;
		y = text->els[i].y;

		tm.e = x;
		tm.f = y;
		trm = fz_concat(tm, ctm);

		ix = fz_floor(trm.e);
		iy = fz_floor(trm.f);

		trm.e = (trm.e - fz_floor(trm.e));
		trm.f = (trm.f - fz_floor(trm.f));

		error = fz_renderglyph(gc->cache, &gl, text->font, g, trm);
		if (error)
			return error;

		blitcolorglyph(gc->acc, &gl, ix, iy, gc);
	}

	return nil;
}