Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Returned facets indexes are not always aligned with text #52

Closed
valvoline opened this issue Nov 21, 2024 · 3 comments
Closed

[Bug]: Returned facets indexes are not always aligned with text #52

valvoline opened this issue Nov 21, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@valvoline
Copy link

Summary

Returned facets indexes are not always aligned with text

Reproduction Steps

Probably due to the emoji handling in terms of bytes, the returned start and end indexes are not always usable.

For example, take into account the following record.text:

Kind of makes you wonder why the same thing isn’t happening in the EU’s legal universe 👀\n\nwww.macrumors.com/2024/11/18/u...

The returned indexes are as follow:

(lldb) po facet
▿ Facet
  ▿ index : ByteSlice
    - byteStart : 97
    - byteEnd : 130
  ▿ features : 1 element
    ▿ 0 : FacetFeatureUnion
      ▿ link : Link
        - uri : "https://www.macrumors.com/2024/11/18/us-doj-wants-google-to-sell-chrome/"

But they're not working nor aligned with the text, producing a glitched output as well.

Expected Results

(lldb) po facet
▿ Facet
  ▿ index : ByteSlice
    **- byteStart : 93
    - byteEnd : 127**
  ▿ features : 1 element
    ▿ 0 : FacetFeatureUnion
      ▿ link : Link
        - uri : "https://www.macrumors.com/2024/11/18/us-doj-wants-google-to-sell-chrome/"

Actual Results

(lldb) po facet
▿ Facet
  ▿ index : ByteSlice
    **- byteStart : 97
    - byteEnd : 130**
  ▿ features : 1 element
    ▿ 0 : FacetFeatureUnion
      ▿ link : Link
        - uri : "https://www.macrumors.com/2024/11/18/us-doj-wants-google-to-sell-chrome/"

What operating systems did you experience this bug? (We'll count Docker as an operating system.)

macOS

Operating System Version

14.6.1 (23G93)

ATProtoKit Version

0.19.2

Additional Context

No response

@valvoline valvoline added the bug Something isn't working label Nov 21, 2024
@valvoline
Copy link
Author

Update: Seems that the problem is not due to emoji but somehow related to something else. It also occur with simple texts without emoji or special characters at all.

@valvoline
Copy link
Author

Update2: For the ones that are facing the same problem, the issue is not due to the ATProtoKit itself, that indeed behaves correctly. The problem is due to the fact that UTF8, UTF16 and RAW bytes conversion is needed in order to work with AttributedString (in my case I'm using SwiftUI).

Below is a simple helper method to deal with that:

    private func colorSubstring(in text: String,
                                attributedText: AttributedString,
                                start: Int,
                                end: Int,
                                color: Color = .accentColor,
                                font: Font = .system(size: 15.0, weight: .regular, design: .rounded)
    ) -> AttributedString {
        var retString = attributedText
        let utf8View = text.utf8

        let highlightedRange: Range<AttributedString.Index>? = {
            guard let utf8Start = utf8View.index(utf8View.startIndex, offsetBy: start, limitedBy: utf8View.endIndex),
                  let utf8End = utf8View.index(utf8Start, offsetBy: end - start, limitedBy: utf8View.endIndex),
                  let stringStart = String.Index(utf8Start, within: text),
                  let stringEnd = String.Index(utf8End, within: text),
                  let attrStart = AttributedString.Index(stringStart, within: attributedText),
                  let attrEnd = AttributedString.Index(stringEnd, within: attributedText) else { return nil }
            return attrStart..<attrEnd
        }()

        if let highlightedRange {
            retString[highlightedRange].foregroundColor = color
            retString[highlightedRange].font = font
        }
        return retString
    }

@MasterJ93
Copy link
Owner

Thanks for posting up a solution. I'll take this into account in a future update so you don't have to make a helper method yourself. ATProtoKit should be responsible with handling this, ultimately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants