Calculating position for FreeType glyph rendering?










1















I'm trying to render some text using FreeType and OpenGL:



example image



text = "fghjRT-123VWYUGHJ$@%"



And have two questions:



  1. Why there are so weird spaces between letters? (what wrong with advance?)

  2. How to calculate top position (or origin position)? (there are some space between yellow border and characters. I want to attach it to the top border)

My render code:



/* top left position on screen to render text (yellow border) */
Vector<2> pos = params.rect.left_top();

/* adjust to bottom (how to get correct origin position?) */
pos.y() += font->char_size();

for (char ch : params.text)

/* contains info after freetype FT_LoadGlyph */
FontChar const* char_info = font->find_char(ch);

RectF char_rect(
pos - char_info->bearing(), /* left, top */
char_info->glyph_rect().size() /* width, height */
);

/* convert screen coordinates to OpenGL coordinates */
RectF dr = calc_coord(m_screen_size, char_rect);

/* pack position of glyph and it's texture from bitmap
* into one sequence of data
*/
Vector<16> spr_coords = pack_spr_info(dr, char_info->rect());

m_sprite_buffer.push_back(spr_coords);

/* move pen by advance */
pos.x() += char_info->advance();



Piece of my glyph loading code:



FT_GlyphSlot slot = face->glyph;

char_info->bearing() =
slot->metrics.horiBearingX / 64.0f,
slot->metrics.horiBearingY / 64.0f
;

char_info->glyph_rect() =
slot->metrics.horiBearingX / 64.0f, /* left */
slot->metrics.horiBearingY / 64.0f, /* top */
slot->metrics.width / 64.0f, /* width */
slot->metrics.height / 64.0f /* height */
;

char_info->advance() = slot->metrics.horiAdvance / 64.0f;









share|improve this question




























    1















    I'm trying to render some text using FreeType and OpenGL:



    example image



    text = "fghjRT-123VWYUGHJ$@%"



    And have two questions:



    1. Why there are so weird spaces between letters? (what wrong with advance?)

    2. How to calculate top position (or origin position)? (there are some space between yellow border and characters. I want to attach it to the top border)

    My render code:



    /* top left position on screen to render text (yellow border) */
    Vector<2> pos = params.rect.left_top();

    /* adjust to bottom (how to get correct origin position?) */
    pos.y() += font->char_size();

    for (char ch : params.text)

    /* contains info after freetype FT_LoadGlyph */
    FontChar const* char_info = font->find_char(ch);

    RectF char_rect(
    pos - char_info->bearing(), /* left, top */
    char_info->glyph_rect().size() /* width, height */
    );

    /* convert screen coordinates to OpenGL coordinates */
    RectF dr = calc_coord(m_screen_size, char_rect);

    /* pack position of glyph and it's texture from bitmap
    * into one sequence of data
    */
    Vector<16> spr_coords = pack_spr_info(dr, char_info->rect());

    m_sprite_buffer.push_back(spr_coords);

    /* move pen by advance */
    pos.x() += char_info->advance();



    Piece of my glyph loading code:



    FT_GlyphSlot slot = face->glyph;

    char_info->bearing() =
    slot->metrics.horiBearingX / 64.0f,
    slot->metrics.horiBearingY / 64.0f
    ;

    char_info->glyph_rect() =
    slot->metrics.horiBearingX / 64.0f, /* left */
    slot->metrics.horiBearingY / 64.0f, /* top */
    slot->metrics.width / 64.0f, /* width */
    slot->metrics.height / 64.0f /* height */
    ;

    char_info->advance() = slot->metrics.horiAdvance / 64.0f;









    share|improve this question


























      1












      1








      1








      I'm trying to render some text using FreeType and OpenGL:



      example image



      text = "fghjRT-123VWYUGHJ$@%"



      And have two questions:



      1. Why there are so weird spaces between letters? (what wrong with advance?)

      2. How to calculate top position (or origin position)? (there are some space between yellow border and characters. I want to attach it to the top border)

      My render code:



      /* top left position on screen to render text (yellow border) */
      Vector<2> pos = params.rect.left_top();

      /* adjust to bottom (how to get correct origin position?) */
      pos.y() += font->char_size();

      for (char ch : params.text)

      /* contains info after freetype FT_LoadGlyph */
      FontChar const* char_info = font->find_char(ch);

      RectF char_rect(
      pos - char_info->bearing(), /* left, top */
      char_info->glyph_rect().size() /* width, height */
      );

      /* convert screen coordinates to OpenGL coordinates */
      RectF dr = calc_coord(m_screen_size, char_rect);

      /* pack position of glyph and it's texture from bitmap
      * into one sequence of data
      */
      Vector<16> spr_coords = pack_spr_info(dr, char_info->rect());

      m_sprite_buffer.push_back(spr_coords);

      /* move pen by advance */
      pos.x() += char_info->advance();



      Piece of my glyph loading code:



      FT_GlyphSlot slot = face->glyph;

      char_info->bearing() =
      slot->metrics.horiBearingX / 64.0f,
      slot->metrics.horiBearingY / 64.0f
      ;

      char_info->glyph_rect() =
      slot->metrics.horiBearingX / 64.0f, /* left */
      slot->metrics.horiBearingY / 64.0f, /* top */
      slot->metrics.width / 64.0f, /* width */
      slot->metrics.height / 64.0f /* height */
      ;

      char_info->advance() = slot->metrics.horiAdvance / 64.0f;









      share|improve this question
















      I'm trying to render some text using FreeType and OpenGL:



      example image



      text = "fghjRT-123VWYUGHJ$@%"



      And have two questions:



      1. Why there are so weird spaces between letters? (what wrong with advance?)

      2. How to calculate top position (or origin position)? (there are some space between yellow border and characters. I want to attach it to the top border)

      My render code:



      /* top left position on screen to render text (yellow border) */
      Vector<2> pos = params.rect.left_top();

      /* adjust to bottom (how to get correct origin position?) */
      pos.y() += font->char_size();

      for (char ch : params.text)

      /* contains info after freetype FT_LoadGlyph */
      FontChar const* char_info = font->find_char(ch);

      RectF char_rect(
      pos - char_info->bearing(), /* left, top */
      char_info->glyph_rect().size() /* width, height */
      );

      /* convert screen coordinates to OpenGL coordinates */
      RectF dr = calc_coord(m_screen_size, char_rect);

      /* pack position of glyph and it's texture from bitmap
      * into one sequence of data
      */
      Vector<16> spr_coords = pack_spr_info(dr, char_info->rect());

      m_sprite_buffer.push_back(spr_coords);

      /* move pen by advance */
      pos.x() += char_info->advance();



      Piece of my glyph loading code:



      FT_GlyphSlot slot = face->glyph;

      char_info->bearing() =
      slot->metrics.horiBearingX / 64.0f,
      slot->metrics.horiBearingY / 64.0f
      ;

      char_info->glyph_rect() =
      slot->metrics.horiBearingX / 64.0f, /* left */
      slot->metrics.horiBearingY / 64.0f, /* top */
      slot->metrics.width / 64.0f, /* width */
      slot->metrics.height / 64.0f /* height */
      ;

      char_info->advance() = slot->metrics.horiAdvance / 64.0f;






      c++ opengl true-type-fonts freetype






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 18:32









      genpfault

      42.3k953100




      42.3k953100










      asked Nov 12 '18 at 18:17









      kerrytazikerrytazi

      29127




      29127






















          1 Answer
          1






          active

          oldest

          votes


















          2














          Ok, I found out the answer by myself.

          There are was a mistake in my calculation for char_rect position. Correct way:



          RectF char_rect(
          pos.x() + char_info->bearing().x(), /* left */
          pos.y() - char_info->bearing().y(), /* top */
          char_info->glyph_rect().size() /* width, height */
          );


          And the way to find baseline(origin) adjusted from top border:



          Vector<2> pos = params.rect.left_top();
          pos.y() += font->ascend(); /* move pen down on the screen */


          Where:



          FT_Face face = ...;
          font->ascend() = face->ascender / 32;





          share|improve this answer






















            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%2f53267905%2fcalculating-position-for-freetype-glyph-rendering%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Ok, I found out the answer by myself.

            There are was a mistake in my calculation for char_rect position. Correct way:



            RectF char_rect(
            pos.x() + char_info->bearing().x(), /* left */
            pos.y() - char_info->bearing().y(), /* top */
            char_info->glyph_rect().size() /* width, height */
            );


            And the way to find baseline(origin) adjusted from top border:



            Vector<2> pos = params.rect.left_top();
            pos.y() += font->ascend(); /* move pen down on the screen */


            Where:



            FT_Face face = ...;
            font->ascend() = face->ascender / 32;





            share|improve this answer



























              2














              Ok, I found out the answer by myself.

              There are was a mistake in my calculation for char_rect position. Correct way:



              RectF char_rect(
              pos.x() + char_info->bearing().x(), /* left */
              pos.y() - char_info->bearing().y(), /* top */
              char_info->glyph_rect().size() /* width, height */
              );


              And the way to find baseline(origin) adjusted from top border:



              Vector<2> pos = params.rect.left_top();
              pos.y() += font->ascend(); /* move pen down on the screen */


              Where:



              FT_Face face = ...;
              font->ascend() = face->ascender / 32;





              share|improve this answer

























                2












                2








                2







                Ok, I found out the answer by myself.

                There are was a mistake in my calculation for char_rect position. Correct way:



                RectF char_rect(
                pos.x() + char_info->bearing().x(), /* left */
                pos.y() - char_info->bearing().y(), /* top */
                char_info->glyph_rect().size() /* width, height */
                );


                And the way to find baseline(origin) adjusted from top border:



                Vector<2> pos = params.rect.left_top();
                pos.y() += font->ascend(); /* move pen down on the screen */


                Where:



                FT_Face face = ...;
                font->ascend() = face->ascender / 32;





                share|improve this answer













                Ok, I found out the answer by myself.

                There are was a mistake in my calculation for char_rect position. Correct way:



                RectF char_rect(
                pos.x() + char_info->bearing().x(), /* left */
                pos.y() - char_info->bearing().y(), /* top */
                char_info->glyph_rect().size() /* width, height */
                );


                And the way to find baseline(origin) adjusted from top border:



                Vector<2> pos = params.rect.left_top();
                pos.y() += font->ascend(); /* move pen down on the screen */


                Where:



                FT_Face face = ...;
                font->ascend() = face->ascender / 32;






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 20:05









                kerrytazikerrytazi

                29127




                29127





























                    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%2f53267905%2fcalculating-position-for-freetype-glyph-rendering%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

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    How do I collapse sections of code in Visual Studio Code for Windows?

                    ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ