Blob Blame Raw

#include <ft2build.h>
#include FT_FREETYPE_H

#include <cairo.h>
#include <cairo-ft.h>

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


typedef struct _HeliFontInstance {
	char *path;
	cairo_font_face_t *font_face;
	int refcount;
} HeliFontInstance;

struct _Font {
	HeliFontInstance *instance;
};


static int ftInitialized;
static FT_Library ftLibrary;
static HeliArray fontCache;

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 *fontFamily;
static Font 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; }

const 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;
}

const 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*sizeof(*path));
		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); }


static HeliFontInstance* loadFont(const char *path) {
	static cairo_user_data_key_t key = {};
	
	HeliFontInstance *fi = calloc(1, sizeof(*fi));
	fi->path = heliStringCopy(path);
	
	if (!ftInitialized) {
		if (FT_Init_FreeType(&ftLibrary))
			printf("Cannot initialize FreeType library");
		ftInitialized = TRUE;
	}
	if (ftLibrary) {
		FT_Face face;
		if (FT_New_Face(ftLibrary, path, 0, &face)) {
			printf("Cannot load font from file: %s", path);
		} else {
			fi->font_face = cairo_ft_font_face_create_for_ft_face(face, 0);
			cairo_font_face_set_user_data(fi->font_face, &key, face, (cairo_destroy_func_t)FT_Done_Face);
		}
	}
	
	return fi;
}

static void unloadFont(HeliFontInstance *fi) {
	assert(!fi->refcount);
	free(fi->path);
	if (fi->font_face) cairo_font_face_destroy(fi->font_face);
	free(fi);
}

Font createFont(const char *path) {
	if (!heliInitialized) return NULL;
	Font f = calloc(1, sizeof(*f));
	f->instance = (HeliFontInstance*)heliStringmapGet(&fontCache, path);
	if (!f->instance) {
		f->instance = loadFont(path);
		heliStringmapAdd(&fontCache, path, f->instance, (HeliFreeCallback)&unloadFont);
	}
	++f->instance->refcount;
	heliObjectRegister(f, (HeliFreeCallback)&fontDestroy);
	return f;
}

void fontDestroy(Font f) {
	heliObjectUnregister(f);
	if (font == f) font = NULL;
	--f->instance->refcount;
	if (f->instance->refcount <= 0)
		heliStringmapRemove(&fontCache, f->instance->path);
	free(f);
}

void textAlign(HAlign hor, VAlign vert)
	{ horAlign = hor; vertAlign = vert; }
void textFontFamily(const char *family)
	{ font = NULL; free(fontFamily); fontFamily = heliStringCopy(family); }
void textFontDefault()
	{ font = NULL; free(fontFamily); fontFamily = NULL; }
void textFont(Font f)
	{ font = 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 && font->instance->font_face) {
		cairo_set_font_face(cr, font->instance->font_face);
	} else
	if (fontFamily) {
		cairo_select_font_face(cr, fontFamily, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
	} else {
		cairo_set_font_face(cr, NULL);
	}
	cairo_set_font_size(cr, fontSize);
	
	cairo_text_extents_t extents;
	cairo_text_extents(cr, text, &extents);
	double w = extents.width;
	double h = 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 - extents.x_bearing, y - extents.y_bearing);
	cairo_show_text(cr, text);
	
	if (font && font->instance->font_face)
		cairo_set_font_face(cr, NULL);
	
	cairo_restore(cr);
}

void heliDrawingClearFrame(cairo_t *cr) {
	cairo_save(cr);
	cairo_set_source_rgba(cr, colorBack[0], colorBack[1], colorBack[2], 1);
	cairo_paint(cr);
	cairo_restore(cr);
}

void heliDrawingPrepareFrame() {
	resetPath();
	if (heliCairo) heliDrawingClearFrame(heliCairo);
}

void heliDrawingFinish() {
	resetPath();
	free(path);
	path = NULL;
	pathAllocated = 0;
	free(fontFamily);
	fontFamily = NULL;
	heliArrayDestroy(&fontCache);
	if (ftInitialized && ftLibrary) FT_Done_FreeType(ftLibrary);
	ftLibrary = NULL;
	ftInitialized = FALSE;
}