Moved 100% of opengl work to opengl file
This commit is contained in:
parent
1adfd737a5
commit
a56b7575b7
|
@ -24,10 +24,14 @@ global_const Glyph_Bounds null_glyph_bounds = {0};
|
||||||
|
|
||||||
struct Glyph_Page{
|
struct Glyph_Page{
|
||||||
u32 page_number;
|
u32 page_number;
|
||||||
|
|
||||||
|
b32 has_layout;
|
||||||
f32 advance[ITEM_PER_FONT_PAGE];
|
f32 advance[ITEM_PER_FONT_PAGE];
|
||||||
Glyph_Bounds glyphs[ITEM_PER_FONT_PAGE];
|
Glyph_Bounds glyphs[ITEM_PER_FONT_PAGE];
|
||||||
u32 tex;
|
|
||||||
i32 tex_width, tex_height;
|
i32 tex_width, tex_height;
|
||||||
|
|
||||||
|
b32 has_gpu_setup;
|
||||||
|
u32 gpu_tex;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define FONT_PAGE_EMPTY ((Glyph_Page*)0)
|
#define FONT_PAGE_EMPTY ((Glyph_Page*)0)
|
||||||
|
@ -40,6 +44,8 @@ struct Render_Font{
|
||||||
f32 byte_advance;
|
f32 byte_advance;
|
||||||
f32 byte_sub_advances[3];
|
f32 byte_sub_advances[3];
|
||||||
i32 height, ascent, descent, line_skip, advance;
|
i32 height, ascent, descent, line_skip, advance;
|
||||||
|
i32 pt_size;
|
||||||
|
b32 use_hinting;
|
||||||
|
|
||||||
u32 filename_len;
|
u32 filename_len;
|
||||||
u32 name_len;
|
u32 name_len;
|
||||||
|
@ -57,6 +63,7 @@ struct Render_Font{
|
||||||
|
|
||||||
struct Glyph_Data{
|
struct Glyph_Data{
|
||||||
Glyph_Bounds bounds;
|
Glyph_Bounds bounds;
|
||||||
|
b32 has_gpu_setup;
|
||||||
u32 tex;
|
u32 tex;
|
||||||
i32 tex_width, tex_height;
|
i32 tex_width, tex_height;
|
||||||
};
|
};
|
||||||
|
|
|
@ -108,7 +108,7 @@ font_get_glyph(System_Functions *system, Render_Font *font, u32 codepoint){
|
||||||
|
|
||||||
Glyph_Page *page = 0;
|
Glyph_Page *page = 0;
|
||||||
|
|
||||||
// Hack optimizations
|
// HACK(allen): Hack optimizations
|
||||||
u32 cache_index = page_number % ArrayCount(font->cache);
|
u32 cache_index = page_number % ArrayCount(font->cache);
|
||||||
if (font->cache[cache_index].page_number == page_number){
|
if (font->cache[cache_index].page_number == page_number){
|
||||||
page = font->cache[cache_index].page;
|
page = font->cache[cache_index].page;
|
||||||
|
@ -122,10 +122,12 @@ font_get_glyph(System_Functions *system, Render_Font *font, u32 codepoint){
|
||||||
|
|
||||||
if (page != 0 && page->advance[glyph_index] > 0.f){
|
if (page != 0 && page->advance[glyph_index] > 0.f){
|
||||||
result.bounds = page->glyphs[glyph_index];
|
result.bounds = page->glyphs[glyph_index];
|
||||||
result.tex = page->tex;
|
result.has_gpu_setup = page->has_gpu_setup;
|
||||||
|
result.tex = page->gpu_tex;
|
||||||
result.tex_width = page->tex_width;
|
result.tex_width = page->tex_width;
|
||||||
result.tex_height = page->tex_height;
|
result.tex_height = page->tex_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
return(result);
|
return(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,305 @@
|
||||||
|
/*
|
||||||
|
* Mr. 4th Dimention - Allen Webster
|
||||||
|
*
|
||||||
|
* 14.11.2017
|
||||||
|
*
|
||||||
|
* FreeType font loader implementation
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// TOP
|
||||||
|
|
||||||
|
// NOTE(allen): Thanks to insofaras. This is copy-pasted from some work he originally did to get free type working on Linux.
|
||||||
|
|
||||||
|
#undef internal
|
||||||
|
#include <ft2build.h>
|
||||||
|
#include FT_FREETYPE_H
|
||||||
|
#define internal static
|
||||||
|
|
||||||
|
internal u32
|
||||||
|
font_ft_flags(b32 use_hinting){
|
||||||
|
u32 ft_flags = FT_LOAD_RENDER;
|
||||||
|
|
||||||
|
if (use_hinting){
|
||||||
|
// NOTE(inso): FT_LOAD_TARGET_LIGHT does hinting only vertically, which looks nicer imo
|
||||||
|
// maybe it could be exposed as an option for hinting, instead of just on/off.
|
||||||
|
ft_flags |= FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
ft_flags |= (FT_LOAD_NO_AUTOHINT | FT_LOAD_NO_HINTING);
|
||||||
|
}
|
||||||
|
|
||||||
|
return(ft_flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void
|
||||||
|
font_load_page_layout(Render_Font *font, Glyph_Page *page, u32 page_number, u32 pt_size, b32 use_hinting){
|
||||||
|
Assert(page != 0);
|
||||||
|
memset(page, 0, sizeof(*page));
|
||||||
|
|
||||||
|
char *filename = font->filename;
|
||||||
|
|
||||||
|
// TODO(allen): Stop redoing all this init for each call.
|
||||||
|
FT_Library ft;
|
||||||
|
FT_Init_FreeType(&ft);
|
||||||
|
|
||||||
|
FT_Face face;
|
||||||
|
FT_New_Face(ft, filename, 0, &face);
|
||||||
|
|
||||||
|
FT_Size_RequestRec_ size = {};
|
||||||
|
size.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
|
||||||
|
size.height = pt_size << 6;
|
||||||
|
FT_Request_Size(face, &size);
|
||||||
|
|
||||||
|
page->page_number = page_number;
|
||||||
|
|
||||||
|
i32 tab_width = 4;
|
||||||
|
|
||||||
|
// NOTE(allen): Determine glyph layout dimensions
|
||||||
|
i32 max_glyph_w = face->size->metrics.x_ppem;
|
||||||
|
i32 max_glyph_h = font_get_height(font);
|
||||||
|
i32 pen_y_descent = max_glyph_h + 2;
|
||||||
|
i32 tex_width = 64;
|
||||||
|
i32 tex_height = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
tex_width *= 2;
|
||||||
|
f32 glyphs_per_row = ceilf(tex_width/(f32)max_glyph_w);
|
||||||
|
f32 rows = ceilf(ITEM_PER_FONT_PAGE/glyphs_per_row);
|
||||||
|
tex_height = ceil32(rows*pen_y_descent);
|
||||||
|
} while(tex_height > tex_width);
|
||||||
|
tex_height = round_up_pot_u32(tex_height);
|
||||||
|
|
||||||
|
i32 pen_x = 0;
|
||||||
|
i32 pen_y = 0;
|
||||||
|
|
||||||
|
// NOTE(allen): Fill the glyph bounds array
|
||||||
|
u32 ft_flags = font_ft_flags(use_hinting);
|
||||||
|
|
||||||
|
u32 codepoint = (page_number << 8);
|
||||||
|
Glyph_Bounds *glyph_out = &page->glyphs[0];
|
||||||
|
f32 *advance_out = &page->advance[0];
|
||||||
|
for (u32 i = 0; i < ITEM_PER_FONT_PAGE; ++i, ++codepoint, ++glyph_out, ++advance_out){
|
||||||
|
if (FT_Load_Char(face, codepoint, ft_flags) == 0){
|
||||||
|
i32 w = face->glyph->bitmap.width;
|
||||||
|
i32 h = face->glyph->bitmap.rows;
|
||||||
|
i32 ascent = font_get_ascent(font);
|
||||||
|
|
||||||
|
// NOTE(allen): Move to next line if necessary
|
||||||
|
if (pen_x + w >= tex_width){
|
||||||
|
pen_x = 0;
|
||||||
|
pen_y += pen_y_descent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE(allen): Set all this stuff the renderer needs
|
||||||
|
glyph_out->x0 = (f32)(pen_x);
|
||||||
|
glyph_out->y0 = (f32)(pen_y);
|
||||||
|
glyph_out->x1 = (f32)(pen_x + w);
|
||||||
|
glyph_out->y1 = (f32)(pen_y + h + 1);
|
||||||
|
|
||||||
|
glyph_out->xoff = (f32)(face->glyph->bitmap_left);
|
||||||
|
glyph_out->yoff = (f32)(ascent - face->glyph->bitmap_top);
|
||||||
|
glyph_out->xoff2 = glyph_out->xoff + w;
|
||||||
|
glyph_out->yoff2 = glyph_out->yoff + h + 1;
|
||||||
|
|
||||||
|
// TODO(allen): maybe advance data should be integers?
|
||||||
|
*advance_out = (f32)ceil32(face->glyph->advance.x / 64.0f);
|
||||||
|
|
||||||
|
pen_x = ceil32(glyph_out->x1 + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(allen): Not sure setting tex_height here is right... double check.
|
||||||
|
tex_height = round_up_pot_u32(pen_y + pen_y_descent);
|
||||||
|
|
||||||
|
page->tex_width = tex_width;
|
||||||
|
page->tex_height = tex_height;
|
||||||
|
page->has_layout = true;
|
||||||
|
|
||||||
|
// HACK(allen): Put this somewhere else!
|
||||||
|
// NOTE(allen): whitespace spacing stuff
|
||||||
|
if (page_number == 0){
|
||||||
|
f32 space_adv = page->advance[' '];
|
||||||
|
f32 backslash_adv = page->advance['\\'];
|
||||||
|
f32 r_adv = page->advance['r'];
|
||||||
|
|
||||||
|
page->advance['\n'] = space_adv;
|
||||||
|
page->advance['\r'] = backslash_adv + r_adv;
|
||||||
|
page->advance['\t'] = space_adv*tab_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
FT_Done_FreeType(ft);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal u32*
|
||||||
|
font_load_page_pixels(Partition *part, Render_Font *font, Glyph_Page *page, u32 page_number, i32 *tex_width_out, i32 *tex_height_out){
|
||||||
|
Assert(page != 0);
|
||||||
|
Assert(page->has_layout);
|
||||||
|
Assert(page->page_number == page_number);
|
||||||
|
|
||||||
|
char *filename = font->filename;
|
||||||
|
i32 pt_size = font->pt_size;
|
||||||
|
b32 use_hinting = font->use_hinting;
|
||||||
|
|
||||||
|
// TODO(allen): Stop redoing all this init for each call.
|
||||||
|
FT_Library ft;
|
||||||
|
FT_Init_FreeType(&ft);
|
||||||
|
|
||||||
|
FT_Face face;
|
||||||
|
FT_New_Face(ft, filename, 0, &face);
|
||||||
|
|
||||||
|
FT_Size_RequestRec_ size = {};
|
||||||
|
size.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
|
||||||
|
size.height = pt_size << 6;
|
||||||
|
FT_Request_Size(face, &size);
|
||||||
|
|
||||||
|
page->page_number = page_number;
|
||||||
|
|
||||||
|
// NOTE(allen): Prepare a pixel buffer.
|
||||||
|
i32 tex_width = page->tex_width;
|
||||||
|
i32 tex_height = page->tex_height;
|
||||||
|
|
||||||
|
u32* pixels = push_array(part, u32, tex_width*tex_height);
|
||||||
|
memset(pixels, 0, tex_width*tex_height*sizeof(u32));
|
||||||
|
|
||||||
|
// NOTE(allen): Fill the texture
|
||||||
|
u32 ft_flags = font_ft_flags(use_hinting);
|
||||||
|
|
||||||
|
u32 codepoint = (page_number << 8);
|
||||||
|
Glyph_Bounds *glyph_ptr = &page->glyphs[0];
|
||||||
|
for (u32 i = 0; i < ITEM_PER_FONT_PAGE; ++i, ++codepoint, ++glyph_ptr){
|
||||||
|
if (FT_Load_Char(face, codepoint, ft_flags) == 0){
|
||||||
|
// NOTE(allen): Extract this glyph's dimensions.
|
||||||
|
i32 x = (i32)glyph_ptr->x0;
|
||||||
|
i32 y = (i32)glyph_ptr->y0;
|
||||||
|
i32 w = (i32)(glyph_ptr->x1 - glyph_ptr->x0);
|
||||||
|
i32 h = (i32)(glyph_ptr->y1 - glyph_ptr->y0 - 1);
|
||||||
|
|
||||||
|
// NOTE(allen): Write to the pixels.
|
||||||
|
u8 *src = face->glyph->bitmap.buffer;
|
||||||
|
i32 pitch = face->glyph->bitmap.pitch;
|
||||||
|
i32 end_x = x + w;
|
||||||
|
i32 end_y = y + h;
|
||||||
|
for (i32 Y = y, YY = 0; Y < end_y; ++Y, ++YY){
|
||||||
|
for (i32 X = x, XX = 0; X < end_x; ++X, ++XX){
|
||||||
|
pixels[Y*tex_width + X] = 0x00FFFFFF + (0x01000000*src[YY*pitch + XX]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*tex_width_out = tex_width;
|
||||||
|
*tex_height_out = tex_height;
|
||||||
|
|
||||||
|
FT_Done_FreeType(ft);
|
||||||
|
|
||||||
|
return(pixels);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal b32
|
||||||
|
font_load(System_Functions *system, Partition *part, Render_Font *font, i32 pt_size, b32 use_hinting){
|
||||||
|
char *filename = font->filename;
|
||||||
|
|
||||||
|
// TODO(allen): Stop redoing all this init for each call.
|
||||||
|
FT_Library ft;
|
||||||
|
FT_Init_FreeType(&ft);
|
||||||
|
|
||||||
|
FT_Face face;
|
||||||
|
FT_New_Face(ft, filename, 0, &face);
|
||||||
|
|
||||||
|
FT_Size_RequestRec_ size = {};
|
||||||
|
size.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
|
||||||
|
size.height = pt_size << 6;
|
||||||
|
FT_Request_Size(face, &size);
|
||||||
|
|
||||||
|
// NOTE(allen): Set size and metrics
|
||||||
|
char *name = face->family_name;
|
||||||
|
u32 name_len = 0;
|
||||||
|
for (;name[name_len];++name_len);
|
||||||
|
name_len = clamp_top(name_len, sizeof(font->name)-1);
|
||||||
|
memcpy(font->name, name, name_len);
|
||||||
|
font->name[name_len] = 0;
|
||||||
|
font->name_len = name_len;
|
||||||
|
|
||||||
|
font->ascent = ceil32 (face->size->metrics.ascender / 64.0f);
|
||||||
|
font->descent = floor32 (face->size->metrics.descender / 64.0f);
|
||||||
|
font->advance = ceil32 (face->size->metrics.max_advance / 64.0f);
|
||||||
|
font->height = ceil32 (face->size->metrics.height / 64.0f);
|
||||||
|
font->line_skip = font->height - (font->ascent - font->descent);
|
||||||
|
|
||||||
|
font->height -= font->line_skip;
|
||||||
|
font->line_skip = 0;
|
||||||
|
|
||||||
|
font->pt_size = pt_size;
|
||||||
|
font->use_hinting = use_hinting;
|
||||||
|
|
||||||
|
// NOTE(allen): Set texture and glyph data.
|
||||||
|
Glyph_Page *page = font_get_or_make_page(system, font, 0);
|
||||||
|
|
||||||
|
// NOTE(allen): Setup some basic spacing stuff.
|
||||||
|
f32 backslash_adv = page->advance['\\'];
|
||||||
|
f32 max_hex_advance = 0.f;
|
||||||
|
for (u32 i = '0'; i <= '9'; ++i){
|
||||||
|
f32 adv = page->advance[i];
|
||||||
|
max_hex_advance = Max(max_hex_advance, adv);
|
||||||
|
}
|
||||||
|
for (u32 i = 'a'; i <= 'f'; ++i){
|
||||||
|
f32 adv = page->advance[i];
|
||||||
|
max_hex_advance = Max(max_hex_advance, adv);
|
||||||
|
}
|
||||||
|
for (u32 i = 'A'; i <= 'F'; ++i){
|
||||||
|
f32 adv = page->advance[i];
|
||||||
|
max_hex_advance = Max(max_hex_advance, adv);
|
||||||
|
}
|
||||||
|
|
||||||
|
font->byte_advance = backslash_adv + max_hex_advance*2;
|
||||||
|
font->byte_sub_advances[0] = backslash_adv;
|
||||||
|
font->byte_sub_advances[1] = max_hex_advance;
|
||||||
|
font->byte_sub_advances[2] = max_hex_advance;
|
||||||
|
|
||||||
|
FT_Done_FreeType(ft);
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(allen): Remove Partition
|
||||||
|
internal void
|
||||||
|
system_set_page(System_Functions *system, Partition *part, Render_Font *font, Glyph_Page *page, u32 page_number, u32 pt_size, b32 use_hinting){
|
||||||
|
Assert(pt_size >= 8);
|
||||||
|
font_load_page_layout(font, page, page_number, pt_size, use_hinting);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void
|
||||||
|
system_set_font(System_Functions *system, Partition *part, Render_Font *font, char *filename, u32 pt_size, b32 use_hinting){
|
||||||
|
memset(font, 0, sizeof(*font));
|
||||||
|
|
||||||
|
u32 filename_len = 0;
|
||||||
|
for (;filename[filename_len];++filename_len);
|
||||||
|
|
||||||
|
if (filename_len <= sizeof(font->filename) - 1){
|
||||||
|
memcpy(font->filename, filename, filename_len);
|
||||||
|
font->filename[filename_len] = 0;
|
||||||
|
font->filename_len = filename_len;
|
||||||
|
|
||||||
|
if (part->base == 0){
|
||||||
|
*part = sysshared_scratch_partition(MB(8));
|
||||||
|
}
|
||||||
|
|
||||||
|
b32 success = false;
|
||||||
|
for (u32 R = 0; R < 3; ++R){
|
||||||
|
success = font_load(system, part, font, pt_size, use_hinting);
|
||||||
|
if (success){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
sysshared_partition_double(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
LOGF("font \"%.*s\" name is too long to load in current build (max %u)\n", filename_len, filename, sizeof(font->filename) - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BOTTOM
|
||||||
|
|
|
@ -17,6 +17,20 @@
|
||||||
|
|
||||||
// OpenGL 2.1 implementation
|
// OpenGL 2.1 implementation
|
||||||
|
|
||||||
|
internal GLuint
|
||||||
|
private_texture_initialize(GLint tex_width, GLint tex_height, u32 *pixels){
|
||||||
|
GLuint tex;
|
||||||
|
glGenTextures(1, &tex);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, tex);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, tex_width, tex_height, 0, GL_ALPHA, GL_UNSIGNED_INT, pixels);
|
||||||
|
return(tex);
|
||||||
|
}
|
||||||
|
|
||||||
inline void
|
inline void
|
||||||
private_draw_bind_texture(Render_Target *t, i32 texid){
|
private_draw_bind_texture(Render_Target *t, i32 texid){
|
||||||
if (t->bound_texture != texid){
|
if (t->bound_texture != texid){
|
||||||
|
@ -36,6 +50,9 @@ private_draw_set_color(Render_Target *t, u32 color){
|
||||||
|
|
||||||
internal void
|
internal void
|
||||||
interpret_render_buffer(System_Functions *system, Render_Target *t){
|
interpret_render_buffer(System_Functions *system, Render_Target *t){
|
||||||
|
// HACK(allen): Probably should use a different partition that can be resized, but whatevs for now scro.
|
||||||
|
Partition *part = &t->buffer;
|
||||||
|
|
||||||
local_persist b32 first_opengl_call = true;
|
local_persist b32 first_opengl_call = true;
|
||||||
if (first_opengl_call){
|
if (first_opengl_call){
|
||||||
first_opengl_call = false;
|
first_opengl_call = false;
|
||||||
|
@ -140,9 +157,29 @@ interpret_render_buffer(System_Functions *system, Render_Target *t){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HACK(allen): Super stupid... gotta fucking cleanup the font loading fiasco system.
|
||||||
Glyph_Data g = font_get_glyph(system, font, glyph->codepoint);
|
Glyph_Data g = font_get_glyph(system, font, glyph->codepoint);
|
||||||
if (g.tex == 0){
|
if (g.tex == 0){
|
||||||
break;
|
if (g.has_gpu_setup){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
u32 page_number = (glyph->codepoint/ITEM_PER_FONT_PAGE);
|
||||||
|
Glyph_Page *page = font_get_or_make_page(system, font, page_number);
|
||||||
|
|
||||||
|
Temp_Memory temp = begin_temp_memory(part);
|
||||||
|
i32 tex_width = 0;
|
||||||
|
i32 tex_height = 0;
|
||||||
|
u32 *pixels = font_load_page_pixels(part, font, page, page_number, &tex_width, &tex_height);
|
||||||
|
page->has_gpu_setup = true;
|
||||||
|
page->gpu_tex = private_texture_initialize(tex_width, tex_height, pixels);
|
||||||
|
end_temp_memory(temp);
|
||||||
|
|
||||||
|
g = font_get_glyph(system, font, glyph->codepoint);
|
||||||
|
if (g.tex == 0){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f32 x = glyph->pos.x;
|
f32 x = glyph->pos.x;
|
||||||
|
@ -188,272 +225,5 @@ interpret_render_buffer(System_Functions *system, Render_Target *t){
|
||||||
glFlush();
|
glFlush();
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef ExtractStruct
|
|
||||||
|
|
||||||
// NOTE(allen): Thanks to insofaras. This is copy-pasted from some work he originally did to get free type working on Linux.
|
|
||||||
|
|
||||||
#undef internal
|
|
||||||
#include <ft2build.h>
|
|
||||||
#include FT_FREETYPE_H
|
|
||||||
#define internal static
|
|
||||||
|
|
||||||
internal void
|
|
||||||
font_load_page_inner(Partition *part, Render_Font *font, FT_Library ft, FT_Face face, b32 use_hinting, Glyph_Page *page, u32 page_number, i32 tab_width){
|
|
||||||
Temp_Memory temp = begin_temp_memory(part);
|
|
||||||
Assert(page != 0);
|
|
||||||
page->page_number = page_number;
|
|
||||||
|
|
||||||
// prepare to read glyphs into a temporary texture buffer
|
|
||||||
i32 max_glyph_w = face->size->metrics.x_ppem;
|
|
||||||
|
|
||||||
i32 max_glyph_h = font_get_height(font);
|
|
||||||
i32 tex_width = 64;
|
|
||||||
i32 tex_height = 0;
|
|
||||||
|
|
||||||
do {
|
|
||||||
tex_width *= 2;
|
|
||||||
float glyphs_per_row = ceilf(tex_width / (float) max_glyph_w);
|
|
||||||
float rows = ceilf(ITEM_PER_FONT_PAGE / glyphs_per_row);
|
|
||||||
tex_height = ceil32(rows * (max_glyph_h + 2));
|
|
||||||
} while(tex_height > tex_width);
|
|
||||||
|
|
||||||
tex_height = round_up_pot_u32(tex_height);
|
|
||||||
|
|
||||||
i32 pen_x = 0;
|
|
||||||
i32 pen_y = 0;
|
|
||||||
|
|
||||||
u32* pixels = push_array(part, u32, tex_width * tex_height);
|
|
||||||
memset(pixels, 0, tex_width * tex_height * sizeof(u32));
|
|
||||||
|
|
||||||
u32 ft_flags = FT_LOAD_RENDER;
|
|
||||||
if (use_hinting){
|
|
||||||
// NOTE(inso): FT_LOAD_TARGET_LIGHT does hinting only vertically, which looks nicer imo
|
|
||||||
// maybe it could be exposed as an option for hinting, instead of just on/off.
|
|
||||||
ft_flags |= FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
ft_flags |= (FT_LOAD_NO_AUTOHINT | FT_LOAD_NO_HINTING);
|
|
||||||
}
|
|
||||||
|
|
||||||
// fill the texture
|
|
||||||
u32 base_codepoint = (page_number << 8);
|
|
||||||
Glyph_Bounds *glyphs = &page->glyphs[0];
|
|
||||||
Glyph_Bounds *glyph_ptr = glyphs;
|
|
||||||
|
|
||||||
f32 *advances = &page->advance[0];
|
|
||||||
f32 *advance_ptr = advances;
|
|
||||||
for(u32 i = 0; i < ITEM_PER_FONT_PAGE; ++i, ++glyph_ptr, ++advance_ptr){
|
|
||||||
u32 codepoint = i + base_codepoint;
|
|
||||||
|
|
||||||
if(FT_Load_Char(face, codepoint, ft_flags) == 0){
|
|
||||||
i32 w = face->glyph->bitmap.width;
|
|
||||||
i32 h = face->glyph->bitmap.rows;
|
|
||||||
|
|
||||||
i32 ascent = font_get_ascent(font);
|
|
||||||
|
|
||||||
// move to next line if necessary
|
|
||||||
if(pen_x + w >= tex_width){
|
|
||||||
pen_x = 0;
|
|
||||||
pen_y += (max_glyph_h + 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// set all this stuff the renderer needs
|
|
||||||
glyph_ptr->x0 = (f32)(pen_x);
|
|
||||||
glyph_ptr->y0 = (f32)(pen_y);
|
|
||||||
glyph_ptr->x1 = (f32)(pen_x + w);
|
|
||||||
glyph_ptr->y1 = (f32)(pen_y + h + 1);
|
|
||||||
|
|
||||||
glyph_ptr->xoff = (f32)(face->glyph->bitmap_left);
|
|
||||||
glyph_ptr->yoff = (f32)(ascent - face->glyph->bitmap_top);
|
|
||||||
glyph_ptr->xoff2 = glyph_ptr->xoff + w;
|
|
||||||
glyph_ptr->yoff2 = glyph_ptr->yoff + h + 1;
|
|
||||||
|
|
||||||
// TODO(allen): maybe advance data should be integers?
|
|
||||||
*advance_ptr = (f32)ceil32(face->glyph->advance.x / 64.0f);
|
|
||||||
|
|
||||||
// write to texture atlas
|
|
||||||
i32 pitch = face->glyph->bitmap.pitch;
|
|
||||||
for(i32 Y = 0; Y < h; ++Y){
|
|
||||||
for(i32 X = 0; X < w; ++X){
|
|
||||||
i32 x = pen_x + X;
|
|
||||||
i32 y = pen_y + Y;
|
|
||||||
|
|
||||||
pixels[y * tex_width + x] = face->glyph->bitmap.buffer[Y * pitch + X] * 0x01010101;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pen_x = ceil32(glyph_ptr->x1 + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// upload texture
|
|
||||||
tex_height = round_up_pot_u32(pen_y + max_glyph_h + 2);
|
|
||||||
|
|
||||||
page->tex_width = tex_width;
|
|
||||||
page->tex_height = tex_height;
|
|
||||||
|
|
||||||
glGenTextures(1, &page->tex);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, page->tex);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, tex_width, tex_height, 0, GL_ALPHA, GL_UNSIGNED_INT, pixels);
|
|
||||||
|
|
||||||
end_temp_memory(temp);
|
|
||||||
|
|
||||||
// whitespace spacing stuff
|
|
||||||
if (page_number == 0){
|
|
||||||
f32 space_adv = advances[' '];
|
|
||||||
f32 backslash_adv = advances['\\'];
|
|
||||||
f32 r_adv = advances['r'];
|
|
||||||
|
|
||||||
advances['\n'] = space_adv;
|
|
||||||
advances['\r'] = backslash_adv + r_adv;
|
|
||||||
advances['\t'] = space_adv*tab_width;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal b32
|
|
||||||
font_load_page(System_Functions *system, Partition *part, Render_Font *font, Glyph_Page *page, u32 page_number, u32 pt_size, b32 use_hinting){
|
|
||||||
|
|
||||||
char *filename = font->filename;
|
|
||||||
|
|
||||||
// TODO(allen): Stop redoing all this init for each call.
|
|
||||||
FT_Library ft;
|
|
||||||
FT_Init_FreeType(&ft);
|
|
||||||
|
|
||||||
FT_Face face;
|
|
||||||
FT_New_Face(ft, filename, 0, &face);
|
|
||||||
|
|
||||||
FT_Size_RequestRec_ size = {};
|
|
||||||
size.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
|
|
||||||
size.height = pt_size << 6;
|
|
||||||
FT_Request_Size(face, &size);
|
|
||||||
|
|
||||||
// NOTE(allen): set texture and glyph data.
|
|
||||||
font_load_page_inner(part, font, ft, face, use_hinting, page, page_number, 4);
|
|
||||||
|
|
||||||
FT_Done_FreeType(ft);
|
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal b32
|
|
||||||
font_load(System_Functions *system, Partition *part, Render_Font *font, i32 pt_size, b32 use_hinting){
|
|
||||||
|
|
||||||
char *filename = font->filename;
|
|
||||||
|
|
||||||
// TODO(allen): Stop redoing all this init for each call.
|
|
||||||
FT_Library ft;
|
|
||||||
FT_Init_FreeType(&ft);
|
|
||||||
|
|
||||||
FT_Face face;
|
|
||||||
FT_New_Face(ft, filename, 0, &face);
|
|
||||||
|
|
||||||
FT_Size_RequestRec_ size = {};
|
|
||||||
size.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
|
|
||||||
size.height = pt_size << 6;
|
|
||||||
FT_Request_Size(face, &size);
|
|
||||||
|
|
||||||
// set size & metrics
|
|
||||||
char *name = face->family_name;
|
|
||||||
u32 name_len = 0;
|
|
||||||
for (;name[name_len];++name_len);
|
|
||||||
name_len = clamp_top(name_len, sizeof(font->name)-1);
|
|
||||||
memcpy(font->name, name, name_len);
|
|
||||||
font->name[name_len] = 0;
|
|
||||||
font->name_len = name_len;
|
|
||||||
|
|
||||||
font->ascent = ceil32 (face->size->metrics.ascender / 64.0f);
|
|
||||||
font->descent = floor32 (face->size->metrics.descender / 64.0f);
|
|
||||||
font->advance = ceil32 (face->size->metrics.max_advance / 64.0f);
|
|
||||||
font->height = ceil32 (face->size->metrics.height / 64.0f);
|
|
||||||
font->line_skip = font->height - (font->ascent - font->descent);
|
|
||||||
|
|
||||||
font->height -= font->line_skip;
|
|
||||||
font->line_skip = 0;
|
|
||||||
|
|
||||||
// NOTE(allen): set texture and glyph data.
|
|
||||||
Glyph_Page *page = font_get_or_make_page(system, font, 0);
|
|
||||||
|
|
||||||
// NOTE(allen): Setup some basic spacing stuff.
|
|
||||||
f32 backslash_adv = page->advance['\\'];
|
|
||||||
f32 max_hex_advance = 0.f;
|
|
||||||
for (u32 i = '0'; i <= '9'; ++i){
|
|
||||||
f32 adv = page->advance[i];
|
|
||||||
max_hex_advance = Max(max_hex_advance, adv);
|
|
||||||
}
|
|
||||||
for (u32 i = 'a'; i <= 'f'; ++i){
|
|
||||||
f32 adv = page->advance[i];
|
|
||||||
max_hex_advance = Max(max_hex_advance, adv);
|
|
||||||
}
|
|
||||||
for (u32 i = 'A'; i <= 'F'; ++i){
|
|
||||||
f32 adv = page->advance[i];
|
|
||||||
max_hex_advance = Max(max_hex_advance, adv);
|
|
||||||
}
|
|
||||||
|
|
||||||
font->byte_advance = backslash_adv + max_hex_advance*2;
|
|
||||||
font->byte_sub_advances[0] = backslash_adv;
|
|
||||||
font->byte_sub_advances[1] = max_hex_advance;
|
|
||||||
font->byte_sub_advances[2] = max_hex_advance;
|
|
||||||
|
|
||||||
FT_Done_FreeType(ft);
|
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void
|
|
||||||
system_set_page(System_Functions *system, Partition *part, Render_Font *font, Glyph_Page *page, u32 page_number, u32 pt_size, b32 use_hinting){
|
|
||||||
Assert(pt_size >= 8);
|
|
||||||
|
|
||||||
memset(page, 0, sizeof(*page));
|
|
||||||
|
|
||||||
if (part->base == 0){
|
|
||||||
*part = sysshared_scratch_partition(MB(8));
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 success = false;
|
|
||||||
for (u32 R = 0; R < 3; ++R){
|
|
||||||
success = font_load_page(system, part, font, page, page_number, pt_size, use_hinting);
|
|
||||||
if (success){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
sysshared_partition_double(part);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void
|
|
||||||
system_set_font(System_Functions *system, Partition *part, Render_Font *font, char *filename, u32 pt_size, b32 use_hinting){
|
|
||||||
memset(font, 0, sizeof(*font));
|
|
||||||
|
|
||||||
u32 filename_len = 0;
|
|
||||||
for (;filename[filename_len];++filename_len);
|
|
||||||
|
|
||||||
if (filename_len <= sizeof(font->filename)-1){
|
|
||||||
memcpy(font->filename, filename, filename_len);
|
|
||||||
font->filename[filename_len] = 0;
|
|
||||||
font->filename_len = filename_len;
|
|
||||||
|
|
||||||
if (part->base == 0){
|
|
||||||
*part = sysshared_scratch_partition(MB(8));
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 success = false;
|
|
||||||
for (u32 R = 0; R < 3; ++R){
|
|
||||||
success = font_load(system, part, font, pt_size, use_hinting);
|
|
||||||
if (success){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
sysshared_partition_double(part);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BOTTOM
|
// BOTTOM
|
||||||
|
|
||||||
|
|
|
@ -69,7 +69,6 @@
|
||||||
#include <X11/extensions/Xfixes.h>
|
#include <X11/extensions/Xfixes.h>
|
||||||
|
|
||||||
#include <GL/glx.h>
|
#include <GL/glx.h>
|
||||||
#include <GL/gl.h>
|
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
|
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
|
@ -324,6 +323,9 @@ Sys_Send_Exit_Signal_Sig(system_send_exit_signal){
|
||||||
|
|
||||||
#include "4ed_coroutine_functions.cpp"
|
#include "4ed_coroutine_functions.cpp"
|
||||||
|
|
||||||
|
#include "4ed_font_data.h"
|
||||||
|
#include "4ed_system_shared.cpp"
|
||||||
|
|
||||||
//
|
//
|
||||||
// Clipboard
|
// Clipboard
|
||||||
//
|
//
|
||||||
|
@ -445,8 +447,9 @@ Sys_CLI_End_Update_Sig(system_cli_end_update){
|
||||||
return(close_me);
|
return(close_me);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "4ed_font_data.h"
|
#include "4ed_font_system_functions.cpp"
|
||||||
#include "4ed_system_shared.cpp"
|
|
||||||
|
#include <GL/gl.h>
|
||||||
#include "opengl/4ed_opengl_render.cpp"
|
#include "opengl/4ed_opengl_render.cpp"
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -48,9 +48,6 @@
|
||||||
#include "unix_4ed_headers.h"
|
#include "unix_4ed_headers.h"
|
||||||
#include <sys/syslimits.h>
|
#include <sys/syslimits.h>
|
||||||
|
|
||||||
#include <OpenGL/OpenGL.h>
|
|
||||||
#include <OpenGL/gl.h>
|
|
||||||
|
|
||||||
#undef external
|
#undef external
|
||||||
#undef internal
|
#undef internal
|
||||||
#include <mach/mach.h>
|
#include <mach/mach.h>
|
||||||
|
@ -187,6 +184,9 @@ Sys_Send_Exit_Signal_Sig(system_send_exit_signal){
|
||||||
|
|
||||||
#include "4ed_coroutine_functions.cpp"
|
#include "4ed_coroutine_functions.cpp"
|
||||||
|
|
||||||
|
#include "4ed_font_data.h"
|
||||||
|
#include "4ed_system_shared.cpp"
|
||||||
|
|
||||||
//
|
//
|
||||||
// Clipboard
|
// Clipboard
|
||||||
//
|
//
|
||||||
|
@ -323,8 +323,10 @@ Sys_CLI_End_Update_Sig(system_cli_end_update){
|
||||||
return(close_me);
|
return(close_me);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "4ed_font_data.h"
|
#include "4ed_font_system_functions.cpp"
|
||||||
#include "4ed_system_shared.cpp"
|
|
||||||
|
#include <OpenGL/OpenGL.h>
|
||||||
|
#include <OpenGL/gl.h>
|
||||||
#include "opengl/4ed_opengl_render.cpp"
|
#include "opengl/4ed_opengl_render.cpp"
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
|
|
@ -53,7 +53,6 @@
|
||||||
#include "4ed.h"
|
#include "4ed.h"
|
||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <GL/gl.h>
|
|
||||||
#include "win32_gl.h"
|
#include "win32_gl.h"
|
||||||
|
|
||||||
#define GL_TEXTURE_MAX_LEVEL 0x813D
|
#define GL_TEXTURE_MAX_LEVEL 0x813D
|
||||||
|
@ -504,6 +503,8 @@ Sys_CLI_End_Update_Sig(system_cli_end_update){
|
||||||
return(close_me);
|
return(close_me);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "4ed_font_system_functions.cpp"
|
||||||
|
#include <GL/gl.h>
|
||||||
#include "opengl/4ed_opengl_render.cpp"
|
#include "opengl/4ed_opengl_render.cpp"
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in New Issue