render_cairo.h 12.9 KB
Newer Older
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
/*
 * Copyright (c) 2016 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#ifndef _LV2_CANVAS_RENDER_CAIRO_H
#define _LV2_CANVAS_RENDER_CAIRO_H

#include <assert.h>

#include <canvas.lv2/canvas.h>

#include <cairo/cairo.h>

#ifdef __cplusplus
extern "C" {
#endif

static inline void
_lv2_canvas_render_beginPath(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	cairo_new_sub_path(ctx);
}

static inline void
_lv2_canvas_render_closePath(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	cairo_close_path(ctx);
}

static inline void
_lv2_canvas_render_arc(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 5);

	if(v)
	{
		cairo_arc(ctx, v[0], v[1], v[2], v[3], v[4]);
	}
}

static inline void
_lv2_canvas_render_curveTo(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 6);

	if(v)
	{
		cairo_curve_to(ctx, v[0], v[1], v[2], v[3], v[4], v[5]);
	}
}

static inline void
_lv2_canvas_render_lineTo(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 2);

	if(v)
	{
		cairo_line_to(ctx, v[0], v[1]);
	}
}

static inline void
_lv2_canvas_render_moveTo(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 2);

	if(v)
	{
		cairo_move_to(ctx, v[0], v[1]);
	}
}

static inline void
_lv2_canvas_render_rectangle(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 4);

	if(v)
	{
		cairo_rectangle(ctx, v[0], v[1], v[2], v[3]);
	}
}

static inline void
_lv2_canvas_render_polyline(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	uint32_t N;
	const float *v = _lv2_canvas_render_get_float_vecs(urid, body, &N);

	if(v)
	{
		for(uint32_t i = 0; i < 2; i += 2)
		{
			cairo_move_to(ctx, v[i], v[i+1]);
		}

		for(uint32_t i = 2; i < N; i += 2)
		{
			cairo_line_to(ctx, v[i], v[i+1]);
		}
	}
}

static inline void
_lv2_canvas_render_style(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const int64_t *v = _lv2_canvas_render_get_type(body, urid->forge.Long);

	if(v)
	{
		const float r = (float)((*v >> 24) & 0xff) / 0xff;
		const float g = (float)((*v >> 16) & 0xff) / 0xff;
		const float b = (float)((*v >>  8) & 0xff) / 0xff;
		const float a = (float)((*v >>  0) & 0xff) / 0xff;

		cairo_set_source_rgba(ctx, r, g, b, a);
	}
}

static inline void
_lv2_canvas_render_lineWidth(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_type(body, urid->forge.Float);

	if(v)
	{
		cairo_set_line_width(ctx, *v);
	}
}

static inline void
_lv2_canvas_render_lineDash(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 2);

	if(v)
	{
		const double d[2] = {v[0], v[1]};
		cairo_set_dash(ctx, d, 2, 0);
	}
}

static inline void
_lv2_canvas_render_lineCap(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const LV2_URID *v = _lv2_canvas_render_get_type(body, urid->forge.URID);

	if(v)
	{
		cairo_line_cap_t cap = CAIRO_LINE_CAP_BUTT;

		if(*v == urid->Canvas_lineCapButt)
			cap = CAIRO_LINE_CAP_BUTT;
		else if(*v == urid->Canvas_lineCapRound)
			cap = CAIRO_LINE_CAP_ROUND;
		else if(*v == urid->Canvas_lineCapSquare)
			cap = CAIRO_LINE_CAP_SQUARE;

		cairo_set_line_cap(ctx, cap);
	}
}

static inline void
_lv2_canvas_render_lineJoin(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const LV2_URID *v = _lv2_canvas_render_get_type(body, urid->forge.URID);

	if(v)
	{
		cairo_line_join_t join = CAIRO_LINE_JOIN_MITER;

		if(*v == urid->Canvas_lineJoinMiter)
			join = CAIRO_LINE_JOIN_MITER;
		else if(*v == urid->Canvas_lineJoinRound)
			join = CAIRO_LINE_JOIN_ROUND;
		else if(*v == urid->Canvas_lineJoinBevel)
			join = CAIRO_LINE_JOIN_BEVEL;

		cairo_set_line_join(ctx, join);
	}
}

static inline void
_lv2_canvas_render_miterLimit(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_type(body, urid->forge.Float);

	if(v)
	{
		cairo_set_miter_limit(ctx, *v);
	}
}

static inline void
_lv2_canvas_render_stroke(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	cairo_stroke(ctx);
}

static inline void
_lv2_canvas_render_fill(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	cairo_fill(ctx);
}

static inline void
_lv2_canvas_render_clip(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	cairo_clip(ctx);
}

static inline void
_lv2_canvas_render_save(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	cairo_save(ctx);
}

static inline void
_lv2_canvas_render_restore(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	cairo_restore(ctx);
}

static inline void
_lv2_canvas_render_translate(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 2);

	if(v)
	{
		cairo_translate(ctx, v[0], v[1]);
	}
}

static inline void
_lv2_canvas_render_scale(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 2);

	if(v)
	{
		cairo_scale(ctx, v[0], v[1]);
	}
}

static inline void
_lv2_canvas_render_rotate(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_type(body, urid->forge.Float);

	if(v)
	{
		cairo_rotate(ctx, *v);
	}
}

static inline void
_lv2_canvas_render_transform(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_float_vec(urid, body, 6);

	if(v)
	{
		const cairo_matrix_t matrix = {
			.xx = v[0],
			.xy = v[1],
			.x0 = v[2],
			.yy = v[3],
			.yx = v[4],
			.y0 = v[5]
		};

		cairo_transform(ctx, &matrix);
	}
}

static inline void
_lv2_canvas_render_reset(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
		cairo_identity_matrix(ctx);
}

static inline void
_lv2_canvas_render_fontSize(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const float *v = _lv2_canvas_render_get_type(body, urid->forge.Float);

	if(v)
	{
		cairo_set_font_size(ctx, *v);
	}
}

static inline void
_lv2_canvas_render_fillText(void *data,
	LV2_Canvas_URID *urid, const LV2_Atom *body)
{
	cairo_t *ctx = data;
	const char *v = _lv2_canvas_render_get_type(body, urid->forge.String);

	if(v)
	{
		cairo_text_extents_t extents;
		cairo_text_extents(ctx, v, &extents);
		const float dx = extents.width/2 + extents.x_bearing;
		const float dy = extents.height/2 + extents.y_bearing;
		cairo_rel_move_to(ctx, -dx, -dy);
		cairo_show_text(ctx, v);
	}
}

static inline void
_lv2_canvas_qsort(LV2_Canvas_Meth *A, int n)
{
	if(n < 2)
		return;

	LV2_Canvas_Meth *p = A;

	int i = -1;
	int j = n;

	while(true)
	{
		do {
			i += 1;
		} while(A[i].command < p->command);

		do {
			j -= 1;
		} while(A[j].command > p->command);

		if(i >= j)
			break;

		const LV2_Canvas_Meth tmp = A[i];
		A[i] = A[j];
		A[j] = tmp;
	}

	_lv2_canvas_qsort(A, j + 1);
	_lv2_canvas_qsort(A + j + 1, n - j - 1);
}

static inline LV2_Canvas_Meth *
_lv2_canvas_bsearch(LV2_URID p, LV2_Canvas_Meth *a, int n)
{
	LV2_Canvas_Meth *base = a;

	for(int N = n, half; N > 1; N -= half)
	{
		half = N/2;
		LV2_Canvas_Meth *dst = &base[half];
		base = (dst->command > p) ? base : dst;
	}

	return (base->command == p) ? base : NULL;
}

static inline void
lv2_canvas_init(LV2_Canvas *canvas, LV2_URID_Map *map)
{
	lv2_canvas_urid_init(&canvas->urid, map);

	unsigned ptr = 0;

	canvas->methods[ptr].command = canvas->urid.Canvas_BeginPath;
	canvas->methods[ptr++].func = _lv2_canvas_render_beginPath;

	canvas->methods[ptr].command = canvas->urid.Canvas_ClosePath;
	canvas->methods[ptr++].func = _lv2_canvas_render_closePath;

	canvas->methods[ptr].command = canvas->urid.Canvas_Arc;
	canvas->methods[ptr++].func = _lv2_canvas_render_arc;

	canvas->methods[ptr].command = canvas->urid.Canvas_CurveTo;
	canvas->methods[ptr++].func = _lv2_canvas_render_curveTo;

	canvas->methods[ptr].command = canvas->urid.Canvas_LineTo;
	canvas->methods[ptr++].func = _lv2_canvas_render_lineTo;

	canvas->methods[ptr].command = canvas->urid.Canvas_MoveTo;
	canvas->methods[ptr++].func = _lv2_canvas_render_moveTo;

	canvas->methods[ptr].command = canvas->urid.Canvas_Rectangle;
	canvas->methods[ptr++].func = _lv2_canvas_render_rectangle;

	canvas->methods[ptr].command = canvas->urid.Canvas_PolyLine;
	canvas->methods[ptr++].func = _lv2_canvas_render_polyline;

	canvas->methods[ptr].command = canvas->urid.Canvas_Style;
	canvas->methods[ptr++].func = _lv2_canvas_render_style;

	canvas->methods[ptr].command = canvas->urid.Canvas_LineWidth;
	canvas->methods[ptr++].func = _lv2_canvas_render_lineWidth;

	canvas->methods[ptr].command = canvas->urid.Canvas_LineDash;
	canvas->methods[ptr++].func = _lv2_canvas_render_lineDash;

	canvas->methods[ptr].command = canvas->urid.Canvas_LineCap;
	canvas->methods[ptr++].func = _lv2_canvas_render_lineCap;

	canvas->methods[ptr].command = canvas->urid.Canvas_LineJoin;
	canvas->methods[ptr++].func = _lv2_canvas_render_lineJoin;

	canvas->methods[ptr].command = canvas->urid.Canvas_MiterLimit;
	canvas->methods[ptr++].func = _lv2_canvas_render_miterLimit;

	canvas->methods[ptr].command = canvas->urid.Canvas_Stroke;
	canvas->methods[ptr++].func = _lv2_canvas_render_stroke;

	canvas->methods[ptr].command = canvas->urid.Canvas_Fill;
	canvas->methods[ptr++].func = _lv2_canvas_render_fill;

	canvas->methods[ptr].command = canvas->urid.Canvas_Clip;
	canvas->methods[ptr++].func = _lv2_canvas_render_clip;

	canvas->methods[ptr].command = canvas->urid.Canvas_Save;
	canvas->methods[ptr++].func = _lv2_canvas_render_save;

	canvas->methods[ptr].command = canvas->urid.Canvas_Restore;
	canvas->methods[ptr++].func = _lv2_canvas_render_restore;

	canvas->methods[ptr].command = canvas->urid.Canvas_Translate;
	canvas->methods[ptr++].func = _lv2_canvas_render_translate;

	canvas->methods[ptr].command = canvas->urid.Canvas_Scale;
	canvas->methods[ptr++].func = _lv2_canvas_render_scale;

	canvas->methods[ptr].command = canvas->urid.Canvas_Rotate;
	canvas->methods[ptr++].func = _lv2_canvas_render_rotate;

	canvas->methods[ptr].command = canvas->urid.Canvas_Transform;
	canvas->methods[ptr++].func = _lv2_canvas_render_transform;

	canvas->methods[ptr].command = canvas->urid.Canvas_Reset;
	canvas->methods[ptr++].func = _lv2_canvas_render_reset;

	canvas->methods[ptr].command = canvas->urid.Canvas_FontSize;
	canvas->methods[ptr++].func = _lv2_canvas_render_fontSize;

	canvas->methods[ptr].command = canvas->urid.Canvas_FillText;
	canvas->methods[ptr++].func = _lv2_canvas_render_fillText;

	assert(ptr == LV2_CANVAS_NUM_METHODS);

	_lv2_canvas_qsort(canvas->methods, LV2_CANVAS_NUM_METHODS);
}

static inline bool
lv2_canvas_render(LV2_Canvas *canvas, cairo_t *ctx, const LV2_Atom_Tuple *tup)
{
	LV2_Canvas_URID *urid = &canvas->urid;

	if(!tup || (tup->atom.type != urid->forge.Tuple) )
		return false;

	// save state
	cairo_save(ctx);

	// clear surface
	cairo_set_operator(ctx, CAIRO_OPERATOR_CLEAR);
	cairo_paint(ctx);

	// default attributes
	cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
	cairo_set_font_size(ctx, 0.1);
	cairo_set_line_width(ctx, 0.01);
	cairo_set_source_rgba(ctx, 1.0, 1.0, 1.0, 1.0);

	LV2_ATOM_TUPLE_FOREACH(tup, itm)
	{
		if(lv2_atom_forge_is_object_type(&urid->forge, itm->type))
		{
			const LV2_Atom_Object *obj = (const LV2_Atom_Object *)itm;
			const LV2_Atom *body = NULL;

			lv2_atom_object_get(obj, urid->Canvas_body, &body, 0);

			LV2_Canvas_Meth *meth = _lv2_canvas_bsearch(obj->body.otype,
				canvas->methods, LV2_CANVAS_NUM_METHODS);

			if(meth)
			{
				meth->func(ctx, urid, body);
			}
		}
	}

	// save state
	cairo_restore(ctx);

	// flush
	cairo_surface_t *surface = cairo_get_target(ctx);
	cairo_surface_flush(surface);

	return true;
}

#ifdef __cplusplus
}
#endif

#endif // LV2_CANVAS_RENDER_CAIRO_H