Skip to content

Commit bc4c320

Browse files
committed
src/qml/controls/subtitletext: reduce artifacting and shifting
There was problems with artifacting of text in subtitles. This still ocurred when using a TextEdit, which meant there was something else at play. It turns out using 8 samples was the cause of the artifacting, which the TextEdit inherited since it was a child of the Shape which used multiple samples. This fixes the issue by antialiasing the shape and using 4 samples instead of 8. This has harder edges, but it has less obvious artifacting. This also fixes text shifting by clearing out the selection before setting the new selection.
1 parent ba2e887 commit bc4c320

2 files changed

Lines changed: 45 additions & 29 deletions

File tree

src/qml/controls/StrokeLabel.qml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ Item {
9292
delegate: Shape {
9393
id: shape
9494
anchors.horizontalCenter: parent.horizontalCenter
95+
antialiasing: true
9596
layer.enabled: true
96-
layer.samples: 8
97+
layer.samples: 4
98+
layer.smooth: true
9799

98100
// This draws the background
99101
ShapePath {

src/qml/controls/SubtitleText.qml

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Item {
4949
return;
5050
}
5151

52+
root.clearSelection();
5253
root.selectionStart = start;
5354
root.selectionEnd = end;
5455
}
@@ -144,8 +145,10 @@ Item {
144145
delegate: Shape {
145146
id: shape
146147
anchors.horizontalCenter: parent.horizontalCenter
148+
antialiasing: true
147149
layer.enabled: true
148-
layer.samples: 8
150+
layer.samples: 4
151+
layer.smooth: true
149152

150153
// This draws the background
151154
ShapePath {
@@ -212,8 +215,8 @@ Item {
212215
return Qt.rect(0, 0, 0, 0);
213216
}
214217

215-
const startRect = shadowText.positionToRectangle(start);
216-
const endRect = shadowText.positionToRectangle(end);
218+
const startRect = textEdit.positionToRectangle(start);
219+
const endRect = textEdit.positionToRectangle(end);
217220

218221
const startX = root.margin + startRect.x;
219222
const endX = root.margin + endRect.x + endRect.width;
@@ -247,10 +250,10 @@ Item {
247250

248251
// This text edit is only used for hit testing, it is not shown
249252
TextEdit {
250-
id: shadowText
253+
id: textEdit
251254

252-
x: root.margin // This centers the text horizontally perfectly
253-
anchors.verticalCenter: shape.verticalCenter
255+
x: root.margin
256+
y: root.margin - (textMetrics.tightBoundingRect.y - textMetrics.boundingRect.y)
254257
font: root.font
255258
color: "transparent"
256259
readOnly: true
@@ -259,42 +262,53 @@ Item {
259262
wrapMode: TextEdit.NoWrap
260263
text: modelData.text
261264

265+
TextMetrics {
266+
id: textMetrics
267+
font: textEdit.font
268+
text: textEdit.text
269+
renderType: textEdit.renderType
270+
}
271+
262272
/**
263273
* Returns the index into the root.text property for the
264274
* given x-coordinate.
265-
* @param x The x-coordinate in shadowText space.
275+
* @param x The x-coordinate in textEdit space.
266276
* @return The index of the given coordinates in root.text.
267277
*/
268278
function getCharacterIndexAt(x) {
269-
const pos = shadowText.positionAt(x, 0);
270-
const rect = shadowText.positionToRectangle(pos);
279+
const pos = textEdit.positionAt(x, 0);
280+
const rect = textEdit.positionToRectangle(pos);
271281
const actualIndex = (x < rect.x) ? pos - 1 : pos;
272-
const lineIndex = Math.max(0, Math.min(shadowText.text.length - 1, actualIndex));
282+
const lineIndex = Math.max(0, Math.min(textEdit.text.length - 1, actualIndex));
273283
return lineIndex + modelData.offset;
274284
}
285+
}
275286

276-
MouseArea {
277-
anchors.fill: parent
278-
hoverEnabled: true
279-
acceptedButtons: Qt.LeftButton | Qt.MiddleButton
280-
cursorShape: root.cursorShape
281-
onPositionChanged: function(mouse) {
282-
let index = shadowText.getCharacterIndexAt(mouse.x);
287+
MouseArea {
288+
anchors.fill: parent
289+
hoverEnabled: true
290+
acceptedButtons: Qt.LeftButton | Qt.MiddleButton
291+
cursorShape: root.cursorShape
292+
onPositionChanged: function(mouse) {
293+
let x = mouse.x - root.margin;
294+
if (x >= 0)
295+
{
296+
let index = textEdit.getCharacterIndexAt(x);
283297
root.hoverIndex = index;
284298
}
285-
onClicked: function(event) {
286-
if (event.button === Qt.LeftButton)
287-
{
288-
root.clicked();
289-
}
290-
else if (event.button === Qt.MiddleButton)
291-
{
292-
root.middleClicked();
293-
}
299+
}
300+
onClicked: function(event) {
301+
if (event.button === Qt.LeftButton)
302+
{
303+
root.clicked();
304+
}
305+
else if (event.button === Qt.MiddleButton)
306+
{
307+
root.middleClicked();
294308
}
295-
onDoubleClicked: root.doubleClicked()
296-
onExited: root.hoverIndex = -1
297309
}
310+
onDoubleClicked: root.doubleClicked()
311+
onExited: root.hoverIndex = -1
298312
}
299313
}
300314
}

0 commit comments

Comments
 (0)