Skip to content

Fix text selection rectangles on BiDi/RTL text#649

Merged
poire-z merged 3 commits into
koreader:masterfrom
poire-z:bidi_rects
Feb 2, 2026
Merged

Fix text selection rectangles on BiDi/RTL text#649
poire-z merged 3 commits into
koreader:masterfrom
poire-z:bidi_rects

Conversation

@poire-z

@poire-z poire-z commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

::first-letter: fix when single letter text node

On single letter text nodes handled by ::first-letter, getRect() would not find the text node, resulting in that letter being absent from highlights.
ie. with:

<style>p::first-letter {color: blue}</style>
<p>A<span> single letter before a span</span></p>
<p><span>A</span> single letter in a span</p>

ldomXPointer::getRectEx(): return optional context

Add optional *ctxFlags parameter, allowing getting more info about the context of the char we return the Rect for: BiDi and paragraph and char RTL status.

ldomXRange::getSegmentRects(): fix when BiDi/RTL

Previous implementations had shortcuts that were valid only on plain LTR or RTL text.
With BiDi text, it gets a lot more complex: we can't take the shortcuts and need to iterate char by change, handling direction changes, pending or adding or merging rects.

I asked about some situations with holes at BiDi boundaries it they should be avoided at koreader/koreader#5359 (comment) - and ended up adding stuff to avoid them.

Started initially with the help of Github copilot in #645, who nearly gave up at #645 (comment).
Then, man beat the machine at #645 (comment), and I continued it manually.

Closes #644 (where I express my initial amazement and followup desperation about working with Github copilot).


This change is Reviewable

@poire-z poire-z force-pushed the bidi_rects branch 3 times, most recently from 9db8ee9 to b644992 Compare February 2, 2026 00:13
On single letter text nodes handled by ::first-letter,
getRect() would not find the text node, resulting
in that letter being absent from highlights.
ie. with:
<style>p::first-letter {color: blue}</style>
<p>A<span> single letter before a span</span></p>
<p><span>A</span> single letter in a span</p>
Add optional *ctxFlags parameter, allowing getting
more info about the context of the char we return
the Rect for: BiDi and paragraph and char RTL status.
Previous implementations had shortcuts that were
valid only on plain LTR or RTL text.
With BiDi text, it gets a lot more complex: we
can't take the shortcuts and need to iterate char
by char, handling direction changes, pending or
adding or merging rects.

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool :-)

@poire-z poire-z merged commit eb1b3c1 into koreader:master Feb 2, 2026
1 check passed
@poire-z poire-z deleted the bidi_rects branch February 2, 2026 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text selection does not handle very well lines with BiDi text

2 participants