How to achieve good text height for freetypegl?












1















I am struggling with freetypegl and height calculations. I found the cherno used the library in his Sparky engine and he calculates it like so



float Font::GetHeight(const String& text) const
{
using namespace ftgl;

const maths::vec2& scale = m_Scale;
float min = 0.0f;
float max = 0.0f;
for (int i = 0; i < text.size(); i++)
{
texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
SP_ASSERT(glyph);
float height = glyph->height / scale.y;
float offset = glyph->offset_y / scale.y - height;
if (offset < min)
min = offset;
if (height > max)
max = height;
}
return abs(min) + abs(max);
}

maths::vec2 Font::GetOffsets(const String& text) const
{
using namespace ftgl;

if (text.empty())
return maths::vec2(0.0f, 0.0f);

texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[0]);
SP_ASSERT(glyph);

float yo = 0.0f;
const maths::vec2& scale = m_Scale;
for (int i = 0; i < text.size(); i++)
{
texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
SP_ASSERT(glyph);
float height = glyph->height / scale.y;
float offset = glyph->offset_y / scale.y - height;
if (offset < yo)
yo = offset;
}

return maths::vec2(glyph->offset_x / scale.x, yo);
}


and it does not satisfy me, because for example [fps] string is a bit too height as I'd like it to be.



my code example:



//                                                        A B G R
// text = "[fps]"; position = {200.0f, 200.0f}; color = 0xffffffff
renderer2D->DrawString("[fps]", 200.0f, 0xffffffff, font);

//position X = 200.0f; position Y = 200.0f + font.getOffsets("[fps]").y
//width = font.getWidth("[fps]"; height = font.getHeight("[fps]");
// A B G R
//color = 0xff0000ff
//thickness = 0.2f
renderer2D->DrawRect(Rect(200.0f,
200.0f + font.getOffsets("[fps]").y,
font.getWidth("[fps]"),
font.getHeight("[fps]")), 0xff0000ff, 0.2f);


I want the rect to be around the text so I add offset (which is negative) to position Y, because letters like p, q, g etc. have a bit different height, so when I add offset Y it will cover whole letter.



the problem shows when the text has chars like: @, [, ]



its height is a bit too big



enter image description here



it should be a bit lower like this:



enter image description here



and that'd be perfect.



How can I achieve this?










share|improve this question























  • What is the scale? And what is height for '[' and 'f'? Is the font generated for the text size and display dpi you are using? The text on the images look blurry

    – Andreas
    Nov 25 '18 at 11:23
















1















I am struggling with freetypegl and height calculations. I found the cherno used the library in his Sparky engine and he calculates it like so



float Font::GetHeight(const String& text) const
{
using namespace ftgl;

const maths::vec2& scale = m_Scale;
float min = 0.0f;
float max = 0.0f;
for (int i = 0; i < text.size(); i++)
{
texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
SP_ASSERT(glyph);
float height = glyph->height / scale.y;
float offset = glyph->offset_y / scale.y - height;
if (offset < min)
min = offset;
if (height > max)
max = height;
}
return abs(min) + abs(max);
}

maths::vec2 Font::GetOffsets(const String& text) const
{
using namespace ftgl;

if (text.empty())
return maths::vec2(0.0f, 0.0f);

texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[0]);
SP_ASSERT(glyph);

float yo = 0.0f;
const maths::vec2& scale = m_Scale;
for (int i = 0; i < text.size(); i++)
{
texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
SP_ASSERT(glyph);
float height = glyph->height / scale.y;
float offset = glyph->offset_y / scale.y - height;
if (offset < yo)
yo = offset;
}

return maths::vec2(glyph->offset_x / scale.x, yo);
}


and it does not satisfy me, because for example [fps] string is a bit too height as I'd like it to be.



my code example:



//                                                        A B G R
// text = "[fps]"; position = {200.0f, 200.0f}; color = 0xffffffff
renderer2D->DrawString("[fps]", 200.0f, 0xffffffff, font);

//position X = 200.0f; position Y = 200.0f + font.getOffsets("[fps]").y
//width = font.getWidth("[fps]"; height = font.getHeight("[fps]");
// A B G R
//color = 0xff0000ff
//thickness = 0.2f
renderer2D->DrawRect(Rect(200.0f,
200.0f + font.getOffsets("[fps]").y,
font.getWidth("[fps]"),
font.getHeight("[fps]")), 0xff0000ff, 0.2f);


I want the rect to be around the text so I add offset (which is negative) to position Y, because letters like p, q, g etc. have a bit different height, so when I add offset Y it will cover whole letter.



the problem shows when the text has chars like: @, [, ]



its height is a bit too big



enter image description here



it should be a bit lower like this:



enter image description here



and that'd be perfect.



How can I achieve this?










share|improve this question























  • What is the scale? And what is height for '[' and 'f'? Is the font generated for the text size and display dpi you are using? The text on the images look blurry

    – Andreas
    Nov 25 '18 at 11:23














1












1








1








I am struggling with freetypegl and height calculations. I found the cherno used the library in his Sparky engine and he calculates it like so



float Font::GetHeight(const String& text) const
{
using namespace ftgl;

const maths::vec2& scale = m_Scale;
float min = 0.0f;
float max = 0.0f;
for (int i = 0; i < text.size(); i++)
{
texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
SP_ASSERT(glyph);
float height = glyph->height / scale.y;
float offset = glyph->offset_y / scale.y - height;
if (offset < min)
min = offset;
if (height > max)
max = height;
}
return abs(min) + abs(max);
}

maths::vec2 Font::GetOffsets(const String& text) const
{
using namespace ftgl;

if (text.empty())
return maths::vec2(0.0f, 0.0f);

texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[0]);
SP_ASSERT(glyph);

float yo = 0.0f;
const maths::vec2& scale = m_Scale;
for (int i = 0; i < text.size(); i++)
{
texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
SP_ASSERT(glyph);
float height = glyph->height / scale.y;
float offset = glyph->offset_y / scale.y - height;
if (offset < yo)
yo = offset;
}

return maths::vec2(glyph->offset_x / scale.x, yo);
}


and it does not satisfy me, because for example [fps] string is a bit too height as I'd like it to be.



my code example:



//                                                        A B G R
// text = "[fps]"; position = {200.0f, 200.0f}; color = 0xffffffff
renderer2D->DrawString("[fps]", 200.0f, 0xffffffff, font);

//position X = 200.0f; position Y = 200.0f + font.getOffsets("[fps]").y
//width = font.getWidth("[fps]"; height = font.getHeight("[fps]");
// A B G R
//color = 0xff0000ff
//thickness = 0.2f
renderer2D->DrawRect(Rect(200.0f,
200.0f + font.getOffsets("[fps]").y,
font.getWidth("[fps]"),
font.getHeight("[fps]")), 0xff0000ff, 0.2f);


I want the rect to be around the text so I add offset (which is negative) to position Y, because letters like p, q, g etc. have a bit different height, so when I add offset Y it will cover whole letter.



the problem shows when the text has chars like: @, [, ]



its height is a bit too big



enter image description here



it should be a bit lower like this:



enter image description here



and that'd be perfect.



How can I achieve this?










share|improve this question














I am struggling with freetypegl and height calculations. I found the cherno used the library in his Sparky engine and he calculates it like so



float Font::GetHeight(const String& text) const
{
using namespace ftgl;

const maths::vec2& scale = m_Scale;
float min = 0.0f;
float max = 0.0f;
for (int i = 0; i < text.size(); i++)
{
texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
SP_ASSERT(glyph);
float height = glyph->height / scale.y;
float offset = glyph->offset_y / scale.y - height;
if (offset < min)
min = offset;
if (height > max)
max = height;
}
return abs(min) + abs(max);
}

maths::vec2 Font::GetOffsets(const String& text) const
{
using namespace ftgl;

if (text.empty())
return maths::vec2(0.0f, 0.0f);

texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[0]);
SP_ASSERT(glyph);

float yo = 0.0f;
const maths::vec2& scale = m_Scale;
for (int i = 0; i < text.size(); i++)
{
texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
SP_ASSERT(glyph);
float height = glyph->height / scale.y;
float offset = glyph->offset_y / scale.y - height;
if (offset < yo)
yo = offset;
}

return maths::vec2(glyph->offset_x / scale.x, yo);
}


and it does not satisfy me, because for example [fps] string is a bit too height as I'd like it to be.



my code example:



//                                                        A B G R
// text = "[fps]"; position = {200.0f, 200.0f}; color = 0xffffffff
renderer2D->DrawString("[fps]", 200.0f, 0xffffffff, font);

//position X = 200.0f; position Y = 200.0f + font.getOffsets("[fps]").y
//width = font.getWidth("[fps]"; height = font.getHeight("[fps]");
// A B G R
//color = 0xff0000ff
//thickness = 0.2f
renderer2D->DrawRect(Rect(200.0f,
200.0f + font.getOffsets("[fps]").y,
font.getWidth("[fps]"),
font.getHeight("[fps]")), 0xff0000ff, 0.2f);


I want the rect to be around the text so I add offset (which is negative) to position Y, because letters like p, q, g etc. have a bit different height, so when I add offset Y it will cover whole letter.



the problem shows when the text has chars like: @, [, ]



its height is a bit too big



enter image description here



it should be a bit lower like this:



enter image description here



and that'd be perfect.



How can I achieve this?







c++ opengl text rendering freetype






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 21:15









ANewGuyANewGuy

404




404













  • What is the scale? And what is height for '[' and 'f'? Is the font generated for the text size and display dpi you are using? The text on the images look blurry

    – Andreas
    Nov 25 '18 at 11:23



















  • What is the scale? And what is height for '[' and 'f'? Is the font generated for the text size and display dpi you are using? The text on the images look blurry

    – Andreas
    Nov 25 '18 at 11:23

















What is the scale? And what is height for '[' and 'f'? Is the font generated for the text size and display dpi you are using? The text on the images look blurry

– Andreas
Nov 25 '18 at 11:23





What is the scale? And what is height for '[' and 'f'? Is the font generated for the text size and display dpi you are using? The text on the images look blurry

– Andreas
Nov 25 '18 at 11:23












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462439%2fhow-to-achieve-good-text-height-for-freetypegl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462439%2fhow-to-achieve-good-text-height-for-freetypegl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Fotorealismo