Enter-tainer / typst-preview

[DEPRECATED] Use tinymist instead
https://Enter-tainer.github.io/typst-preview/
MIT License
450 stars 21 forks source link

feat: resolve spans in granularity of char #225

Closed Myriad-Dreamin closed 8 months ago

Myriad-Dreamin commented 8 months ago

Corresponds to https://github.com/Myriad-Dreamin/typst.ts/pull/468

The bounding boxes of chars are calculated based on <use> elements and enlarged according to their bbox and flow direction automatically. The selection fallback is calculated by:

for (const use of elem.children) {
    const useRect = use.getBoundingClientRect();
    const selRect = isHorizontalFlow() ? {
      left: useRect.left,
      right: useRect.right,
      top: textRect.top,
      bottom: textRect.bottom,
    } : {
      left: textRect.left,
      right: textRect.right,
      top: useRect.top,
      bottom: useRect.bottom,
    };
    previousSelRect = unionRect(selRect, previousSelRect);

    // set index to end range of this char
    useIndex++;
    if (inRect(selRect, mouseX, mouseY)) {
      foundIndex = useIndex;
    } else if (previousSelRect) { // may fallback to space in between chars
      if (inRect(previousSelRect, mouseX, mouseY)) {
        foundIndex = useIndex - 1;
        previousSelRect = selRect;
      }
    }
  }

== Demo

#let text-to-jump = [Text to jump]

#scale(x: 150%, {
  h(5em)
  text-to-jump
}) 

#v(5em)

#rotate(90deg, text-to-jump)

#v(5em)

#rotate(180deg, text-to-jump)

#v(5em)

#rotate(270deg, text-to-jump)

#v(5em)

U1%1 BD~5R4 %(ND8_ H6UM

Enter-tainer commented 8 months ago

Thank you!