Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Char measurement - #6083

Closed
as-cii wants to merge 67 commits into
masterfrom
as-char-measurement
Closed

as-cii wants to merge 67 commits into
masterfrom
as-char-measurement

Conversation

@as-cii

@as-cii as-cii commented Mar 24, 2015

Copy link
Copy Markdown
Contributor

Fixes #6055

This is still a work in progress: code is messy and specs are not passing yet. With this PR we basically wrap every character with a span so that we're able to measure it. This helps to calculate the real character's width on screen: whenever this is not possible, however, the current solution falls back to @defaultCharWidth.

We have split the TextEditorPresenter and the TextEditorComponent updates in two phases: pre-measure and post-measure. The former should calculate anything related to lines, whereas the latter performs updates which need to be based on lines measurement.

  • Write new specs to consolidate this behavior
  • Make existing specs pass
  • Remove references to scoped char width
  • Scrolling into an unmeasured line with decorations (or cursors) has issues (since we don't measure such lines on scroll anymore and updating them doesn't require any user action)

Comment thread src/display-buffer.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I very much like this approach of just keeping a parallel array.

@nathansobo

Copy link
Copy Markdown
Contributor

I'm wondering if rather than storing the widths of each character at each position if it wouldn't be easier to just store a running total. So if every character were 10px wide on a line with 4 characters, it would be [10, 20, 30, 40]. Basically, rather than an array of widths it is now an array of the horizontal position of the right side of that character in absolute terms.

@nathansobo

Copy link
Copy Markdown
Contributor

🚀 Looking forward to collaborating on this problem with you.

@as-cii
as-cii force-pushed the as-char-measurement branch 2 times, most recently from 8c8d691 to a9f3d88 Compare March 25, 2015 09:09
@as-cii
as-cii force-pushed the as-char-measurement branch from a9f3d88 to b0ec19f Compare March 25, 2015 09:11
@as-cii
as-cii force-pushed the as-char-measurement branch from 55d5281 to f87ad85 Compare March 25, 2015 09:53
@as-cii

as-cii commented Mar 25, 2015

Copy link
Copy Markdown
Contributor Author

I'm wondering if rather than storing the widths of each character at each position if it wouldn't be easier to just store a running total. So if every character were 10px wide on a line with 4 characters, it would be [10, 20, 30, 40]. Basically, rather than an array of widths it is now an array of the horizontal position of the right side of that character in absolute terms.

I was just thinking about this and it's definitely something we could do. I wonder, however, if we could make it even faster 😮 What if, while measuring lines, we stored:

  • A matrix of rows and columns, with the absolute left position of each column
  • An interval skip list (or a similar data structure) of absolute left positions (this is needed for clipping when in the middle)

If we manage to do this, then screenPositionForPixelPosition and pixelPositionForScreenPosition would basically avoid tokens iteration altogether, making finally use of the existing computation performed by measureCharactersInVisibleLines. What do you think @nathansobo? This would be huge in terms of speed! 🐎

By the way, specs are 💚 now (exception made for those referring to scoped char widths) and it would be great if you could have a second look at this. Thank you 🙇

In the meantime I'll try to put a bit of thought into a reflow-less solution, even though I feel like this PR performs quite well.

@as-cii

as-cii commented Apr 24, 2015

Copy link
Copy Markdown
Contributor Author

@nathansobo: as discussed yesterday, I am still working to make this mergeable and this will hopefully happen by the end of this week 🔥

Right now there are still a few things I need to address. Specifically:

  1. 💚 Look into two tests that are currently failing for no apparent good reason (it should be quite straightforward, though);
  2. 🐎 Avoid computing scroll width in DisplayBuffer every single time setCharLeftForPoint is called;
  3. 🐛 Scrolling from model (i.e. editor.setScrollTop) should trigger a character measurement on newly built lines;
  4. Resolve conflicts with master

In the meantime, however, it would be great if you could start having a look at this and leave some comments wherever you feel we should change something. Code-wise this is not perfect yet and, since it has been up for a while and I experimented a lot, there may be a bunch of things we want to refine and do better.

Thanks! 🙇

Comment thread src/display-buffer.coffee

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not super happy with the name of this method. I couldn't come up with something clever and everything always felt like a mouthful. Any ideas how could we improve it? 💭

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only a slight improvement: setLeftPixelPositionForPoint. I think emphasizing pixels is a good idea.

# Conflicts:
#	src/lines-component.coffee
#	src/text-editor-presenter.coffee
@asamiam

asamiam commented Jun 3, 2015

Copy link
Copy Markdown

Will this address #3821? I switched from Sublime originally b/c ligatures worked in Atom... they broke a few versions back and it would be great to have it for fonts like Hasklig and Fire Code.

@nathansobo

Copy link
Copy Markdown
Contributor

Yeah, it should fix ligatures. We need to try something more efficient than the current state of this PR because it has a bit too much overhead.

@nathansobo

Copy link
Copy Markdown
Contributor

Wanted to provide a status update on this because implementing correct character measurement is becoming a bottleneck for other efforts, such as CJK-aware softwrap. @as-cii feel free to chime in and fill in any details I may be missing.

@as-cii and I had a screen sharing session about this PR a few weeks back, and concluded that the measurement overheads for the current state of this PR are probably too high. Our overall goal is to be able to compute the horizontal pixel position of a column at any time without reading from the document (which causes reflows).

  • The existing solution on master caches the width of each character in each scope whenever the DOM is updated, allowing us to compute the horizontal location of a column by summing up characters and reading out of this cache. Unfortunately, this approach is broken with ligatures and doesn't work when subpixel font scaling is enabled.
  • The solution on this PR is to use the canvas API to measure text. The advantage to measuring via canvas is that we don't interact with the document at all, but unfortunately, the canvas text measurement API seems to be too slow for our purposes, requiring every prefix of the line to be measured in a separate call.
  • The idea @as-cii and I explored on our call is to instead perform measurement in a separate document, via an iframe. We would need to sample styling information for every unique scope in the document via dummy DOM nodes, similar to @abe33's approach for the minimap. Of course all we're interested in is font styling, since that's the only thing that affects measurement. Then, to measure the position of all columns on a line, we would construct a simpler version of that line in a different document with the same styling applied inline, then use the DOM range API to iterate over the characters and measure. The advantage of this is that we only need to perform layout once.

Will this new approach work? Not sure. But it's the next thing we'd like to try. Other ideas are welcome, but remember that it can't rely on reading from the document.

@superlou

superlou commented Jun 4, 2015

Copy link
Copy Markdown

@nathansobo: Will this approach allow positioning on lines that have margins or padding, or allow some other means of changing softwrap boundaries throughout the text editor?

@nathansobo

Copy link
Copy Markdown
Contributor

@superlou I'm not sure how much CSS applied to lines it's going to be realistic to support. Any supported styling will need to be sampled for measurement in the other document, and covering every possible thing people may want to style positioning wise could end up being pretty complicated. So we can try, but we may need to stick to CSS for styling fonts and colors and add other editor-specific mechanisms for controlling soft wrap behavior etc. We'll see how it goes.

@larsenwork

Copy link
Copy Markdown

Not sure if it's related to this but I'm having issues with opentype calt stopped working

@nathansobo

Copy link
Copy Markdown
Contributor

@larsenwork Nothing has really changed in this area for a while, so I'd say it's unlikely. You can still open another issue however.

@alexandernst

Copy link
Copy Markdown

Is this still a thing?

@nathansobo

Copy link
Copy Markdown
Contributor

@alexandernst Yep. It's still happening. @as-cii has been churning through a zillion alternatives looking for the best approach. It's looking increasingly like just eating a small forced reflow might be the best approach. We're getting there.

@as-cii as-cii mentioned this pull request Sep 16, 2015
11 tasks done
@as-cii
as-cii deleted the as-char-measurement branch February 22, 2016 12:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

We need a better approach to text measurement

6 participants