Blame src/font.c

f63775
f63775
#include <ft2build.h></ft2build.h>
f63775
#include FT_FREETYPE_H
f63775
f63775
#include "private.h"
f63775
#include "font.h"
a20939
#include "drawing.h"
f63775
f63775
f63775
f63775
#define FONT_MAP_GLYPHSIZE      64
f63775
#define FONT_MAP_BORDERSIZE     16
f63775
#define FONT_MAP_CNT            16
f63775
#define FONT_MAP_GLYPHWORKSIZE  (FONT_MAP_GLYPHSIZE - FONT_MAP_BORDERSIZE)
f63775
#define FONT_MAP_FULLCNT        (FONT_MAP_CNT*FONT_MAP_CNT)
f63775
#define FONT_MAP_TEXSIZE        (FONT_MAP_CNT*FONT_MAP_GLYPHSIZE)
f63775
#define FONT_MAX_GLYPHS         (256*256)
f63775
#define FONT_BASE_SIZE          72
f63775
#define FONT_BADCHAR_CODE       0xFFFD
f63775
f63775
f63775
f63775
typedef struct _HeliFontMap HeliFontMap;
f63775
typedef struct _HeliFontInstance HeliFontInstance;
f63775
typedef struct _HeliCharDesc HeliCharDesc;
f63775
f63775
typedef struct _HeliGlyph {
f63775
	HeliFontMap *map;
f63775
	HeliFontInstance *fi;
f63775
	int code;
f63775
	int index;
f63775
	double l, t, r, b;
f63775
	double advance;
f63775
} HeliGlyph;
f63775
f63775
struct _HeliFontMap {
f63775
	HeliFontMap *prev, *next;
f63775
	unsigned int texid;
f63775
	int count;
f63775
	HeliGlyph glyphs[FONT_MAP_FULLCNT];
f63775
};
f63775
f63775
struct _HeliFontInstance {
f63775
	char *path;
f63775
	FT_Face face;
f63775
	double height;
f63775
	HeliGlyph *glyphs[FONT_MAX_GLYPHS];
f63775
	int refcount;
f63775
};
f63775
f63775
struct _HeliCharDesc {
f63775
	HeliGlyph *glyph;
da4619
	int charpos;
f63775
	double pos;
f63775
	double x, y;
f63775
	HeliCharDesc *next;
f63775
};
f63775
f63775
typedef struct _HeliLineDesc {
f63775
	int begin, end;
f63775
	double x, y;
f63775
	double l, t, r, b;
f63775
	double height;
f63775
} HeliLineDesc;
f63775
da4619
struct _TextLayout {
da4619
	Font font;
da4619
	int linesCount;
da4619
	HeliLineDesc *lines;
da4619
	HeliCharDesc *chars;
da4619
	double l, t, r, b;
da4619
};
da4619
f63775
struct _Font {
f63775
	HeliFontInstance *instance;
f63775
};
f63775
f63775
f63775
f63775
static int ftInitialized = 0;
f63775
static FT_Library ftLibrary = NULL;
f63775
static HeliArray fontCache;
f63775
static HeliFontMap *fontMapFirst, *fontMapLast;
f63775
static HeliGlyph blankGlyph;
f63775
static const char blankPath[] = "";
3954ba
static Font defaultFont = NULL;
f63775
f63775
static HeliFontMap* createFontMap() {
f63775
	HeliFontMap *map = calloc(1, sizeof(*map));
f63775
	map->prev = fontMapLast;
f63775
	*(fontMapLast ? &fontMapLast->next : &fontMapFirst) = map;
f63775
	fontMapLast = map;
f63775
	
f63775
	for(int i = 0; i < FONT_MAP_FULLCNT; ++i)
f63775
		map->glyphs[i].map = map;
f63775
	
f63775
	glGenTextures(1, &map->texid);
f63775
	glBindTexture(GL_TEXTURE_2D, map->texid);
f63775
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
f63775
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
f63775
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
f63775
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
b9c036
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -0.7f);
b9c036
	
f63775
	char *buffer = calloc(1, FONT_MAP_TEXSIZE*FONT_MAP_TEXSIZE);
b9c036
	int level = 0;
b9c036
	for(int size = FONT_MAP_TEXSIZE, border = FONT_MAP_BORDERSIZE; border > 0; border >>= 1, size >>= 1, ++level)
b9c036
		glTexImage2D(GL_TEXTURE_2D, level, GL_ALPHA, size, size, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
b9c036
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level - 1);
f63775
	free(buffer);
f63775
	glBindTexture(GL_TEXTURE_2D, 0);
f63775
	
f63775
	return map;
f63775
}
f63775
f63775
static void destroyFontMap(HeliFontMap *map) {
f63775
	assert(!map->count);
f63775
	glDeleteTextures(1, &map->texid);
f63775
	*(map->prev ? &map->prev->next : &fontMapFirst) = map->next;
f63775
	*(map->next ? &map->next->prev : &fontMapLast ) = map->prev;
f63775
	free(map);
f63775
}
f63775
f63775
f63775
static HeliGlyph* loadGlyph(HeliFontInstance *fi, int code) {
f63775
	int index = FT_Get_Char_Index(fi->face, (unsigned int)code);
f63775
	if (!index) return &blankGlyph;
f63775
	
f63775
	if (FT_Load_Glyph(fi->face, index, FT_LOAD_NO_HINTING))
f63775
		return &blankGlyph;
f63775
	
f63775
	HeliFontMap *map;
f63775
	for(map = fontMapFirst; map; map = map->next)
f63775
		if (map->count < FONT_MAP_FULLCNT) break;
f63775
	if (!map) map = createFontMap();
f63775
	
f63775
	HeliGlyph *g = NULL;
f63775
	for(int i = 0; i < FONT_MAP_FULLCNT; ++i)
f63775
		if (!map->glyphs[i].fi) { g = &map->glyphs[i]; break; }
f63775
	assert(g);
f63775
	
f63775
	const double ftkf = 1/64.0;
f63775
	const FT_Glyph_Metrics *metrics = &fi->face->glyph->metrics;
f63775
	g->l = metrics->horiBearingX*ftkf;
f63775
	g->r = (metrics->horiBearingX + metrics->width)*ftkf;
f63775
	g->t = -metrics->horiBearingY*ftkf;
f63775
	g->b = (-metrics->horiBearingY + metrics->height)*ftkf;
f63775
	g->advance = metrics->horiAdvance*ftkf;
f63775
	
b9c036
	unsigned char buffer[FONT_MAP_GLYPHSIZE * FONT_MAP_GLYPHSIZE] = {};
b9c036
	if (metrics->width > 0 && metrics->height > 0) {
b9c036
		FT_Matrix matrix = {};
b9c036
		matrix.xx = (FONT_MAP_GLYPHWORKSIZE-2)*64*65536/metrics->width;
b9c036
		matrix.yy = (FONT_MAP_GLYPHWORKSIZE-2)*64*65536/metrics->height;
b9c036
		FT_Vector offset = {};
c629d4
		offset.x = 64 - metrics->horiBearingX;
b9c036
		offset.y = -(64 + metrics->horiBearingY - metrics->height);
b9c036
		FT_Set_Transform(fi->face, &matrix, &offset);
b9c036
		int success = !FT_Load_Glyph(fi->face, index, FT_LOAD_NO_HINTING)
b9c036
				&& !FT_Render_Glyph(fi->face->glyph, FT_RENDER_MODE_NORMAL);
b9c036
		FT_Set_Transform(fi->face, NULL, NULL);
b9c036
		if (!success) return &blankGlyph;
b9c036
		
b9c036
		const FT_Bitmap *bitmap = &fi->face->glyph->bitmap;
c629d4
		int dr = 0;//fi->face->glyph->bitmap_top + 1 - FONT_MAP_GLYPHWORKSIZE;
c629d4
		int dc = 0;//-fi->face->glyph->bitmap_left;
b9c036
		int r0 = -dr;
b9c036
		int r1 = r0 + bitmap->rows;
b9c036
		int c0 = -dc;
b9c036
		int c1 = c0 + bitmap->width;
b9c036
		if (r0 < 0) r0 = 0;
b9c036
		if (c0 < 0) c0 = 0;
b9c036
		if (r1 > FONT_MAP_GLYPHWORKSIZE) r1 = FONT_MAP_GLYPHWORKSIZE;
b9c036
		if (c1 > FONT_MAP_GLYPHWORKSIZE) c1 = FONT_MAP_GLYPHWORKSIZE;
b9c036
		for(int r = r0; r < r1; ++r)
b9c036
			for(int c = c0; c < c1; ++c)
b9c036
				buffer[r*FONT_MAP_GLYPHSIZE + c] = *(bitmap->buffer + bitmap->pitch*(r + dr) + c + dc);
b9c036
	}
f63775
	
f63775
	glBindTexture(GL_TEXTURE_2D, g->map->texid);
f63775
	int gi = g - g->map->glyphs;
f63775
	int r = gi/FONT_MAP_CNT;
f63775
	int c = gi%FONT_MAP_CNT;
b9c036
	int size = FONT_MAP_GLYPHSIZE;
b9c036
	for(int level = 0, border = FONT_MAP_BORDERSIZE; border > 0; border >>= 1, ++level) {
f63775
		glTexSubImage2D(
f63775
			GL_TEXTURE_2D,
f63775
			level,
b9c036
			c*size,
b9c036
			r*size,
f63775
			size,
f63775
			size,
f63775
			GL_ALPHA,
f63775
			GL_UNSIGNED_BYTE,
f63775
			buffer );
f63775
		if (border > 1) {
b9c036
			size >>= 1;
f63775
			for(int r = 0; r < size; ++r)
f63775
				for(int c = 0; c < size; ++c)
f63775
					buffer[r*size + c] = (unsigned char)((
b9c036
					    (unsigned int)buffer[((r*2 + 0)*size + c)*2 + 0]
b9c036
					  + (unsigned int)buffer[((r*2 + 0)*size + c)*2 + 1]
b9c036
					  + (unsigned int)buffer[((r*2 + 1)*size + c)*2 + 0]
b9c036
					  + (unsigned int)buffer[((r*2 + 1)*size + c)*2 + 1] + 2) >> 2 );
f63775
		}
f63775
	}
f63775
	glBindTexture(GL_TEXTURE_2D, 0);
f63775
	
f63775
	g->fi = fi;
f63775
	g->code = code;
f63775
	g->index = index;
b9c036
	++g->map->count;
f63775
	return g;
f63775
}
f63775
f63775
f63775
static void unloadGlyph(HeliGlyph *g) {
f63775
	g->fi = NULL;
f63775
	g->code = 0;
f63775
	g->index = 0;
f63775
	if (g->map && --g->map->count <= 0) 
f63775
		destroyFontMap(g->map);
f63775
}
f63775
f63775
f63775
static HeliFontInstance* loadFont(const char *path) {
f63775
	HeliFontInstance *fi = calloc(1, sizeof(*fi));
f63775
	fi->path = heliStringCopy(path);
f63775
	
f63775
	if (!ftInitialized) {
f63775
		if (FT_Init_FreeType(&ftLibrary))
3954ba
			fprintf(stderr, "helianthus: cannot initialize FreeType library\n");
f63775
		ftInitialized = TRUE;
f63775
	}
f63775
	if (ftLibrary) {
f63775
		if (path[0]) {
f63775
			if (FT_New_Face(ftLibrary, path, 0, &fi->face)) {
3954ba
				fprintf(stderr, "helianthus: cannot load font from file: %s\n", path);
f63775
				fi->face = NULL;
f63775
			}
f63775
		} else {
f63775
			FT_Open_Args args = {};
f63775
			args.flags = FT_OPEN_MEMORY;
f63775
			args.memory_base = (FT_Byte*)heliBlobDefaultFont;
f63775
			args.memory_size = heliBlobDefaultFontSize;
b9c036
			if (FT_Open_Face(ftLibrary, &args, 0, &fi->face)) {
3954ba
				fprintf(stderr, "helianthus: cannot initialize default font\n");
f63775
				fi->face = NULL;
f63775
			}
f63775
		}
f63775
		if (fi->face) {
f63775
			FT_Set_Char_Size(fi->face, 0, FONT_BASE_SIZE*64, 72, 72);
f63775
			fi->height = fi->face->size->metrics.height/64.0;
f63775
		}
f63775
	}
f63775
	
f63775
	return fi;
f63775
}
f63775
f63775
f63775
static void unloadFont(HeliFontInstance *fi) {
f63775
	assert(!fi->refcount);
f63775
	for(int i = 0; i < FONT_MAX_GLYPHS; ++i)
f63775
		if (fi->glyphs[i]) unloadGlyph(fi->glyphs[i]);
f63775
	free(fi->path);
f63775
	if (fi->face) FT_Done_Face(fi->face);
f63775
	free(fi);
f63775
}
f63775
f63775
f63775
static HeliGlyph* getGlyph(HeliFontInstance *fi, int code) {
f63775
	if (code < 0 || code >= FONT_MAX_GLYPHS) return &blankGlyph;
a20939
	if (!fi->glyphs[code]) fi->glyphs[code] = loadGlyph(fi, code);
f63775
	return fi->glyphs[code];
f63775
}
f63775
f63775
f63775
Font createFont(const char *path) {
f63775
	if (!heliInitialized) return NULL;
f63775
	if (!path) path = blankPath;
f63775
	Font f = calloc(1, sizeof(*f));
da4619
	HeliPair *item = heliStringmapGet(&fontCache, path);
da4619
	if (!item) item = heliStringmapAdd(&fontCache, path, loadFont(path), (HeliFreeCallback)&unloadFont);
da4619
	f->instance = (HeliFontInstance*)item->value;
f63775
	++f->instance->refcount;
f63775
	heliObjectRegister(f, (HeliFreeCallback)&fontDestroy);
f63775
	return f;
f63775
}
f63775
f63775
void fontDestroy(Font f) {
f63775
	heliObjectUnregister(f);
3954ba
	HeliDrawingState *ss = heliDrawingGetState();
3954ba
	for(HeliDrawingState *s = heliDrawingGetStateStack(); s <= ss; ++s)
3954ba
		{ if (ss->font == f) ss->font = NULL; }
f63775
	--f->instance->refcount;
f63775
	if (f->instance->refcount <= 0)
f63775
		heliStringmapRemove(&fontCache, f->instance->path);
f63775
	free(f);
f63775
}
f63775
3954ba
void textAlign(HAlign hor, VAlign vert) {
3954ba
	HeliDrawingState *s = heliDrawingGetState();
3954ba
	s->horAlign = hor;
3954ba
	s->vertAlign = vert;
3954ba
}
f63775
void textFontDefault()
3954ba
	{ heliDrawingGetState()->font = NULL; }
f63775
void textFont(Font f)
3954ba
	{ heliDrawingGetState()->font = f; }
f63775
void textSize(double size)
3954ba
	{ heliDrawingGetState()->fontSize = size; }
f63775
f63775
da4619
static void expand(double *min, double *max, double x) {
da4619
	if (*min > x) *min = x;
da4619
	if (*max < x) *max = x;
da4619
}
da4619
da4619
da4619
TextLayout createTextLayout(const char *text) {
da4619
	TextLayout layout = calloc(1, sizeof(*layout));
f63775
	
da4619
	if (!text || !text[0]) return layout;
534343
	
3954ba
	HeliDrawingState *drawingState = heliDrawingGetState();
3954ba
	
f63775
	if (!defaultFont) defaultFont = createFont(NULL);
3954ba
	HeliFontInstance *f = drawingState->font ? drawingState->font->instance : NULL;
f63775
	HeliFontInstance *ff = defaultFont ? defaultFont->instance : NULL;
f63775
	if (!f) f = ff;
f63775
	if (!ff) ff = f;
da4619
	if (!f) return layout;
f63775
	
f63775
	if (!f->face) f = ff;
f63775
	if (!ff->face) ff = f;
da4619
	if (!f->face) return layout;
f63775
	
f63775
	int linesAllocated = 256;
da4619
	layout->linesCount = 1;
da4619
	layout->lines = calloc(linesAllocated, sizeof(HeliLineDesc));
da4619
	HeliLineDesc *line = layout->lines;
f63775
	line->height = f->height;
da4619
	layout->chars = calloc(strlen(text) + 1, sizeof(HeliCharDesc));
da4619
	if (defaultFont && defaultFont->instance != f)
da4619
		layout->font = createFont(f->path);
f63775
	
f63775
	// gather glyphs
f63775
	const char *c = text;
f63775
	while(1) {
da4619
		const char *charpos = c;
f63775
		int code = FONT_BADCHAR_CODE;
f63775
		while((*c & 0xC0) == 0x80) ++c;
f63775
		if ((*c & 0x80) == 0x00) {
f63775
			code = *c;
f63775
		} else
f63775
		if ((*c & 0xE0) == 0xC0) {
f63775
			if ((c[1] & 0xC0) == 0x80) {
f63775
				code = ((c[0] & 0x1F) << 6) | (c[1] & 0x3F);
f63775
				c += 1;
f63775
			}
f63775
		} else
f63775
		if ((*c & 0xF0) == 0xE0) {
f63775
			if ((c[1] & 0xC0) == 0x80 && (c[2] & 0xC0) == 0x80) {
f63775
				code = ((c[0] & 0x0F) << 12) | ((c[1] & 0x3F) << 6) | (c[2] & 0x3F);
f63775
				c += 2;
f63775
			}
f63775
		}
f63775
		++c;
f63775
		
f63775
		HeliGlyph *glyph = &blankGlyph;
da4619
		int code2 = (code == 0 || code == 10 || code == 13) ? 32 : code;
da4619
		if (!glyph->fi) glyph = getGlyph(f, code2);
da4619
		if (!glyph->fi) glyph = getGlyph(ff, code2);
a20939
		if (!glyph->fi) glyph = getGlyph(f, FONT_BADCHAR_CODE);
a20939
		if (!glyph->fi) glyph = getGlyph(ff, FONT_BADCHAR_CODE);
f63775
		if (!glyph->fi) continue;
f63775
		
da4619
		HeliCharDesc *cc = &layout->chars[line->end++];
da4619
		cc->charpos = charpos - text;
f63775
		cc->glyph = glyph;
f63775
		if (line->height < glyph->fi->height) line->height = glyph->fi->height;
da4619
		if (line->begin < line->end - 1) {
f63775
			HeliCharDesc *cp = cc - 1;
f63775
			cc->pos = cp->pos + cp->glyph->advance;
f63775
			FT_Vector kerning = {};
f63775
			if ( cp->glyph->fi == cc->glyph->fi 
f63775
			  && !FT_Get_Kerning(
f63775
					cc->glyph->fi->face,
f63775
					cp->glyph->index,
f63775
					cc->glyph->index,
f63775
					FT_KERNING_UNFITTED,
f63775
					&kerning ))
f63775
				cc->pos += kerning.x/64.0;
f63775
		}
da4619
		expand(&line->l, &line->r, cc->pos + cc->glyph->l);
da4619
		expand(&line->l, &line->r, cc->pos + cc->glyph->r);
da4619
		expand(&line->t, &line->b, cc->glyph->t);
da4619
		expand(&line->t, &line->b, cc->glyph->b);
da4619
		expand(&line->t, &line->b, -glyph->fi->height);
da4619
		
da4619
		if (code == 10 || code == 13) {
da4619
			if ((code == 10 && *c == 13) || (code == 13 && *c == 10)) ++c;
da4619
			if (layout->linesCount >= linesAllocated) {
da4619
				linesAllocated += linesAllocated/4;
da4619
				layout->lines = realloc(layout->lines, linesAllocated*sizeof(HeliLineDesc));
da4619
				memset(layout->lines + layout->linesCount, 0, (linesAllocated - layout->linesCount)*sizeof(HeliLineDesc));
da4619
				line = &layout->lines[layout->linesCount - 1];
da4619
			}
da4619
			++line;
da4619
			++layout->linesCount;
da4619
			line->begin = line->end = (line - 1)->end;
da4619
			line->height = f->height;
da4619
		}
da4619
		if (code == 0) break;
f63775
	}
f63775
	
da4619
	int charsCount = layout->lines[layout->linesCount-1].end;
da4619
	if (charsCount <= 0) return layout;
534343
f63775
	// bounds
534343
	double l = 0;
da4619
	double t = -layout->lines->height;
534343
	double r = 0;
534343
	double b = 0;
da4619
	for(int i = 0; i < layout->linesCount; ++i) {
da4619
		HeliLineDesc *line = layout->lines + i;
534343
		if (i > 0) line->y = (line-1)->y + line->height;
f63775
		if (l > line->l) l = line->l;
f63775
		if (r < line->r) r = line->r;
f63775
		if (t > line->t + line->y) t = line->t + line->y;
f63775
		if (b < line->b + line->y) b = line->b + line->y;
f63775
	}
f63775
	
f63775
	// alignment
da4619
	double dx = drawingState->horAlign  == HALIGN_RIGHT  ? -r
da4619
	          : drawingState->horAlign  == HALIGN_CENTER ? -0.5*(l + r)
da4619
	          :                                            -l;
3954ba
	double dy = drawingState->vertAlign == VALIGN_BOTTOM ? -b
3954ba
	          : drawingState->vertAlign == VALIGN_CENTER ? -0.5*(t + b)
3954ba
	          :                                            -t;
da4619
	for(int i = 0; i < layout->linesCount; ++i) {
da4619
		HeliLineDesc *line = layout->lines + i;
3954ba
		line->x = drawingState->horAlign == HALIGN_RIGHT  ? -line->r
3954ba
		        : drawingState->horAlign == HALIGN_CENTER ? -0.5*(line->l + line->r)
3954ba
		        :                                           -line->l;
f63775
		line->y += dy;
f63775
		for(int j = line->begin; j < line->end; ++j) {
da4619
			HeliCharDesc *cc = layout->chars + j;
f63775
			cc->x = cc->pos + line->x;
f63775
			cc->y = line->y;
f63775
		}
f63775
	}
f63775
	
da4619
	// bounds
da4619
	layout->l = l + dx;
da4619
	layout->t = t + dy;
da4619
	layout->r = r + dx;
da4619
	layout->b = b + dy;
da4619
	
f63775
	// sort by textures	
b9c036
	for(int i = 0; i < charsCount-1; ++i)
da4619
		layout->chars[i].next = &layout->chars[i+1];
da4619
	HeliCharDesc *first = NULL, *last = NULL, *current = layout->chars;
f63775
	while(current) {
f63775
		if (!current->next) {
f63775
			current = current->next = first;
f63775
		} else
f63775
		if (current->glyph->map != current->next->glyph->map) {
f63775
			*(last ? &last->next : &first) = current->next;
f63775
			last = current->next;
f63775
			current->next = current->next->next;
f63775
			last->next = NULL;
f63775
		} else {
f63775
			current = current->next;
f63775
		}
f63775
	}
da4619
da4619
	return layout;
da4619
}
da4619
da4619
da4619
void textLayoutDestroy(TextLayout layout) {
da4619
	if (layout) {
da4619
		free(layout->lines);
da4619
		free(layout->chars);
da4619
		if (layout->font) fontDestroy(layout->font);
da4619
	}
da4619
	free(layout);
da4619
}
da4619
da4619
da4619
void textLayoutDrawSubstr(TextLayout layout, double x, double y, int start, int length) {
da4619
	resetPath();
da4619
	
da4619
	if (layout->linesCount <= 0) return;
da4619
	assert(layout->lines);
da4619
	assert(layout->chars);
da4619
	int charsCount = layout->lines[layout->linesCount - 1].end;
da4619
	if (charsCount <= 0) return;
da4619
	
da4619
	int textEnd = layout->chars[charsCount - 1].charpos;
da4619
	int begin = start;
da4619
	if (begin < 0) begin = 0;
da4619
	long long endll = (long long)begin + length; // to avoid overflow on length == INT_MAX and begin > 0
da4619
	if (endll > textEnd) endll = textEnd;
da4619
	int end = (int)endll;
da4619
	if (begin >= end) return;
da4619
	
da4619
	HeliDrawingState *drawingState = heliDrawingGetState();
f63775
	
da4619
	double fontScale = drawingState->fontSize/FONT_BASE_SIZE;
da4619
	if (fontScale <= HELI_PRECISION) return;
da4619
	if ( drawingState->colorStroke[3] <= HELI_PRECISION
da4619
	  && drawingState->colorFill[3] <= HELI_PRECISION ) return;
da4619
f63775
	const double borderK = 0.5*FONT_MAP_BORDERSIZE/FONT_MAP_GLYPHWORKSIZE;
f63775
	const double texStep = 1.0/FONT_MAP_CNT;
f63775
	const double texOffset = -0.5*FONT_MAP_BORDERSIZE/FONT_MAP_TEXSIZE;
f63775
	
8bc1f1
	if (!heliGLIsIntegerClipping()) glEnable(GL_MULTISAMPLE);
f63775
	glPushMatrix();
f63775
	glTranslated(x, y, 0);
f63775
	glScaled(fontScale, fontScale, 1);
da4619
	
da4619
	if (drawingState->colorFill[3] > HELI_PRECISION) {
da4619
		double strokeAlpha = drawingState->colorStroke[3];
da4619
		drawingState->colorStroke[3] = 0;
da4619
		for(int i = 0; i < layout->linesCount; ++i) {
da4619
			HeliLineDesc *line = layout->lines + i;
da4619
			int found = FALSE;
da4619
			double l = 0, r = 0;
da4619
			for(int j = line->begin; j < line->end; ++j) {
da4619
				HeliCharDesc *cc = layout->chars + j;
da4619
				if (cc->charpos < begin || cc->charpos > end) continue;
da4619
				if (!found) { l = r = cc->x; found = TRUE; }
da4619
				expand(&l, &r, cc->x);
da4619
				if (cc->glyph && cc->charpos < end) {
da4619
					expand(&l, &r, cc->x + cc->glyph->l);
da4619
					expand(&l, &r, cc->x + cc->glyph->r);
da4619
				}
da4619
			}
da4619
			if (found)
da4619
				rect(l, line->t + line->y, r-l, line->b - line->t);
f63775
		}
da4619
		drawingState->colorStroke[3] = strokeAlpha;
f63775
	}
da4619
	
da4619
	if (drawingState->colorStroke[3] > HELI_PRECISION) {
da4619
		glColor4dv(drawingState->colorStroke);
da4619
		HeliFontMap *currentMap = NULL;
da4619
		glEnable(GL_TEXTURE_2D);
da4619
		glBegin(GL_QUADS);
da4619
		for(HeliCharDesc *cc = layout->chars; cc; cc = cc->next) {
da4619
			if (cc->charpos < begin || cc->charpos >= end) continue;
da4619
			if ( !cc->glyph->map
da4619
			|| cc->glyph->r - cc->glyph->l <= HELI_PRECISION
da4619
			|| cc->glyph->b - cc->glyph->t <= HELI_PRECISION )
da4619
				continue;
da4619
			if (currentMap != cc->glyph->map) {
da4619
				glEnd();
da4619
				currentMap = cc->glyph->map;
da4619
				glBindTexture(GL_TEXTURE_2D, currentMap->texid);
da4619
				glBegin(GL_QUADS);
da4619
			}
da4619
			
da4619
			double dx = (cc->glyph->r - cc->glyph->l)*borderK;
da4619
			double dy = (cc->glyph->b - cc->glyph->t)*borderK;
da4619
			double l = cc->x + cc->glyph->l - dx;
da4619
			double t = cc->y + cc->glyph->t - dy;
da4619
			double r = cc->x + cc->glyph->r + dx;
da4619
			double b = cc->y + cc->glyph->b + dy;
da4619
			
da4619
			int gi = cc->glyph - cc->glyph->map->glyphs;
da4619
			double tl = (gi % FONT_MAP_CNT)*texStep + texOffset;
da4619
			double tt = (gi / FONT_MAP_CNT)*texStep + texOffset;
da4619
			double tr = tl + texStep;
da4619
			double tb = tt + texStep;
da4619
			
da4619
			glTexCoord2d(tl, tt); glVertex2d(l, t);
da4619
			glTexCoord2d(tr, tt); glVertex2d(r, t);
da4619
			glTexCoord2d(tr, tb); glVertex2d(r, b);
da4619
			glTexCoord2d(tl, tb); glVertex2d(l, b);
da4619
		}
da4619
		glEnd();
da4619
		glBindTexture(GL_TEXTURE_2D, 0);
da4619
		glDisable(GL_TEXTURE_2D);
da4619
	}
da4619
	
f63775
	glPopMatrix();
8bc1f1
	glDisable(GL_MULTISAMPLE);
f63775
	glColor4d(1, 1, 1, 1);
f63775
}
a20939
a20939
da4619
void textLayoutDrawFrom(TextLayout layout, double x, double y, int start)
da4619
	{ textLayoutDrawSubstr(layout, x, y, start, INT_MAX); }
da4619
void textLayoutDraw(TextLayout layout, double x, double y)
da4619
	{ textLayoutDrawFrom(layout, x, y, 0); }
da4619
da4619
da4619
void text(const char *text, double x, double y) {
da4619
	resetPath();
da4619
	if (!text || !text[0]) return;
da4619
	
da4619
	HeliDrawingState *drawingState = heliDrawingGetState();
da4619
	double fontScale = drawingState->fontSize/FONT_BASE_SIZE;
da4619
	if (fontScale < HELI_PRECISION) return;
da4619
	if ( drawingState->colorStroke[3] < HELI_PRECISION
da4619
	  && drawingState->colorFill[3] < HELI_PRECISION ) return;
da4619
	
da4619
	TextLayout layout = createTextLayout(text);
da4619
	textLayoutDraw(layout, x, y);
da4619
	textLayoutDestroy(layout);
da4619
}
da4619
da4619
da4619
double textLayoutGetWidth(TextLayout layout) {
da4619
	HeliDrawingState *drawingState = heliDrawingGetState();
da4619
	double fontScale = drawingState->fontSize/FONT_BASE_SIZE;
da4619
	return (layout->r - layout->l)*fontScale;
da4619
}
da4619
da4619
da4619
double textLayoutGetHeight(TextLayout layout) {
da4619
	HeliDrawingState *drawingState = heliDrawingGetState();
da4619
	double fontScale = drawingState->fontSize/FONT_BASE_SIZE;
da4619
	return (layout->b - layout->t)*fontScale;
da4619
}
da4619
da4619
a20939
void heliFontFinish() {
a20939
	heliArrayDestroy(&fontCache);
a20939
	if (ftInitialized && ftLibrary) FT_Done_FreeType(ftLibrary);
a20939
	ftLibrary = NULL;
a20939
	ftInitialized = FALSE;
a20939
}
da4619
da4619
static void findChar(TextLayout layout, int cursor, int *lineIndex, int *charIndex) {
da4619
	*lineIndex = 0;
da4619
	*charIndex = 0;
da4619
	if (!layout || layout->linesCount <= 0 || !layout->lines || !layout->chars) return;
da4619
	
da4619
	*lineIndex = layout->linesCount - 1;
da4619
	*charIndex = layout->lines[*lineIndex].end - 1;
da4619
	for(int i = 0; i < layout->linesCount; ++i) {
da4619
		HeliLineDesc *line = layout->lines + i;
da4619
		if (line->begin < line->end) {
da4619
			if (layout->chars[line->end-1].charpos >= cursor) {
da4619
				*lineIndex = i;
da4619
				*charIndex = line->end-1;
da4619
				for(int j = line->begin; j < line->end; ++j)
da4619
					if (layout->chars[j].charpos >= cursor)
da4619
						{ *charIndex = j; break; }
da4619
				return;
da4619
			}
da4619
		}
da4619
	}
da4619
}
da4619
da4619
static int moveCursorVert(TextLayout layout, int cursor, int offset) {
da4619
	if (!layout || layout->linesCount <= 0 || !layout->lines || !layout->chars) return 0;
da4619
	
da4619
	int li = 0, ci = 0;
da4619
	findChar(layout, cursor, &li, &ci);
da4619
	
da4619
	li += offset;
da4619
	if (li < 0)
da4619
			return 0;
da4619
	if (li >= layout->linesCount)
da4619
		return layout->chars[ layout->lines[layout->linesCount-1].end - 1 ].charpos;
da4619
	
da4619
	double x = layout->chars[ci].x;
da4619
	HeliLineDesc *line = layout->lines + li;
da4619
	for(int j = line->begin; j < line->end; ++j) {
da4619
		HeliCharDesc *cc1 = layout->chars + j;
da4619
		double xx = layout->chars[j].x; 
da4619
		if (xx + HELI_PRECISION >= x || j == line->end - 1) {
da4619
			if (j > line->begin) {
da4619
				HeliCharDesc *cc0 = cc1 - 1;
da4619
				if (fabs(cc0->x - x) < fabs(xx - x)) return cc0->charpos;
da4619
			}
da4619
			return cc1->charpos;
da4619
		}
da4619
	}
da4619
	return cursor;
da4619
}
da4619
da4619
int textLayoutCursorUp(TextLayout layout, int cursor)
da4619
	{ return moveCursorVert(layout, cursor, -1); }
da4619
da4619
int textLayoutCursorDown(TextLayout layout, int cursor)
da4619
	{ return moveCursorVert(layout, cursor, 1); }
da4619
da4619
double textLayoutCursorGetX(TextLayout layout, int cursor) {
da4619
	if (!layout || layout->linesCount <= 0 || !layout->lines || !layout->chars) return 0;
da4619
	HeliDrawingState *drawingState = heliDrawingGetState();
da4619
	double fontScale = drawingState->fontSize/FONT_BASE_SIZE;
da4619
	int li = 0, ci = 0;
da4619
	findChar(layout, cursor, &li, &ci);
da4619
	return layout->chars[ci].x * fontScale;
da4619
}
da4619
da4619
double textLayoutCursorGetY(TextLayout layout, int cursor) {
da4619
	if (!layout || layout->linesCount <= 0 || !layout->lines || !layout->chars) return 0;
da4619
	HeliDrawingState *drawingState = heliDrawingGetState();
da4619
	double fontScale = drawingState->fontSize/FONT_BASE_SIZE;
da4619
	int li = 0, ci = 0;
da4619
	findChar(layout, cursor, &li, &ci);
da4619
	return layout->chars[ci].y * fontScale;
da4619
}
da4619
da4619
double textLayoutCursorGetHeight(TextLayout layout, int cursor) {
da4619
	if (!layout || layout->linesCount <= 0 || !layout->lines || !layout->chars) return 0;
da4619
	HeliDrawingState *drawingState = heliDrawingGetState();
da4619
	double fontScale = drawingState->fontSize/FONT_BASE_SIZE;
da4619
	int li = 0, ci = 0;
da4619
	findChar(layout, cursor, &li, &ci);
da4619
	return layout->lines[li].height * fontScale;
da4619
}