milky jing | 1 Mar 15:23 2012
Picon

[ft] font size confusion (NOMINAL & REAL_DIM)

Hi List,


I am facing a problem which requires the calculation of the equivalent font size (fontSize2) for FT_SIZE_REQUEST_TYPE_NOMINAL, i.e., FT_Set_Char_Size(face, fontSize2 * 64, 0, 0, 0), from the font size set for FT_SIZE_REQUEST_TYPE_REAL_DIM, here equivalent is that, I mean the two ways of setting the font size will produce the same output on my PC screen, (I don't know whether the DPI matters, but it is 96).

The reason is that I am using two external projects (which both use FreeType) on my own. One uses NOMINAL font size, the other uses REAL_DIM. I've tried to hack the definition, but it's a way too complicated. So, any ideas/hints/solutions will be eagerly welcomed.


p.s. My first time to use a mailing list :-P


Thanks,
Albert

_______________________________________________
Freetype mailing list
Freetype <at> nongnu.org
https://lists.nongnu.org/mailman/listinfo/freetype
Werner LEMBERG | 1 Mar 19:04 2012
Picon

Re: [ft] font size confusion (NOMINAL & REAL_DIM)


> I am facing a problem which requires the calculation of the
> equivalent font size (fontSize2) for FT_SIZE_REQUEST_TYPE_NOMINAL,
> [...]

Answering your question would be much simpler if you provided a
(compilable) code snippet which demonstrates what you expect and what
you get instead, ideally using a freely available font.

    Werner
milky jing | 2 Mar 07:05 2012
Picon

Re: [ft] font size confusion (NOMINAL & REAL_DIM)

Thank for your reply Werner,


Here is a piece of code I wrote moments ago, 

#include "./FreeType/ft2build.h"
#include FT_FREETYPE_H
#pragma comment(lib, "./FreeType/freetype248.lib")

int main() {
    FT_Library library;
    FT_Face face1;
    FT_Face face2;
    const char *filename = "C:\\Windows\\Fonts\\Arial.ttf";     /* the algorithm should not be font specific, i.e., suitable for a variety of fonts */
    int fontSize1 = 40; /* fontSize1 is a given value */
    int fontSize2;      /* fontSize2 need to be calculated, derived from fontSize1 */
    FT_Size_RequestRec rq;
    FT_Init_FreeType(&library);
    FT_New_Face(library, filename, 0, &face1);
    FT_New_Face(library, filename, 0, &face2);
    /* set the REAL_DIM size, fontSize1 */
    rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
    rq.width = 0;
    rq.height = fontSize1 << 6;
    rq.horiResolution = 0;
    rq.vertResolution = 0;
    FT_Request_Size(face1, &rq);
    /* set the NOMINAL size, fontSize2 */
    fontSize2 = fontSize1 - 4;    /* How to calculate fontSize2 to meet the following requirements? I tried to -1, -2, -3, and finally found -4 is good */
    FT_Set_Char_Size(face2, 0, fontSize2 << 6, 0, 0);
    /* the requirement for the calculation of fontSize2 is, for example */
    if (face2->size->metrics.ascender == face1->size->metrics.ascender && face2->size->metrics.descender == face1->size->metrics.descender) {
        printf("Good, fontSize2 is just what we wanted!\n");
        printf("ascender1=%i\tascneder2=%i\ndescender1=%i\tdescender2=%i\n", face1->size->metrics.ascender, face2->size->metrics.ascender, face1->size->metrics.descender, face1->size->metrics.descender);
    } else {
        printf("Bad, try another fontSize2.\n");
        printf("ascender1=%i\tascneder2=%i\ndescender1=%i\tdescender2=%i\n", face1->size->metrics.ascender, face2->size->metrics.ascender, face1->size->metrics.descender, face1->size->metrics.descender);
    }
    /* clean up */
    FT_Done_Face(face1);
    FT_Done_Face(face2);
    FT_Done_FreeType(library);
    return 0;
}



On Fri, Mar 2, 2012 at 2:04 AM, Werner LEMBERG <wl <at> gnu.org> wrote:

> I am facing a problem which requires the calculation of the
> equivalent font size (fontSize2) for FT_SIZE_REQUEST_TYPE_NOMINAL,
> [...]

Answering your question would be much simpler if you provided a
(compilable) code snippet which demonstrates what you expect and what
you get instead, ideally using a freely available font.


   Werner

_______________________________________________
Freetype mailing list
Freetype <at> nongnu.org
https://lists.nongnu.org/mailman/listinfo/freetype
milky jing | 2 Mar 07:42 2012
Picon

Re: [ft] font size confusion (NOMINAL & REAL_DIM)

Just after I wrote the code, I found a tricky way to calculate the required font size, that is, using a loop to test the limited amount of possible font sizes, until we find the correct one.


But, surely I'd prefer a more formal algorithm to this problem.

Regards,
Albert


On Fri, Mar 2, 2012 at 2:05 PM, milky jing <milkyjing <at> gmail.com> wrote:
Thank for your reply Werner,

Here is a piece of code I wrote moments ago, 

#include "./FreeType/ft2build.h"
#include FT_FREETYPE_H
#pragma comment(lib, "./FreeType/freetype248.lib")

int main() {
    FT_Library library;
    FT_Face face1;
    FT_Face face2;
    const char *filename = "C:\\Windows\\Fonts\\Arial.ttf";     /* the algorithm should not be font specific, i.e., suitable for a variety of fonts */
    int fontSize1 = 40; /* fontSize1 is a given value */
    int fontSize2;      /* fontSize2 need to be calculated, derived from fontSize1 */
    FT_Size_RequestRec rq;
    FT_Init_FreeType(&library);
    FT_New_Face(library, filename, 0, &face1);
    FT_New_Face(library, filename, 0, &face2);
    /* set the REAL_DIM size, fontSize1 */
    rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
    rq.width = 0;
    rq.height = fontSize1 << 6;
    rq.horiResolution = 0;
    rq.vertResolution = 0;
    FT_Request_Size(face1, &rq);
    /* set the NOMINAL size, fontSize2 */
    fontSize2 = fontSize1 - 4;    /* How to calculate fontSize2 to meet the following requirements? I tried to -1, -2, -3, and finally found -4 is good */
    FT_Set_Char_Size(face2, 0, fontSize2 << 6, 0, 0);
    /* the requirement for the calculation of fontSize2 is, for example */
    if (face2->size->metrics.ascender == face1->size->metrics.ascender && face2->size->metrics.descender == face1->size->metrics.descender) {
        printf("Good, fontSize2 is just what we wanted!\n");
        printf("ascender1=%i\tascneder2=%i\ndescender1=%i\tdescender2=%i\n", face1->size->metrics.ascender, face2->size->metrics.ascender, face1->size->metrics.descender, face1->size->metrics.descender);
    } else {
        printf("Bad, try another fontSize2.\n");
        printf("ascender1=%i\tascneder2=%i\ndescender1=%i\tdescender2=%i\n", face1->size->metrics.ascender, face2->size->metrics.ascender, face1->size->metrics.descender, face1->size->metrics.descender);
    }
    /* clean up */
    FT_Done_Face(face1);
    FT_Done_Face(face2);
    FT_Done_FreeType(library);
    return 0;
}



On Fri, Mar 2, 2012 at 2:04 AM, Werner LEMBERG <wl <at> gnu.org> wrote:

> I am facing a problem which requires the calculation of the
> equivalent font size (fontSize2) for FT_SIZE_REQUEST_TYPE_NOMINAL,
> [...]

Answering your question would be much simpler if you provided a
(compilable) code snippet which demonstrates what you expect and what
you get instead, ideally using a freely available font.


   Werner


_______________________________________________
Freetype mailing list
Freetype <at> nongnu.org
https://lists.nongnu.org/mailman/listinfo/freetype
Werner LEMBERG | 2 Mar 10:10 2012
Picon

Re: [ft] font size confusion (NOMINAL & REAL_DIM)


Thanks for the example.

>     /* set the REAL_DIM size, fontSize1 */
>     rq.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
>
>     /* set the NOMINAL size, fontSize2 */
>     fontSize2 = fontSize1 - 4;
>     /* How to calculate fontSize2 to meet the following
>        requirements? I tried to -1, -2, -3, and finally found -4 is
>        good */

As the documentation describes, the scaling value of `nominal' is the
units per EM, and the scaling value of `real dim' is the sum of the
ascender and the descender (since descender has negative values if
below the baseline, it's actually a subtraction):

  scaling_nominal = face->units_per_EM
  scaling_realdim = face->ascender - face->descender

Remember, these values are *scaling values*.  Consequently, to
transform from one scaling framework to another, simply compute the
quotient and use it as a factor.

Using arial (version 5.06), I get

  face->ascender  = 1854
  face->descender = -434
  units per EM    = 2048

This gives

       1854 + 434
  f = ------------ = 1.11719...
         2048

In your example, the `real dim' font size is 40, thus the `nominal'
font size is

  40 / 1.11719 = 35.804...

which rounds to 36, as you've deduced by experimentation.

It would be great if you could provide a patch to enhance the
documentation in case you consider it as necessary.

    Werner
milky jing | 4 Mar 12:10 2012
Picon

Re: [ft] font size confusion (NOMINAL & REAL_DIM)

Thanks Werner :-)


Your algorithm works fine.

I also have some other issues about font size, but I think I'd better try to solve them first util I really need help. :-P

p.s. Sorry to reply late.

Regards,
Albert


On Fri, Mar 2, 2012 at 5:10 PM, Werner LEMBERG <wl <at> gnu.org> wrote:

As the documentation describes, the scaling value of `nominal' is the
units per EM, and the scaling value of `real dim' is the sum of the
ascender and the descender (since descender has negative values if
below the baseline, it's actually a subtraction):

 scaling_nominal = face->units_per_EM
 scaling_realdim = face->ascender - face->descender

Remember, these values are *scaling values*.  Consequently, to
transform from one scaling framework to another, simply compute the
quotient and use it as a factor.

Using arial (version 5.06), I get

 face->ascender  = 1854
 face->descender = -434
 units per EM    = 2048

This gives

      1854 + 434
 f = ------------ = 1.11719...
        2048

In your example, the `real dim' font size is 40, thus the `nominal'
font size is

 40 / 1.11719 = 35.804...

which rounds to 36, as you've deduced by experimentation.

It would be great if you could provide a patch to enhance the
documentation in case you consider it as necessary.


   Werner

_______________________________________________
Freetype mailing list
Freetype <at> nongnu.org
https://lists.nongnu.org/mailman/listinfo/freetype
Werner LEMBERG | 8 Mar 22:10 2012
Picon

[ft] FreeType 2.4.9 has been released


FreeType 2.4.9 has been released.

It is available from

    http://savannah.nongnu.org/download/freetype/

(please read the footnote on this page!)

or

    http://sourceforge.net/projects/freetype/files/

The latter site also holds older versions of the FreeType library.

See below for  the relevant snippet  from the  CHANGES file; all users
should upgrade.

Enjoy!

   Werner

----------------------------------------------------------------------

FreeType 2  is a software  font engine that  is designed to  be small,
efficient,  highly   customizable,  and  portable   while  capable  of
producing high-quality output (glyph images) of most vector and bitmap
font formats.

Note that  FreeType 2 is  a font service  and doesn't provide  APIs to
perform higher-level features, like text layout or graphics processing
(e.g.,  colored  text  rendering,  `hollowing',  etc.).   However,  it
greatly simplifies these tasks by providing a simple, easy to use, and
uniform interface to access the content of font files.

FreeType  2  is  released  under  two open-source  licenses:  our  own
BSD-like FreeType  License and the  GPL.  It can  thus be used  by any
kind of projects, be they proprietary or not.

----------------------------------------------------------------------

CHANGES BETWEEN 2.4.8 and 2.4.9

  I. IMPORTANT BUG FIXES

    - Another round of fixes to better handle invalid fonts.   Many of
      them are vulnerabilities  (see CVE-2012-1126 up to CVE-2012-1144
      and SA48320) so all users should upgrade.

  II. MISCELLANEOUS

    - The `ENCODING -1 <n>' format of BDF fonts is now supported.

    - For BDF fonts,  support for the whole Unicode encoding range has
      been added.

    - Better TTF support for x_ppem != y_ppem.

    - `FT_Get_Advances' sometimes returned bogus values.

    - The  demo  programs  no  longer  recognize  and  handle  default
      suffixes; you now have to always specify the complete font name.

    - Better rendering and LCD mode cycling added to ftview.
宋江山 | 15 Mar 07:12 2012
Picon

[ft] how to put the character into a rect instead of the baseline corrdinate?

I have done something like that:

// set size
FT_Set_Pixel_Sizes(face, 24, 24);

// generate bitmap
 FT_Load_Char( face, 0x30, FT_LOAD_RENDER|FT_LOAD_MONOCHROME );

//draw the bitmap
RECT my_rect;
my_rect.left=100;
my_rect.top=100;
my_rect.right=100+240;
my_rect.bottom=100+24;

I have two problems:

1. how to put the text in between my_rect.top and my_rect.bottom,
meanwhile all the letters are aligned?
I find that face->glyph->bitmap_top is in baseline corrdinate.
If I use my_rect.top + 24 - face->glyph->bitmap_top as the bitmap base
y point, all the letters are aligned, but the 'g' letter will out of
my_rect.

2. what is the difference between the value of FT_Set_Pixel_Sizes
setted and face->glyph->bitmap.width and rows ?
I know face->glyph->bitmap could no be larger than FT_Set_Pixel_Sizes
setted value;
But is there any letter that face->glyph->bitmap size is equal with
the setted size?

In my situation, I use a 24x240 textout box, the pixel size is set to
24x24, but why the face->glyph->bitmap.width and rows I got is always
smaller than 20?

--

-- 
Best wishes,
Jiangshan
_______________________________________________
Freetype mailing list
Freetype <at> nongnu.org
https://lists.nongnu.org/mailman/listinfo/freetype
Werner LEMBERG | 15 Mar 22:44 2012
Picon

Re: [ft] how to put the character into a rect instead of the baseline corrdinate?


[Please *never* post BMP images to this list!  Use PNG or JPG
 instead.]

> 1. how to put the text in between my_rect.top and my_rect.bottom,
>    meanwhile all the letters are aligned?  I find that
>    face->glyph->bitmap_top is in baseline corrdinate.  If I use
>    my_rect.top + 24 - face->glyph->bitmap_top as the bitmap base y
>    point, all the letters are aligned, but the 'g' letter will out
>    of my_rect.

This problem...

> 2. what is the difference between the value of FT_Set_Pixel_Sizes
>    setted and face->glyph->bitmap.width and rows ?  I know
>    face->glyph->bitmap could no be larger than FT_Set_Pixel_Sizes
>    setted value; But is there any letter that face->glyph->bitmap
>    size is equal with the setted size?

and this one are related.  The very problem is that for outline fonts
the the value set with FT_Set_Pixel_Sizes is *not* related to the
actual dimensions of the rendered glyphs.  If you set this to 24px,
you really set the EM size to 24px, but there is no guarantee that
glyphs are really smaller than this value.

To render a text string into a given rectangle, you have to compute
the bounding boxes of all glyphs (e.g. using FT_Glyph_Get_CBox) to
check whether everything fits into the rectangle.  If necessary, you
have to select a different size to get improved results.

    Werner
Werner LEMBERG | 21 Mar 12:10 2012
Picon

[ft-devel] ttfautohint 0.7 has been released


ttfautohint 0.8 has been released.

The source tarball and statically linked Win32 binaries are available
from

    http://savannah.nongnu.org/download/freetype/

or

    http://sourceforge.net/projects/freetype/files/ttfautohint/0.8

Enjoy!

   Werner

PS: Downloads from savannah.nongnu.org will redirect to your nearest
    mirror site.  Files on mirrors may be subject to a replication
    delay of up to 24 hours.  In case of problems use
    http://download-mirror.savannah.gnu.org/releases/

----------------------------------------------------------------------

This project provides a library which takes a TrueType font as the input,
remove its bytecode instructions (if any), and return a new font where all
glyphs are bytecode hinted using the information given by FreeType's
autohinting module.  The idea is to provide the excellent quality of the
autohinter on platforms which don't use FreeType.

The library has a single API function, `TTF_autohint'; see
`lib/ttfautohint.h' for a detailed description.  Note that the library
itself won't get installed currently.

A command-line interface to the library is the `ttfautohint' program; after
compilation and installation, say

  ttfautohint --help

for usage information, or say

  man ttfautohint

to read its manual page.

A GUI to the library is `ttfautohintGUI'; it uses the Qt4 framework.  The
compilation of this application can be disabled with the `--without-qt'
configuration option.

----------------------------------------------------------------------

New in 0.8:

* Implement option `-x' to increase the x height of the font for small PPEM
  values by rounding up far more often then rounding down.

* Add option `-G N' to switch off hinting completely above value N.

* ttfautohint now appends version information and the used parameters to the
  `Version' field(s) in the `name' table.  This can be suppressed with
  option `-n'.

Gmane