71 lines
2.2 KiB
C++
71 lines
2.2 KiB
C++
#include "new.h"
|
|
#include "font_layout.h"
|
|
|
|
static inline size_t texture_offset(font * font)
|
|
{
|
|
return (sizeof (struct font)) + (sizeof (struct glyph)) * font->glyph_count;
|
|
}
|
|
|
|
extern "C" void log(int);
|
|
|
|
void FontLayout::init(size_t buffer, int layout_buffer_length)
|
|
{
|
|
this->font = reinterpret_cast<struct font *>(buffer + 0);
|
|
this->glyphs = reinterpret_cast<struct glyph *>(buffer + (sizeof (struct font)));
|
|
|
|
this->texture = reinterpret_cast<void *>(buffer + texture_offset(this->font));
|
|
|
|
this->layout_buffer_length = layout_buffer_length;
|
|
this->layout_buffer_index = 0;
|
|
this->layout_buffer = New<LayoutBuffer>(layout_buffer_length);
|
|
|
|
this->glyph_buffer = New<GlyphBuffer>(this->font->glyph_count);
|
|
int o = 0;
|
|
log(o);
|
|
for (uint16_t i = 0; i < this->font->glyph_count; i++) {
|
|
glyph_texture const & texture = this->glyphs[i].texture;
|
|
this->glyph_buffer[i].texPosition.x = float(texture.x - o) / float(this->font->texture_width);
|
|
this->glyph_buffer[i].texPosition.y = float(texture.y - o) / float(this->font->texture_height);
|
|
this->glyph_buffer[i].texSize.x = float(texture.width - o) / float(this->font->texture_width);
|
|
this->glyph_buffer[i].texSize.y = float(texture.height - o) / float(this->font->texture_height);
|
|
this->glyph_buffer[i].size.x = float(texture.width);
|
|
this->glyph_buffer[i].size.y = float(texture.height);
|
|
}
|
|
}
|
|
|
|
void FontLayout::draw_string(char const * string)
|
|
{
|
|
int i = 0;
|
|
|
|
int32_t x = 0;
|
|
int32_t y = 0;
|
|
|
|
while (true) {
|
|
char c = string[i++];
|
|
if (c == 0)
|
|
return;
|
|
|
|
if (c < font->first_char_code || c > font->last_char_code) {
|
|
continue;
|
|
}
|
|
int char_index = c - font->first_char_code;
|
|
glyph const & glyph = glyphs[char_index];
|
|
|
|
if (layout_buffer_index >= layout_buffer_length)
|
|
return;
|
|
|
|
int32_t bearingY = glyph.metrics.height - glyph.metrics.horiBearingY;
|
|
XMFLOAT3 position {
|
|
float(x + glyph.metrics.horiBearingX) * 0.015625f,
|
|
float(y - bearingY) * 0.015625f,
|
|
0.0f,
|
|
};
|
|
|
|
layout_buffer[layout_buffer_index].position = position;
|
|
layout_buffer[layout_buffer_index].glyph_index = char_index;
|
|
layout_buffer_index += 1;
|
|
|
|
x += glyph.metrics.horiAdvance;
|
|
}
|
|
}
|