| 1 |
/* Copyright © 2006 Ian Osgood |
| 2 |
* |
| 3 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 4 |
* copy of this software and associated documentation files (the "Software"), |
| 5 |
* to deal in the Software without restriction, including without limitation |
| 6 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 8 |
* Software is furnished to do so, subject to the following conditions: |
| 9 |
* |
| 10 |
* The above copyright notice and this permission notice shall be included in |
| 11 |
* all copies or substantial portions of the Software. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
* AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 17 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 |
* |
| 20 |
* Except as contained in this notice, the names of the authors or their |
| 21 |
* institutions shall not be used in advertising or otherwise to promote the |
| 22 |
* sale, use or other dealings in this Software without prior written |
| 23 |
* authorization from the authors. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include "config.h" |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "xcb_renderutil.h" |
| 34 |
|
| 35 |
typedef struct _glyph_header_t { |
| 36 |
uint8_t count; |
| 37 |
uint8_t pad0[3]; |
| 38 |
int16_t dx, dy; |
| 39 |
} _glyph_header_t; |
| 40 |
|
| 41 |
/* implementation of the opaque stream */ |
| 42 |
struct xcb_render_util_composite_text_stream_t { |
| 43 |
/* state info */ |
| 44 |
uint32_t glyph_size; /* 0 for unset, 1/2/4 for 8/16/32 */ |
| 45 |
xcb_render_glyphset_t initial_glyphset; |
| 46 |
xcb_render_glyphset_t current_glyphset; |
| 47 |
|
| 48 |
/* dynamically allocated stream */ |
| 49 |
/* contents are 32-bit aligned, network byte order */ |
| 50 |
size_t stream_len; |
| 51 |
uint32_t *stream; |
| 52 |
uint32_t *current; |
| 53 |
}; |
| 54 |
|
| 55 |
#define CURRENT_LEN(s) (((char *)s->current - (char *)s->stream)) |
| 56 |
|
| 57 |
xcb_render_util_composite_text_stream_t * |
| 58 |
xcb_render_util_composite_text_stream ( |
| 59 |
xcb_render_glyphset_t initial_glyphset, |
| 60 |
uint32_t total_glyphs, |
| 61 |
uint32_t total_glyphset_changes ) |
| 62 |
{ |
| 63 |
xcb_render_util_composite_text_stream_t *stream; |
| 64 |
size_t size = 32; |
| 65 |
|
| 66 |
/* assume worst case: each glyph has its own dx,dy */ |
| 67 |
if (total_glyphs || total_glyphset_changes) { |
| 68 |
size = total_glyphs * 3 * sizeof(uint32_t) |
| 69 |
+ total_glyphset_changes * 3 * sizeof(uint32_t); |
| 70 |
} |
| 71 |
|
| 72 |
stream = malloc(sizeof(xcb_render_util_composite_text_stream_t)); |
| 73 |
if (!stream) |
| 74 |
return NULL; |
| 75 |
|
| 76 |
stream->glyph_size = 0; |
| 77 |
stream->initial_glyphset = initial_glyphset; |
| 78 |
stream->current_glyphset = initial_glyphset; |
| 79 |
|
| 80 |
stream->stream_len = size; |
| 81 |
stream->stream = malloc(size); |
| 82 |
stream->current = stream->stream; |
| 83 |
|
| 84 |
return stream; |
| 85 |
} |
| 86 |
|
| 87 |
static void |
| 88 |
_grow_stream( xcb_render_util_composite_text_stream_t *stream, size_t increase ) |
| 89 |
{ |
| 90 |
size_t current_len = CURRENT_LEN(stream); |
| 91 |
if (current_len + increase > stream->stream_len) { |
| 92 |
uint32_t *s = realloc(stream->stream, 2 * stream->stream_len); |
| 93 |
if (s != NULL) { |
| 94 |
stream->stream_len *= 2; |
| 95 |
stream->stream = s; |
| 96 |
stream->current = stream->stream + (current_len>>2); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void |
| 102 |
xcb_render_util_glyphs_8 ( |
| 103 |
xcb_render_util_composite_text_stream_t *stream, |
| 104 |
int16_t dx, |
| 105 |
int16_t dy, |
| 106 |
uint32_t count, |
| 107 |
const uint8_t *glyphs ) |
| 108 |
{ |
| 109 |
_glyph_header_t header = { count, {0,0,0}, dx, dy }; |
| 110 |
|
| 111 |
if (count > 252) return; /* FIXME */ |
| 112 |
|
| 113 |
if (stream->glyph_size != sizeof(*glyphs)) { |
| 114 |
if (stream->glyph_size != 0) |
| 115 |
return; |
| 116 |
stream->glyph_size = sizeof(*glyphs); |
| 117 |
} |
| 118 |
_grow_stream(stream, sizeof(header) + count+3); |
| 119 |
|
| 120 |
memcpy(stream->current, &header, sizeof(header)); |
| 121 |
stream->current += 2; |
| 122 |
|
| 123 |
memcpy(stream->current, glyphs, header.count); |
| 124 |
stream->current += ((int)header.count+3)>>2; |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
xcb_render_util_glyphs_16 ( |
| 129 |
xcb_render_util_composite_text_stream_t *stream, |
| 130 |
int16_t dx, |
| 131 |
int16_t dy, |
| 132 |
uint32_t count, |
| 133 |
const uint16_t *glyphs ) |
| 134 |
{ |
| 135 |
_glyph_header_t header = { count, {0,0,0}, dx, dy }; |
| 136 |
|
| 137 |
if (count > 254) return; /* FIXME */ |
| 138 |
|
| 139 |
if (stream->glyph_size != sizeof(*glyphs)) { |
| 140 |
if (stream->glyph_size != 0) |
| 141 |
return; |
| 142 |
stream->glyph_size = sizeof(*glyphs); |
| 143 |
} |
| 144 |
_grow_stream(stream, sizeof(header) + count*sizeof(*glyphs)+1); |
| 145 |
|
| 146 |
memcpy(stream->current, &header, sizeof(header)); |
| 147 |
stream->current += 2; |
| 148 |
|
| 149 |
memcpy(stream->current, glyphs, header.count*sizeof(*glyphs)); |
| 150 |
stream->current += ((int)header.count*sizeof(*glyphs)+3)>>2; |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
xcb_render_util_glyphs_32 ( |
| 155 |
xcb_render_util_composite_text_stream_t *stream, |
| 156 |
int16_t dx, |
| 157 |
int16_t dy, |
| 158 |
uint32_t count, |
| 159 |
const uint32_t *glyphs ) |
| 160 |
{ |
| 161 |
_glyph_header_t header = { count, {0,0,0}, dx, dy }; |
| 162 |
|
| 163 |
if (count > 254) return; /* FIXME */ |
| 164 |
|
| 165 |
if (stream->glyph_size != sizeof(*glyphs)) { |
| 166 |
if (stream->glyph_size != 0) |
| 167 |
return; |
| 168 |
stream->glyph_size = sizeof(*glyphs); |
| 169 |
} |
| 170 |
_grow_stream(stream, sizeof(header) + count*sizeof(*glyphs)+1); |
| 171 |
|
| 172 |
memcpy(stream->current, &header, sizeof(header)); |
| 173 |
stream->current += 2; |
| 174 |
|
| 175 |
memcpy(stream->current, glyphs, header.count*sizeof(*glyphs)); |
| 176 |
stream->current += header.count; |
| 177 |
} |
| 178 |
|
| 179 |
/* note: these glyph arrays must be swapped to network byte order */ |
| 180 |
|
| 181 |
void |
| 182 |
xcb_render_util_change_glyphset ( |
| 183 |
xcb_render_util_composite_text_stream_t *stream, |
| 184 |
xcb_render_glyphset_t glyphset ) |
| 185 |
{ |
| 186 |
static _glyph_header_t header = { 255, {0,0,0}, 0, 0 }; |
| 187 |
|
| 188 |
if (glyphset == stream->current_glyphset) |
| 189 |
return; |
| 190 |
|
| 191 |
_grow_stream(stream, 3*sizeof(uint32_t)); |
| 192 |
|
| 193 |
memcpy(stream->current, &header, sizeof(header)); |
| 194 |
stream->current += 2; |
| 195 |
|
| 196 |
*stream->current = glyphset; |
| 197 |
stream->current++; |
| 198 |
|
| 199 |
stream->current_glyphset = glyphset; |
| 200 |
} |
| 201 |
|
| 202 |
typedef xcb_void_cookie_t |
| 203 |
(*xcb_render_composite_glyphs_func) (xcb_connection_t *c, |
| 204 |
uint8_t op, |
| 205 |
xcb_render_picture_t src, |
| 206 |
xcb_render_picture_t dst, |
| 207 |
xcb_render_pictformat_t mask_format, |
| 208 |
xcb_render_glyphset_t glyphset, |
| 209 |
int16_t src_x, |
| 210 |
int16_t src_y, |
| 211 |
uint32_t glyphcmds_len, |
| 212 |
const uint8_t *glyphcmds); |
| 213 |
|
| 214 |
|
| 215 |
xcb_void_cookie_t |
| 216 |
xcb_render_util_composite_text ( |
| 217 |
xcb_connection_t *xc, |
| 218 |
uint8_t op, |
| 219 |
xcb_render_picture_t src, |
| 220 |
xcb_render_picture_t dst, |
| 221 |
xcb_render_pictformat_t mask_format, |
| 222 |
int16_t src_x, |
| 223 |
int16_t src_y, |
| 224 |
xcb_render_util_composite_text_stream_t *stream ) |
| 225 |
{ |
| 226 |
xcb_render_composite_glyphs_func f; |
| 227 |
|
| 228 |
switch (stream->glyph_size) |
| 229 |
{ |
| 230 |
case 1: |
| 231 |
f = xcb_render_composite_glyphs_8; |
| 232 |
break; |
| 233 |
case 2: |
| 234 |
f = xcb_render_composite_glyphs_16; |
| 235 |
break; |
| 236 |
case 4: |
| 237 |
f = xcb_render_composite_glyphs_32; |
| 238 |
break; |
| 239 |
default: /* uninitialized */ |
| 240 |
return xcb_no_operation(xc); |
| 241 |
} |
| 242 |
return f( |
| 243 |
xc, op, src, dst, mask_format, |
| 244 |
stream->initial_glyphset, |
| 245 |
src_x, src_y, |
| 246 |
CURRENT_LEN(stream), |
| 247 |
(uint8_t *)stream->stream |
| 248 |
); |
| 249 |
} |
| 250 |
|
| 251 |
xcb_void_cookie_t |
| 252 |
xcb_render_util_composite_text_checked ( |
| 253 |
xcb_connection_t *xc, |
| 254 |
uint8_t op, |
| 255 |
xcb_render_picture_t src, |
| 256 |
xcb_render_picture_t dst, |
| 257 |
xcb_render_pictformat_t mask_format, |
| 258 |
int16_t src_x, |
| 259 |
int16_t src_y, |
| 260 |
xcb_render_util_composite_text_stream_t *stream ) |
| 261 |
{ |
| 262 |
xcb_render_composite_glyphs_func f; |
| 263 |
|
| 264 |
switch (stream->glyph_size) |
| 265 |
{ |
| 266 |
case 1: |
| 267 |
f = xcb_render_composite_glyphs_8_checked; |
| 268 |
break; |
| 269 |
case 2: |
| 270 |
f = xcb_render_composite_glyphs_16_checked; |
| 271 |
break; |
| 272 |
case 4: |
| 273 |
f = xcb_render_composite_glyphs_32_checked; |
| 274 |
break; |
| 275 |
default: /* uninitialized */ |
| 276 |
return xcb_no_operation_checked(xc); |
| 277 |
} |
| 278 |
return f( |
| 279 |
xc, op, src, dst, mask_format, |
| 280 |
stream->initial_glyphset, |
| 281 |
src_x, src_y, |
| 282 |
CURRENT_LEN(stream), |
| 283 |
(uint8_t *)stream->stream |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
void |
| 288 |
xcb_render_util_composite_text_free ( |
| 289 |
xcb_render_util_composite_text_stream_t *stream ) |
| 290 |
{ |
| 291 |
free(stream->stream); |
| 292 |
free(stream); |
| 293 |
} |