summaryrefslogtreecommitdiff
path: root/graphics/fonts.cpp
blob: 760e67694fab90243b608357e5a2fac542326152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

#include "fonts.h"
#include <algorithm>
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtx/transform.hpp"

#ifdef __APPLE__
#define sggBindVertexArray glBindVertexArrayAPPLE
#define sggGenVertexArrays glGenVertexArraysAPPLE
#else
#define sggBindVertexArray glBindVertexArray
#define sggGenVertexArrays glGenVertexArrays
#endif

const char* __FontVertexShader = R"(
#version 120

attribute vec4 coord;
varying vec2 texcoord;
uniform mat4 projection;
uniform mat4 modelview;

void main(void) {
  gl_Position = projection * modelview * vec4(coord.xy, 0, 1);
  texcoord = coord.zw;
}
)";

const char* __FontFragmentShader = R"(
#version 120

varying vec2 texcoord;
uniform vec4 color1;
uniform vec4 color2;
uniform sampler2D tex;
uniform vec2 gradient;

void main(void) {
	vec4 color = mix( color1, color2, dot(texcoord,gradient));
	gl_FragColor = vec4(1, 1, 1, texture2D(tex, texcoord).r) * color;
}
)";



bool FontLib::init()
{
	if (FT_Init_FreeType(&m_ft))
	{
		return false;
	}
	
	glGetError();
	const GLubyte * s = glewGetErrorString(glGetError());

	m_font_shader = Shader(__FontVertexShader, __FontFragmentShader);
		

	if (!m_font_shader.init())
		return false;
	
	unsigned int attrib_position = m_font_shader.getAttributeLocation("coord");
		
	sggGenVertexArrays(1, &m_font_vao);
	sggBindVertexArray(m_font_vao);
		
	glGenBuffers(1, &m_font_vbo);
	glBindBuffer(GL_ARRAY_BUFFER, m_font_vbo);
	glEnableVertexAttribArray(attrib_position); 
	glVertexAttribPointer(attrib_position, 4, GL_FLOAT, GL_FALSE, 0, 0);

	GLfloat box[4][4] = {
		{ -1, 1    , 0, 0 },
		{ 1, 1    , 1, 0 },
		{ -1, -1, 0, 1 },
		{ 1, -1, 1, 1 },
	};
	glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);

	m_curr_font = m_fonts.end();

	return true;
}

void FontLib::submitText(const TextRecord & text)
{
	m_content.push_back(text);
}

void FontLib::drawText(TextRecord entry)
{
	float x = 0.0f;  
	float y = 0.0f;  
	
	const char *p;
	if (m_curr_font == m_fonts.end())
		return;
	Font font = m_curr_font->second;
	FT_GlyphSlot g = font.face->glyph;

#ifndef __APPLE__
	//glFrontFace(GL_CW);
#endif
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, font.font_tex);
	static int c = 0;

	m_font_shader.use();

	m_font_shader["tex"] = 0;
	
	m_font_shader["color1"] = entry.color1;
	m_font_shader["color2"] = (entry.use_gradient? entry.color2 : entry.color1);
	m_font_shader["gradient"] = entry.gradient;
	m_font_shader["projection"] = entry.proj;
	   
	for (p = entry.text.c_str(); *p; p++) {
		if (FT_Load_Char(font.face, *p, FT_LOAD_RENDER))
			continue;
		
		
		glTexImage2D(
			GL_TEXTURE_2D,
			0,
			GL_RED,
			g->bitmap.width,
			g->bitmap.rows,
			0,
			GL_RED,
			GL_UNSIGNED_BYTE,
			g->bitmap.buffer
			);


		float w = g->bitmap.width;
		float h = g->bitmap.rows; 
		w = entry.size.x * g->bitmap.width / (float)m_font_res;
		h = entry.size.y * g->bitmap.rows / (float)m_font_res;
		float b = g->metrics.horiBearingY/(64* (float)m_font_res)*entry.size.y - h;

		GLfloat box[4][4] = {
			{ x,     y-b    , 0, 1 },
			{ x + w, y-b    , 1, 1 },
			{ x,     y - h - b, 0, 0 },
			{ x + w, y - h -b, 1, 0 },
		};
		
		m_font_shader["modelview"] = glm::translate(glm::vec3(entry.pos.x, entry.pos.y, 0.0f)) * entry.mv;


		sggBindVertexArray(m_font_vao);
		glBindBuffer(GL_ARRAY_BUFFER, m_font_vbo);
		glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
		
		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
		x += std::max(w+ entry.size.x*0.05f, entry.size.x*0.15f);
		y += entry.size.y*1.1f*(g->advance.y);
	}
	
	glBindTexture(GL_TEXTURE_2D, 0);
	glFrontFace(GL_CCW);
}

void FontLib::commitText()
{
	m_font_shader.use();
	glEnable(GL_SCISSOR_TEST);
	for (auto item : m_content)
	{
		drawText(item);
	}
	m_content.clear();
	
}

void FontLib::setCanvas(glm::vec2 sz)
{
	m_canvas = sz;
}

bool FontLib::setCurrentFont(std::string fontname)
{
	m_curr_font = m_fonts.find(fontname);
	if (m_curr_font != m_fonts.end())
		return true;

	Font font;
	if (FT_New_Face(m_ft, fontname.c_str(), 0, &font.face))
	{
		return false;
	}
	FT_Set_Pixel_Sizes(font.face, 0, m_font_res);

	glActiveTexture(GL_TEXTURE0);
	glGenTextures(1, &font.font_tex);
	glBindTexture(GL_TEXTURE_2D, font.font_tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	m_curr_font = m_fonts.insert(std::pair<std::string, Font>(fontname,font)).first;
	return true;
}



FT_Library FontLib::m_ft = nullptr;