Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions engine/src/components/textrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace {
const char *gColor("mainColor");
const char *gTexture("mainTexture");
const char *gWeight("weight");
const char *gUseSDF("useSdf");
};

/*!
Expand Down Expand Up @@ -246,6 +247,8 @@ MaterialInstance *TextRender::materialInstance(int index) {
it->setFloat(gWeight, &m_fontWeight);
it->setTransform(transform());
it->setPriority(m_priority);
int sdf = m_flags & Font::Sdf;
it->setInteger(gUseSDF, &sdf);
}
}
m_dirtyMaterial = false;
Expand Down
39 changes: 17 additions & 22 deletions engine/src/resources/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ float Font::textWidth(const TString &text, int size, int flags) {

for(uint32_t i = 0; i < length; i++) {
uint32_t ch = u32[i];
Mathf::hashCombine(ch, size);
Mathf::hashCombine(ch, adjustedSize);
switch(ch) {
case ' ': {
pos += spaceWidth;
Expand Down Expand Up @@ -176,18 +176,11 @@ void Font::composeMesh(Mesh *mesh, const TString &text, int size, int alignment,
FT_Face face = reinterpret_cast<FT_Face>(m_face);

float spaceWidth = 0;
float spaceLine = 0;
float spaceLine = size * 1.2f;

FT_Error error = FT_Load_Glyph( face, FT_Get_Char_Index( face, ' ' ), FT_LOAD_BITMAP_METRICS_ONLY );
if(!error) {
spaceWidth = (adjustedSize == DF_GLYPH_SIZE) ? (DF_GLYPH_SIZE * 64.0f * size) : 64.0f;
spaceWidth = face->glyph->advance.x / spaceWidth;
}

error = FT_Load_Glyph( face, FT_Get_Char_Index( face, '\n' ), FT_LOAD_BITMAP_METRICS_ONLY );
if(!error) {
spaceLine = (adjustedSize == DF_GLYPH_SIZE) ? (adjustedSize + size) : size;
spaceLine = face->glyph->metrics.height / spaceLine / 2;
spaceWidth = face->glyph->advance.x / 64.0f;
}

IndexVector &indices = mesh->indices();
Expand All @@ -209,7 +202,6 @@ void Font::composeMesh(Mesh *mesh, const TString &text, int size, int alignment,

for(uint32_t i = 0; i < length; i++) {
uint32_t ch = u32[i];
Mathf::hashCombine(ch, size);
switch(ch) {
case ' ': {
pos += Vector3(spaceWidth, 0.0f, 0.0f);
Expand All @@ -229,8 +221,10 @@ void Font::composeMesh(Mesh *mesh, const TString &text, int size, int alignment,
default: {
if(flags & Kerning) {
pos.x += requestKerning(ch, previous);
previous = ch;
}

Mathf::hashCombine(ch, adjustedSize);
GlyphData *data = glyph(ch);
if(data == nullptr) {
continue;
Expand Down Expand Up @@ -277,7 +271,7 @@ void Font::composeMesh(Mesh *mesh, const TString &text, int size, int alignment,
it++;
} break;
}
previous = ch;

}

width.push_back(pos.x);
Expand Down Expand Up @@ -392,12 +386,6 @@ void Font::packSheets(int padding) {
uint32_t atlasWidth = 1024;
uint32_t atlasHeight = 1024;

if(m_page == nullptr) {
m_page = Engine::objectCreate<Texture>();
m_page->incRef();
m_page->setFiltering(Texture::None);
}

while(true) {
m_root->w = atlasWidth;
m_root->h = atlasHeight;
Expand Down Expand Up @@ -435,13 +423,14 @@ void Font::packSheets(int padding) {
}
}

if(m_page) {
m_page->resize(atlasWidth, atlasHeight);
Texture *page = Font::page();
if(page) {
page->resize(atlasWidth, atlasHeight);
for(auto &it : m_shapes) {
if(!it.second.copied) {
AtlasNode *node = it.second.node;
uint8_t *src = it.second.data.data();
uint8_t *dst = m_page->surface(0).front().data();
uint8_t *dst = page->surface(0).front().data();
for(int32_t y = 0; y < it.second.height; y++) {
uint32_t index = (node->y + y + padding) * atlasWidth + node->x + padding;
memcpy(&dst[index], &src[y * it.second.width], it.second.width);
Expand All @@ -462,7 +451,7 @@ void Font::packSheets(int padding) {
}
}

m_page->setDirty();
page->setDirty();
}
}
/*!
Expand All @@ -471,5 +460,11 @@ void Font::packSheets(int padding) {
Texture *Font::page() {
PROFILE_FUNCTION();

if(m_page == nullptr) {
m_page = Engine::objectCreate<Texture>();
m_page->incRef();
m_page->setFiltering(Texture::None);
}

return m_page;
}
6 changes: 4 additions & 2 deletions modules/uikit/src/components/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ void Label::draw(CommandBuffer &buffer) {
m_mesh->setColors(Vector4Vector(m_mesh->vertices().size(), Vector4(1.0f)));

m_material->setTexture(gTexture, m_font->page());
bool sdf = m_flags & Font::Sdf;
m_material->setBool(gUseSDF, &sdf);


m_dirty = false;
}

int sdf = m_flags & Font::Sdf;
m_material->setInteger(gUseSDF, &sdf);

buffer.drawMesh(m_mesh, 0, Material::Translucent, *m_material);
}

Expand Down
5 changes: 2 additions & 3 deletions worldeditor/bin/engine/materials/DefaultFont.shader
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<properties>
<property name="mainColor" type="vec4" />
<property name="weight" type="float" />
<property name="useSdf" type="bool" />
<property name="useSdf" type="int" />
<property name="mainTexture" binding="0" type="texture2d" />
</properties>
<fragment><![CDATA[
Expand All @@ -26,9 +26,8 @@ const float softness = 0.0625f;

void main() {
#pragma instance

float mask = texture(mainTexture, _uv).x;
if(useSdf) {
if(useSdf > 0) {
float min = 1.0f - weight - softness;
float max = 1.0f - weight + softness;

Expand Down
Loading