Add Font BoundingBox calculation

This commit is contained in:
Thraix
2023-05-10 22:53:34 +02:00
parent 35ae3a13d2
commit 94d4aa9356
10 changed files with 108 additions and 19 deletions
+2
View File
@@ -201,6 +201,7 @@
<ClCompile Include="src\copium\pipeline\DescriptorPool.cpp" /> <ClCompile Include="src\copium\pipeline\DescriptorPool.cpp" />
<ClCompile Include="src\copium\pipeline\DescriptorSet.cpp" /> <ClCompile Include="src\copium\pipeline\DescriptorSet.cpp" />
<ClCompile Include="src\copium\sampler\Font.cpp" /> <ClCompile Include="src\copium\sampler\Font.cpp" />
<ClCompile Include="src\copium\util\BoundingBox.cpp" />
<ClCompile Include="src\copium\util\RuntimeException.cpp" /> <ClCompile Include="src\copium\util\RuntimeException.cpp" />
<ClCompile Include="src\copium\util\FileSystem.cpp" /> <ClCompile Include="src\copium\util\FileSystem.cpp" />
<ClCompile Include="src\copium\buffer\Framebuffer.cpp" /> <ClCompile Include="src\copium\buffer\Framebuffer.cpp" />
@@ -260,6 +261,7 @@
<ClInclude Include="src\copium\buffer\CommandBuffer.h" /> <ClInclude Include="src\copium\buffer\CommandBuffer.h" />
<ClInclude Include="src\copium\sampler\Font.h" /> <ClInclude Include="src\copium\sampler\Font.h" />
<ClInclude Include="src\copium\sampler\Glyph.h" /> <ClInclude Include="src\copium\sampler\Glyph.h" />
<ClInclude Include="src\copium\util\BoundingBox.h" />
<ClInclude Include="src\copium\util\Common.h" /> <ClInclude Include="src\copium\util\Common.h" />
<ClInclude Include="src\copium\core\DebugMessenger.h" /> <ClInclude Include="src\copium\core\DebugMessenger.h" />
<ClInclude Include="src\copium\pipeline\DescriptorSet.h" /> <ClInclude Include="src\copium\pipeline\DescriptorSet.h" />
@@ -183,6 +183,9 @@
<ClCompile Include="src\copium\sampler\Font.cpp"> <ClCompile Include="src\copium\sampler\Font.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\copium\util\BoundingBox.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\copium\sampler\DepthAttachment.h"> <ClInclude Include="src\copium\sampler\DepthAttachment.h">
@@ -374,5 +377,8 @@
<ClInclude Include="src\copium\util\Enum.h"> <ClInclude Include="src\copium\util\Enum.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\copium\util\BoundingBox.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
+1 -1
View File
@@ -1,3 +1,3 @@
[Font] [Font]
filepath=res/fonts/Roboto-Thin.ttf filepath=res/fonts/Roboto-Regular.ttf
uuid=3e9ef1a2-59ba-2e4d-b2cd-f5efaa531af7 uuid=3e9ef1a2-59ba-2e4d-b2cd-f5efaa531af7
+4 -1
View File
@@ -223,7 +223,10 @@ namespace Copium
renderer->Quad(glm::vec2{ 0.1, -0.4}, glm::vec2{0.8, 0.8}, AssetManager::GetAsset<Font>(font)); renderer->Quad(glm::vec2{ 0.1, -0.4}, glm::vec2{0.8, 0.8}, AssetManager::GetAsset<Font>(font));
renderer->Quad(mousePos - glm::vec2(0.1), glm::vec2{0.2}, AssetManager::GetAsset<Texture2D>(texture2D2)); renderer->Quad(mousePos - glm::vec2(0.1), glm::vec2{0.2}, AssetManager::GetAsset<Texture2D>(texture2D2));
std::string s = std::to_string(fps) + " fps"; std::string s = std::to_string(fps) + " fps";
renderer->Text(s, glm::vec2{-aspect + 0.01, 0.94}, AssetManager::GetAsset<Font>(font), 0.06, glm::vec3{0.3, 0.2, 0.8}); BoundingBox boundingBox = AssetManager::GetAsset<Font>(font).GetTextBoundingBox(s, 0.06);
glm::vec2 pos = glm::vec2{-aspect + 0.01, 0.94};
renderer->Quad(pos + boundingBox.lb, boundingBox.GetSize());
renderer->Text(s, pos, AssetManager::GetAsset<Font>(font), 0.06, glm::vec3{0.0f});
renderer->End(); renderer->End();
fb.Unbind(*commandBuffer); fb.Unbind(*commandBuffer);
@@ -74,10 +74,10 @@ namespace Copium
const Glyph& glyph = font.GetGlyph(c); const Glyph& glyph = font.GetGlyph(c);
AllocateQuad(); AllocateQuad();
int texIndex = AllocateSampler(font); int texIndex = AllocateSampler(font);
AddVertex(offset + glm::vec2{glyph.pos1.x * size, glyph.pos1.y * size}, color, texIndex, glyph.texCoord1, RendererVertex::TYPE_TEXT); AddVertex(offset + glm::vec2{glyph.boundingBox.l * size, glyph.boundingBox.b * size}, color, texIndex, glyph.texCoordBoundingBox.lb, RendererVertex::TYPE_TEXT);
AddVertex(offset + glm::vec2{glyph.pos2.x * size, glyph.pos1.y * size}, color, texIndex, glm::vec2{glyph.texCoord2.x, glyph.texCoord1.y}, RendererVertex::TYPE_TEXT); AddVertex(offset + glm::vec2{glyph.boundingBox.r * size, glyph.boundingBox.b * size}, color, texIndex, glm::vec2{glyph.texCoordBoundingBox.r, glyph.texCoordBoundingBox.b}, RendererVertex::TYPE_TEXT);
AddVertex(offset + glm::vec2{glyph.pos2.x * size, glyph.pos2.y * size}, color, texIndex, glyph.texCoord2, RendererVertex::TYPE_TEXT); AddVertex(offset + glm::vec2{glyph.boundingBox.r * size, glyph.boundingBox.t * size}, color, texIndex, glyph.texCoordBoundingBox.rt, RendererVertex::TYPE_TEXT);
AddVertex(offset + glm::vec2{glyph.pos1.x * size, glyph.pos2.y * size}, color, texIndex, glm::vec2{glyph.texCoord1.x, glyph.texCoord2.y}, RendererVertex::TYPE_TEXT); AddVertex(offset + glm::vec2{glyph.boundingBox.l * size, glyph.boundingBox.t * size}, color, texIndex, glm::vec2{glyph.texCoordBoundingBox.l, glyph.texCoordBoundingBox.t}, RendererVertex::TYPE_TEXT);
offset.x += glyph.advance * size; offset.x += glyph.advance * size;
} }
return offset; return offset;
+38 -4
View File
@@ -60,11 +60,9 @@ namespace Copium
glyph.advance = glyphGeom.getAdvance(); glyph.advance = glyphGeom.getAdvance();
double l, b, r, t; double l, b, r, t;
glyphGeom.getQuadPlaneBounds(l, b, r, t); glyphGeom.getQuadPlaneBounds(l, b, r, t);
glyph.pos1 = glm::vec2{l, b}; glyph.boundingBox = BoundingBox{(float)l, (float)b, (float)r, (float)t};
glyph.pos2 = glm::vec2{r, t};
glyphGeom.getQuadAtlasBounds(l, b, r, t); glyphGeom.getQuadAtlasBounds(l, b, r, t);
glyph.texCoord1 = glm::vec2{l / width, b / height}; glyph.texCoordBoundingBox = BoundingBox{(float)l / width, (float)b / height, (float)r / width, (float)t / height};
glyph.texCoord2 = glm::vec2{r / width, t / height};
this->glyphs.emplace((char)glyphGeom.getCodepoint(), glyph); this->glyphs.emplace((char)glyphGeom.getCodepoint(), glyph);
} }
lineHeight = fontGeometry.getMetrics().lineHeight; lineHeight = fontGeometry.getMetrics().lineHeight;
@@ -101,6 +99,42 @@ namespace Copium
return lineHeight; return lineHeight;
} }
BoundingBox Font::GetTextBoundingBox(const std::string& str, float size) const
{
BoundingBox boundingBox{0.0f};
glm::vec2 offset{0.0f};
for (auto c : str)
{
if(c == ' ')
{
const Glyph& glyph = GetGlyph(c);
offset.x += glyph.advance;
continue;
}
else if (c == '\t')
{
const Glyph& glyph = GetGlyph(' ');
offset.x += glyph.advance * 4;
continue;
}
else if (c == '\n')
{
boundingBox.r = std::max(boundingBox.r, offset.x);
offset.y -= GetLineHeight();
offset.x = 0.0f;
continue;
}
const Glyph& glyph = GetGlyph(c);
boundingBox.l = std::min(boundingBox.l, offset.x + glyph.boundingBox.l);
boundingBox.t = std::max(boundingBox.t, offset.y + glyph.boundingBox.t);
offset.x += glyph.advance;
boundingBox.b = std::min(boundingBox.b, offset.y + glyph.boundingBox.b);
}
boundingBox.r = std::max(boundingBox.r, offset.x);
boundingBox.lbrt *= size;
return boundingBox;
}
void Font::InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height) void Font::InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height)
{ {
VkDeviceSize bufferSize = width * height * 4; VkDeviceSize bufferSize = width * height * 4;
+3 -4
View File
@@ -2,16 +2,13 @@
#include "copium/sampler/Sampler.h" #include "copium/sampler/Sampler.h"
#include "copium/sampler/Glyph.h" #include "copium/sampler/Glyph.h"
#include "copium/util/BoundingBox.h"
namespace Copium namespace Copium
{ {
class Font : public Sampler class Font : public Sampler
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Font); CP_DELETE_COPY_AND_MOVE_CTOR(Font);
struct GlyphData
{
};
private: private:
VkImage image; VkImage image;
VkDeviceMemory imageMemory; VkDeviceMemory imageMemory;
@@ -27,6 +24,8 @@ namespace Copium
const Glyph& GetGlyph(char c) const; const Glyph& GetGlyph(char c) const;
float GetLineHeight() const; float GetLineHeight() const;
BoundingBox GetTextBoundingBox(const std::string& str, float size) const;
private: private:
void InitializeTextureImageFromFile(const std::string& filename); void InitializeTextureImageFromFile(const std::string& filename);
void InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height); void InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height);
+3 -5
View File
@@ -1,15 +1,13 @@
#pragma once #pragma once
#include <glm/glm.hpp> #include "copium/util/BoundingBox.h"
namespace Copium namespace Copium
{ {
struct Glyph struct Glyph
{ {
float advance; float advance;
glm::vec2 pos1; BoundingBox boundingBox;
glm::vec2 pos2; BoundingBox texCoordBoundingBox;
glm::vec2 texCoord1;
glm::vec2 texCoord2;
}; };
} }
@@ -0,0 +1,25 @@
#include "copium/util/BoundingBox.h"
namespace Copium
{
BoundingBox::BoundingBox()
: l{0.0f}, b{0.0f}, r{0.0f}, t{0.0f}
{}
BoundingBox::BoundingBox(float all)
: l{all}, b{all}, r{all}, t{all}
{}
BoundingBox::BoundingBox(float l, float b, float r, float t)
: l{l}, b{b}, r{r}, t{t}
{}
BoundingBox::BoundingBox(glm::vec2 lb, glm::vec2 rt)
: lb{lb}, rt{rt}
{}
glm::vec2 BoundingBox::GetSize() const
{
return rt - lb;
}
}
@@ -0,0 +1,22 @@
#pragma once
#include <glm/glm.hpp>
namespace Copium
{
struct BoundingBox
{
union
{
struct { float l; float b; float r; float t; };
struct { glm::vec2 lb; glm::vec2 rt; };
struct { glm::vec4 lbrt; };
};
BoundingBox();
BoundingBox(float all);
BoundingBox(float l, float b, float r, float t);
BoundingBox(glm::vec2 lb, glm::vec2 rt);
glm::vec2 GetSize() const;
};
}