Discussion:
[Development] Why on Linux using FreeType engine fonts are rendered in 72 dpi instead of Screen DPI?
Tomasz Olszak
2018-10-17 15:59:26 UTC
Permalink
Hi,

I would like just to ensure that I missed something and there is
documentation somewhere informing that Linux fonts are rendered
*always* in 72 dpi
(https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Set_Char_Size)
(which makes font.pixelSize inaccurate for regular screens).

I found: https://bugreports.qt.io/browse/QTBUG-8890 which fixes the
problem but it was rejected. I see the default 72 dpi is also used in
current Qt 5.11 FT engine
(https://code.woboq.org/qt5/qtbase/src/platformsupport/fontdatabases/freetype/qfontengine_ft.cpp.html#297).

Does it surprise any of Qt developers or is it only me? Am I right
thinking that when we do following:
Rectangle {
height: 100
Text {font.pixelSize: 100}
}
then the relation between height of text and height of rectangle will
differ depending on screen dpi (don't have a way to test it
currently)?

Moreover I'm pretty sure that's the cause I very often use
Text.fontSizeMode: Text.Fit because setting pixelSize to height of
desired text size never worked correctly :)

tl;dr: Is it a bug?
Jason H
2018-10-17 16:21:24 UTC
Permalink
A pixel is a pixel, regardless of DPI.

However things matter when you use point sizes (which assumes 72 points per inch) there are also Screen.devicePixelRatio ( http://doc.qt.io/qt-5/qml-qtquick-window-screen.html#devicePixelRatio-attached-prop ) to contend with. I haven't had to worry about this for some time, so my intel may be out of date. It was a mess around 5.6...

I'm assuming the 72 has something to do with the point size, and being able to calculate scaling relative to 72.
Sent: Wednesday, October 17, 2018 at 11:59 AM
Subject: [Development] Why on Linux using FreeType engine fonts are rendered in 72 dpi instead of Screen DPI?
Hi,
I would like just to ensure that I missed something and there is
documentation somewhere informing that Linux fonts are rendered
*always* in 72 dpi
(https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Set_Char_Size)
(which makes font.pixelSize inaccurate for regular screens).
I found: https://bugreports.qt.io/browse/QTBUG-8890 which fixes the
problem but it was rejected. I see the default 72 dpi is also used in
current Qt 5.11 FT engine
(https://code.woboq.org/qt5/qtbase/src/platformsupport/fontdatabases/freetype/qfontengine_ft.cpp.html#297).
Does it surprise any of Qt developers or is it only me? Am I right
Rectangle {
height: 100
Text {font.pixelSize: 100}
}
then the relation between height of text and height of rectangle will
differ depending on screen dpi (don't have a way to test it
currently)?
Moreover I'm pretty sure that's the cause I very often use
Text.fontSizeMode: Text.Fit because setting pixelSize to height of
desired text size never worked correctly :)
tl;dr: Is it a bug?
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Tomasz Olszak
2018-10-17 16:48:17 UTC
Permalink
If I understand correctly font.pixelSize is actually not the same as
pixels user for with and height of items. So if you render rectangle
of height e.g 100 pixels on 96 dpi screen, and render Text with
font.pixelSize = 100 on 96 screen the iheight of text will be exactly
100 * 72/96 which is exactly 75% of rectangle's height. If you render
the same example on screen with 72 dpi they will be exactly the same,
on 192 dpi text will be ~37% of rectangle's height. It is just a
guess, all my 4 screens I have currently access to are 96 dpi. It is
just a guess and if Qt doesn't do something under the hood, FT engine
renders fonts in 72 dpi which means that actually on screen text with
a font with 100 pixels size is smaller than item with 100 pixel size .
That's why I'm asking here not on Interest.

Device pixel ratio can be used to calculate screen DPI in Qt Quick - I
use it now to correctly render fonts using pixelSize (fontPixelSize
factor == Screen.dpi / 72). Pixel size is given by ocring image in 300
dpi. But pixel is pixel and it should work. Text however always was a
bit smaller. When I calculated pointSize then the text was correct. I
dug more and ended up with conclusions as described above.
Post by Jason H
A pixel is a pixel, regardless of DPI.
However things matter when you use point sizes (which assumes 72 points per inch) there are also Screen.devicePixelRatio ( http://doc.qt.io/qt-5/qml-qtquick-window-screen.html#devicePixelRatio-attached-prop ) to contend with. I haven't had to worry about this for some time, so my intel may be out of date. It was a mess around 5.6...
I'm assuming the 72 has something to do with the point size, and being able to calculate scaling relative to 72.
Sent: Wednesday, October 17, 2018 at 11:59 AM
Subject: [Development] Why on Linux using FreeType engine fonts are rendered in 72 dpi instead of Screen DPI?
Hi,
I would like just to ensure that I missed something and there is
documentation somewhere informing that Linux fonts are rendered
*always* in 72 dpi
(https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Set_Char_Size)
(which makes font.pixelSize inaccurate for regular screens).
I found: https://bugreports.qt.io/browse/QTBUG-8890 which fixes the
problem but it was rejected. I see the default 72 dpi is also used in
current Qt 5.11 FT engine
(https://code.woboq.org/qt5/qtbase/src/platformsupport/fontdatabases/freetype/qfontengine_ft.cpp.html#297).
Does it surprise any of Qt developers or is it only me? Am I right
Rectangle {
height: 100
Text {font.pixelSize: 100}
}
then the relation between height of text and height of rectangle will
differ depending on screen dpi (don't have a way to test it
currently)?
Moreover I'm pretty sure that's the cause I very often use
Text.fontSizeMode: Text.Fit because setting pixelSize to height of
desired text size never worked correctly :)
tl;dr: Is it a bug?
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Tomasz Olszak
2018-10-17 17:04:50 UTC
Permalink
Post by Tomasz Olszak
If I understand correctly font.pixelSize is actually not the same as
pixels user for with and height of items. So if you render rectangle
of height e.g 100 pixels on 96 dpi screen, and render Text with
font.pixelSize = 100 on 96 screen the iheight of text will be exactly
100 * 72/96 which is exactly 75% of rectangle's height. If you render
the same example on screen with 72 dpi they will be exactly the same,
on 192 dpi text will be ~37% of rectangle's height. It is just a
guess, all my 4 screens I have currently access to are 96 dpi. It is
just a guess and if Qt doesn't do something under the hood, FT engine
renders fonts in 72 dpi which means that actually on screen text with
a font with 100 pixels size is smaller than item with 100 pixel size .
That's why I'm asking here not on Interest.
Device pixel ratio can be used to calculate screen DPI in Qt Quick - I
use it now to correctly render fonts using pixelSize (fontPixelSize
factor == Screen.dpi / 72). Pixel size is given by ocring image in 300
dpi. But pixel is pixel and it should work. Text however always was a
bit smaller. When I calculated pointSize then the text was correct. I
dug more and ended up with conclusions as described above.
Post by Jason H
A pixel is a pixel, regardless of DPI.
However things matter when you use point sizes (which assumes 72 points per inch) there are also Screen.devicePixelRatio ( http://doc.qt.io/qt-5/qml-qtquick-window-screen.html#devicePixelRatio-attached-prop ) to contend with. I haven't had to worry about this for some time, so my intel may be out of date. It was a mess around 5.6...
I'm assuming the 72 has something to do with the point size, and being able to calculate scaling relative to 72.
Sent: Wednesday, October 17, 2018 at 11:59 AM
Subject: [Development] Why on Linux using FreeType engine fonts are rendered in 72 dpi instead of Screen DPI?
Hi,
I would like just to ensure that I missed something and there is
documentation somewhere informing that Linux fonts are rendered
*always* in 72 dpi
(https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Set_Char_Size)
(which makes font.pixelSize inaccurate for regular screens).
I found: https://bugreports.qt.io/browse/QTBUG-8890 which fixes the
problem but it was rejected. I see the default 72 dpi is also used in
current Qt 5.11 FT engine
(https://code.woboq.org/qt5/qtbase/src/platformsupport/fontdatabases/freetype/qfontengine_ft.cpp.html#297).
Does it surprise any of Qt developers or is it only me? Am I right
Rectangle {
height: 100
Text {font.pixelSize: 100}
}
then the relation between height of text and height of rectangle will
differ depending on screen dpi (don't have a way to test it
currently)?
Moreover I'm pretty sure that's the cause I very often use
Text.fontSizeMode: Text.Fit because setting pixelSize to height of
desired text size never worked correctly :)
tl;dr: Is it a bug?
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Tomasz Olszak
2018-10-17 17:06:09 UTC
Permalink
Sorry accidental Shift+Enter

https://pastebin.com/m2tAgT3f

For my screens QScreen::physicalDotsPerInch() return ~92dpi instead of
96 but it is probably due to some platform edid implementation or
something like that.
Post by Tomasz Olszak
If I understand correctly font.pixelSize is actually not the same as
pixels user for with and height of items. So if you render rectangle
of height e.g 100 pixels on 96 dpi screen, and render Text with
font.pixelSize = 100 on 96 screen the iheight of text will be exactly
100 * 72/96 which is exactly 75% of rectangle's height. If you render
the same example on screen with 72 dpi they will be exactly the same,
on 192 dpi text will be ~37% of rectangle's height. It is just a
guess, all my 4 screens I have currently access to are 96 dpi. It is
just a guess and if Qt doesn't do something under the hood, FT engine
renders fonts in 72 dpi which means that actually on screen text with
a font with 100 pixels size is smaller than item with 100 pixel size .
That's why I'm asking here not on Interest.
Device pixel ratio can be used to calculate screen DPI in Qt Quick - I
use it now to correctly render fonts using pixelSize (fontPixelSize
factor == Screen.dpi / 72). Pixel size is given by ocring image in 300
dpi. But pixel is pixel and it should work. Text however always was a
bit smaller. When I calculated pointSize then the text was correct. I
dug more and ended up with conclusions as described above.
Post by Jason H
A pixel is a pixel, regardless of DPI.
However things matter when you use point sizes (which assumes 72 points per inch) there are also Screen.devicePixelRatio ( http://doc.qt.io/qt-5/qml-qtquick-window-screen.html#devicePixelRatio-attached-prop ) to contend with. I haven't had to worry about this for some time, so my intel may be out of date. It was a mess around 5.6...
I'm assuming the 72 has something to do with the point size, and being able to calculate scaling relative to 72.
Sent: Wednesday, October 17, 2018 at 11:59 AM
Subject: [Development] Why on Linux using FreeType engine fonts are rendered in 72 dpi instead of Screen DPI?
Hi,
I would like just to ensure that I missed something and there is
documentation somewhere informing that Linux fonts are rendered
*always* in 72 dpi
(https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_Set_Char_Size)
(which makes font.pixelSize inaccurate for regular screens).
I found: https://bugreports.qt.io/browse/QTBUG-8890 which fixes the
problem but it was rejected. I see the default 72 dpi is also used in
current Qt 5.11 FT engine
(https://code.woboq.org/qt5/qtbase/src/platformsupport/fontdatabases/freetype/qfontengine_ft.cpp.html#297).
Does it surprise any of Qt developers or is it only me? Am I right
Rectangle {
height: 100
Text {font.pixelSize: 100}
}
then the relation between height of text and height of rectangle will
differ depending on screen dpi (don't have a way to test it
currently)?
Moreover I'm pretty sure that's the cause I very often use
Text.fontSizeMode: Text.Fit because setting pixelSize to height of
desired text size never worked correctly :)
tl;dr: Is it a bug?
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Allan Sandfeld Jensen
2018-10-17 17:07:36 UTC
Permalink
Hi

That is a incorrect assesment. Fonts are rendered in pixel sizes. When you
request a QFont at a certain pixel size you get the font in that size. It
should be noted that those sizes ONLY matter for things like pixel hinting.
The font will typically be rendered in the same size it was requested, but in
some cases a specific font-size is used at a different resolution (for
instance with hidpi modes), in that case pixel-hinting is disable, and we do
vector based rendering of the glyphs.

In any case the DPI is irrelevant and only matters if you select fonts by
point instead of pixels, and then the translation to pixels is done before we
get anywhere near the font engine.

'Allan
Post by Tomasz Olszak
Hi,
I would like just to ensure that I missed something and there is
documentation somewhere informing that Linux fonts are rendered
*always* in 72 dpi
(https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#F
T_Set_Char_Size) (which makes font.pixelSize inaccurate for regular
screens).
I found: https://bugreports.qt.io/browse/QTBUG-8890 which fixes the
problem but it was rejected. I see the default 72 dpi is also used in
current Qt 5.11 FT engine
(https://code.woboq.org/qt5/qtbase/src/platformsupport/fontdatabases/freetyp
e/qfontengine_ft.cpp.html#297).
Does it surprise any of Qt developers or is it only me? Am I right
Rectangle {
height: 100
Text {font.pixelSize: 100}
}
then the relation between height of text and height of rectangle will
differ depending on screen dpi (don't have a way to test it
currently)?
Moreover I'm pretty sure that's the cause I very often use
Text.fontSizeMode: Text.Fit because setting pixelSize to height of
desired text size never worked correctly :)
tl;dr: Is it a bug?
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Tomasz Olszak
2018-10-17 17:10:45 UTC
Permalink
Could you please try example I sent on Linux and explain why font size
doesn't match rectangle size? Maybe the ft font engine has bug?
Post by Allan Sandfeld Jensen
Hi
That is a incorrect assesment. Fonts are rendered in pixel sizes. When you
request a QFont at a certain pixel size you get the font in that size. It
should be noted that those sizes ONLY matter for things like pixel hinting.
The font will typically be rendered in the same size it was requested, but in
some cases a specific font-size is used at a different resolution (for
instance with hidpi modes), in that case pixel-hinting is disable, and we do
vector based rendering of the glyphs.
In any case the DPI is irrelevant and only matters if you select fonts by
point instead of pixels, and then the translation to pixels is done before we
get anywhere near the font engine.
'Allan
Post by Tomasz Olszak
Hi,
I would like just to ensure that I missed something and there is
documentation somewhere informing that Linux fonts are rendered
*always* in 72 dpi
(
https://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#F
Post by Tomasz Olszak
T_Set_Char_Size) (which makes font.pixelSize inaccurate for regular
screens).
I found: https://bugreports.qt.io/browse/QTBUG-8890 which fixes the
problem but it was rejected. I see the default 72 dpi is also used in
current Qt 5.11 FT engine
(
https://code.woboq.org/qt5/qtbase/src/platformsupport/fontdatabases/freetyp
Post by Tomasz Olszak
e/qfontengine_ft.cpp.html#297).
Does it surprise any of Qt developers or is it only me? Am I right
Rectangle {
height: 100
Text {font.pixelSize: 100}
}
then the relation between height of text and height of rectangle will
differ depending on screen dpi (don't have a way to test it
currently)?
Moreover I'm pretty sure that's the cause I very often use
Text.fontSizeMode: Text.Fit because setting pixelSize to height of
desired text size never worked correctly :)
tl;dr: Is it a bug?
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Allan Sandfeld Jensen
2018-10-17 17:23:23 UTC
Permalink
Post by Tomasz Olszak
Could you please try example I sent on Linux and explain why font size
doesn't match rectangle size? Maybe the ft font engine has bug?
Try putting a capital Å or any other capital with accents in your example, and
you will see that it fits snugly.

Though it should be noted fonts are a bit inconsistent in what they consider
their heights, some use the capital-height as their height, some use the
maximum including accents. I believe we normally always render text with the
leading on top, so there is room for all the Latin-1 characters and the
results are always the same.

'Allan
Jason H
2018-10-17 19:17:27 UTC
Permalink
+1. Make sure that you are accounting for the full font height, see QFontMetrics::ascent() and descent()
Sent: Wednesday, October 17, 2018 at 1:23 PM
Subject: Re: [Development] Why on Linux using FreeType engine fonts are rendered in 72 dpi instead of Screen DPI?
Post by Tomasz Olszak
Could you please try example I sent on Linux and explain why font size
doesn't match rectangle size? Maybe the ft font engine has bug?
Try putting a capital Å or any other capital with accents in your example, and
you will see that it fits snugly.
Though it should be noted fonts are a bit inconsistent in what they consider
their heights, some use the capital-height as their height, some use the
maximum including accents. I believe we normally always render text with the
leading on top, so there is room for all the Latin-1 characters and the
results are always the same.
'Allan
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Tomasz Olszak
2018-10-17 20:11:38 UTC
Permalink
Ok,
It's a lot more clear. Thank you for explanation!

I used default font with pixel size 100 and according to FontMetrics:
1. family: "Sans Serif"
2. pointSize: 75
3. pixelSize: 100
4. ascent 93
5. descent: 24
6. height: 117
7. leading -1

So how ascent, descent, leading is related to 100. How can I calculate
font.pixelSiize knowing that capital letter height is 100. By trial
and errors the font with pixel size = 137 and ascent = 129 have
capital letter(without accent) of 100 pixel height.
Is it a way to determin font.pixelSize if we want the non accent
capilat letter to be e.g 100px? Or should I just iterate over pixel
sizes and calculate using fontMetrics and tightBoundingRect for
capitalized letter without accent?
Post by Jason H
+1. Make sure that you are accounting for the full font height, see QFontMetrics::ascent() and descent()
Sent: Wednesday, October 17, 2018 at 1:23 PM
Subject: Re: [Development] Why on Linux using FreeType engine fonts are rendered in 72 dpi instead of Screen DPI?
Post by Tomasz Olszak
Could you please try example I sent on Linux and explain why font size
doesn't match rectangle size? Maybe the ft font engine has bug?
Try putting a capital Å or any other capital with accents in your example, and
you will see that it fits snugly.
Though it should be noted fonts are a bit inconsistent in what they consider
their heights, some use the capital-height as their height, some use the
maximum including accents. I believe we normally always render text with the
leading on top, so there is room for all the Latin-1 characters and the
results are always the same.
'Allan
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Allan Sandfeld Jensen
2018-10-17 22:58:14 UTC
Permalink
Post by Tomasz Olszak
Ok,
It's a lot more clear. Thank you for explanation!
1. family: "Sans Serif"
2. pointSize: 75
3. pixelSize: 100
4. ascent 93
5. descent: 24
6. height: 117
7. leading -1
So how ascent, descent, leading is related to 100. How can I calculate
font.pixelSiize knowing that capital letter height is 100. By trial
and errors the font with pixel size = 137 and ascent = 129 have
capital letter(without accent) of 100 pixel height.
Is it a way to determin font.pixelSize if we want the non accent
capilat letter to be e.g 100px? Or should I just iterate over pixel
sizes and calculate using fontMetrics and tightBoundingRect for
capitalized letter without accent?
You should read the capHeight of the font. I think QFontMetrics can give you
that (https://doc.qt.io/qt-5/qfontmetrics.html#capHeight), though I am not
sure if there are QML bindings for that. In any case capital latin letters are
generally all the same height, so you could probably just measure the height
of H or M.
Allan Sandfeld Jensen
2018-10-17 23:04:11 UTC
Permalink
Post by Allan Sandfeld Jensen
Post by Tomasz Olszak
Ok,
It's a lot more clear. Thank you for explanation!
1. family: "Sans Serif"
2. pointSize: 75
3. pixelSize: 100
4. ascent 93
5. descent: 24
6. height: 117
7. leading -1
So how ascent, descent, leading is related to 100. How can I calculate
font.pixelSiize knowing that capital letter height is 100. By trial
and errors the font with pixel size = 137 and ascent = 129 have
capital letter(without accent) of 100 pixel height.
Is it a way to determin font.pixelSize if we want the non accent
capilat letter to be e.g 100px? Or should I just iterate over pixel
sizes and calculate using fontMetrics and tightBoundingRect for
capitalized letter without accent?
You should read the capHeight of the font. I think QFontMetrics can give you
that (https://doc.qt.io/qt-5/qfontmetrics.html#capHeight), though I am not
sure if there are QML bindings for that. In any case capital latin letters
are generally all the same height, so you could probably just measure the
height of H or M.
See also https://en.wikipedia.org/wiki/Typeface#Font_metrics
Tomasz Olszak
2018-10-18 05:14:13 UTC
Permalink
Ok,
I just thought that knowing capital letter height I can calculate correct
font pixel size but seems using font metrics and iteration over pixel sizes
is the only option. Thank you all for help!
Post by Allan Sandfeld Jensen
Post by Allan Sandfeld Jensen
Post by Tomasz Olszak
Ok,
It's a lot more clear. Thank you for explanation!
1. family: "Sans Serif"
2. pointSize: 75
3. pixelSize: 100
4. ascent 93
5. descent: 24
6. height: 117
7. leading -1
So how ascent, descent, leading is related to 100. How can I calculate
font.pixelSiize knowing that capital letter height is 100. By trial
and errors the font with pixel size = 137 and ascent = 129 have
capital letter(without accent) of 100 pixel height.
Is it a way to determin font.pixelSize if we want the non accent
capilat letter to be e.g 100px? Or should I just iterate over pixel
sizes and calculate using fontMetrics and tightBoundingRect for
capitalized letter without accent?
You should read the capHeight of the font. I think QFontMetrics can give
you
Post by Allan Sandfeld Jensen
that (https://doc.qt.io/qt-5/qfontmetrics.html#capHeight), though I am
not
Post by Allan Sandfeld Jensen
sure if there are QML bindings for that. In any case capital latin
letters
Post by Allan Sandfeld Jensen
are generally all the same height, so you could probably just measure the
height of H or M.
See also https://en.wikipedia.org/wiki/Typeface#Font_metrics
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Loading...