Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle multiple fragments in the getCharNumberAtPosition SVGTextQuery
https://bugs.webkit.org/show_bug.cgi?id=257429

Reviewed by Simon Fraser.

This patch aligns WebKit with Blink / Chromium and Gecko / Firefox.

Merge: https://src.chromium.org/viewvc/blink?view=revision&revision=176936

The start position (and thus indirectly end position) used to compute the
extents of a glyph to check against the point is computed based on the
'processedCharacter' query data state. Following state is only updated after
each text box has been processed, meaning that for a text box with
multiple fragments, the offset of the fragment within the box needs to be
included to get the correct start/end position.

* Source/WebCore/rendering/svg/SVGTextQuery.cpp:
(SVGTextQuery::characterNumberAtPositionCallback): As above
* LayoutTests/svg/text/getcharnumatposition-multiple-fragments.html: Add Test Case
* LayoutTests/svg/text/getcharnumatposition-multiple-fragments-expected.txt: Add Test Case Expectation

Canonical link: https://commits.webkit.org/264796@main
  • Loading branch information
Ahmad-S792 authored and Ahmad Saleem committed Jun 1, 2023
1 parent ab72213 commit c01e3ba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
@@ -0,0 +1,4 @@
AAAA

PASS SVGTextContentElement.getCharNumAtPosition w/ multiple fragments per text box.

21 changes: 21 additions & 0 deletions LayoutTests/svg/text/getcharnumatposition-multiple-fragments.html
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<svg height="0">
<defs><path id="p" d="M0,20h100"/></defs>
<text y="20" font-size="20" font-family="Ahem"><textPath xlink:href="#p">AAAA</textPath></text>
</svg>
<script>
test(function() {
var text = document.querySelector('text');
var extents = text.getExtentOfChar(0);
var point = document.querySelector('svg').createSVGPoint();
point.x = extents.width / 2;
point.y = 10;
for (var i = 0; i < 4; ++i) {
assert_equals(text.getCharNumAtPosition(point), i);

point.x += extents.width;
}
}, 'SVGTextContentElement.getCharNumAtPosition w/ multiple fragments per text box.');
</script>
8 changes: 6 additions & 2 deletions Source/WebCore/rendering/svg/SVGTextQuery.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (C) Research In Motion Limited 2010-2012. All rights reserved.
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
Expand Down Expand Up @@ -521,16 +522,19 @@ bool SVGTextQuery::characterNumberAtPositionCallback(Data* queryData, const SVGT
{
CharacterNumberAtPositionData* data = static_cast<CharacterNumberAtPositionData*>(queryData);

// Offset of the fragment within the text box.
unsigned boxOffset = fragment.characterOffset - queryData->textBox->start();

FloatRect extent;
for (unsigned i = 0; i < fragment.length; ++i) {
unsigned startPosition = data->processedCharacters + i;
unsigned startPosition = data->processedCharacters + boxOffset + i;
unsigned endPosition = startPosition + 1;
if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startPosition, endPosition))
continue;

calculateGlyphBoundaries(queryData, fragment, startPosition, extent);
if (extent.contains(data->position)) {
data->processedCharacters += i;
data->processedCharacters += i + boxOffset;
return true;
}
}
Expand Down

0 comments on commit c01e3ba

Please sign in to comment.