Browse Source

Text: return rendered rectangle from TextRenderer::render().

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
2267627242
  1. 22
      src/Text/TextRenderer.cpp
  2. 13
      src/Text/TextRenderer.h

22
src/Text/TextRenderer.cpp

@ -30,7 +30,7 @@ namespace Magnum { namespace Text {
namespace {
std::tuple<std::vector<Vector2>, std::vector<Vector2>, std::vector<std::uint32_t>> renderInternal(Font& font, GLfloat size, const std::string& text) {
std::tuple<std::vector<Vector2>, std::vector<Vector2>, std::vector<std::uint32_t>, Rectangle> renderInternal(Font& font, GLfloat size, const std::string& text) {
/* Prepare HarfBuzz buffer */
hb_buffer_t *buffer = hb_buffer_create();
hb_buffer_set_unicode_funcs(buffer, hb_icu_get_unicode_funcs());
@ -106,10 +106,14 @@ std::tuple<std::vector<Vector2>, std::vector<Vector2>, std::vector<std::uint32_t
cursorPosition += advance;
}
/* Rendered rectangle */
Rectangle rectangle;
if(glyphCount) rectangle = {positions[1], positions[positions.size()-2]};
/* Destroy HarfBuzz buffer */
hb_buffer_destroy(buffer);
return std::make_tuple(std::move(positions), std::move(texcoords), std::move(indices));
return std::make_tuple(std::move(positions), std::move(texcoords), std::move(indices), rectangle);
}
template<std::uint8_t dimensions> typename DimensionTraits<dimensions>::PointType point(const Vector2& vec);
@ -124,10 +128,11 @@ template<> inline Point3D point<3>(const Vector2& vec) {
}
template<std::uint8_t dimensions> std::tuple<std::vector<typename DimensionTraits<dimensions>::PointType>, std::vector<Vector2>, std::vector<std::uint32_t>> TextRenderer<dimensions>::render(Font& font, GLfloat size, const std::string& text) {
template<std::uint8_t dimensions> std::tuple<std::vector<typename DimensionTraits<dimensions>::PointType>, std::vector<Vector2>, std::vector<std::uint32_t>, Rectangle> TextRenderer<dimensions>::render(Font& font, GLfloat size, const std::string& text) {
std::vector<Vector2> positions, textureCoordinates;
std::vector<std::uint32_t> indices;
std::tie(positions, textureCoordinates, indices) = renderInternal(font, size, text);
Rectangle rectangle;
std::tie(positions, textureCoordinates, indices, rectangle) = renderInternal(font, size, text);
/* Create PointXD from Vector2 */
std::vector<typename DimensionTraits<dimensions>::PointType> positionsXD;
@ -135,16 +140,17 @@ template<std::uint8_t dimensions> std::tuple<std::vector<typename DimensionTrait
for(const Vector2& position: positions)
positionsXD.push_back(point<dimensions>(position));
return std::make_tuple(std::move(positionsXD), std::move(textureCoordinates), std::move(indices));
return std::make_tuple(std::move(positionsXD), std::move(textureCoordinates), std::move(indices), rectangle);
}
template<std::uint8_t dimensions> Mesh TextRenderer<dimensions>::render(Font& font, GLfloat size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage) {
template<std::uint8_t dimensions> std::tuple<Mesh, Rectangle> TextRenderer<dimensions>::render(Font& font, GLfloat size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage) {
Mesh mesh;
std::vector<typename DimensionTraits<dimensions>::PointType> positions;
std::vector<Vector2> textureCoordinates;
std::vector<std::uint32_t> indices;
std::tie(positions, textureCoordinates, indices) = render(font, size, text);
Rectangle rectangle;
std::tie(positions, textureCoordinates, indices, rectangle) = render(font, size, text);
MeshTools::interleave(&mesh, vertexBuffer, usage, positions, textureCoordinates);
MeshTools::compressIndices(&mesh, indexBuffer, usage, indices);
@ -153,7 +159,7 @@ template<std::uint8_t dimensions> Mesh TextRenderer<dimensions>::render(Font& fo
typename Shaders::AbstractTextShader<dimensions>::Position(),
typename Shaders::AbstractTextShader<dimensions>::TextureCoordinates());
return mesh;
return std::make_tuple(std::move(mesh), rectangle);
}
template class TextRenderer<2>;

13
src/Text/TextRenderer.h

@ -47,9 +47,11 @@ template<std::uint8_t dimensions> class MAGNUM_TEXT_EXPORT TextRenderer {
* @param font %Font to use
* @param size %Font size
* @param text %Text to render
* @return Tuple with vertex positions, texture coordinates and indices
*
* Returns tuple with vertex positions, texture coordinates, indices
* and rectangle spanning the rendered text.
*/
static std::tuple<std::vector<typename DimensionTraits<dimensions>::PointType>, std::vector<Vector2>, std::vector<std::uint32_t>> render(Font& font, GLfloat size, const std::string& text);
static std::tuple<std::vector<typename DimensionTraits<dimensions>::PointType>, std::vector<Vector2>, std::vector<std::uint32_t>, Rectangle> render(Font& font, GLfloat size, const std::string& text);
/**
* @brief Render text
@ -59,10 +61,11 @@ template<std::uint8_t dimensions> class MAGNUM_TEXT_EXPORT TextRenderer {
* @param vertexBuffer %Buffer where to store vertices
* @param indexBuffer %Buffer where to store indices
* @param usage Usage of vertex and index buffer
* @return Indexed mesh prepared for use with Shaders::AbstractTextShader
* subclasses
*
* Returns mesh prepared for use with Shaders::AbstractTextShader
* subclasses and rectangle spanning the rendered text.
*/
static Mesh render(Font& font, GLfloat size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage);
static std::tuple<Mesh, Rectangle> render(Font& font, GLfloat size, const std::string& text, Buffer* vertexBuffer, Buffer* indexBuffer, Buffer::Usage usage);
private:
#ifndef DOXYGEN_GENERATING_OUTPUT

Loading…
Cancel
Save