Reputation: 5132
Here my code:
FontLoader { id: font_bold; source: "qrc:/font/MyFont Bold.ttf" }
FontLoader { id: font_medium; source: "qrc:/font/MyFont Medium.ttf" }
FontMetrics { id: fontMetrics }
function getBaseline(fontFamily, fontPixelSize)
{
fontMetrics.font.family = fontFamily;
fontMetrics.font.pixelSize = fontPixelSize;
return fontMetrics.ascent;
}
function getY(desiredY, fontFamily, fontPixelSize)
{
return desiredY - getBaseline(fontFamily, fontPixelSize);
}
Text {
x: 100
y: getY(100, font.family, font.pixelSize)
font.family: font_bold.name
font.pixelSize: 96
text: "foo"
}
Text {
x: 200
y: getY(150, font.family, font.pixelSize)
font.family: font_medium.name
font.pixelSize: 48
text: "foo"
}
The goal is to calculate the actual y
position from the desired one, removing the ascent offset of the current font, so the baseline will sit on the desired position.
I got this error for both lines y: getY(...)
:
QML QQuickText: Binding loop detected for property "y"
I don't see where is the binding loop. The y
property is calculated as desired position - font ascent. Both are not related to y
itself.
Upvotes: 0
Views: 210
Reputation: 4208
This is because you are using the FontMetrics
for both font's, changing the ascent continuously. Remember that you are binding a function to the y-position and thus every time the FontMetrics
is set to another font it will trigger an update on the y-position of both Text
's.
So, I propose to use two FontMetrics
, one for the bold and one for the medium.
FontMetrics { id: fontMetricsBold; font: font_bold }
FontMetrics { id: fontMetricsMedium; font: font_medium }
Text {
x: 100
y: 100 - fontMetricsBold.ascent
font.family: font_bold.name
font.pixelSize: 96
text: "foo"
}
Text {
x: 200
y: 150 - fontMetricsMedium.ascent
font.family: font_medium.name
font.pixelSize: 48
text: "bar"
}
Note, I don't have the fonts so I leave that as an exercise. Maybe configuring the FontMetrics
has to be done differently with respect to the FontLoader
.
Upvotes: 2