summaryrefslogtreecommitdiff
path: root/renderutil/glyph.c
blob: da8c53f8791585632800755d407384e96091cdab (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Copyright © 2006 Ian Osgood
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * Except as contained in this notice, the names of the authors or their
 * institutions shall not be used in advertising or otherwise to promote the
 * sale, use or other dealings in this Software without prior written
 * authorization from the authors.
 */

#include <stdlib.h>
#include <string.h>

#include "xcb_renderutil.h"

typedef struct _glyph_header_t {
	uint8_t count;
	uint8_t pad0[3];
	int16_t dx, dy;
} _glyph_header_t;

/* implementation of the opaque stream */
struct xcb_render_util_composite_text_stream_t {
	/* state info */
	uint32_t glyph_size;		/* 0 for unset, 1/2/4 for 8/16/32 */
	xcb_render_glyphset_t initial_glyphset;
	xcb_render_glyphset_t current_glyphset;

	/* dynamically allocated stream */
	/* contents are 32-bit aligned, network byte order */
	size_t stream_len;
	uint32_t *stream;
	uint32_t *current;
};

#define CURRENT_LEN(s) (((char *)s->current - (char *)s->stream))

xcb_render_util_composite_text_stream_t *
xcb_render_util_composite_text_stream (
	xcb_render_glyphset_t initial_glyphset,
	uint32_t              total_glyphs,
	uint32_t              total_glyphset_changes )
{
	xcb_render_util_composite_text_stream_t *stream;
	size_t size = 32;

	/* assume worst case: each glyph has its own dx,dy */
	if (total_glyphs || total_glyphset_changes) {
		size = total_glyphs * 3 * sizeof(uint32_t)
		     + total_glyphset_changes * 3 * sizeof(uint32_t);
	}

	stream = malloc(sizeof(xcb_render_util_composite_text_stream_t));

	stream->glyph_size = 0;
	stream->initial_glyphset = initial_glyphset;
	stream->current_glyphset = initial_glyphset;

	stream->stream_len = size;
	stream->stream = malloc(size);
	stream->current = stream->stream;

	return stream;
}

static void
_grow_stream( xcb_render_util_composite_text_stream_t *stream, size_t increase )
{
	size_t current_len = CURRENT_LEN(stream);
	if (current_len + increase > stream->stream_len) {
		uint32_t *s = realloc(stream->stream, 2 * stream->stream_len);
		if (s != NULL) {
			stream->stream_len *= 2;
			stream->stream = s;
			stream->current = stream->stream + (current_len>>2);
		}
	}
}

void
xcb_render_util_glyphs_8 (
	xcb_render_util_composite_text_stream_t *stream,
	int16_t  dx,
	int16_t  dy,
	uint32_t count,
	const uint8_t *glyphs )
{
	_glyph_header_t header = { count, {0,0,0}, htons(dx), htons(dy) };

	if (count > 252) return; /* FIXME */

	if (stream->glyph_size != sizeof(*glyphs)) {
		if (stream->glyph_size != 0)
			return;
		stream->glyph_size = sizeof(*glyphs);
	}
	_grow_stream(stream, sizeof(header) + count+3);

	memcpy(stream->current, &header, sizeof(header));
	stream->current += 2;

	memcpy(stream->current, glyphs, header.count);
	stream->current += ((int)header.count+3)>>2;
}

void
xcb_render_util_glyphs_16 (
	xcb_render_util_composite_text_stream_t *stream,
	int16_t  dx,
	int16_t  dy,
	uint32_t count,
	const uint16_t *glyphs )
{
	uint16_t *current16;
	_glyph_header_t header = { count, {0,0,0}, htons(dx), htons(dy) };

	if (count > 254) return; /* FIXME */

	if (stream->glyph_size != sizeof(*glyphs)) {
		if (stream->glyph_size != 0)
			return;
		stream->glyph_size = sizeof(*glyphs);
	}
	_grow_stream(stream, sizeof(header) + count*sizeof(*glyphs)+1);

	memcpy(stream->current, &header, sizeof(header));
	stream->current += 2;

	current16 = (uint16_t *)stream->current;
	while (count--) {
		*current16++ = htons(*glyphs++);
	}
	stream->current += ((int)header.count*sizeof(*glyphs)+1)>>2;
}

void
xcb_render_util_glyphs_32 (
	xcb_render_util_composite_text_stream_t *stream,
	int16_t  dx,
	int16_t  dy,
	uint32_t count,
	const uint32_t *glyphs )
{
	_glyph_header_t header = { count, {0,0,0}, htons(dx), htons(dy) };

	if (count > 254) return; /* FIXME */

	if (stream->glyph_size != sizeof(*glyphs)) {
		if (stream->glyph_size != 0)
			return;
		stream->glyph_size = sizeof(*glyphs);
	}
	_grow_stream(stream, sizeof(header) + count*sizeof(*glyphs)+1);

	memcpy(stream->current, &header, sizeof(header));
	stream->current += 2;

	while (count--) {
		*stream->current++ = htonl(*glyphs++);
	}
}

/* note: these glyph arrays must be swapped to network byte order */

void
xcb_render_util_change_glyphset (
	xcb_render_util_composite_text_stream_t *stream,
	xcb_render_glyphset_t glyphset )
{
	static _glyph_header_t header = { 255, {0,0,0}, 0, 0 };

	if (glyphset == stream->current_glyphset)
		return;

	_grow_stream(stream, 3*sizeof(uint32_t));

	memcpy(stream->current, &header, sizeof(header));
	stream->current += 2;

	*stream->current = htonl(glyphset);
	stream->current++;

	stream->current_glyphset = glyphset;
}
	

xcb_void_cookie_t
xcb_render_util_composite_text (
	xcb_connection_t        *xc,
	uint8_t                  op,
	xcb_render_picture_t     src,
	xcb_render_picture_t     dst,
	xcb_render_pictformat_t  mask_format,
	int16_t                  src_x,
	int16_t                  src_y,
	xcb_render_util_composite_text_stream_t *stream )
{
	xcb_void_cookie_t reply = { 0 };
	uint32_t stream_len = CURRENT_LEN(stream);

	switch (stream->glyph_size)
	{
	case 1:
		reply = xcb_render_composite_glyphs_8 (
			xc, op, src, dst, mask_format,
			stream->initial_glyphset,
			src_x, src_y,
			stream_len,
			(uint8_t *)stream->stream
		);
		break;
	case 2:
		reply = xcb_render_composite_glyphs_16 (
			xc, op, src, dst, mask_format,
			stream->initial_glyphset,
			src_x, src_y,
			stream_len,
			(uint8_t *)stream->stream
		);
		break;
	case 4:
		reply = xcb_render_composite_glyphs_32 (
			xc, op, src, dst, mask_format,
			stream->initial_glyphset,
			src_x, src_y,
			stream_len,
			(uint8_t *)stream->stream
		);
		break;
	default: /* uninitialized */
		break;
	}
	return reply;
}

/* FIXME: xcb_render_util_composite_text_checked */

void
xcb_render_util_composite_text_free (
	xcb_render_util_composite_text_stream_t *stream )
{
	free(stream->stream);
	free(stream);
}