Skip to content

Commit

Permalink
Add Font::getTextWidth(std::string_view)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Feb 8, 2024
1 parent f38a747 commit 14bf5d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/jngl/font.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2022 Jan Niklas Hasse <[email protected]>
// Copyright 2012-2024 Jan Niklas Hasse <[email protected]>
// For conditions of distribution and use, see copyright notice in LICENSE.txt

#include "font.hpp"
Expand Down Expand Up @@ -58,6 +58,10 @@ void Font::print(const Mat3& modelview, const std::string& text) const {
impl->print(modelview, text);
}

double Font::getTextWidth(std::string_view text) {
return static_cast<double>(static_cast<ScaleablePixels>(impl->getTextWidth(std::string(text))));
}

std::shared_ptr<FontImpl> Font::getImpl() {
return impl;
}
Expand Down
9 changes: 7 additions & 2 deletions src/jngl/font.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2022 Jan Niklas Hasse <[email protected]>
// Copyright 2012-2024 Jan Niklas Hasse <[email protected]>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
/// Contains jngl::Font class and related functions
/// @file
Expand Down Expand Up @@ -30,6 +30,10 @@ class Font {
/// Draw \a text using \a modelview
void print(const Mat3& modelview, const std::string& text) const;

/// Calculates the width of \a text in scale-independent pixels if it would be drawn with this
/// font
double getTextWidth(std::string_view);

/// Internal function
std::shared_ptr<FontImpl> getImpl();

Expand Down Expand Up @@ -93,7 +97,8 @@ double getLineHeight();
/// Set line height used by print() in scale-independent pixel
void setLineHeight(double);

/// Calculates the width of \a text in pixels if it would be drawn with the currently active font
/// Calculates the width of \a text in scale-independent pixels if it would be drawn with the
/// currently active font
double getTextWidth(const std::string& text);

} // namespace jngl

0 comments on commit 14bf5d1

Please sign in to comment.