Blob Blame Raw

#include <cairo.h>

#include "private.h"
#include "drawing.h"

static double colorBack[4] = {1, 1, 1, 1};
static double colorFill[4] = {0.5, 0.5, 0.5, 1};
static double colorStroke[4] = {0, 0, 0, 1};
static double lineWidth = 1;
static double *path;
static size_t pathSize;
static size_t pathAllocated;

static HAlign horAlign = HALIGN_LEFT;
static VAlign vertAlign = VALIGN_TOP;
static char *font;
static double fontSize = 24;


void background(const char *color)
	{ heliParseColor(color, colorBack); }
void fill(const char *color)
	{ heliParseColor(color, colorFill); }
void noFill()
	{ fill("transparent"); }
void stroke(const char *color)
	{ heliParseColor(color, colorStroke); }
void noStroke()
	{ stroke("transparent"); }

void strokeWeight(double weight)
	{ lineWidth = weight; }

char* rgba(double r, double g, double b, double a) {
	static char buf[1024];
	snprintf(buf, sizeof(buf) - 1, "%f %f %f %f", r, g, b, a);
	return buf;
}

char* rgb(double r, double g, double b)
	{ return rgba(r, g, b, 1); }

void rect(double x, double y, double width, double height) {
	resetPath();
	moveTo(x, y);
	lineTo(x + width, y);
	lineTo(x + width, y + height);
	lineTo(x, y + height);
	closePath();
}

void line(double x1, double y1, double x2, double y2) {
	resetPath();
	moveTo(x1, y1);
	lineTo(x2, y2);
	strokePath();
}

void ellipse(double x, double y, double width, double height) {
	resetPath();
	arcPath(x, y, width, height, 0, 360);
	closePath();
}

void point(double x, double y)
	{ ellipse(x - lineWidth*0.25, y - lineWidth*0.25, lineWidth*0.5, lineWidth*0.5); }

void arcPath(double x, double y, double w, double h, double start, double stop) {
	double step = PI/180;
	start *= PI/180;
	stop *= PI/180;
	if (start < stop) { double s = start; stop = start; start = s; }
	for(double a = start; a < stop; a += step) {
		double lx = x + cos(a)*w;
		double ly = y + sin(a)*h;
		lineTo(lx, ly);
	}
}

void arc(double x, double y, double w, double h, double start, double stop) {
	resetPath();
	arcPath(x, y, w, h, start, stop);
	strokePath();
}

void regularPolygon(double x, double y, int sides, double size) {
	resetPath();
	size *= 0.5;
	moveTo(x + size, y);
	for(int i = 1; i < sides; ++i) {
		double a = i*PI/sides;
		lineTo(x + size*cos(a), y + size*sin(a));
	}
	closePath();
}


void resetPath()
	{ pathSize = 0; }

static void endPath(int close, int stroke, int fill) {
	cairo_t *cr = heliCairo;
	if (cr && pathSize >= 8) {
		cairo_save(cr);
		cairo_move_to(cr, path[0], path[1]);
		for(int i = 2; i < pathSize; i += 2)
			cairo_line_to(cr, path[i], path[i+1]);
		if (close)
			cairo_close_path(cr);
		if (fill) {
			cairo_set_source_rgba(cr, colorFill[0], colorFill[1], colorFill[2], colorFill[3]);
			if (stroke) cairo_fill_preserve(cr); else cairo_fill(cr);
		}
		if (stroke) {
			cairo_set_line_width(cr, lineWidth);
			cairo_set_source_rgba(cr, colorStroke[0], colorStroke[1], colorStroke[2], colorStroke[3]);
			cairo_stroke(cr);
		}
		cairo_restore(cr);
	}
	resetPath();
}

static void pushPathPoint(double x, double y) {
	if (pathAllocated < pathSize + 2) {
		pathAllocated += pathAllocated/4 + 32;
		path = realloc(path, pathAllocated);
		memset(&path[pathSize], 0, (pathAllocated - pathSize)*sizeof(*path));
	}
	path[pathSize++] = x;
	path[pathSize++] = y;
}

void closePath()
	{ endPath(TRUE, TRUE, TRUE); }
void strokePath()
	{ endPath(FALSE, TRUE, FALSE); }
void lineTo(double x, double y)
	{ pushPathPoint(x, y); }
void moveTo(double x, double y)
	{ resetPath(); lineTo(x, y); }


void textAlign(HAlign hor, VAlign vert)
	{ horAlign = hor; vertAlign = vert; }
void textFont(const char *f)
	{ free(font); font = heliStringCopy(f); }
void textSize(double size)
	{ fontSize = size; }

void text(const char *text, double x, double y) {
	resetPath();

	cairo_t *cr = heliCairo;
	if (!cr) return;
	cairo_save(cr);

	if (font) cairo_select_font_face(cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
	cairo_set_font_size(cr, fontSize);
	
	cairo_text_extents_t extents;
	cairo_text_extents(cr, text, &extents);
	double w = extents.x_bearing + extents.width;
	double h = extents.y_bearing + extents.height;
	if (horAlign  == HALIGN_CENTER) x -= w*0.5;
	if (horAlign  == HALIGN_RIGHT ) x -= w;
	if (vertAlign == VALIGN_CENTER) y -= h*0.5;
	if (vertAlign == VALIGN_BOTTOM) y -= h;
	
	cairo_set_source_rgba(cr, colorStroke[0], colorStroke[1], colorStroke[2], colorStroke[3]);
	cairo_move_to(cr, x, y);
	cairo_show_text(cr, text);
	
	cairo_restore(cr);
}

void heliDrawingPrepareFrame() {
	resetPath();
	cairo_t *cr = heliCairo;
	if (!cr) return;
	cairo_save(cr);
	cairo_set_source_rgba(cr, colorBack[0], colorBack[1], colorBack[2], 1);
	cairo_paint(cr);
	cairo_restore(cr);
}

void heliDrawingFinish() {
	resetPath();
	free(path);
	path = NULL;
	pathAllocated = 0;
	free(font);
	font = NULL;
}