Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi-line strings #1212

Merged
merged 4 commits into from
Feb 11, 2025
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
13 changes: 10 additions & 3 deletions NAS2D/Renderer/RendererOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,18 +487,25 @@ void RendererOpenGL::drawText(const Font& font, std::string_view text, Point<flo
const auto& gml = font.metrics();
if (gml.empty()) { return; }

int offset = 0;
Vector<int> offset{0, 0};
for (auto character : text)
{
if (character == '\n')
{
offset.y += font.height();
offset.x = 0;
continue;
}

const auto& gm = gml[std::clamp<std::size_t>(static_cast<uint8_t>(character), 0, 255)];

const auto glyphCellSize = font.glyphCellSize().to<float>();
const auto adjustX = (gm.minX < 0) ? gm.minX : 0;
const auto vertexArray = rectToQuad({{position.x + offset + adjustX, position.y}, glyphCellSize});
const auto vertexArray = rectToQuad({{position.x + offset.x + adjustX, position.y + offset.y}, glyphCellSize});
const auto textureCoordArray = rectToQuad(gm.uvRect);

drawTexturedQuad(font.textureId(), vertexArray, textureCoordArray);
offset += gm.advance;
offset.x += gm.advance;
}
}

Expand Down
41 changes: 27 additions & 14 deletions NAS2D/Resource/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,32 @@ Vector<int> Font::glyphCellSize() const

Vector<int> Font::size(std::string_view string) const
{
return {width(string), height()};
const auto& gml = mFontInfo.metrics;
if (gml.empty()) { return {0, 0}; }

Vector<int> size{0, 0};
int lineWidth = 0;
for (auto character : string)
{
if (character == '\n')
{
size.y += height();
size.x = std::max(size.x, lineWidth);
lineWidth = 0;
}
else
{
auto glyph = std::clamp<std::size_t>(static_cast<uint8_t>(character), 0, 255);
lineWidth += gml[glyph].advance;
}
}
if (!string.empty())
{
size.y += height();
size.x = std::max(size.x, lineWidth);
}

return size;
}


Expand All @@ -130,19 +155,7 @@ Vector<int> Font::size(std::string_view string) const
*/
int Font::width(std::string_view string) const
{
if (string.empty()) { return 0; }

int width = 0;
auto& gml = mFontInfo.metrics;
if (gml.empty()) { return 0; }

for (auto character : string)
{
auto glyph = std::clamp<std::size_t>(static_cast<uint8_t>(character), 0, 255);
width += gml[glyph].advance;
}

return width;
return size(string).x;
}


Expand Down
2 changes: 2 additions & 0 deletions test-graphics/TestGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ NAS2D::State* TestGraphics::update()
drawBoundedText(r, {360, 82}, "iii");
drawBoundedText(r, {360, 106}, " ");

drawBoundedText(r, {630, 10}, "A\nmulti\nline\nstring.");

r.drawGradient({{10, 60}, {64, 64}}, NAS2D::Color::Blue, NAS2D::Color::Green, NAS2D::Color::Red, NAS2D::Color::Magenta);

for (auto i = 0u; i < 2000u; ++i)
Expand Down