1 module bgfx_extras.fontstash;
2 
3 private {
4     import derelict.freetype.types : FT_Face;
5 }
6 
7 //
8 // Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty.  In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 // Permission is granted to anyone to use this software for any purpose,
14 // including commercial applications, and to alter it and redistribute it
15 // freely, subject to the following restrictions:
16 // 1. The origin of this software must not be misrepresented; you must not
17 //    claim that you wrote the original software. If you use this software
18 //    in a product, an acknowledgment in the product documentation would be
19 //    appreciated but is not required.
20 // 2. Altered source versions must be plainly marked as such, and must not be
21 //    misrepresented as being the original software.
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
24 
25 enum FONS_INVALID = -1;
26 
27 enum FONS_SCRATCH_BUF_SIZE = 16000;
28 enum FONS_HASH_LUT_SIZE = 256;
29 enum FONS_INIT_FONTS = 4;
30 enum FONS_INIT_GLYPHS = 256;
31 enum FONS_INIT_ATLAS_NODES = 256;
32 enum FONS_VERTEX_COUNT = 1024;
33 enum FONS_MAX_STATES = 20;
34 
35 enum FONSflags {
36 	FONS_ZERO_TOPLEFT = 1,
37 	FONS_ZERO_BOTTOMLEFT = 2,
38 }
39 
40 enum FONSalign {
41 	// Horizontal align
42 	FONS_ALIGN_LEFT 	= 1<<0,	// Default
43 	FONS_ALIGN_CENTER 	= 1<<1,
44 	FONS_ALIGN_RIGHT 	= 1<<2,
45 	// Vertical align
46 	FONS_ALIGN_TOP 		= 1<<3,
47 	FONS_ALIGN_MIDDLE	= 1<<4,
48 	FONS_ALIGN_BOTTOM	= 1<<5,
49 	FONS_ALIGN_BASELINE	= 1<<6, // Default
50 }
51 
52 enum FONSerrorCode {
53 	// Font atlas is full.
54 	FONS_ATLAS_FULL = 1,
55 	// Scratch memory used to render glyphs is full, requested size reported in 'val', you may need to bump up FONS_SCRATCH_BUF_SIZE.		
56 	FONS_SCRATCH_FULL = 2,
57 	// Calls to fonsPushState has created too large stack, if you need deep state stack bump up FONS_MAX_STATES.
58 	FONS_STATES_OVERFLOW = 3,
59 	// Trying to pop too many states fonsPopState().
60 	FONS_STATES_UNDERFLOW = 4,
61 }
62 
63 struct FONSparams {
64 	int width, height;
65 	ubyte flags;
66 	void* userPtr;
67     int delegate(void* uptr, int width, int height) renderCreate;
68 	int delegate(void* uptr, int width, int height) renderResize;
69 	void delegate(void* uptr, int* rect, ubyte* data) renderUpdate;
70 	void delegate(void* uptr, float* verts, float* tcoords, uint* colors, int nverts) renderDraw;
71 	void delegate(void* uptr) renderDelete;
72 }
73 
74 struct FONSquad {
75 	float x0,y0,s0,t0;
76 	float x1,y1,s1,t1;
77 }
78 
79 struct FONStextIter {
80 	float x, y, nextx, nexty, scale, spacing;
81 	uint codepoint;
82 	short isize, iblur;
83 	FONSfont* font;
84 	int prevGlyphIndex;
85 	const(char)* str;
86 	const(char)* next;
87 	const(char)* end;
88 	uint utf8state;
89 }
90 
91 struct FONSttFontImpl {
92 	FT_Face font;
93 }
94 
95 struct FONSglyph {
96 	uint codepoint;
97 	int index;
98 	int next;
99 	short size, blur;
100 	short x0,y0,x1,y1;
101 	short xadv,xoff,yoff;
102 }
103 
104 struct FONSfont {
105 	FONSttFontImpl font;
106 	char[64] name;
107 	ubyte* data;
108 	int dataSize;
109 	ubyte freeData;
110 	float ascender;
111 	float descender;
112 	float lineh;
113 	FONSglyph* glyphs;
114 	int cglyphs;
115 	int nglyphs;
116 	int[FONS_HASH_LUT_SIZE] lut;
117 }
118 
119 struct FONSstate {
120 	int font;
121 	int align_;
122 	float size;
123 	uint color;
124 	float blur;
125 	float spacing;
126 }
127 
128 struct FONSatlasNode {
129     short x, y, width;
130 }
131 
132 struct FONSatlas {
133 	int width, height;
134 	FONSatlasNode* nodes;
135 	int nnodes;
136 	int cnodes;
137 }
138 
139 struct FONScontext {
140 	FONSparams params;
141 	float itw,ith;
142 	ubyte* texData;
143 	int[4] dirtyRect;
144 	FONSfont** fonts;
145 	FONSatlas* atlas;
146 	int cfonts;
147 	int nfonts;
148 	float[FONS_VERTEX_COUNT*2] verts;
149 	float[FONS_VERTEX_COUNT*2] tcoords;
150 	uint[FONS_VERTEX_COUNT] colors;
151 	int nverts;
152 	ubyte* scratch;
153 	int nscratch;
154 	FONSstate[FONS_MAX_STATES] states;
155 	int nstates;
156 	void delegate(void* uptr, int error, int val) handleError;
157 	void* errorUptr;
158 }
159 
160 // Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
161 // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
162 
163 enum FONS_UTF8_ACCEPT = 0;
164 enum FONS_UTF8_REJECT = 12;