[update] Implemented getting font glyphs.

This commit is contained in:
falsycat 2019-10-12 00:00:00 +00:00
parent d49aa08b45
commit 309d5fff47
2 changed files with 41 additions and 41 deletions

View File

@ -1,7 +1,8 @@
/// License: MIT /// License: MIT
module sj.Font; module sj.Font;
import std.conv, import std.array,
std.conv,
std.exception, std.exception,
std.format, std.format,
std.string; std.string;
@ -23,48 +24,44 @@ class Font {
} }
/// ///
Texture2DRef CreateTexture( vec2i[] CreateTextureUvArray(dstring str, size_t px) {
vec2i sz, string str, vec4 color, size_t px) { auto glyphs = appender!(sfGlyph[]);
auto text = sfText_create().enforce; glyphs.reserve(str.length);
scope(exit) sfText_destroy(text); foreach (c; str) {
glyphs ~= sfFont_getGlyph(font_, c, px.to!uint, false, 0f);
sfText_setFont(text, font_);
sfText_setString(text, str.toStringz);
sfText_setCharacterSize(text, px.to!uint);
sfText_setFillColor(text, sfBlack); // TODO: change color
auto buf = sfRenderTexture_create(sz.x, sz.y, false).enforce;
scope(exit) sfRenderTexture_destroy(buf);
sfRenderTexture_drawText(buf, text, null);
auto sftex = sfRenderTexture_getTexture(buf).enforce;
auto tex = Texture2D.Create();
Texture2DAllocator allocator;
with (allocator) {
internalFormat = GL_RGBA8;
size = sz;
format = GL_RED;
type = GL_UNSIGNED_BYTE;
Allocate(tex);
} }
gl.CopyImageSubData( auto uv = appender!(vec2i[]);
sfTexture_getNativeHandle(sftex), uv.reserve(str.length * 4);
GL_TEXTURE_2D, foreach (const ref g; glyphs[]) {
0, const rc = &g.textureRect;
0,
0, const left = rc.left;
0, const right = (rc.left + rc.width);
tex.id, const top = rc.top;
GL_TEXTURE_2D, const bottom = (rc.top + rc.height);
0,
0, uv ~= [
0, vec2i(left, top),
0, vec2i(left, bottom),
sz.x, sz.y, 1 vec2i(right, bottom),
); vec2i(right, top),
return tex; ];
}
return uv[];
}
///
vec2i GetTextureSize(size_t px) {
const sz = sfTexture_getSize(
sfFont_getTexture(font_, px.to!uint).enforce);
return vec2i(sz.x, sz.y);
}
///
void BindTextureToUnit(GLenum unit, size_t px) {
gl.ActiveTexture(unit);
sfTexture_bind(sfFont_getTexture(font_, px.to!uint).enforce);
} }
private: private:

View File

@ -15,6 +15,9 @@ class FontSet {
gothic_heavy_ = new Font(dir~"/fonts/SourceHanSansJP-Heavy.otf"); gothic_heavy_ = new Font(dir~"/fonts/SourceHanSansJP-Heavy.otf");
} }
~this() {
gothic_heavy_.destroy();
}
/// ///
@property Font gothicHeavy() { @property Font gothicHeavy() {