Skip to content

Commit

Permalink
Merge pull request #1212 from lairworks/supportMultiLineStrings
Browse files Browse the repository at this point in the history
Support multi-line strings
  • Loading branch information
DanRStevens authored Feb 11, 2025
2 parents 4641613 + 5ede3be commit 10ed618
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
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

0 comments on commit 10ed618

Please sign in to comment.