mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 15:00:04 +00:00
Text alignment and point size
This commit is contained in:
parent
ee0f3b35bf
commit
89ca94eda8
4 changed files with 42 additions and 9 deletions
|
|
@ -55,11 +55,12 @@ bool TextShader::Initialize()
|
||||||
void TextShader::RenderText(const std::string& text,
|
void TextShader::RenderText(const std::string& text,
|
||||||
float x,
|
float x,
|
||||||
float y,
|
float y,
|
||||||
float scale,
|
float pointSize,
|
||||||
const glm::mat4& projection,
|
const glm::mat4& projection,
|
||||||
const boost::gil::rgba8_pixel_t& color,
|
const boost::gil::rgba8_pixel_t& color,
|
||||||
std::shared_ptr<util::Font> font,
|
std::shared_ptr<util::Font> font,
|
||||||
GLuint textureId)
|
GLuint textureId,
|
||||||
|
TextAlign align)
|
||||||
{
|
{
|
||||||
OpenGLFunctions& gl = p->gl_;
|
OpenGLFunctions& gl = p->gl_;
|
||||||
|
|
||||||
|
|
@ -74,9 +75,26 @@ void TextShader::RenderText(const std::string& text,
|
||||||
gl.glActiveTexture(GL_TEXTURE0);
|
gl.glActiveTexture(GL_TEXTURE0);
|
||||||
gl.glBindTexture(GL_TEXTURE_2D, textureId);
|
gl.glBindTexture(GL_TEXTURE_2D, textureId);
|
||||||
|
|
||||||
|
switch (align)
|
||||||
|
{
|
||||||
|
case TextAlign::Left:
|
||||||
|
// Do nothing
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TextAlign::Center:
|
||||||
|
// X position is the center of text, subtract half length
|
||||||
|
x -= font->TextLength(text, pointSize) * 0.5f;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TextAlign::Right:
|
||||||
|
// X position is the end of text, subtract length
|
||||||
|
x -= font->TextLength(text, pointSize);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<util::FontBuffer> buffer =
|
std::shared_ptr<util::FontBuffer> buffer =
|
||||||
std::make_shared<util::FontBuffer>();
|
std::make_shared<util::FontBuffer>();
|
||||||
font->BufferText(buffer, text, x, y, scale, color);
|
font->BufferText(buffer, text, x, y, pointSize, color);
|
||||||
buffer->Render(gl);
|
buffer->Render(gl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,13 @@ namespace qt
|
||||||
namespace gl
|
namespace gl
|
||||||
{
|
{
|
||||||
|
|
||||||
|
enum class TextAlign
|
||||||
|
{
|
||||||
|
Left,
|
||||||
|
Center,
|
||||||
|
Right
|
||||||
|
};
|
||||||
|
|
||||||
class TextShaderImpl;
|
class TextShaderImpl;
|
||||||
|
|
||||||
class TextShader : public ShaderProgram
|
class TextShader : public ShaderProgram
|
||||||
|
|
@ -37,7 +44,8 @@ public:
|
||||||
const glm::mat4& projection,
|
const glm::mat4& projection,
|
||||||
const boost::gil::rgba8_pixel_t& color,
|
const boost::gil::rgba8_pixel_t& color,
|
||||||
std::shared_ptr<util::Font> font,
|
std::shared_ptr<util::Font> font,
|
||||||
GLuint textureId);
|
GLuint textureId,
|
||||||
|
TextAlign align = TextAlign::Left);
|
||||||
void SetProjection(const glm::mat4& projection);
|
void SetProjection(const glm::mat4& projection);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,9 @@ static const std::string CODEPOINTS =
|
||||||
|
|
||||||
static const std::string logPrefix_ = "[scwx::qt::util::font] ";
|
static const std::string logPrefix_ = "[scwx::qt::util::font] ";
|
||||||
|
|
||||||
|
static constexpr float BASE_POINT_SIZE = 72.0f;
|
||||||
|
static constexpr float POINT_SCALE = 1.0f / BASE_POINT_SIZE;
|
||||||
|
|
||||||
static std::unordered_map<std::string, std::shared_ptr<Font>> fontMap_;
|
static std::unordered_map<std::string, std::shared_ptr<Font>> fontMap_;
|
||||||
|
|
||||||
class FontImpl
|
class FontImpl
|
||||||
|
|
@ -96,11 +99,13 @@ float Font::BufferText(std::shared_ptr<FontBuffer> buffer,
|
||||||
const std::string& text,
|
const std::string& text,
|
||||||
float x,
|
float x,
|
||||||
float y,
|
float y,
|
||||||
float scale,
|
float pointSize,
|
||||||
boost::gil::rgba8_pixel_t color) const
|
boost::gil::rgba8_pixel_t color) const
|
||||||
{
|
{
|
||||||
static constexpr float colorScale = 1.0f / 255.0f;
|
static constexpr float colorScale = 1.0f / 255.0f;
|
||||||
|
|
||||||
|
const float scale = pointSize * POINT_SCALE;
|
||||||
|
|
||||||
float r = color[0] * colorScale;
|
float r = color[0] * colorScale;
|
||||||
float g = color[1] * colorScale;
|
float g = color[1] * colorScale;
|
||||||
float b = color[2] * colorScale;
|
float b = color[2] * colorScale;
|
||||||
|
|
@ -154,8 +159,10 @@ float Font::Kerning(char c1, char c2) const
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Font::TextLength(const std::string& text, float scale) const
|
float Font::TextLength(const std::string& text, float pointSize) const
|
||||||
{
|
{
|
||||||
|
const float scale = pointSize * POINT_SCALE;
|
||||||
|
|
||||||
float x = 0.0f;
|
float x = 0.0f;
|
||||||
|
|
||||||
for (size_t i = 0; i < text.length(); ++i)
|
for (size_t i = 0; i < text.length(); ++i)
|
||||||
|
|
@ -232,7 +239,7 @@ std::shared_ptr<Font> Font::Create(const std::string& resource)
|
||||||
|
|
||||||
font->p->atlas_ = ftgl::texture_atlas_new(512, 512, 1);
|
font->p->atlas_ = ftgl::texture_atlas_new(512, 512, 1);
|
||||||
ftgl::texture_font_t* textureFont = ftgl::texture_font_new_from_memory(
|
ftgl::texture_font_t* textureFont = ftgl::texture_font_new_from_memory(
|
||||||
font->p->atlas_, 72, fontData.constData(), fontData.size());
|
font->p->atlas_, BASE_POINT_SIZE, fontData.constData(), fontData.size());
|
||||||
|
|
||||||
textureFont->rendermode = ftgl::RENDER_SIGNED_DISTANCE_FIELD;
|
textureFont->rendermode = ftgl::RENDER_SIGNED_DISTANCE_FIELD;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,10 +33,10 @@ public:
|
||||||
const std::string& text,
|
const std::string& text,
|
||||||
float x,
|
float x,
|
||||||
float y,
|
float y,
|
||||||
float scale,
|
float pointSize,
|
||||||
boost::gil::rgba8_pixel_t color) const;
|
boost::gil::rgba8_pixel_t color) const;
|
||||||
float Kerning(char c1, char c2) const;
|
float Kerning(char c1, char c2) const;
|
||||||
float TextLength(const std::string& text, float scale) const;
|
float TextLength(const std::string& text, float pointSize) const;
|
||||||
|
|
||||||
GLuint GenerateTexture(OpenGLFunctions& gl);
|
GLuint GenerateTexture(OpenGLFunctions& gl);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue