Blame demo/src/font.c

b9c036
b9c036
#include <math.h></math.h>
b9c036
b9c036
#include <helianthus.h></helianthus.h>
b9c036
b9c036
#include "font.h"
b9c036
b9c036
b9c036
28a314
static Font font;
b9c036
b9c036
b9c036
void fontInit() {
b9c036
	font = createFont("data/fonts/blackcry.ttf");
b9c036
}
b9c036
b9c036
b9c036
void fontDraw() {
deef1d
	saveState();
3954ba
	
b9c036
	double x = 512;
b9c036
	double y = 256;
b9c036
	
d4e89f
	fill(colorByName("1 0 0 0.5"));
d7d433
	textSize(64*(1+sin(windowGetSeconds()/4)));
5d2371
	text(x, y, "Here is the\nleft aligned\ntext. VAW.");
b9c036
	
da4619
	noFill();
b9c036
	textFont(font);
b9c036
	textAlign(HALIGN_RIGHT, VALIGN_TOP);
5d2371
	text(x, y, "Here is the\nright aligned\ntext. VAW.");
b9c036
	
47ef74
	resetState();
20d197
	
47ef74
	x = 196;
47ef74
	y = 384;
47ef74
	double w = 128;
47ef74
	double h = 96;
47ef74
	int haligns[] = { HALIGN_LEFT, HALIGN_CENTER, HALIGN_RIGHT };
47ef74
	int valigns[] = { VALIGN_TOP, VALIGN_CENTER, VALIGN_BOTTOM };
47ef74
	const char *ht[] = { "left", "center I", "right" };
47ef74
	const char *vt[] = { "top", "center", "bottom q" };
47ef74
	for(int j = 0; j < 3; ++j) {
47ef74
		for(int i = 0; i < 3; ++i) {
47ef74
			double xx = x + (1-i)*w/2;
47ef74
			double yy = y + (1-j)*h/2;
47ef74
			textAlign(haligns[i], valigns[j]);
47ef74
			if (i == 1 && j == 1) {
7ebc4f
				TextLayout layout = createTextLayoutf("%s\n%s", ht[i], vt[j]);
7ebc4f
				textLayoutDraw(layout, xx, yy);
47ef74
				saveState();
47ef74
				strokeWidth(0.5);
47ef74
				double ex = 10;
47ef74
				double l = textLayoutGetLeft(layout);
47ef74
				double t = textLayoutGetTop(layout);
47ef74
				double w = textLayoutGetWidth(layout);
47ef74
				double h = textLayoutGetHeight(layout);
47ef74
				double a = textLayoutGetTopAscenderLine(layout);
47ef74
				double x = textLayoutGetTopXLine(layout);
47ef74
				double b = textLayoutGetBottomBaseline(layout);
47ef74
				line( xx+l-ex , yy+t    , xx+l+w+ex , yy+t      );
47ef74
				line( xx+l-ex , yy+t+h  , xx+l+w+ex , yy+t+h    );
47ef74
				line( xx+l    , yy+t-ex , xx+l      , yy+t+h+ex );
47ef74
				line( xx+l+w  , yy+t-ex , xx+l+w    , yy+t+h+ex );
47ef74
				line( xx+l-ex , yy+a    , xx+l+w+ex , yy+a      );
47ef74
				line( xx+l-ex , yy+x    , xx+l+w+ex , yy+x      );
47ef74
				line( xx+l-ex , yy+b    , xx+l+w+ex , yy+b      );
47ef74
				restoreState();
7ebc4f
				textLayoutDestroy(layout);
7ebc4f
			} else {
7ebc4f
				textf(xx, yy, "%s\n%s", ht[i], vt[j]);
47ef74
			}
47ef74
		}
47ef74
	}
47ef74
	line(x - 1.5*w, y - h/2, x + 1.5*w, y - h/2);
47ef74
	line(x - 1.5*w, y + h/2, x + 1.5*w, y + h/2);
47ef74
	line(x - w/2, y - 1.5*h, x - w/2, y + 1.5*h);
47ef74
	line(x + w/2, y - 1.5*h, x + w/2, y + 1.5*h);
47ef74
	
47ef74
	noFill();
47ef74
	textAlign(HALIGN_CENTER, VALIGN_CENTER);
47ef74
	const char *chars = "zlpvoxS";
47ef74
	for(int i = 0; i < 7; ++i)
47ef74
		textf(x - w/2 + (i + 1)/8.0*w, y + h/2, "%c", chars[i]);
47ef74
	
deef1d
	restoreState();
b9c036
}
b9c036
b9c036